Blockchain - Ethereum Virtual Machine


The Ethereum Virtual Machine (EVM) operates as a straightforward stack-based execution environment that processes bytecode instructions to transition the system state from one condition to another.

While the EVM is Turing-complete, its operations are constrained by the gas required for executing instructions. This limitation prevents the occurrence of infinite loops that could lead to denial-of-service attacks. Additionally, the EVM is equipped with exception handling mechanisms to address potential issues, such as insufficient gas or invalid instructions, causing the machine to halt and return an error to the executing agent.

Ethereum Virtual Machine Structure

The Ethereum Virtual Machine (EVM) utilizes a 256-bit word size, and its stack can accommodate up to 1,024 elements, functioning on a Last In, First Out (LIFO) principle. It operates as a completely isolated and sandboxed runtime environment. Code executed within the EVM is restricted from accessing external resources, including networks or filesystems.

This design enhances security, ensures deterministic execution, and permits the execution of untrusted codecode that can be run by any useron the Ethereum blockchain. As previously mentioned, the EVM utilizes a stack-based architecture. It is inherently big-endian and employs 256-bit-wide words, which facilitate Keccak 256-bit hashing and elliptic curve cryptography (ECC) computations.

Types of Storage

There are three primary types of storage utilized for contracts and the Ethereum Virtual Machine (EVM) −

Memory

The first type is referred to as memory or volatile memory, which functions as a word-addressed byte array. Once a contract completes its execution, the memory is reset. This is similar to RAM in traditional computing. Write operations can be performed in either 8 or 256 bits, while read operations are restricted to 256-bit words.

Storage

The second type is known as storage, which operates as a key-value store and is permanently recorded on the blockchain. Both keys and values are 256 bits in size. It can be compared to hard disk storage in conventional systems.

Stack

The EVM functions as a stack-based machine, executing all computations within a data area called the stack. All values held in memory are also stored in the stack. The stack has a maximum depth of 1024 elements and accommodates a word size of 256 bits.

Types of Storage in Ethereum Virtual Machine

Execution Environment

The execution environment necessitates several essential components to run the code effectively. These critical parameters are supplied by the execution agent, such as a transaction.

The following elements are required −

  • The current system state.
  • The available gas for execution.
  • The address of the account that owns the code being executed.
  • The address of the transaction sender, which serves as the origin for this execution and may differ from the sender's address.
  • The gas price associated with the initiating transaction.
  • Input data or transaction data, depending on the executing agent type. This is represented as a byte array.
  • The address of the account that initiated the code execution or the transaction sender.
  • The value or transaction value, expressed in Wei. If the execution agent is a transaction, this represents the transaction value.
  • The code to be executed, provided as a byte array, which the iterator function retrieves during each execution cycle.
  • The block header of the current block.
  • The count of message calls or contract creation transactions (CALLs, CREATEs, or CREATE2s) that are currently being executed.
  • Authorization to modify the state.
Execution Environment

The Machine State

The internal maintenance of the machine state occurs, with updates made following each execution cycle of the EVM. An iterator function, which will be elaborated on in the subsequent section, operates within the EVM to produce the results from a single cycle of the state machine.

The machine state is represented as a tuple containing the following components −

  • Available gas
  • The program counter, a positive integer that can be as large as 256
  • The memory contents, represented as a series of zeroes totaling 2256 in size
  • The count of active words in memory, continuously tracked from position 0
  • The contents of the stack.

Iterator Function

The Iterator function carries out several essential tasks that are crucial for determining the next state of both the machine and the overall world state. These tasks encompass the following −

  • It retrieves the next instruction from a byte array that contains the machine code within the execution environment.
  • It appropriately adds or removes items from the stack using PUSH or POP operations.
  • Gas consumption is adjusted based on the gas costs associated with the instructions or opcodes, and the Program Counter (PC) is incremented accordingly.

Additionally, the EVM has the capability to halt under normal circumstances when it encounters the STOP, SUICIDE, or RETURN opcodes during its execution cycle.