Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit bbd0f59

Browse files
committed
feat: completed descriptions of immutability, builders and slices
1 parent 3b3c73a commit bbd0f59

File tree

4 files changed

+88
-35
lines changed

4 files changed

+88
-35
lines changed

cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"Jetton",
2525
"Jettons",
2626
"jettons",
27+
"jojo",
2728
"masterchain",
2829
"Masterchain",
2930
"mathrm",

pages/book/cells.mdx

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,51 @@
22

33
import { Callout } from 'nextra/components'
44

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.
96

107
## Cells
118

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.
1318

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]
35+
36+
</Callout>
37+
38+
[c-pruned]: https://docs.ton.org/develop/data-formats/exotic-cells#pruned-branch
39+
[c-library]: https://docs.ton.org/develop/data-formats/library-cells
40+
[c-mproof]: https://docs.ton.org/develop/data-formats/exotic-cells#merkle-proof
41+
[c-mupdate]: https://docs.ton.org/develop/data-formats/exotic-cells#merkle-update
42+
[mproof]: https://docs.ton.org/develop/data-formats/exotic-cells#simple-proof-verifying-example
1543

1644
### Levels [#cells-levels]
1745

46+
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+
1850
### Standard representation [#cells-representation]
1951

2052
<Callout>
@@ -25,28 +57,46 @@ TODO:
2557

2658
### Immutability [#cells-immutability]
2759

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.
3268

3369
## Builders
3470

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).
4184

4285
## Slices
4386

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).
50100

51101
## Serialization
52102

@@ -89,7 +139,7 @@ contract SerializationExample {
89139

90140
### Bag of Cells [#cells-boc]
91141

92-
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].
93143

94144
Read more about BoC in TON Docs: [Bag of Cells](https://docs.ton.org/develop/data-formats/cell-boc#bag-of-cells).
95145

@@ -101,10 +151,9 @@ Read more about BoC in TON Docs: [Bag of Cells](https://docs.ton.org/develop/dat
101151

102152
## Operations
103153

104-
105154
### Construct and parse [#operations-cnp]
106155

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:
108157

109158
* [Manually](#cnp-manually), which involves active use of [`Builder{:tact}`](#builders), [`Slice{:tact}`](#slices) and [relevant methods](/ref/core-cells).
110159
* [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}`
146195

147196
[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.
148197

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.
150199

151200
The examples of manual parsing [above](#cnp-manually) could be re-written using [Structs][struct], with descriptive names of fields if one so desires:
152201

@@ -155,7 +204,7 @@ The examples of manual parsing [above](#cnp-manually) could be re-written using
155204
struct Showcase {
156205
id: Int as uint8;
157206
someImportantNumber: Int as int8;
158-
isntThatCool: Bool;
207+
isThatCool: Bool;
159208
payload: Slice;
160209
nanoToncoins: Int as coins;
161210
wackyTacky: Address;
@@ -169,17 +218,18 @@ struct Adventure {
169218
}
170219
171220
fun example() {
221+
// Basics
172222
let s = Showcase.fromCell(
173223
Showcase{
174224
id: 7,
175225
someImportantNumber: 42,
176-
isntThatCool: true,
226+
isThatCool: true,
177227
payload: emptySlice(),
178228
nanoToncoins: 1330 + 7,
179229
wackyTacky: myAddress(),
180230
jojoRef: Adventure{ bizarre: true, time: false },
181231
}.toCell());
182-
s.isntThatCool; // true
232+
s.isThatCool; // true
183233
}
184234
```
185235

@@ -309,11 +359,13 @@ let areSlicesNotEqual = aSlice.hash() != bSlice.hash(); // false
309359

310360
</Callout>
311361

312-
[bin-eq]: /book/operators#binary-equality
362+
[p]: /book/types#primitive-types
363+
[struct]: /book/structs-and-messages#structs
364+
[message]: /book/structs-and-messages#messages
313365

314366
[tvm]: https://docs.ton.org/learn/tvm-instructions/tvm-overview
315367
[tlb]: https://docs.ton.org/develop/data-formats/tl-b-language
316368

317-
[p]: /book/types#primitive-types
318-
[struct]: /book/structs-and-messages#structs
319-
[message]: /book/structs-and-messages#messages
369+
[quadtree]: https://en.wikipedia.org/wiki/Quadtree
370+
[bin-eq]: /book/operators#binary-equality
371+

pages/cookbook/single-communication.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ send(SendParameters{
8989

9090
**Useful links:**\
9191
["Sending messages" in the Book](/book/send#send-message)\
92-
["Message `mode`" in the Book](/book/message-mode)
92+
["Message `mode`" in the Book](/book/message-mode)\
9393
[`StringBuilder{:tact}` in the Book](/book/types#primitive-types)\
9494
[`Cell{:tact}` in Core library](/ref/core-cells)
9595

pages/ref/core-cells.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { Callout } from 'nextra-theme-docs'
44

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.
66

77
[`Builder{:tact}`][builder] is an immutable [primitive][p] to construct cells, and [`Slice{:tact}`][slice] is a mutable [primitive][p] to parse them.
88

0 commit comments

Comments
 (0)