EVM
What is EVM?¶
EVM (Ethereum Virtual Machine) is one of the core components of Ethereum. It is a Turing-complete virtual machine that allows anyone to run code on the blockchain through smart contracts.
The relationship between Solidity and EVM is similar to that between Java and JVM. Thanks to the EVM, Ethereum functions as a global, permissionless computer.
EVM Execution Principles¶
- Contract Compilation/Deployment: Smart contracts are typically written in Solidity, then compiled into EVM bytecode. The compiled bytecode is deployed to the Ethereum chain via a transaction.
- Transaction Execution: Each time a smart contract method is called, a transaction is generated containing the function to execute (selector) and the associated input data.
- Gas Calculation: In the EVM, executing each instruction requires a certain amount of Gas. The Gas mechanism prevents network resource abuse. Each transaction sender must specify the maximum amount of Gas they are willing to pay when initiating a transaction.
- Instruction Execution: The EVM is stack-based in architecture. After bytecode is loaded into the EVM, each instruction is executed sequentially. These instructions can perform arithmetic operations, stack operations, storage operations, conditional jumps, and more.
- Storage and Memory: The EVM has two main storage areas: permanent Storage and temporary Memory. Permanent storage is used to save smart contract state and is stored on the blockchain. Temporary memory is used to store intermediate data during transaction execution and is cleared after the transaction completes.
- Result Return: After instruction execution is complete, the EVM returns the execution result. If execution succeeds, state changes and output results are recorded on the blockchain. If execution fails, all state changes are rolled back and an error message is returned.
- Account State Update: Based on the transaction execution result, the state of related accounts is updated (e.g., balance, stored data, etc.).
The key aspect of EVM execution is its decentralization and consistency. All nodes run the same EVM code, ensuring that every node arrives at the same result when processing the same transaction.
The EVM is a closed sandbox environment that can only read state from within the chain.
EVM-Compatible Chains¶
Due to the EVM's flexibility and powerful computational capabilities, it can support a wide variety of complex decentralized applications (DApps), which forms the foundation of the thriving Ethereum ecosystem.
This has led to the development of many EVM-compatible chains. These are blockchains that implement the EVM specification, enabling them to run smart contracts written for Ethereum and maintain compatibility with Ethereum's tools and infrastructure.
Common EVM-compatible chains include: BNB Chain (formerly Binance Smart Chain, BSC), Polygon (formerly Matic), Avalanche, and Fantom.
Further Reading¶
Article: Deep Dive into the Ethereum Virtual Machine
Column: Understanding the EVM - Exploring the Secrets Behind Solidity
Column: Ethereum EVM Puzzle Solutions