Understanding the Ethereum Virtual Machine: A Comprehensive Guide with Diagrams

Fabi Async
6 min readJun 25, 2023

--

EVM Diagrams

Ethereum Virtual Machine or EVM is Ethereum’s computational engine that handles smart contract execution and deployment. Generally speaking, it defines the rules for computing a new valid state from block to block.

This blog will delve into the working of the Ethereum Virtual Machine.

¿What is a Virtual Machine?

First, you need to understand what a virtual machine is. The virtual machine is one part of our interpreter’s internal architecture. A high-level abstraction that simulates a physical machine based on the native operating system. They allow the same platform to run on many different hardware architectures and operating systems, which makes VM very suitable for distributed networks like Ethereum.

What is the Ethereum virtual machine in blockchain?

As the name indicates, a virtual machine is related to the virtualization of the actual computer. The EVM blockchain is designed to be the infrastructure for smart contracts based on Ethereum. Initially, built to avoid the Denial-of-service attacks that pose a grave threat to the crypto space then.

Execution process of Ethereum Virtual Machine

EVM is similar on a high level, where developers code in Solidity (or other languages like Vyper) and compile down to EVM bytecode and EVM is a runtime engine that executes EVM bytecode.

Its primary function is to compute the network’s status and to execute and compile different types of smart contract code into a human-readable format known as “Bytecode.”

Blockchain ecosystem diagram

In the world of blockchain, virtual machines are mainly responsible for running smart contracts and providing an operating environment for them. The execution code of smart contracts is distributed and stored on the computer of each node, which solves the problem of interactive instructions (decentralised execution).

In this blog describe the eight major characteristics of EVM.

A virtual machine can be accessed from anywhere in the world through participating Ethereum nodes. Lending computing power to the network is voluntary and is governed by several incentives.

EVM business flow chart

EVM instructions

The EVM is a simple stack-based architecture

The Ethereum Virtual Machine (EVM) is a type of virtual machine that operates on a concept called a stack. Imagine a stack of items where you can only access the top item. The EVM follows a last-in, first-out (LIFO) structure, meaning the last item that enters the stack is the first one to be processed.

The EVM’s stack has a depth limit of 1024 items, and each item in the stack is a 256-bit word. This choice of word size makes it compatible with cryptographic operations like hashing or digital signatures.

While the EVM is running and executing transactions, it keeps track of a temporary memory, which is like a short-term storage for the computations being performed. However, this memory is not persistent and does not retain information between different transactions.

On the other hand, contracts (smart contracts) in the Ethereum network have a storage component known as a Merkle Patricia storage trie. This trie is a data structure that allows efficient storage and retrieval of information associated with a specific account. It forms part of the overall global state of the blockchain.

When a smart contract is compiled, it is transformed into bytecode, which consists of a series of instructions known as opcodes. These opcodes represent various operations that the EVM can perform, such as mathematical calculations (e.g., XOR, AND, ADD, SUB) and blockchain-specific actions like retrieving an account’s address, balance, or block information.

A “stack machine” and a “state machine”

A schematic representing EVM

The Ethereum Virtual Machine (EVM) is not only a virtual machine but also a “stack machine” and a “state machine.” A “state machine” is a concept that takes inputs, processes them, and transforms them into a new state based on those inputs. In the case of the EVM, it reads inputs (such as transactions or smart contract invocations) and updates the state of the Ethereum network accordingly.

The EVM is also referred to as a “stack machine” because it organizes its memory structure using a stack. A stack is a data structure where items are added or removed from the top. The EVM accesses and manipulates data in the memory using this stack-based approach.

At a higher level, the EVM is composed of three main components:

  • the world state
  • machine state
  • virtual ROM

The world state represents the storage of all accounts on the Ethereum network, keeping track of their balances, contract code, and other relevant data.

The machine state includes essential elements such as the program counter (keeps track of the current instruction being executed), available gas (the computational resource needed to execute operations), stack (for data manipulation), and memory (temporary storage during execution).

The virtual ROM is a read-only memory that contains the immutable “EVM bytecode,” which only the EVM can interpret and execute.

In the broader context of the blockchain ecosystem, the EVM operates at the contract level. It offers advantages such as high scalability, availability, and performance, enabling smart contracts to run smoothly. Smart contracts, written in languages like Solidity, are executed by the EVM, and their outcomes are recorded in the world state.

Gas acts as a unit to quantify the computational cost of running smart contracts

A schematic representing the gas on Ethereum via EVM

In the Ethereum Virtual Machine (EVM), gas serves as a measure of computational effort or complexity required to perform certain operations or execute transactions on the blockchain network. Gas acts as a unit to quantify the computational cost of running smart contracts or processing transactions.

In Ethereum, every operation or transaction consumes a certain amount of gas, which is determined by its complexity and resource requirements. Gas is essential to prevent abuse and ensure the efficient allocation of network resources. It helps incentivize users to prioritize their computations and transactions, as they need to pay for the corresponding gas cost.

Gas also plays a role in preventing denial-of-service (DoS) attacks. By requiring users to pay for the computational resources they consume, the Ethereum network can mitigate excessive resource consumption and ensure fair and sustainable operation.

In conclusion, the Ethereum Virtual Machine (EVM) is a pivotal component within the Ethereum blockchain, serving as the computational engine for executing and deploying smart contracts. With its stack-based architecture, EVM bytecode, and unique characteristics, it provides a powerful and secure environment for running decentralized applications.

References:

https://ethereum.org/en/developers/docs/evm/

https://craftinginterpreters.com/a-virtual-machine.html

https://caduceusmetaverseprotocol.medium.com/the-benefits-of-ethereum-virtual-machine-evm-compatibility-fc0033360c54

https://medium.com/coinmonks/ethereum-virtual-machine-ea62cdea954d

https://www.immunebytes.com/blog/ethereum-virtual-machine-in-blockchain/

https://ethereum.org/en/developers/docs/evm/

--

--