TVM Commitment
Last updated
Last updated
All TON Smart Contracts are executed on their own TON Virtual Machine (TVM). TVM is built on the stack principle, which makes it efficient and easy to implement.
When some event happens on the account in one of the TON chains, it causes a transaction. The most common event is the "arrival of some message", but generally speaking there could be tick-tock
, merge
, split
and other events.
Each transaction consists of up to 5 phases:
Storage phase - in this phase, storage fees accumulated by the contract due to the occupation of some space in the chain state are calculated. Read more in Storage Fees.
Credit phase - in this phase, the balance of the contract with respect to a (possible) incoming message value and collected storage fee are calculated.
Compute phase - in this phase, TVM is executing the contract (see below) and the result of the contract execution is an aggregation of exit_code
, actions
(serialized list of actions), gas_details
, new_storage
and some others.
Action phase - if the compute phase was successful, in this phase, actions
from the compute phase are processed. In particular, actions may include sending messages, updating the smart contract code, updating the libraries, etc. Note that some actions may fail during processing (for instance, if we try to send message with more TON than the contract has), in that case the whole transaction may revert or this action may be skipped (it depends on the mode of the actions, in other words, the contract may send a send-or-revert
or try-send-if-no-ignore
type of message).
Bounce phase - if the compute phase failed (it returned exit_code >= 2
), in this phase, the bounce message is formed for the transactions initiated by an incoming message.
At any given moment, the TVM state is fully determined by 6 properties:
Stack (see below)
Control registers - (see below) to put it simply, this means up to 16 variables which may be directly set and read during execution
Current continuation - object which describes a currently executed sequence of instructions
Current codepage - in simple terms, this means the version of TVM which is currently running
Gas limits - a set of 4 integer values; the current gas limit gl, the maximal gas limit gm, the remaining gas gr and the gas credit gc
Library context - the hashmap of libraries which can be called by TVM
c0
— Contains the next continuation or return continuation (similar to the subroutine return address in conventional designs). This value must be a Continuation.
c1
— Contains the alternative (return) continuation; this value must be a Continuation.
c2
— Contains the exception handler. This value is a Continuation, invoked whenever an exception is triggered.
c3
— Supporting register, contains the current dictionary, essentially a hashmap containing the code of all functions used in the program. This value must be a Continuation.
c4
— Contains the root of persistent data, or simply the data
section of the contract. This value is a Cell.
c5
— Contains the output actions. This value is a Cell.
c7
— Contains the root of temporary data. It is a Tuple.