12 min read
Overview
Ethereum processes billions of dollars in transactions daily (source), requiring developers to understand not just Solidity but also the Ethereum Virtual Machine (EVM). This guide explores EVM's execution of bytecode (opcodes), its components, and step-by-step transaction execution.
What You Will Need
- Basic knowledge of EVM and Ethereum smart contracts.
Recommended reading:
What You Will Do
- Learn about EVM components (Stack, Memory, Storage).
- Understand opcodes and their execution.
What Is a Virtual Machine (VM)?
A VM emulates physical hardware, enabling software to run isolated environments. VMs operate via machine code (binary) represented by opcodes like ADD, PUSH, or POP.
What Is the EVM?
The EVM executes Ethereum’s smart contract opcodes, defined in the Ethereum Yellow Paper. Key components:
- Stack: Holds 32-byte data for computations (LIFO structure).
- Memory: Temporary storage for arrays/strings during execution.
- Storage: Persistent state (account balances, contract code).
- Program Counter (PC): Tracks the next opcode to execute.
- Gas: Measures computational effort (e.g.,
SSTOREcosts more thanADD).
EVM Architecture (Source: Ethereum.org)
Ethereum State Transition
The EVM’s deterministic function Y(S, T) = S' ensures transactions yield predictable results.
EVM Opcodes
Bytecode translates to opcodes instructing EVM components. Examples:
- Arithmetic:
ADD,MUL. - Control Flow:
JUMP,JUMPI. - Ethereum-Specific:
CREATE,CALL.
Full list: EVM Opcodes.
From Solidity to Bytecode
Example contract:
contract SimpleStorage {
uint storedData;
function set(uint x) public { storedData = x; }
function get() public view returns (uint) { return storedData; }
}Compiled bytecode (hex):
6080604052348015600e575f80fd5b506101438061001c5f395ff3fe...Decompiled opcodes:
[00] PUSH1 80
[02] PUSH1 40
[04] MSTORE
...👉 Explore opcodes interactively
Transaction Execution Flow
- Decode Transaction: Validate nonce/signature (
v,r,s). - Execute Opcodes: Sequentially run bytecode (e.g.,
PUSH4for function calls). - Update State: Store results in persistent
Storage.
Example calldata for set():
0x60fe47b10000000000000000000000000000000000000000000000000000000000010f2c60fe47b1: Function selector.- Remaining bytes: Argument (uint).
FAQ
Q: Why does the EVM use gas?
A: Gas prevents spam and ensures efficient resource use.
Q: What’s the max contract size?
A: 24 KB, but stored data is limited by gas, not size.
Q: How is bytecode stored?
A: In contract account state fields (codeHash).
Final Thoughts
Understanding EVM opcodes deepens smart contract development. For a deeper dive, explore:
Join us on Discord or follow Twitter for updates!