TRON Virtual Machine & Contracts

State Machine

TRON operates as a distributed state machine. This architecture allows for the execution of complex and customizable functions through smart contracts, making TRON a formidable platform for building decentralized applications (dApps). Let’s delve into the technical details of TRON‘s state machine and its key components.

Transitions

At the heart of TRON’s distributed state machine is the Transaction Virtual Machine (TVM), which behaves like a mathematical function. Given an input, it produces a deterministic output, making it a state transition function denoted as:

Y(S, T) = S'
where:
  Y = State transition function
  S = Old state
  T = New set of valid transactions
  S'= New valid state

The TVM, and, in essence, also the above function are responsible for processing transactions, executing smart contracts, and updating the internal state of the TRON network.

Structure

In TRON, the state is represented by a massive data structure called a Merkle Patricia Trie. This data structure links all accounts using hashes and is reducible to a single root hash stored on the blockchain. This enables fast and secure data management.

Signed Instructions

Transactions in TRON are cryptographically signed instructions from accounts. They are divided into two categories: system contract transactions and smart contract transactions. Smart contract transactions include those that result in contract message calls and those that result in contract creation.

Contracts

Creation

Contract creation in the TRON network results in the creation of a new contract account containing compiled smart contract bytecode. This bytecode is stored on the blockchain and can be executed by the TVM whenever another account makes a message call to that contract account.

Opcodes

Compiled smart contract bytecode in TRON executes as a series of opcodes. These opcodes execute standard stack operations like XOR, AND, ADD, SUB, etc. Additionally, the machine implements a range of blockchain-specific stack operations, such as ADDRESS, BALANCE, BLOCKHASH, and others, to enable smart contract functionality.

Execution

TRON executes smart contract bytecode utilizing a stack machine known as the Ethereum Virtual Machine (EVM). The EVM operates with a depth of 1024 items, with each item being a 256-bit word. This choice of word size aligns with the use of 256-bit cryptography, such as Keccak-256 (SHA-3) hashes or secp256k1 signatures, for enhanced security capabilities.


Smart contracts in the TRON network have certain properties. First, they are permissionless, meaning anyone can write and deploy a smart contract on the TRON network by learning how to code in a smart contract language and having enough TRX to deploy the contract. Deploying a smart contract is considered a transaction and requires resource fees. TRON provides a developer-friendly language called Solidity for writing smart contracts, but they must be compiled before being deployed for interpretation and storage by the TRON virtual machine.

Another important property of smart contracts in the TRON network is composability. Smart contracts are public and can be considered as open APIs, allowing other smart contracts to be called within a smart contract to greatly extend their capabilities. Contracts can even deploy other contracts, eliminating the need to start from scratch when developing dApps. For example, existing smart contracts of decentralized exchanges like SunSwap can be used to handle token swap logic in custom apps without starting from scratch.

However, smart contracts in the TRON network also have limitations. They are unable to communicate directly with external systems, which means they cannot obtain information about real-world events. This limitation is intentional, as relying on external information could compromise consensus, which is crucial for security and decentralization. Oracles can be used to overcome this limitation.

Another limitation is the maximum execution time of smart contracts. To ensure high throughput and stable operation of the network, TRON sets the maximum execution time of the TRON Virtual Machine (TVM) to 80ms, ensuring that the network can generate a new block every 3 seconds.