TVM Initialization
To maximize your comprehension of this page, familiarizing yourself with the TL-B language is highly recommended.
TVM is invoked during the computing phase of ordinary and/or other transactions.
Initial state
A new instance of TVM is initialized prior to the execution of a smart contract as follows:
-
The original cc (current continuation) is initialized using the cell slice created from the
code
section of the smart contract. In case of a frozen or uninitialized state of the account, the code must be supplied in theinit
field of the incoming message. -
The cp (current TVM codepage) is set to the default value, which is 0. If the smart contract wants to use another TVM codepage x, then it must switch to it by using
SETCODEPAGE
x as the first instruction of its code. -
The gas values (gas limits) are initialized in accordance to Credit phase results.
-
The libraries (library context) computation is described below.
-
The stack initialization process depends on the event which caused the transaction, and its contents are described below.
-
Control register c0 (return continuation) is initialized by extraordinary continuation
ec_quit
with parameter 0. When executed, this continuation leads to a termination of TVM with exit code 0. -
Control register c1 (alternative return continuation) is initialized by extraordinary continuation
ec_quit
with parameter 1. When invoked, it leads to a termination of TVM with exit code 1. Notice, that both exit codes 0 and 1 are considered a successful termination of TVM. -
Control register c2 (exception handler) is initialized by extraordinary continuation
ec_quit_exc
. When invoked, it takes the top integer from the stack (equal to the exception number) and terminates TVM with exit code equal to that integer. This way, by default all exceptions terminate the smart contract execution with exit code equal to the exception number. -
Control register c3 (code dictionary) is initialized by the cell with the smart contract code like cc (current continuation) described above.
-
Control register c4 (root of persistent data) is initialized by the persistent data of the smart contract, stored in its
data
section. In case of a frozen or uninitialized state of the account, the data must be supplied in theinit
field of the incoming message. Notice, that the persistent data of the smart contract does not need to be loaded in its entirely for this to occur. The root is loaded instead, and TVM may load other cells by their references from the root only when they are accessed, thus providing a form of virtual memory. -
Control register c5 (root of actions) is initialized by an empty cell. The "output action" primitives of TVM, such as
SENDMSG
, accumulate output actions (e.g., outbound messages) in this register, to be performed upon successful termination of the smart contract. The TL-B scheme for its serialization is described below -
Control register c7 (root of temporary data) is initialized as a tuple and its structure is described below
Library context
The library context (library environment) of a smart contract is a hashmap mapping 256-bit cell (representation) hashes into the corresponding cells themselves. When an external cell reference is accessed during the execution of the smart contract, the cell referred to is looked up in the library environment and the external cell reference is transparently replaced by the cell found.
The library environment for an invocation of a smart contract is computed as follows:
- The global library environment for the current workchain is taken from the current state of the masterchain.
- Then, it is augmented by the local library environment of the smart contract, stored in the
library
field of the smart contract's state. Only 256-bit keys equal to the hashes of the corresponding value cells are taken into account. If a key is present in both the global and local library environments, the local environment takes precedence in the merge. - Finally, it is augmented by the
library
field of theinit
field of the incoming message (if any). Notice, that if the account is frozen or uninitialized, thelibrary
field of the message would be used over the local library environment from the previous step. The message library has lower precedence than both the local and the global library environments.
Most common way of creating shared libraries for TVM is to publish a reference to the root cell of the library in the masterchain.
Stack
Initialization of the TVM stack comes after the formation of the initial state of the TVM, and it depends on the event which caused the transaction:
- internal message
- external message
- tick-tock
- split prepare
- merge install
The last item pushed to the stack is always the function selector, which is an Integer that identifies the event that caused the transaction.