TVM vs EVM
Ethereum Virtual Machine (EVM) and TON Virtual Machine (TVM) are both stack-based virtual machines developed for running smart contract code. Although they have common features, there are notable distinctions between them.
Data presentation
Ethereum Virtual Machine (EVM)
- Fundamental Data Units
- The EVM operates primarily on 256-bit integers, reflecting its design around Ethereum's cryptographic functions (e.g., Keccak-256 hashing and elliptic curve operations).
- Data types are limited mainly to integers, bytes, and occasionally arrays of these types, but all must conform to 256-bit processing rules.
- State Storage
- The entire state of the Ethereum blockchain is a mapping of 256-bit addresses to 256-bit values. This mapping is maintained in a data structure known as the Merkle Patricia Trie (MPT).
- The MPT enables Ethereum to efficiently prove the consistency and integrity of the blockchain state through cryptographic verification, which is vital for a decentralized system like Ethereum.
- Data Structure Limitations
- The simplification to 256-bit word constraints means that the EVM is not inherently designed to handle complex or custom data structures directly.
- Developers often need to implement additional logic within smart contracts to simulate more complex data structures, which can lead to increased gas costs and complexity.
TON Virtual Machine (TVM)
- Cell-Based Architecture
- TVM uses a unique "bag of cells" model to represent data. Each cell can contain up to 128 data bytes and can have up to 4 references to other cells.
- This structure allows the TVM to natively support arbitrary algebraic data types and more complex constructions such as trees or directed acyclic graphs (DAGs) directly within its storage model.
- Flexibility and Efficiency
- The cell model provides significant flexibility, enabling the TVM to handle a wide variety of data structures more naturally and efficiently than the EVM.
- For example, the ability to create linked structures through cell references allows for dynamic and potentially infinite data structures, which are crucial for certain types of applications like decentralized social networks or complex decentralized finance (DeFi) protocols.
- Complex Data Handling
- The ability to manage complex data types inherently within the VM architecture reduces the need for workaround implementations in smart contracts, potentially lowering the execution cost and increasing execution speed.
- TVM's design is particularly advantageous for applications requiring complex state management or interlinked data structures, providing a robust foundation for developers to build sophisticated and scalable decentralized applications.
Stack machine
Ethereum Virtual Machine (EVM)
- The EVM operates as a traditional stack-based machine, where it uses a last-in, first-out (LIFO) stack to manage computation.
- It processes operations by pushing and popping 256-bit integers, which are the standard size for all elements in the stack.
TON Virtual Machine (TVM)
- TVM also functions as a stack-based machine but with a key distinction: it supports both 257-bit integers and references to cells.
- This allows TVM to push and pop these two distinct types of data onto/from the stack, providing enhanced flexibility in direct data manipulation.
Example of stack operations
Suppose we want to add two numbers (2 and 2) in EVM. The process would involve pushing the numbers onto the stack and then calling the ADD
instruction. The result (4) would be left on the top of the stack.
We can do this operation in the same way in TVM. But let’s look at another example with more complex data structures, such as hashmaps and cell reference. Suppose we have a hashmap that stores key-value pairs, where keys are integers and values are either integers or cell references. Let’s say our hashmap contains the following entries:
{
1: 10
2: cell_a (which contains 10)
}
We want to add the values associated with keys 1 and 2 and store the result with key 3. Let’s look at stack operations:
- Push key 1 onto the stack:
stack
= (1) - Call
DICTGET
for key 1 (retrieves the value associated with the key at the top of the stack): Retrieves value 10.stack
= (10) - Push key 2 onto the stack:
stack
= (10, 2) - Call
DICTGET
for key 2: Retrieves reference to Cell_A.stack
= (10, Cell_A) - Load value from Cell_A: An instruction to load the value from the cell reference is executed.
stack
= (10, 10) - Call the
ADD
instruction: When theADD
instruction is executed, the TVM will pop the top two elements from the stack, add them together, and push the result back onto the stack. In this case, the top two elements are 10 and 10. After the addition, the stack will contain the result:stack
= (20) - Push key 3 onto the stack:
stack
= (20, 3) - Call
DICTSET
: Stores 20 with key 3. Updated hashmap:
{
1: 10,
2: cell_a,
3: 20
}
To do the same in EVM, we need to define a mapping that stores key-value pairs and the function where we work directly with 256-bit integers stored in the mapping. It’s essential to note that the EVM supports complex data structures by leveraging Solidity, but these structures are built on top of the EVM’s simpler data model, which is fundamentally different from the more expressive data model of the TVM