Skip to content

3. What is EVM and what it does

EVM: Ethereum Virtual Machine

Introduction

The Ethereum Virtual Machine (EVM) is the runtime environment for smart contracts in Ethereum. It's a crucial component of the Ethereum ecosystem, providing a sandboxed environment for executing code. All nodes in the Ethereum network run an EVM implementation, which allows them to agree on executing the same instructions.

Architecture

The EVM is a stack-based virtual machine. It executes bytecode – a series of opcodes (operation codes) – which are part of smart contracts compiled from high-level languages like Solidity. The EVM is designed to serve as a quasi-Turing-complete machine; it can execute most computations, given enough resources.

Single-Threaded Nature

A key characteristic of the EVM is its single-threaded execution model. This means that all operations, including smart contract executions, are processed sequentially. This sequential processing ensures consistent state changes across all nodes in the Ethereum network, crucial for maintaining network consensus.

EVM Memory

  1. Stack
  2. Volatility: Volatile. It exists only during the execution of a function.
  3. Cost: Relatively cheap in terms of gas usage.
  4. Access: Read-write, but with strict LIFO (Last In, First Out) access.
  5. Addressing: Word-addressable (256-bit words).

  6. Memory

  7. Volatility: Volatile. It's reset and cleared after each transaction.
  8. Cost: Increasingly expensive with expansion (gas cost increases linearly).
  9. Access: Read-write. It's a temporary storage area.
  10. Addressing: Byte-addressable, allowing for more granular access and manipulation.

  11. Storage

  12. Volatility: Permanent. It persists on the blockchain across transactions and function calls.
  13. Cost: Most expensive in terms of gas, especially on writes and modifications.
  14. Access: Read-write, used for storing contract state.
  15. Addressing: Word-addressable. Optimized for storing 256-bit values.

  16. Code

  17. Volatility: Permanent. The code of smart contracts is immutable once deployed.
  18. Cost: No gas cost for reading contract code.
  19. Access: Read-only. It cannot be modified after deployment.
  20. Addressing: Byte-addressable, which facilitates the execution of bytecode.

Machine State

The machine state of EVM includes:

  • Program Counter: Points to the current instruction.
  • Stack: Contains operand stack elements.
  • Memory Size: The current size of the active memory.
  • Gas: Represents the execution fee; each opcode consumes a certain amount of gas.
  • EVM State: Represents the current state of all accounts, including balances, storage, etc.

Types of Accounts in Ethereum

Externally Owned Accounts (EOA)

  • Control: Operated by private individuals using private keys.
  • Capabilities: Can initiate transactions (Ether transfers, smart contract interactions).
  • Characteristics: Do not contain EVM code. Gas is required for transactions.

Contract Accounts (Smart Contracts)

  • Control: Governed by smart contract code, not by a user.
  • Capabilities: Can execute complex operations, hold and transfer Ether, create other contracts.
  • Characteristics: Contain EVM code. Activated by transactions or messages (calls) received from other contracts.
  • Creation: Created by EOAs or other contract accounts.