in

Ethereum Virtual Machine (EVM) Explained in Depth

The Ethereum Virtual Machine (EVM) is the runtime environment that executes smart contracts on the Ethereum blockchain. It provides a isolated sandboxed environment where smart contracts can run without access to each other‘s state. The EVM is a foundational component of Ethereum that enables it to be a programmable blockchain.

In this comprehensive guide, we‘ll cover everything you need to know about EVM including:

  • What is the Ethereum Virtual Machine (EVM)?
  • How the EVM works
  • EVM architecture and components
  • Opcodes and gas in EVM
  • Benefits and limitations of EVM
  • EVM upgrades and proposals
  • The future of EVM

What is the Ethereum Virtual Machine (EVM)?

The Ethereum Virtual Machine (EVM) is a 256-bit virtual machine that serves as the runtime environment for smart contracts in Ethereum. It provides a sandboxed execution environment isolated from the main Ethereum blockchain. The EVM executes bytecode, enabling the deployment of smart contracts on the Ethereum blockchain.

Smart contracts are programs that run on Ethereum. They are compiled down to bytecode that is then executed on the EVM. The EVM ensures determinism and provides security guarantees like isolation of contracts. All transactions on Ethereum involve the execution of smart contract bytecode by the EVM.

The EVM can be thought of like a distributed virtual computer with almost unlimited power. It‘s distributed because every full Ethereum node runs an instance of the EVM to validate transactions and execute smart contracts. And it‘s incredibly powerful because it‘s Turing complete – able to execute arbitrary computation given enough resources.

How the EVM Works

When a user sends a transaction to the Ethereum blockchain that interacts with a smart contract, the following steps occur:

  1. The smart contract bytecode and transaction data are bundled together into a transaction.

  2. The transaction is propagated across Ethereum network nodes.

  3. Miners pick up these transactions and execute the smart contract bytecode on the EVM as part of the validation process.

  4. The EVM executes the bytecode, reading/writing data from the Ethereum state and performing computations.

  5. Once execution finishes, the EVM returns any outputs and state changes.

  6. Miners include the execution results, state changes, logs and other metadata in a new block added to the blockchain.

This execution on the EVM is completely isolated from other contracts for security. The only way contracts can communicate is via messages passed between contract calls.

EVM Architecture and Components

The Ethereum Virtual Machine consists of four core components:

  • Stack – The EVM stack is a last-in-first-out (LIFO) stack with a maximum size of 1024. It stores the state of computations like function arguments, return values and local variables.

  • Memory – The memory unit stores byte arrays of data like contract state and variables. It is volatile and cleared between transactions.

  • Storage – The storage unit contains the permanent storage contents of a contract like state variables. It persists between function calls and transactions.

  • Machine code – The machine code defines the smart contract bytecode that is currently being executed. It includes the contract creation code and runtime code.

In addition, the EVM relies on several data structures that are external to it:

  • Ethereum state – Global world state containing account balances, contract storage, contract code and other system level metadata

  • Transaction data – Data payload attached to each transaction like function arguments and ether value.

  • Block information – Information about the current block like timestamp, block number and more.

  • Message call stack – Allows for synchronous function calls between contracts.

Opcodes and Gas in EVM

Smart contracts are compiled down to EVM bytecode that consists of human-readable opcodes like ADD (addition), MUL (multiplication), SSTORE (storage write), etc.

These opcodes are what actually get executed by the EVM. Each opcode has an associated gas cost based roughly on its complexity and resource utilization. For example, SSTORE costs 20000 gas because it uses costly storage I/O. While ADD only costs 3 gas.

This gas cost ensures that execution always terminates and limits potential spam. The total gas cost of a transaction must be prepaid by the sender. Gas also incentivizes nodes to execute contracts and serves to compensate them for the computational resources consumed.

There are over 140 different opcodes that allow the EVM to execute complex computations. The full list can be found in the Ethereum Yellow Paper.

Benefits and Limitations of EVM

The Ethereum Virtual Machine provides several benefits:

  • Deterministic execution – Opcodes execute in a predefined way, allowing nodes to arrive at consensus on the execution outcome.

  • Isolation of contracts – Contracts run independently without access to each other‘s persistent state. This provides security and sandboxing.

  • Turing completeness – The EVM can execute arbitrary computations, allowing it to run general purpose programs.

  • Established tooling – EVM bytecode and the Solidity language are well-established and supported by many dev tools.

However, the EVM also has some limitations:

  • Lack of scalability – The EVM relies on every node executing every transaction which limits transaction throughput.

  • High gas costs – Computation and storage on the EVM is expensive, making complex contracts costly.

  • Hard forks required – EVM upgrades require hard forks because the EVM exists as part of the Ethereum protocol.

  • Security issues – The downside of Turing completeness is the potential for bugs and security issues in smart contracts.

EVM Upgrades and Proposals

There are several proposals by Ethereum researchers to improve and upgrade the EVM:

  • eWASM – Using WebAssembly as an alternative to EVM bytecode to allow contracts in different languages.

  • EVM 2.0 – Introduces improvements like a new opcode metering model, cheaper SSTORE opcode, and bloom filters.

  • ETP – The Ethereum Transaction Processor proposal outlines upgrades like parallel transaction execution and hardware acceleration.

  • Stateless EVM – Detaches execution from the state root to improve scalability through state distribution.

  • EVM Object Format (EOF) – New standardized format for EVM code that is more compact and efficient.

  • Rent – Discourage spam by requiring contracts to periodically pay rent for the storage they use.

While most of these are still proposals, they give a sense for where Ethereum researchers see the EVM evolving in the future.

The Future of EVM

The Ethereum roadmap outlines several big milestones related to the continued improvement of the EVM:

  • Sharding – Splitting the Ethereum state across multiple shards will enable parallel transaction execution and higher throughput.

  • Rollups – Rollup-based layer 2 scaling relies heavily on the EVM by bundling transactions off-chain and submitting proofs to mainnet.

  • Proof-of-Stake – The transition to PoS will remove the need for mining and allow new validator-based execution models.

  • Fee markets – EIP-1559 introduced base fees for gas that dynamically adjust based on demand. This improves the economics of gas.

  • Storage rent – The introduction of storage rent will help optimize utilization of the EVM by attaching real costs to storage.

The EVM will need to continue evolving to meet the scalability and performance demands of Ethereum‘s growth. But it‘s proven to be a foundational and resilient component of Ethereum‘s design. The future looks bright for new EVM innovations that can enable wider blockchain adoption.

Conclusion

The Ethereum Virtual Machine is a Turing complete distributed virtual machine that serves as the runtime for smart contracts in Ethereum. It provides security guarantees, sandboxing, determinism and allows arbitrary computation. The EVM executes bytecode through an instruction set of opcodes that trigger computations in a stateful environment.

While the EVM has some limitations currently, it‘s also extremely powerful and has first mover advantage. Ongoing research is rapidly improving the EVM‘s capabilities. And it will continue playing a central role in unlocking the potential of smart contract development.

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.