bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 16쪽 중 5쪽
ins. In fact, the network mes-
sage should be forwarded from the validators of the task group (cf. 2.6.8 )
of the original shard to the designated block producer (cf. 2.6.9 ) of the task
group of the destination shard; this might be done directly without going
along the edges of the hypercube. When this message with the Merkle proof
reaches the validators (more precisely, the collators; cf. 2.6.5 ) of the destina-
tion shardchain, they can commit it into a new block immediately, without
waiting for the message to complete its travel along the slow path. Then a
conrmation of delivery along with a suitable Merkle proof is sent back along
the hypercube edges, and it may be used to stop the travel of the message
along the slow path, by committing a special transaction.
Note that this instant delivery mechanism does not replace the slow
but failproof mechanism described in 2.4.19 . The slow path is still needed
because the validators cannot be punished for losing or simply deciding not
to commit the fast path messages into new blocks of their blockchains. 16
Therefore, both message forwarding methods are run in parallel, and the
slow mechanism is aborted only if a proof of success of the fast mechanism
is committed into an intermediate shardchain. 17
2.4.21. Collecting input messages from output queues of neighbor-
ing shardchains. When a new block for a shardchain is proposed, some
of the output messages of the neighboring (in the sense of the routing hy-
percube of 2.4.19 ) shardchains are included in the new block as input
messages and immediately delivered (i.e., processed). There are certain rules
as to the order in which these neighbors' output messages must be processed.
Essentially, an older message (coming from a shardchain block referring to
an older masterchain block) must be delivered before any newer message;
and for messages coming from the same neighboring shardchain, the partial
order of the output queue described in 2.4.17 must be observed.
2.4.22. Deleting messages from output queues. Once an output queue
message is observed as having been delivered by a neighboring shardchain,
it is explicitly deleted from the output queue by a special transaction.
16 However, the validators have some incentive to do so as soon as possible, because they
will be able to collect all forwarding fees associated with the message that have not yet
been consumed along the slow path.
17 In fact, one might temporarily or permanently disable the instant delivery mecha-
nism altogether, and the system would continue working, albeit more slowly.
36
2.4. Messages Between Shardchains
2.4.23. Preventing double delivery of messages. To prevent double
delivery of messages taken from the output queues of the neighboring shard-
chains, each shardchain (more precisely, each account-chain inside it) keeps
the collection of recently delivered messages (or just their hashes) as part of
its state. When a delivered message is observed to be deleted from the out-
put queue by its originating neighboring shardchain (cf. 2.4.22 ), it is deleted
from the collection of recently delivered messages as well.
2.4.24. Forwarding messages intended for other shardchains. Hy-
percube routing (cf. 2.4.19 ) means that sometimes outbound messages are
delivered not to the shardchain containing the intended recipient, but to a
neighboring shardchain lying on the hypercube path to the destination. In
this case, delivery consists in moving the inbound message to the outbound
queue. This is reected explicitly in the block as a special forwarding trans-
action , containing the message itself. Essentially, this looks as if the message
had been received by somebody inside the shardchain, and one identical mes-
sage had been generated as result.
2.4.25. Payment for forwarding and keeping a message. The for-
warding transaction actually spends some gas (depending on the size of the
message being forwarded), so a gas payment is deducted from the value of
the message being forwarded on behalf of the validators of this shardchain.
This forwarding payment is normally considerably smaller than the gas pay-
ment exacted when the message is nally delivered to its recipient, even if
the message has been forwarded several times because of hypercube routing.
Furthermore, as long as a message is kept in the output queue of some shard-
chain, it is part of the shardchain's global state, so a payment for keeping
global data for a long time may be also collected by special transactions.
2.4.26. Messages to and from the masterchain. Messages can be sent
directly from any shardchain to the masterchain, and vice versa. However,
gas prices for sending messages to and for processing messages in the master-
chain are quite high, so this ability will be used only when truly necessary
for example, by the validators to deposit their stakes. In some cases, a
minimal deposit (attached value) for messages sent to the masterchain may
be dened, which is returned only if the message is deemed valid by the
receiving party.
Messages cannot be automatically routed through the masterchain. A
message with workchain_id ⁄=−1 (−1 being the special workchain_id indi-
37
2.5. Global Shardchain State. Bag of Cells Philosophy.
cating the masterchain) cannot be delivered to the masterchain.
In principle, one can create a message-forwarding smart contract inside
the masterchain, but the price of using it would be prohibitive.
2.4.27. Messages between accounts in the same shardchain. In some
cases, a message is generated by an account belonging to some shardchain,
destined to another account in the same shardchain. For example, this hap-
pens in a new workchain which has not yet split into several shardchains
because the load is manageable.
Such messages might be accumulated in the output queue of the shard-
chain and then processed as incoming messages in subsequent blocks (any
shard is considered a neighbor of itself for this purpose). However, in most
cases it is possible to deliver these messages within the originating block
itself.
In order to achieve this, a partial order is imposed on all transactions
included in a shardchain block, and the transactions (each consisting in the
delivery of a message to some account) are processed respecting this partial
order. In particular, a transaction is allowed to process some output message
of a preceding transaction with respect to this partial order.
In this case, the message body is not copied twice. Instead, the originating
and the processing transactions refer to a shared copy of the message.
2.5 Global Shardchain State. Bag of Cells Philosophy.
Now we are ready to describe the global state of a TON blockchain, or at
least of a shardchain of the basic workchain.
We start with a high-level or logical description, which consists in
saying that the global state is a value of algebraic type ShardchainState .
2.5.1. Shardchain state as a collection of account-chain states. Ac-
cording to the Innite Sharding Paradigm (cf. 2.1.2 ), any shardchain is just
a (temporary) collection of virtual account-chains, containing exactly one
account each. This means that, essentially, the global shardchain state must
be a hashmap
ShardchainState := (Account 99K AccountState ) (23)
where all account_id appearing as indices of this hashmap must begin with
prex s, if we are discussing the state of shard (w,s ) (cf. 2.1.8 ).
38
2.5. Global Shardchain State. Bag of Cells Philosophy.
In practice, we might want to split AccountState into several parts (e.g.,
keep the account output message queue separate to simplify its examination
by the neighboring shardchains), and have several hashmaps (Account 99K
AccountStatePart i) inside the ShardchainState . We might also add a small
number of global or integral parameters to the ShardchainState , (e.g., the
total balance of all accounts belonging to this shard, or the total number of
messages in all output queues).
However, (23) is a good rst approximation of what the shardchain global
state looks like, at least from a logical (high-level) perspective. The formal
description of algebraic types AccountState and ShardchainState can be done
with the aid of a TL-scheme (cf. 2.2.5 ), to be provided elsewhere.
2.5.2. Splitting and merging shardchain states. Notice that the Innite
Sharding Paradigm description of the shardchain state (23) shows how this
state should be processed when shards are split or merged. In fact, these
state transformations turn out to be very simple operations with hashmaps.
2.5.3. Account-chain state. The (virtual) account-chain state is just the
state of one account, described by type AccountState . Usually it has all or
some of the elds listed in 2.3.20 , depending on the specic constructor used.
2.5.4. Global workchain state. Similarly to (23), we may dene the global
workchain state by the same formula, but with account_id 's allowed to take
any values, not just those belonging to one shard. Remarks similar to those
made in 2.5.1 apply in this case as well: we might want to split this hashmap
into several hashmaps, and we might want to add some integral parameters
such as the total balance.
Essentially, the global workchain state must be given by the same type
ShardchainState as the shardchain state, because it is the shardchain state
we would obtain if all existing shardchains of this workchain suddenly merged
into one.
2.5.5. Low-level perspective: bag of cells. There is a low-level de-
scription of the account-chain or shardchain state as well, complementary
to the high-level description given above. This description is quite impor-
tant, because it turns out to be pretty universal, providing a common basis
for representing, storing, serializing and transferring by network almost all
data used by the TON Blockchain (blocks, shardchain states, smart-contract
storage, Merkle proofs, etc.). At the same time, such a universal low-level
39
2.5. Global Shardchain State. Bag of Cells Philosophy.
description, once understood and implemented, allows us to concentrate our
attention on the high-level considerations only.
Recall that the TVM represents values of arbitrary algebraic types (in-
cluding, for instance, ShardchainState of (23)) by means of a tree of TVM
cells , or cells for short (cf. 2.3.14 and 2.2.5 ).
Any such cell consists of two descriptor bytes , dening certain ags and
values 0≤ b≤ 128, the quantity of raw bytes, and 0≤ c≤ 4, the quantity
of references to other cells. Then b raw bytes and c cell references follow. 18
The exact format of cell references depends on the implementation and on
whether the cell is located in RAM, on disk, in a network packet, in a block,
and so on. A useful abstract model consists in imagining that all cells are
kept in content-addressable memory, with the address of a cell equal to its
( sha256 ) hash. Recall that the (Merkle) hash of a cell is computed exactly
by replacing the references to its child cells by their (recursively computed)
hashes and hashing the resulting byte string.
In this way, if we use cell hashes to reference cells (e.g., inside descriptions
of other cells), the system simplies somewhat, and the hash of a cell starts
to coincide with the hash of the byte string representing it.
Now we see that any object representable by TVM, the global shardchain
state included, can be represented as a bag of cells i.e., a collection of cells
along with a root reference to one of them (e.g., by hash). Notice that
duplicate cells are removed from this description (the bag of cells is a set
of cells, not a multiset of cells), so the abstract tree representation might
actually become a directed acyclic graph (dag) representation.
One might even keep this state on disk in a B- or B+-tree, containing all
cells in question (maybe with some additional data, such as subtree height or
reference counter), indexed by cell hash. However, a naive implementation
of this idea would result in the state of one smart contract being scattered
among distant parts of the disk le, something we would rather avoid. 19
18 One can show that, if Merkle proofs for all data stored in a tree of cells are needed
equally often, one should use cells with b + ch≈ 2(h + r) to minimize average Merkle proof
size, where h = 32 is the hash size in bytes, and r≈ 4 is the byte size of a cell reference.
In other words, a cell should contain either two references and a few raw bytes, or one
reference and about 36 raw bytes, or no references at all with 72 raw bytes.
19 A better implementation would be to keep the state of the smart contract as a serialized
string, if it is small, or in a separate B-tree, if it is large; then the top-level structure
representing the state of a blockchain would be a B-tree, whose leaves are allowed to
contain references to other B-trees.
40
2.5. Global Shardchain State. Bag of Cells Philosophy.
Now we are going to explain in some detail how almost all objects used by
the TON Blockchain can be represented as bags of cells, thus demonstrating
the universality of this approach.
2.5.6. Shardchain block as a bag of cells. A shardchain block itself
can be also described by an algebraic type, and stored as a bag of cells.
Then a naive binary representation of the block may be obtained simply by
concatenating the byte strings representing each of the cells in the bag of
cells, in arbitrary order. This representation might be improved and opti-
mized, for instance, by providing a list of osets of all cells at the beginning
of the block, and replacing hash references to other cells with 32-bit indices
in this list whenever possible. However, one should imagine that a block
is essentially a bag of cells, and all other technical details are just minor
optimization and implementation issues.
2.5.7. Update to an object as a bag of cells. Imagine that we have an
old version of some object represented as a bag of cells, and that we want
to represent a new version of the same object, supposedly not too dierent
from the previous one. One might simply represent the new state as another
bag of cells with its own root, and remove from it all cells occurring in
the old version . The remaining bag of cells is essentially an update to the
object. Everybody who has the old version of this object and the update
can compute the new version, simply by uniting the two bags of cells, and
removing the old root (decreasing its reference counter and de-allocating the
cell if the reference counter becomes zero).
2.5.8. Updates to the state of an account. In particular, updates to the
state of an account, or to the global state of a shardchain, or to any hashmap
can be represented using the idea described in 2.5.7 . This means that when
we receive a new shardchain block (which is a bag of cells), we interpret
this bag of cells not just by itself, but by uniting it rst with the bag of
cells representing the previous state of the shardchain. In this sense each
block may contain the whole state of the blockchain.
2.5.9. Updates to a block. Recall that a block itself is a bag of cells,
so, if it becomes necessary to edit a block, one can similarly dene a block
update as a bag of cells, interpreted in the presence of the bag of cells
which is the previous version of this block. This is roughly the idea behind
the vertical blocks discussed in 2.1.17 .
41
2.5. Global Shardchain State. Bag of Cells Philosophy.
2.5.10. Merkle proof as a bag of cells. Notice that a (generalized)
Merkle prooffor example, one asserting that x[i] =y starting from a known
value of Hash (x) = h (cf. 2.3.10 and 2.3.15 )may also be represented
as a bag of cells. Namely, one simply needs to provide a subset of cells
corresponding to a path from the root of x : Hashmap (n,X ) to its desired
leaf with index i : 2n and value y :X. References to children of these cells not
lying on this path will be left unresolved in this proof, represented by cell
hashes. One can also provide a simultaneous Merkle proof of, say, x[i] = y
and x[i′] =y′, by including in the bag of cells the cells lying on the union of
the two paths from the root of x to leaves corresponding to indices i and i′.
2.5.11. Merkle proofs as query responses from full nodes. In essence,
a full node with a complete copy of a shardchain (or account-chain) state
can provide a Merkle proof when requested by a light node (e.g., a network
node running a light version of the TON Blockchain client), enabling the
receiver to perform some simple queries without external help, using only
the cells provided in this Merkle proof. The light node can send its queries
in a serialized format to the full node, and receive the correct answers with
Merkle proofsor just the Merkle proofs, because the requester should be
able to compute the answers using only the cells included in the Merkle proof.
This Merkle proof would consist simply of a bag of cells, containing only
those cells belonging to the shardchain's state that have been accessed by
the full node while executing the light node's query. This approach can be
used in particular for executing get queries of smart contracts (cf. 4.3.12 ).
2.5.12. Augmented update, or state update with Merkle proof of
validity. Recall (cf. 2.5.7 ) that we can describe the changes in an object
state from an old value x :X to a new value x′ :X by means of an update,
which is simply a bag of cells, containing those cells that lie in the subtree
representing new value x′, but not in the subtree representing old value x,
because the receiver is assumed to have a copy of the old value x and all its
cells.
However, if the receiver does not have a full copy of x, but knows only
its (Merkle) hash h = Hash (x), it will not be able to check the validity of
the update (i.e., that all dangling cell references in the update do refer to
cells present in the tree of x). One would like to have veriable updates,
augmented by Merkle proofs of existence of all referred cells in the old state.
Then anybody knowing only h = Hash (x) would be able to check the validity
of the update and compute the new h′ = Hash (x′) by itself.
42
2.5. Global Shardchain State. Bag of Cells Philosophy.
Because our Merkle proofs are bags of cells themselves (cf. 2.5.10 ), one
can construct such an augmented update as a bag of cells, containing the
old root of x, some of its descendants along with paths from the root of x to
them, and the new root of x′ and all its descendants that are not part of x.
2.5.13. Account state updates in a shardchain block. In particular,
account state updates in a shardchain block should be augmented as dis-
cussed in 2.5.12 . Otherwise, somebody might commit a block containing
an invalid state update, referring to a cell absent in the old state; proving
the invalidity of such a block would be problematic (how is the challenger to
prove that a cell is not part of the previous state?).
Now, if all state updates included in a block are augmented, their validity
is easily checked, and their invalidity is also easily shown as a violation of
the recursive dening property of (generalized) Merkle hashes.
2.5.14. Everything is a bag of cells philosophy. Previous considera-
tions show that everything we need to store or transfer, either in the TON
Blockchain or in the network, is representable as a bag of cells. This is
an important part of the TON Blockchain design philosophy. Once the bag
of cells approach is explained and some low-level serializations of bags of
cells are dened, one can simply dene everything (block format, shardchain
and account state, etc.) on the high level of abstract (dependent) algebraic
data types.
The unifying eect of the everything is a bag of cells philosophy consid-
erably simplies the implementation of seemingly unrelated services; cf. 5.1.9
for an example involving payment channels.
2.5.15. Bloc프로젝트로 돌아가기