Cell type contains only metadata of the cell: level, hashes, and depths. To read actual data, TVM need to load cell from celldb, key-value storage of the node, which stores cells by their representation hashes. CTOS instruction loads a cell by its metadata from Cell type, and provides a Slice, a read-only wrapper for the cell’s content. To simplify coding cell deserializers in smart contracts, instead of behaving like a simple bit/ref array, Slice is a “read cursor”: it allows loading a piece of data from the beginning of the slice, returning that data and the slice that contains remaining data. On the other hand, there is a Builder type, which provides a convenient way to serialize data to a cell. Only the Cell type can be used outside of TVM: in output actions and in persistent storage.
Builder
Builder provides a way to construct a cell from a sequence of values of the following TVM types: integers, cells (as references), slices, and builders. Tuples, continuations, andnull are not serializable.
For example, serialize the following numbers to a cell:
NEWC:
Fift
Fift
STU (“STore Unsigned integer”) to store integer into builder (swapping builder and value to meet STU input order):
Fift
Fift
ENDC instruction finalizes the builder to a cell.
Slice
Slice allows reading data back from a cell, field by field. For example, deserialize bitstringx{0001001011111111} created above.
CTOS (“Cell TO Slice”) loads a Cell to a Slice.
Fift
LDU (“LoaD Unsigned integer”) to read first value (uint4).
Fift
LDU takes the first 4 bits from a slice and creates a new slice without these 4 bits: x{0001|001011111111} slices into a number 0001 and a slice x{001011111111}.
Similarly, read another two numbers:
Fift
ENDS.