You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/coin_lifecycle.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ You should now understand how to create Chialisp puzzles that lock up coins with
7
7
8
8
## The Coin Set Model
9
9
10
-
Chia's model of how coins are stored is called the **coin set** model and is closely modelled after Bitcoin's UTXO model. The idea is simple, every full node holds onto a database of **coin records**:
10
+
Chia's model of how coins are stored is called the **coin set** model and is closely modeled after Bitcoin's UTXO model. The idea is simple, every full node holds onto a database of **coin records**:
11
11
12
12
```
13
13
class Coin:
@@ -117,4 +117,4 @@ Furthermore, generators also enable the compression of certain puzzles in a bloc
117
117
118
118
You should now have a pretty decent grasp of the environment that your puzzles will be running in. Writing secure Chialisp puzzles requires a somewhat unique way of looking at things that is not shared by many other programming languages or blockchains. Understanding of the network is inextricably intertwined with the creation of puzzles and while that creates a steep learning curve, it also enables incredible functionality in an incredibly compact format.
119
119
120
-
It is highly recommended that you fully understand this section before you decide to deploy any smart contract so that you can ensure they are secure and functional. Simply running the puzzle on your computer is no substitute for having it run on millions of nodes with indeterminable intent.
120
+
It is highly recommended that you fully understand this section before you decide to deploy any smart contract so that you can ensure they are secure and functional. Simply running the puzzle on your computer is no substitute for having it run on millions of nodes with indeterminable intent.
This guide directly continues on from [part 1](/docs/) so if you haven't read that, please do so before reading this.
7
7
8
8
This section of the guide will cover evaluating a program inside a program, how ChiaLisp relates to transactions and coins on the Chia network, and cover some techniques to create smart transactions using ChiaLisp.
9
-
If there are any terms that you aren't sure of, be sure to check the [glossary](/docs/glossary).
9
+
If there are any terms that you are not sure of, be sure to check the [glossary](/docs/glossary).
10
10
11
11
## Coins
12
12
13
13
A coin's ID is constructed from 3 pieces of information.
14
14
15
15
1. The ID of its parent
16
-
2. The hash of its puzzle (AKA the puzzlehash)
16
+
2. The hash of its puzzle (AKA the puzzle hash)
17
17
3. The amount that it is worth
18
18
19
19
To construct a coin ID simply take the hash of these 3 pieces of information concatenated in order.
@@ -79,7 +79,7 @@ Here is the complete list of conditions along with their format and behaviour.
79
79
80
80
***AGG_SIG_UNSAFE - [49] - (49 pubkey message)**: This spend is only valid if the attached aggregated signature contains a signature from the given public key of the given message. This is labeled unsafe because if you sign a message once, any other coins you have that require that signature may potentially also be unlocked. It's probably better just to use AGG_SIG_ME because of the natural entropy introduced by the coin ID.
81
81
***AGG_SIG_ME - [50] - (50 pubkey message)**: This spend is only valid if the attached aggregated signature contains a signature from the specified public key of that message concatenated with the coin's ID.
82
-
***CREATE_COIN - [51] - (51 puzzlehash amount)**: If this spend is valid, then create a new coin with the given puzzlehash and amount.
82
+
***CREATE_COIN - [51] - (51 puzzlehash amount)**: If this spend is valid, then create a new coin with the given puzzle hash and amount.
83
83
***ASSERT_FEE - [52] - (52 amount)**: This spend is only valid if there is unused value in this transaction equal to *amount*, which is explicitly to be used as the fee.
84
84
***CREATE_COIN_ANNOUNCEMENT - [60] - (60 message)**: If this spend is valid, this creates an ephemeral announcement with an ID dependent on the coin that creates it. Other coins can then assert an announcement exists for inter-coin communication inside a block.
85
85
***ASSERT_COIN_ANNOUNCEMENT - [61] - (61 announcementID)**: This spend is only valid if there was an announcement in this block matching the announcementID. The announcementID is the hash of the message that was announced concatenated with the coin ID of the coin that announced it `announcementID == sha256(coinID + message)`.
Regardless of what solution is passed this puzzle will *always* return instructions to create a new coin with the puzzlehash 0x365bdd80582fcc2e4868076ab9f24b482a1f83f6d88fd795c362c43544380e7a and the amount 100.
177
+
Regardless of what solution is passed this puzzle will *always* return instructions to create a new coin with the puzzle hash 0x365bdd80582fcc2e4868076ab9f24b482a1f83f6d88fd795c362c43544380e7a and the amount 100.
In this example the result of spending the coin is entirely determined from the puzzle.
188
-
Even though anybody could initiate the spend of the coin, the person that locked the coin up has all the power in the way that the coin is spent as the solution doesn't matter at all.
188
+
Even though anybody could initiate the spend of the coin, the person that locked the coin up has all the power in the way that the coin is spent as the solution does not matter at all.
189
189
190
190
Conversely lets consider a coin locked up with the following puzzle:
191
191
@@ -274,7 +274,7 @@ Change making is simple.
274
274
If a wallet spends less than the total value of a coin, they can create another coin with the remaining portion of value, and lock it up with the standard puzzle for themselves again.
275
275
You can split a coin up into as many new coins with fractions of the original value as you'd like.
276
276
277
-
You cannot create two coins of the same value, with the same puzzlehash, from the same parent as this will lead to an ID collision and the spend will be rejected.
277
+
You cannot create two coins of the same value, with the same puzzle hash, from the same parent as this will lead to an ID collision and the spend will be rejected.
Copy file name to clipboardExpand all lines: docs/common_functions.md
+9-9
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
id: common_functions
3
-
title: 5 - Common Functions in Chialisp
3
+
title: 5 - Common Functions in ChiaLisp
4
4
---
5
5
6
6
When you start to write full smart contracts, you will start to realize that you will need certain common functionality in a lot of puzzles. Let's go over how to include them and what some of them are:
@@ -36,7 +36,7 @@ Also note that the include files are a special format. Everything that is define
36
36
37
37
When puzzles are hashed, they are not simply serialized and passed to sha256. Instead, we take the *tree hash* of the puzzle.
38
38
39
-
Recall that every clvm program can be represented as a binary tree. Every object is either an atom (a leaf of the tree) or a cons box (a branch of the tree). When we hash the puzzle we start at the leaves of the tree and hash our way up, concatenating either a 1 or a 2 to denote that it's either an atom or a cons box. Once a cons box is hashed, it becomes a new leaf to be hashed into its parent cons box and the process recurses. Here's what that looks like in Chialisp:
39
+
Recall that every clvm program can be represented as a binary tree. Every object is either an atom (a leaf of the tree) or a cons box (a branch of the tree). When we hash the puzzle we start at the leaves of the tree and hash our way up, concatenating either a 1 or a 2 to denote that it's either an atom or a cons box. Once a cons box is hashed, it becomes a new leaf to be hashed into its parent cons box and the process recurses. Here's what that looks like in ChiaLisp:
40
40
41
41
```lisp
42
42
(defun sha256tree1
@@ -48,11 +48,11 @@ Recall that every clvm program can be represented as a binary tree. Every objec
48
48
)
49
49
```
50
50
51
-
It is extremely useful to calculate tree hashes within a Chialisp puzzle. You can assert puzzles of other coins, condense puzzles for easier signing, and make CREATE_COIN conditions that are dependent on some passed in data.
51
+
It is extremely useful to calculate tree hashes within a ChiaLisp puzzle. You can assert puzzles of other coins, condense puzzles for easier signing, and make CREATE_COIN conditions that are dependent on some passed in data.
52
52
53
53
## Currying
54
54
55
-
Currying is an extremely important concept in Chialisp that is responsible for almost the entirety of how state is stored in coins. The idea is to pass in arguments to a puzzle *before* it is hashed. When you curry, you commit to solution values so that the individual solving the puzzle cannot change them. Let's take a look at how this is implemented in Chialisp:
55
+
Currying is an extremely important concept in ChiaLisp that is responsible for almost the entirety of how state is stored in coins. The idea is to pass in arguments to a puzzle *before* it is hashed. When you curry, you commit to solution values so that the individual solving the puzzle cannot change them. Let's take a look at how this is implemented in ChiaLisp:
56
56
57
57
```lisp
58
58
;; utility function used by curry
@@ -82,7 +82,7 @@ You can also do the reverse operation. Given a program, you can *uncurry* the l
82
82
; (c curry_arg_1 (c curry_arg_2 1))
83
83
```
84
84
85
-
Let's take our password locked coin example from earlier, this time as a Chialisp puzzle:
85
+
Let's take our password locked coin example from earlier, this time as a ChiaLisp puzzle:
86
86
87
87
```lisp
88
88
(mod (password new_puzhash amount)
@@ -143,7 +143,7 @@ Note that this required that we run the currying module using `brun` in our own
143
143
144
144
## Outer and Inner puzzles
145
145
146
-
A common design pattern, and one of the most powerful features of Chialisp, is the ability to have an outer smart contract the "wraps" an inner puzzle. This concept is extremely handy because it allows a coin to retain all of it's standard functionality and programmability within the inner puzzle, but be bound to an extra set of rules by the outer puzzle.
146
+
A common design pattern, and one of the most powerful features of ChiaLisp, is the ability to have an outer smart contract the "wraps" an inner puzzle. This concept is extremely handy because it allows a coin to retain all of it's standard functionality and programmability within the inner puzzle, but be bound to an extra set of rules by the outer puzzle.
147
147
148
148
For this example, we're going to continue with our password locking, but this time we're going to require that every time the coin is spent, it requires a new password to be set. Let's look at all the code and then we'll break it down:
149
149
@@ -203,7 +203,7 @@ You may notice that we imported a new library called `curry-and-treehash`. We'l
203
203
204
204
First, let's talk about the arguments. When you create this puzzle for the first time you need to curry in 3 things: `MOD_HASH` which is the tree hash of this code, `PASSWORD_HASH` which is the hash of the password that will unlock this coin, and `INNER_PUZZLE` which is a completely separate puzzle that will have its own rules about how the coin can be spent.
205
205
206
-
Chialisp puzzles have the tendency to be read from the bottom up, so lets start with this chunk:
206
+
ChiaLisp puzzles have the tendency to be read from the bottom up, so lets start with this chunk:
207
207
208
208
```lisp
209
209
; main
@@ -228,7 +228,7 @@ All that's happening here is that we're making sure the password is correct and,
228
228
)
229
229
```
230
230
231
-
Recursion is the foundation of Chialisp and functions like these very commonly show up when writing it. In order to iterate through the list of conditions, we first check if there are still items left (remember that an empty list `()` or **nil** evaluates to false). Then, we morph the first condition and concatenate it with the recursive output of the rest of the list. In the end, we will have the same list of items in the same order, but all of them will have passed thru `morph-condition`.
231
+
Recursion is the foundation of ChiaLisp and functions like these very commonly show up when writing it. In order to iterate through the list of conditions, we first check if there are still items left (remember that an empty list `()` or **nil** evaluates to false). Then, we morph the first condition and concatenate it with the recursive output of the rest of the list. In the end, we will have the same list of items in the same order, but all of them will have passed thru `morph-condition`.
232
232
233
233
```lisp
234
234
;; tweak `CREATE_COIN` condition by wrapping the puzzle hash, forcing it to be a password locked coin
@@ -260,6 +260,6 @@ However, all we care about is generating the correct *puzzle hash* for the next
260
260
261
261
And that's it! When this coin is created, it can only be spent by a password that hashes to the curried in PASSWORD_HASH. The inner puzzle can be anything that you want including other smart contracts that have their own inner puzzles. Whatever coins get created as a result of that inner puzzle will be "wrapped" by this same outer puzzle ensuring that every child of this coin is locked by a password *forever*.
262
262
263
-
We created a simple coin, but you can see the potential of this. You can enforce a set of rules not only on a coin that you lock up, but on *every* descendant coin. Not only that, the rules can be enforced *on top of other smart contracts*. In the Chialisp ecosystem, all smart contracts are interoperable with each other unless otherwise specified by a parent smart contract. The possibilities are endless and represent the vast programmability that Chialisp enables for coins.
263
+
We created a simple coin, but you can see the potential of this. You can enforce a set of rules not only on a coin that you lock up, but on *every* descendant coin. Not only that, the rules can be enforced *on top of other smart contracts*. In the ChiaLisp ecosystem, all smart contracts are interoperable with each other unless otherwise specified by a parent smart contract. The possibilities are endless and represent the vast programmability that ChiaLisp enables for coins.
264
264
265
265
In the next section, we'll talk about the standard transaction format on the Chia network.
0 commit comments