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
{{ message }}
This repository was archived by the owner on Dec 12, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: pages/book/cells.mdx
+85-33Lines changed: 85 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -2,19 +2,51 @@
2
2
3
3
import { Callout } from'nextra/components'
4
4
5
-
[Cells](#cells), [Builders](#builders) and [Slices](#slices) are low-level [primitives][p] of TON Blockchain. The virtual machine of TON Blockchain, [TVM][tvm], uses Cells to represent all data structures in memory and persistent storage.
6
-
7
-
TODO:
8
-
* (Optional?) How to parse emitted messages (in TS and/or other languages with bindings)
5
+
[Cells](#cells), [Builders](#builders) and [Slices](#slices) are low-level [primitives][p] of TON Blockchain. The virtual machine of TON Blockchain, [TVM][tvm], uses cells to represent all data structures in memory and persistent storage.
9
6
10
7
## Cells
11
8
12
-
`Cell{:tact}` is a [primitive][p] and a data structure, which consists of up to 1023 continuously laid out bits and up to 4 references to other Cells. Circular references are forbidden and cannot be created by the means of [TVM][tvm], which means Cells can be viewed as [quadtrees](https://en.wikipedia.org/wiki/Quadtree) or [directed acyclic graphs (DAGs)](https://en.wikipedia.org/wiki/Directed_acyclic_graph) of themselves.
9
+
`Cell{:tact}` is a [primitive][p] and a data structure, which [ordinarly](#cells-kinds) consists of up to $1023$ continuously laid out bits and up to $4$ references to other cells. Circular references are forbidden and cannot be created by the means of [TVM][tvm], which means cells can be viewed as [quadtrees][quadtree] or [directed acyclic graphs (DAGs)](https://en.wikipedia.org/wiki/Directed_acyclic_graph) of themselves. [TVM][tvm] code itself is represented by a tree of cells.
10
+
11
+
Cells and [cell primitives](#cells-immutability) are bit-oriented, not byte-oriented: [TVM][tvm] regards data kept in cells as sequences (strings or streams) of up to $1023$ bits, not bytes. If necessary, contracts are free to use, say, $21$-bit integer fields serialized into [TVM][tvm] cells, thus using fewer persistent storage bytes to represent the same data.
12
+
13
+
### Kinds [#cells-kinds]
14
+
15
+
While the [TVM][tvm] type [`Cell{:tact}`](#cells) refers to all cells, there are different cell kinds with various memory layouts. The one described [earlier](#cells) is commonly referred to as an _ordinary_ (or cell — that's the most simple and most commonly used flavor of cells, which can only contain data. The grand majority of descriptions, guides and [references](/ref/core-cells) to cells and their usage assumes ordinary ones.
16
+
17
+
Other kinds of cells are collectively called _exotic_ (or special) cells. They sometimes appear in actual representations of blocks and other data structures on TON Blockchain. Their memory layouts and purposes significantly differ from ordinary cells.
13
18
14
-
### Types [#cells-types]
19
+
Kinds (or subtypes) of all cells are encoded by an integer between $-1$ and $255$. Ordinary cells are encoded by $-1$, while exotic ones can be encoded by any other integer in that range. The subtype of an exotic cell is stored in the first $8$ bits of its data, which means valid exotic cells always have at least $8$ data bits.
20
+
21
+
[TVM][tvm] currently supports the following exotic cell subtypes:
22
+
*[Pruned branch cell][c-pruned], with subtype encoded as $1$ — they represent deleted subtrees of cells.
23
+
*[Library reference cell][c-library], with subtype encoded as $2$ — they are used for storing libraries, and usually, in [masterchain](/book/masterchain) contexts.
24
+
*[Merkle proof cell][c-mproof], with subtype encoded as $3$ — they are used for verifying that certain portions of other cell's tree data belong to the full tree.
25
+
*[Merkle update cell][c-mupdate], with subtype encoded as $4$ — they always have two references and behave like a [Merkle proof][mproof] for both of them.
26
+
27
+
<Callout>
28
+
29
+
**Useful links:**\
30
+
[Pruned branch cells in TON Docs][c-pruned]\
31
+
[Library reference cells in TON Docs][c-library]\
32
+
[Merkle proof cells in TON Docs][c-mproof]\
33
+
[Merkle update cells in TON Docs][c-mupdate]\
34
+
[Simple proof-verifying example in TON Docs][mproof]
Every cell, being a [quadtree][quadtree], has an attribute called _level_, which is represented by an integer between $0$ and $3$. The level of an [ordinary](#cells-kinds) cell is always equal to the maximum of the levels of all its references. That is, level of an ordinary cell without references is equal to $0$.
47
+
48
+
[Exotic](#cells-kinds) cells have different rules for determining their level, which are described [on this page in TON Docs](https://docs.ton.org/develop/data-formats/exotic-cells).
49
+
18
50
### Standard representation [#cells-representation]
19
51
20
52
<Callout>
@@ -25,28 +57,46 @@ TODO:
25
57
26
58
### Immutability [#cells-immutability]
27
59
28
-
{/*
29
-
TODO: (move from drafts)
30
-
* Don't describe everything, write from the Tact POV. Everything else can be linked to TON Docs.
31
-
*/}
60
+
Cells are read-only and immutable, but there are two major sets of [ordinary](#cells-kinds) cell manipulation instructions in [TVM][tvm]:
61
+
62
+
* Cell creation (or serialization) instructions, which are used to construct new cells from previously kept values and cells;
63
+
* And cell parsing (or deserialization) instructions, which are used to extract or load data previously stored into cells via serialization instructions.
64
+
65
+
On top of that, there are instructions specific to [exotic](#cells-kinds) cells to create them and expect their values. However, [ordinary](#cells-kinds) cell parsing instructions can still be used on [exotic](#cells-kinds) ones, in which case they are automatically replaced by [ordinary](#cells-kinds) cells during such deserialization attempts.
66
+
67
+
All cell manipulation instructions require transforming values of [`Cell{:tact}`](#cells) type to either [`Builder{:tact}`](#builders) or [`Slice{:tact}`](#slices) types before such cells can be modified or inspected.
32
68
33
69
## Builders
34
70
35
-
{/*
36
-
TODO: (move from drafts)
37
-
* Construction of Cells with Builders.
38
-
* Recommend using structs :)
39
-
"I strongly recommend you to use structs in Tact instead of manually composing and parsing cells"
40
-
*/}
71
+
`Builder{:tact}` is a cell manipulation [primitive][p] for using cell creation instructions. They're immutable just like cells are, and allow constructing new cells from previously kept values and cells. Unlike cells, values of type `Builder{:tact}` appear only on [TVM][tvm] stack and cannot be stored in persistent storage. That means, for example, that persistent storage fields with type `Builder{:tact}` would actually be stored as cells under the hood.
72
+
73
+
`Builder{:tact}` type represents partially composed cells, for which fast operations for appending integers, other cells, references to other cells and many others are defined:
74
+
75
+
*[`Builder.storeUint(){:tact}` in Core library][b-2]
76
+
*[`Builder.storeInt(){:tact}` in Core library][b-3]
77
+
*[`Builder.storeBool(){:tact}` in Core library][b-4]
78
+
*[`Builder.storeSlice(){:tact}` in Core library][b-5]
79
+
*[`Builder.storeCoins(){:tact}` in Core library][b-6]
80
+
*[`Builder.storeAddress(){:tact}` in Core library][b-7]
81
+
*[`Builder.storeRef(){:tact}` in Core library][b-8]
82
+
83
+
While you may use them for [manual construction](#cnp-manually) of the cells, it's strongly recommended to use [Structs][struct] instead: [Construction of cells with Structs](#cnp-structs).
41
84
42
85
## Slices
43
86
44
-
{/*
45
-
TODO: (move from drafts)
46
-
* Parsing of Cells with Slices.
47
-
* Recommend using structs :)
48
-
"I strongly recommend you to use structs in Tact instead of manually composing and parsing cells"
49
-
*/}
87
+
`Slice{:tact}` is a cell manipulation [primitive][p] for using cell parsing instructions. Unlike cells, they're mutable and allow extracting or loading data previously stored into cells via serialization instructions. Also unlike cells, values of type `Slice{:tact}` appear only on [TVM][tvm] stack and cannot be stored in persistent storage. That means, for example, that persistent storage fields with type `Slice{:tact}` would actually be stored as cells under the hood.
88
+
89
+
`Slice{:tact}` type represents either the remainder of a partially parsed cell, or a value (subcell) residing inside such a cell and extracted from it by a parsing instruction:
90
+
91
+
*[`Slice.loadUint(){:tact}` in Core library][s-2]
92
+
*[`Slice.loadInt(){:tact}` in Core library][s-3]
93
+
*[`Slice.loadBool(){:tact}` in Core library][s-4]
94
+
*[`Slice.loadSlice(){:tact}` in Core library][s-5]
95
+
*[`Slice.loadCoins(){:tact}` in Core library][s-6]
96
+
*[`Slice.loadAddress(){:tact}` in Core library][s-7]
97
+
*[`Slice.loadRef(){:tact}` in Core library][s-8]
98
+
99
+
While you may use them for [manual parsing](#cnp-manually) of the cells, it's strongly recommended to use [Structs][struct] instead: [Parsing of cells with Structs](#cnp-structs).
Bag of Cells, or BoC_ for short, is a format for serializing and de-serializing Cells into byte arrays as described in [boc.tlb](https://github.com/ton-blockchain/ton/blob/24dc184a2ea67f9c47042b4104bbb4d82289fac1/crypto/tl/boc.tlb#L25)[TL-B schema][tlb].
142
+
Bag of Cells, or _BoC_ for short, is a format for serializing and de-serializing cells into byte arrays as described in [boc.tlb](https://github.com/ton-blockchain/ton/blob/24dc184a2ea67f9c47042b4104bbb4d82289fac1/crypto/tl/boc.tlb#L25)[TL-B schema][tlb].
93
143
94
144
Read more about BoC in TON Docs: [Bag of Cells](https://docs.ton.org/develop/data-formats/cell-boc#bag-of-cells).
95
145
@@ -101,10 +151,9 @@ Read more about BoC in TON Docs: [Bag of Cells](https://docs.ton.org/develop/dat
101
151
102
152
## Operations
103
153
104
-
105
154
### Construct and parse [#operations-cnp]
106
155
107
-
In Tact, there are at least two ways to construct and parse Cells:
156
+
In Tact, there are at least two ways to construct and parse cells:
108
157
109
158
*[Manually](#cnp-manually), which involves active use of [`Builder{:tact}`](#builders), [`Slice{:tact}`](#slices) and [relevant methods](/ref/core-cells).
110
159
*[Using Structs](#cnp-structs), which is a recommended and much more convenient approach.
@@ -146,7 +195,7 @@ Construction via `Builder{:tact}` | Parsing via `Slice{:tact}`
146
195
147
196
[Structs][struct] and [Messages][message] are almost like living [TL-B schemas][tlb]. Which means that they're, essentially, [TL-B schemas][tlb] expressed in maintainable, verifiable and user-friendly Tact code.
148
197
149
-
It is strongly recommended to use them and their [methods](/book/functions#extension-function) like [`Struct.toCell(){:tact}`][st-tc] and [`Struct.fromCell(){:tact}`][st-fc] instead of manually constructing and parsing Cells, as this allows for much more declarative and self-explanatory contracts.
198
+
It is strongly recommended to use them and their [methods](/book/functions#extension-function) like [`Struct.toCell(){:tact}`][st-tc] and [`Struct.fromCell(){:tact}`][st-fc] instead of manually constructing and parsing cells, as this allows for much more declarative and self-explanatory contracts.
150
199
151
200
The examples of manual parsing [above](#cnp-manually) could be re-written using [Structs][struct], with descriptive names of fields if one so desires:
152
201
@@ -155,7 +204,7 @@ The examples of manual parsing [above](#cnp-manually) could be re-written using
Copy file name to clipboardExpand all lines: pages/ref/core-cells.mdx
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
import { Callout } from'nextra-theme-docs'
4
4
5
-
[`Cell{:tact}`][cell] is a low-level [primitive][p] that represents data in TON Blockchain. Cells consist of 1023 bits of data with up to 4 references to another cells. They are read-only and immutable, and cannot have cyclic references.
5
+
[`Cell{:tact}`][cell] is a low-level [primitive][p] that represents data in TON Blockchain. Cells consist of $1023$ bits of data with up to $4$ references to another cells. They are read-only and immutable, and cannot have cyclic references.
6
6
7
7
[`Builder{:tact}`][builder] is an immutable [primitive][p] to construct cells, and [`Slice{:tact}`][slice] is a mutable [primitive][p] to parse them.
0 commit comments