EVM

Notes on evm


Starting from scratch

What is it?

It is the core engine of Ethereum

  • turing complete
  • sandboxed -> operates in an isolated environment, ensure SC exec does not interfere with others or the blockchain.
  • designed to execute contracts on the network
  • uses gas to regulate the usage of resources and network abuse

Components:

  • World State -> Represents state of ETH as well as accounts and associate storage
  • Accounts -> Entities on the ETH network. Includes Contract accoutn and EOA (externally owned account)
  • Storage -> key-value associated with contract accounts, storing its state and data
  • Gas
  • Opcodes -> Low-level ixs executed by the EVM when processing a contract
  • Execution Stack -> LIFO data structure taht temporarily stores values during execution
  • Memory -> Runtime memory used by SC during execution
  • Program counter -> Registry that keeps track of position of the next opcode to be executed
  • Logs -> Events emited by SC during execution

Execution Model:

  • Tx is sent
  • EVM verifies its validity
  • If invalid, reject
  • If valid, setup execution context with current state of the networrk
  • Process bytecode of the contract using opcodes and cosumes gas accordingly, upating the state and generating Logs
  • If an error occurs (ex: out of gas), revert the state to the previous one and return the error

Stack and Memory:

  • Stack is a LIFO data structure that stores values during execution

  • Managed by EVM and separated from contract's storage

  • Supports 2 actions -> push and pop. One adds vale to the top of the stack, the other removes it

  • Used for storing temporary data during opcode execution -> For example, an opcode that performs an addition operation might push the two operands onto the stack, perform the addition, and then pop the result off the top of the stack.

  • During execution, memory serves the purpose of temporarily storing data during opcode execution. It is a region of memory that is allocated to the EVM and is used to store data that is not stored in the contract's storage.

EVM Architecture:

  • Ethereum is a turing complete language and a state machine -> Meaning it can be at exactly one of a finite number of states at any given time. Changing from one state to the other in response to some inputs, this is called transition.

  • Begins with tx initiation

  • the prograsses to gas computations for each operation

  • EVM's stack, memory and storage are enagaged during execution to manage and persist data during the lifecylce of the tx

  • check validation at each step to ensure validity of operations and safeguard integrity of the network.

  • The state of ETH is a gigantic data structure that keeps all account liked by hashes and reducible to a root hash stored on the blockchain

EVM

READ -> https://takenobu-hs.github.io/downloads/ethereum_evm_illustrated.pdf

Links

EVM Basics EVM Architecture