From c38b0dcd33b96ba8bf2508a5d908b4bddecb1dfe Mon Sep 17 00:00:00 2001 From: Alexander Gapak Date: Tue, 29 Aug 2023 21:56:26 +0000 Subject: [PATCH 1/2] rewrite boc topic --- src/arch/07-tlb-and-boc.md | 16 +++++----------- src/img/boc.png | Bin 10244 -> 0 bytes 2 files changed, 5 insertions(+), 11 deletions(-) delete mode 100644 src/img/boc.png diff --git a/src/arch/07-tlb-and-boc.md b/src/arch/07-tlb-and-boc.md index 2d6404b45..d0e56d2c9 100644 --- a/src/arch/07-tlb-and-boc.md +++ b/src/arch/07-tlb-and-boc.md @@ -142,18 +142,12 @@ It is done recursively turning to the serialization of variables (they are TL-B ## BoC -All data in ES is stored and sent in a structure called the BoC (Bag of cell). This is a confusing structure invented by Nikolai Durov, and it’s not very clear whether it was necessary in order to create the blockchain, or Nikolai over-engineered here. In general, all data stored in the contract (not the code, the code is separate, but also the BoC is stored in the contract :-)) is stored in one BoC, this is a given cell (TVMCell) with links to subcells. The TVMCell is a structure that has 1023 Bits of data and 4 references to its child cells. A cell reference is the HASH of that cell. With ES have have a singly connected graph, where each node can have 4 descendants. And we need to pack all smart contract data or messages into a cell with sub cells. Thank God, by using Ton Solidty and knowing the interfaces of contracts, you don’t have to manually pack and unpack cells. The compiler will do everything for you. However, there are rare cases when you will have to do it yourself. In order to do this, there are special primitives in Solidity. When this is necessary, you simply declare state variables in Solidity and read and write to them as usual. The compiler will pack and unpack everything in the BoC for you. +All data in the blocks and state of the Everscale Blockchain is represented as a collection of cells, like in the TON Blockchain, because ES is based on the TON Blockchain technology. Referring to [tblkch.pdf](https://ton.org/tblkch.pdf) document – each cell consists of up to 1023 data bits and up to four references to other cells. Cyclic cell references are not allowed, so the cells are usually organized into trees of cells, or rather directed acyclic graphs (DAGs) of cells. Any value of an abstract algebraic (dependent) data type may be represented (serialized) as a tree of cells. The precise way of representing values of an abstract data type as a tree of cells is expressed by means of a TL-B scheme. -Some BoC properties to understand. BoC is an acyclic graph. The link to a cell c is a hash of its data and its hash links to subcells. So we cannot create a cycle. (Because if we add a link from the parent cell to the child cell, then, by doing so, we recalculate all hashes from the changed cell to the root cell and the link to the parent cell changes). The entire state of the contract is BoC. This is one cell with as many child cells as you like. Ton Solidity takes care of work with states for us, but you need to understand that, because of ES’s tree structure, we normally don’t write contracts with a lot of data. In order to illustrate how it works, consider (schematically) how a dictionary could be implemented in BoC. +TL-B is a fantastic way to describe the structure of a cell tree, but sometimes developers resort to using ABI to simplify the development process. ABI is automatically generated by the [TVM Solidity Compiler](https://github.com/tonlabs/TON-Solidity-Compiler/) in JSON format, and then can be used for encoding/decoding data structures stored in cells. But unfortunately, ABI comes with a set of significant limitations compared to TL-B. Read more about ABI [here](../spec/10-abi.md). -![](../img/boc.png) +Interesting fact – this complicated data storage scheme helps to scale the network, so that the validator can quickly execute smart contracts. Validators do not need to store the entire current state of the blockchain in RAM, because loading the smart contract state from the disk will be fast, since all smart contracts are small. Additionally, since all contracts are small, they are evenly distributed across all shards. -Each circle in the picture is a separate cell. To get the value by key 2, TVM needs to load a cell of depth 0, then depth 1 and then depth 2. We have to pay gas for each time a cell is loaded. And if we change the value by key two, we will need to recalculate all references from the cell with the value of the root cell because the cell reference is a hash (cell.data + cell.refs). +In the Everscale Blockchain, we typically, pay for storage, transfering, and any operations with cells (such as loading them). Hence, it's crucial to consider this when developing your smart contracts. The code and data of smart contracts executed in TVM are also represented in the tree of cells. -So, links to all cells along the way will change and we will need to change them from bottom to top. So, the more elements our dictionary has, the deeper the cell will be and the more expensive it will be to work with. For a dictionary, the cost of gas will increase to O(log n) in a worst case scenario. (In reality, everything would be more complicated but O (log n) can be useful to look at as a worst case scenario). - -Now, if we are creating an ERC20 token, then the more owners this token has, the more expensive the gas will be to use this contract (the size of the owner-number map will grow). And although O(Log n) doesn’t sound scary at all, and the cost of working with the map will increase very, very slowly after the first hundred elements, and then even slower after that, in ES there we have a storage fee that grows linearly. - -If you have accounts in your ERC-20 token that contain pennies, then the fees for holding these accounts will greatly exceed the value of these accounts over the years. Therefore, in ES it is customary to make separate contracts for separate accounts, which themselves pay for their storage. - -Fun fact: this entire overcomplicated data storage scheme helps to scale the network, so that the validator can quickly execute smart contracts. Validators do not need to store the entire current state of the blockchain in Ram, because loading the smart contract state from the disk will be fast, since all smart contracts are small. Additionally, since all contracts are small, they are evenly distributed across all shards. +Read more about bag of cells in the [tvm.pdf](https://ton.org/tvm.pdf) and [tblkch.pdf](https://ton.org/tblkch.pdf) documents. diff --git a/src/img/boc.png b/src/img/boc.png deleted file mode 100644 index ba11af6a97961b7f25c32e9c87cb0f824d8cf821..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 10244 zcmch7cQjmY_ivgoLNI#I#EdS85Yde;x~OLfqUAHb!>N zF19vzOzdr4dpa1w5@eQ-w4MLQ_m?h%b==c^qP|)MNDv2Y-1yu~M+*CDVfhdr@m5*a z9~r`X@H)~wuP4YwYkAy_KYmxuGyUj1>ZSQm*}aKO9o33Y1ti(GUg?v2@1s~w-)(GE znok{I_)uyl?W#;v@)2cp@+{WjYF1bK35#C8J@qE1Y;0-l-ZZGCTE@?#;wG=tfj(Ct zpikAZ^luR%XtH3)h@NkT=cs-cl`KjNcM>nPa$slPsH4)cRgP54O?)l%)OGujjTwuP zz2!G9qh#h_Q~c$H=MdVJ`2N9LrK1dS)_7v~K2Ua>9G=@%X%Vo|xRKJ4ZiT(8;1}i} z3Cy;F{Ft`gXEjzxNJ!v7Lb=Dtleyb8wg_*{ycjpji$z?z#O$IVE&0e@e?9%g;L*_e z#btz~WYbO7TX5{W952U4o`q9wa$CJ2+LHaf zNBfP`*aaMNdp(pine+np(#dhxajp3%PcwsFb4kj`4laXN@qyQd-$B8mzBOTab=wq* zIB5oLdkZyqCq9~(QVIfb#Xk~r(>A=ks0_wY_Gni0X0h>PNq4@0VY%;#5PoIvBUX_Bp#z9mlNJzYrLx>&z zx@xQc`jyuVYU<@hgTVe5HKd6o#P&9vfRIpGRn?%;*Jo#;yL5GN@p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&kEuE0{aQ*;nIBw1d>-J z84q2-?&d8lWhemD)v5`Yguh^|QtO+~apaI0prko9J~0pCBH%qu9#9HO%HZJOOc4(a z+FUjOzL8N;j3Vxvzyqk@ZF}JMThG(8cCWf?9T13ehc_r085sr!2H^G$T(`oH|2@_C zQcYIjlns{CLdEyTI%&A{>7Og+4dB1)=4)+MSIOx6-A@4cIx}TLqK&6Og2u z$Qe&(Mfbza>F1d~RV5{ARfcycsi{2xUGew#2WH^&Hy+Gj9|MK}iI4`Qff0E8EScW7 zqn?3x6~Js6vyZh0OiY9U4FXdG3 z;+y7Q%yEv$IXW<@uN=`2Fb4#rG(Gn=C}4=QqPC6>78%Gnw7w+*?s$_?Y{FDuui@l( z9aT=&JV1t(_4NkeaszO{dZq(&r2v9f`*VWZPsugwkCS;#4EsUl*1E1AAFmd7-fIUH z=IF%D%}p2b8sMQ3E2t794GpO4^R8sSbwEjll00jsR>Y1gF8_gq4z-a(paabKL1Qp7G1Q`uRLx#XiQs_lL@r4+Uqot^_GLp+W|>Ne-`W1j+DtOKYAfL6gaOY?_pG`K_z5&Y<`uU#c& zC}2vN12pXNSj=!kp)9M=mfGnls{81S_{?HYmMV^B(06_nLc^d4`OIDaWz)A+I;J{%0K&7mJCZvF|f|Gxk|IWb89 From 798c7635243fbfc067a216b83fa8e9ab11b19214 Mon Sep 17 00:00:00 2001 From: Alexander Gapak Date: Wed, 30 Aug 2023 16:17:00 +0000 Subject: [PATCH 2/2] fix url --- src/arch/07-tlb-and-boc.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arch/07-tlb-and-boc.md b/src/arch/07-tlb-and-boc.md index d0e56d2c9..f919b14db 100644 --- a/src/arch/07-tlb-and-boc.md +++ b/src/arch/07-tlb-and-boc.md @@ -150,4 +150,4 @@ Interesting fact – this complicated data storage scheme helps to scale the net In the Everscale Blockchain, we typically, pay for storage, transfering, and any operations with cells (such as loading them). Hence, it's crucial to consider this when developing your smart contracts. The code and data of smart contracts executed in TVM are also represented in the tree of cells. -Read more about bag of cells in the [tvm.pdf](https://ton.org/tvm.pdf) and [tblkch.pdf](https://ton.org/tblkch.pdf) documents. +Read more about bag of cells in the [tvm.pdf](https://docs.everscale.network/tvm.pdf) document.