Skip to content

Commit

Permalink
Update TVM Overview to point to the new (empty) TVM Initialization ar…
Browse files Browse the repository at this point in the history
…ticle

And a small polish of TVM Overview contents
  • Loading branch information
novusnota committed Jul 14, 2023
1 parent f4c081b commit 49637a2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
1 change: 1 addition & 0 deletions docs/learn/tvm-instructions/tvm-initialization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

35 changes: 11 additions & 24 deletions docs/learn/tvm-instructions/tvm-overview.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# TVM Overview



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.

This document provides a bird's-eye overview of how TVM executes transactions.
This document provides a bird's-eye view of how TVM executes transactions.

:::tip

Expand All @@ -14,29 +12,29 @@ This document provides a bird's-eye overview of how TVM executes transactions.
:::

## Transactions and phases
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.
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.
Each transaction consists of up to 5 phases:
1. **Storage phase** - in this phase, storage fees accrued by the contract due to the occupation of some space in the chain state are calculated. Read more in [Storage Fees](/develop/smart-contracts/fees#storage-fee).
2. **Credit phase** - in this phase, the balance of the contract with respect to a (possible) incoming message value and collected storage fee are calculated
3. **Compute phase** - in this phase, TVM is executed (see below), the result of the TVM execution is an aggregation of `exit_code`, `actions` (serialized list of actions), `gas_details`, `new_storage` and some others.
4. **Action phase** - if the compute phase was successful, in this phase `actions` from the compute phase are processed. In particular, actions may include the sending of messages, updating the smart contract code, updating the libraries etc. Note that some actions may fail during processing (for instance 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).
4. **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).
5. **Bounce phase** - if the compute phase failed (it returned `exit_code >= 2`), in this phase, _bounce message_ is formed for transactions initiated by an incoming message.

## Compute phase
In this phase, the TVM execution occurs.

### TVM state
At any one moment, the TVM state is fully determined by 6 properties:
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 sequence of instructions which is currently executed
* Current continuation - object which describes a currently executed sequence of instructions
* Current codepage - to put it simply, this means the version of TVM which is currently running
* Gas limits - a set of 4 integer values; the current gas limit g<sub>l</sub>, the maximal gas limit g<sub>m</sub>, the remaining gas g<sub>r</sub> and the gas credit g<sub>c</sub>
* Library context - the hashmap of libraries which can be called by TVM

### TVM is a stack machine
TVM is a last-input-first-output stack machine. There are 7 types of variables which may be stored in stack:
TVM is a last-input-first-output stack machine. In total, there are 7 types of variables which may be stored in stack — three non-cell types:
* Integer - signed 257-bit integers
* Tuple - ordered collection of up to 255 elements having arbitrary value types, possibly distinct.
* Null
Expand All @@ -57,21 +55,10 @@ And four distinct flavours of cells:
* `c7` — Contains the root of temporary data. It is a Tuple.

### Initialization of TVM
So when a transaction execution gets to the Computation phase, TVM initialises and then executes commands (opcodes) from _Current continuation_ until there are no more commands to execute (and no continuation for return jumps).

A detailed description of initialization can be found in [TON Blockchain 4.4](https://ton.org/tblkch.pdf).
For ordinary transactions caused by message the initial state is as follows:
* stack: 5 elements are put to the stack
* The balance of the smart contract (after crediting the value of the inbound message) is passed as an Integer amount of nanotons.
* The balance of inbound message `m` is passed as an Integer amount of nanotons.
* The inbound message is passed as a cell, which contains a serialized value of type Message X, where X is the type of the message body.
* The body of the inbound message, equal to the value of field body of `m`, is passed as a cell slice
* The function selector `s`, an Integer: `0` for tx caused by internal messages, `-1` for external, etc. Generally speaking it is the integer that identifies which event caused the transaction
* Current continuation: continuation converted from the `code` section of the smart contract
* Registers initialize as follows: **c0** is initialized by extraordinary continuation `ec_quit` with parameter 0. **c1** is initialized by extraordinary continuation `ec_quit` with parameter 1. **c2** is initialized by extraordinary continuation `ec_quit_exc`. **c3** is initialized by the cell with the smart-contract code. **c4** contains the cell from the `data` section of the smart contract. **c5** contains an empty list (it is serialized as a cell which contain last action in list plus reference to prev one) of output actions. **c7** is initialized as tuple with some basic blockchain context data such as time, global config, block_data, etc. See [TON Blockchain 4.4.10](https://ton.org/tblkch.pdf)
* Current codepage is set to default value (cp=0)
* Gas limits are initialized in accordance to Credit phase results
* Library context is initialized as result of merging this smart contract library collection, masterchain global library collection and incoming (if any) message library collection

TVM initializes when transaction execution gets to the Computation phase, and then executes commands (opcodes) from _Current continuation_ until there are no more commands to execute (and no continuation for return jumps).

Detailed description of the initialization process can be found here: [TVM Initialization](/learn/tvm-instructions/tvm-initialization.md)

## TVM instructions

Expand Down
1 change: 1 addition & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ const sidebars = {
label: 'TON Virtual Machine (TVM)',
items: [
'learn/tvm-instructions/tvm-overview',
'learn/tvm-instructions/tvm-initialization',
'learn/tvm-instructions/tvm-exit-codes',
'learn/tvm-instructions/instructions',
'learn/tvm-instructions/tvm-upgrade-2023-07',
Expand Down

0 comments on commit 49637a2

Please sign in to comment.