Even though the syntax looks very much like TL-B, it cannot be used in most of the TL-B tooling. Unlike in real TL-B, these schemas serialize to a bitstring with no 1023 bit length limit, and without any refs.
General scheme
Internal references, absent cells, and complete BoCs
For an arbitrary cellc in a given BoC, references to it can be either:
- internal if the cell corresponding to the reference is also represented in BoC,
- external if it’s not in BoC. Such cell
cis called absent from this BoC.
Assigning indices to the cells from a bag of cells
The assignment of indices of its cells plays an important role. Letc1, ..., cn be the n distinct cells belonging to a bag of cells B. The most used options are:
- Order cells by their representation hash. Thus
Hash(ci) < Hash(cj)wheneveri < j. - Topological order.
Outline of serialization process
This and the following paragraphs provide a textual description of the BoC serialization process and the specifications of the TL-B schemas associated with it. The specific implementation of the serialization and TL-B schemes is left to the choice of developers.For a specific example of TL-B schema and pseudocode of related cell serialization, see TL-B scheme.
B consisting of n cells can be outlined as follows.
- List the cells from B in a chosen order:
c1, ..., cn(withc1, ..., c_kas root cells, ifBis a forest). - Choose an integer number
s, such thatn ≤ 2^s. Represent each cellciby an integral number of bytes as in standard representation cell algorithm, but:d1 = r + 8s + 16h + 32lwhereh = 1if the cell’s hashes are explicitly included into the serialization; otherwise,h = 0(whenr = 7,hmust be1);- if
h = 1, after bytesb1andb2the serialization is continued byl + 132-byte higher hashes ofc; - unsigned big-endian s-bit integer
jused instead of hashHash(cj)to represent internal references to cellcj.
- Concatenate the representations of cells
cithus obtained in the increasing order ofi. - Optionally, an index can be constructed that consists of
n + 1t-bit integer entriesL1, ..., Ln, whereLiis the total length (in bytes) of the representations of cellscjwithj ≤ i, and integert ≥ 0is chosen so thatLn ≤ 2^t. If the index is included, any cellcithe serialized bag of cells may be easily accessed by its indexiwithout deserializing all other cells, or even without loading the entire serialized bag of cells in memory. - The serialization of the bag of cells now consists of a magic number indicating the precise format of the serialization, followed by integers
s ≥ 0,t ≥ 0,n ≤ 2^s, an optional index consisting of bytes, andLnbytes with the cell representations. - An optional CRC32C may be appended to the serialization for integrity verification purposes.
A classification of serialization schemes for bags of cells
Each TL-B scheme for a bag of cells must specify the following parameters.- The 4-byte magic number (name of TL-B constructor) prepended to the serialization.
- The number of bits
sused to represent cell indices. Usuallysis a multiple of eight. - The number of bits
tused to represent offsets of cell serializations. Usuallytis also a multiple of eight. - A flag indicating whether an index with offsets
L1, ..., Lnof cell serializations is present. This flag may be combined withtby settingt = 0when the index is absent. - A flag indicating whether the
CRC32Cof the whole serialization is appended to it for integrity verification purposes. - The total number of cells
npresent in the serialization. - The number of root cells
k ≤ npresent in the serialization. The root cells themselves are . All other cells present in the bag of cells are expected to be reachable by chains of references starting from the root cells. - The number of absent cells
l ≤ n − k, which represent cells that are absent from this bag of cells, but are referred to from it. The absent cells themselves are represented by , and only these cells may (and also must) haver = 7. Complete bags of cells havel = 0. - The total length in bytes
Lnof the serialization of all cells. If the index is present,Lnmight not be stored explicitly since it can be recovered as the last entry of the index.
A TL-B scheme
Only one serialization scheme of BoCs is used in TON Blockchain (there are also two outdated BoC serialization schemes in the file):cells is n, roots is k, absent is l, and tot_cells_size is Ln (the total size of the serialization of all cells in bytes). If an index is present, parameters s/8 and t/8 are serialized separately as size and off_bytes, respectively, and the flag has_idx is set. The index itself is contained in index, present only if has_idx is set. The field root_list contains the (zero-based) indices of the root nodes of the bag of cells. Finally, cell_data is a sequence of bits that obtained as a concatenation of the cells representations:
An example of a manual serialization
Let’s consider the following example of a tree of cells:- The first is a 24-bit cell.
- The second is a 8-bit cell that itself references a 24-bit cell.
b1 and b2 for each of the three unique cells.
So, we obtain:
b is not a multiple of eight, a binary 1 and up to six binary 0s are appended to the data bits. After that, the data is split into 8-bit groups.
0x020160000202010102fe00010200060aaaaa0000.
Now that we’ve serialized our cells into a flat 20-byte array, it’s time to pack them into a complete BoC format.
SDK from @ton/core
According to the TL-B scheme above there is the SDK for serialization and parsing BoC. Only serialization of BoCs with one root and no absent cells is supported. There are two main functions:serializeBocfor serialization. It has two parameters:rootand options object with two boolean flags:idxandcrc32. They indicate whether indexes and CRC32C will be included in serialization. The output is a Buffer with serialization.deserializeBocfor parsing. It has one parameter:src, a Buffer that contains a serialized BoC. The output is a roots list of a given BoC.