Ethereum Virtual Machine (EVM) is the runtime environment for executing smart contracts on the Ethereum blockchain. This guide dives deep into how EVM processes transactions, manages gas fees, and executes contract operations.
How EVM Processes Transactions
When a transaction is submitted to Ethereum, it undergoes the following steps:
- Transaction Conversion: The transaction is converted into a
Messageobject and passed to the EVM for execution. Execution Types:
- For simple transfers: Directly updates account balances in the
StateDB. - For smart contracts: EVM's interpreter loads and executes bytecode, potentially querying or modifying the
StateDB.
- For simple transfers: Directly updates account balances in the
1. Intrinsic Gas: Fixed Transaction Costs
Every transaction incurs a base gas fee:
- Standard transfers: 21,000 gas (no payload data).
Smart contracts: Additional fees apply for payload data:
- Zero-byte: 4 gas per byte.
- Non-zero byte: 68 gas per byte.
Optimization Tip: Minimize non-zero bytes in contract data to reduce gas costs.
2. Contract Object Creation
The EVM generates a Contract object from the Message:
- Loads contract code from
StateDBusing the contract address. - Sets execution gas limits based on the block's
GasLimitconfiguration.
3. Interpreter Execution Workflow
EVM operates as a stack-based virtual machine with four core components:
- PC (Program Counter): Tracks the current instruction.
- Stack: 256-bit width, max depth of 1,024.
- Memory: Temporary data storage.
- Gas Pool: Tracks remaining execution budget.
Execution Flow:
- Fetch
OpCode(1-byte instruction) from contract code. - Retrieve corresponding operation from a
JumpTable. - Deduct gas cost; fail if insufficient.
- Execute instruction (modifies stack, memory, or
StateDB).
Example: PUSH1 (0x60) pushes data to the stack; MSTORE (0x52) writes to memory.
4. Calling Contract Functions
Function calls are identified by 4-byte signatures in transaction input:
- First 4 bytes: Keccak-256 hash of the function signature (e.g.,
0x87db03b7foradd(uint256)). - Remaining bytes: Function arguments.
Function Selection Logic:
- Compiler prepends bytecode with a dispatcher.
- Uses
CALLDATALOADto compare signatures and jump to matched functions.
5. Data Loading Instructions
Four key instructions for data handling:
CALLDATALOAD: Loads input to stack.CALLDATACOPY: Copies input to memory.CODECOPY: Copies current contract code to memory.EXTCODECOPY(rare): Copies external contract code (for audits).
6. Contract-to-Contract Calls
Four call methods with distinct behaviors:
CALL: Creates new execution context (default).CALLCODE: Shares caller’s storage.DELEGATECALL: Preserves originalmsg.sender.STATICALL: Prevents state modifications.
👉 Explore Ethereum smart contract interactions
7. Contract Creation
Deploying a contract involves:
- Address Generation:
Keccak256(RLP(sender_address, nonce))[12:](20-byte address). - State Object Creation: Stores code and initializes
storage trie. - Immutable Code: Once deployed, code cannot be modified (though storage can via
SSTORE).
8. Gas Calculation Rules
Gas costs follow Ethereum Yellow Paper specifications (see core/vm/gas.go). Key factors:
- Base fees per opcode.
- Memory expansion costs.
- Storage operation fees.
FAQs
Q1: Why does EVM use gas fees?
Gas prevents infinite loops and spam, ensuring network stability by pricing computational resources.
Q2: How can I reduce contract gas costs?
- Optimize bytecode (minimize non-zero bytes).
- Use cheaper opcodes (e.g.,
SSTOREoverMSTORE). - Batch transactions.
Q3: What happens if a transaction runs out of gas?
Execution reverts, but the sender still pays the gas consumed up to the failure point.
Q4: Can EVM execute non-Ethereum contracts?
No—EVM is Ethereum-specific, though compatible chains (e.g., Polygon) reuse its design.
👉 Master Ethereum development with advanced tutorials
Next Chapter: Ethereum Address Generation Algorithm
Learn how Ethereum addresses derive from public keys and Keccak-256 hashing.
### Key SEO Keywords:
1. Ethereum Virtual Machine (EVM)
2. Smart contract execution
3. Gas fee calculation
4. OpCode instructions
5. Contract deployment