What is the EVM?

Mar 25, 2022

The Ethereum Virtual Machine (EVM) is a deterministic, sandboxed runtime environment and computation engine where smart contracts deployed on Ethereum are executed. The term “virtual machine” is commonly used for a virtualization or emulation of a real computer, with the EVM only providing an abstraction of computation and storage and therefore running in a more limited domain than conventional virtual machines.

The EVM essentially defines the rules for computing a new valid state and serves as the foundation of Ethereum’s operational structure. It is an isolated virtual stack embedded within each Ethereum node, responsible for executing contract bytecode, compiled from higher level languages like e.g. Solidity. Hence, the state is only updated when the execution of the sandboxed version completes successfully. Isolation is important to contain hacks or bugs within a smart contract.

With each node managing the same algorithmic instructions to verify smart contract transactions and achieve and maintain consensus, the EVM is often characterized as a decentralized quasi-Turing-complete ‘world computer’.

Turing Completeness

A ‘Turing complete machine’ is an abstract, automatic computational machine that solves problems based on algorithmic rules and can basically run any program. Thus, the EVM is described as ‘quasi-Turing-complete’ because the EVM is able to halt execution after a predefined amount of computation is reached. Without any limiter, the EVM is at risk of running into the halting problem, a state where the EVM executes a program that never stops. The metric used to overcome the halting problem is gas, Ethereum’s unit for computational and storage effort, that also solves denial-of-service attacks (DoS).

How Does the EVM Work?

Like Bitcoin, the Ethereum blockchain facilitates and records the transfer of digital assets between user wallets, yet on top it also serves as a platform for users to deploy decentralized application (dApps) and smart contracts to manage conditional transactions on the network.

For Ethereum smart contracts to be executed correctly, each must be able to verify that prior conditions have been met. Hence, every node runs an EVM instance which allows them to agree on executing the same instructions.

Some smart contracts involve more complex conditionality, requiring inputs from multiple data sources, or calls of other smart contracts within their infrastructure. With that, the Ethereum Virtual Machine provides the means for all the current states of smart contracts to be tracked and executed on the blockchain. Practically, this means that when Ethereum users in different parts of the world seek to enter into an agreement with each other, the EVM facilitates the agreement between smart contracts and subsequent transfers of value – without any need for a centralized authority. As conditions are met and data/value are transferred, the EVM helps ensure that the correct, current state is maintained by the network.

How Does the EVM Store and Execute Smart Contracts

There are two types of accounts on the Ethereum blockchain: externally owned accounts (EOA) and contract accounts (CA). Information relevant to EOAs is controlled by private keys while information relevant to CAs is held within the smart contracts themselves. Simple value transfers from one EOA to another do not need to involve the EVM, but everything else will involve a state update computed by the EVM. Due to account abstraction, the EVM treats both accounts equally.

Smart contracts written in e.g. Solidity are compiled into bytecode that hold opcodes. These contain instructions for the EVM to execute specific tasks. For instance, certain opcodes manipulate the Ethereum stack, and some manipulate Ethereum’s storage. Every time an opcode is executed, the EVM consumes gas – the computational resource necessary to execute processes on the Ethereum blockchain. Users must pay for gas with the Ethereum digital token ETH in order to execute their smart contracts and facilitate transactions.

EVM Data Storage:

The EVM stores its smart contract data in three different areas: storage, memory, and the stack.

Storage: Every Ethereum account has a persistent storage area, part of Ethereum’s state, for data such as ‘derived calculation, caching, and aggregates outside the contract’. Contracts can only write data to their own storage areas and it is relatively costly in gas fees to read data held there.

Memory: Designed to hold temporary variables, the memory storage area is linear and is erased between calls on a smart contract. Memory expands quadratically and costs more as it scales, but is cheaper than storage.

The stack: All EVM computational activity takes place on the stack. This data storage area can hold up to 1024 local variables, with each stack item holding 256 bits. The stack is the cheapest (gas fee) storage area.

EVM-Compatibility

Due to Ethereum’s network effect, a growing number of other Layer 1 blockchains leverage the EVM and therefore benefit from already available developer tools and blockchain infrastructure. These include Polygon, Avalanche C-Chain, Binance Smart Chain, Fantom Opera and others. EVM compatibility is furthermore important for Layer 2 solutions such as Arbitrum or Optimism.

BTCS-logo-mark_rgb.png

Bitcoin Suisse