Understanding The Ethereum Virtual Machine (EVM)
The EVM powers decentralized, censorship-resistant applications running on the Ethereum protocol.
Beyond enabling the exchange of value, Ethereum also facilitates the creation of “smart contracts.” Smart contracts are self-executing programs that power various applications running on the Ethereum blockchain today.
Non-fungible tokens (NFTs), decentralized finance (DeFi) platforms, blockchain games—all of these rely on smart contracts to work. However, smart contracts themselves can only run because the Ethereum Virtual Machine (EVM) exists.
The EVM is a computation engine that facilitates the deployment and operation of smart contracts. Without the EVM, it’d be impossible to execute software programs on the Ethereum protocol. Thus, the EVM is a critical part of Ethereum's core architecture.
This article offers an introduction to the Ethereum Virtual Machine (EVM). We cover the basics of the EVM, including components of its structure—like opcodes and smart contracts.
What is the Ethereum Virtual Machine (EVM)?
To understand the Ethereum Virtual Machine, we must explore two concepts first: "virtual machines" and "Turing completeness."
Virtual machines are programs that simulate the behavior of a physical computer. A virtual machine has its storage and processing unit and runs as a process on your PC. It's a bit like having a computer running on another computer.
A VM is similar to your average Windows or MacOS software. The difference here is that VMs are designed to perform more high-level functions. Moreover, a VM—unlike a regular OS—has no access to other parts of your computer, like the storage or bandwidth.
If you've played Android games on a PC using an emulator, then a virtual machine may be familiar. While emulators and VMs are significantly different, they are both approximate the powers of hardware and can execute code in a “sandboxed” environment.
British Mathematician Alan Turing developed the first Turing Machine—the forerunner of today's computer. A Turing-complete machine can process any computation, no matter how complex, provided it has enough time and resources.
Now, let's tie these ideas together and define the Ethereum Virtual Machine:
The Ethereum Virtual Machine (EVM) is a giant virtual machine that allows for the deployment and execution of code. You only need to install the necessary client software to access the EVM and use it to execute programs on Ethereum. Essentially, the EVM acts as a “world computer” for performing software operations in a decentralized environment.
Devoid of centralized control, the EVM is sustained by several individuals/companies lending computing power to the system in exchange for incentives. Therefore, it is useful for creating censorship-resistant applications that cannot be unilaterally shut down by any party.
The EVM is Turing-complete, as it can be used to perform computations of varying complexity. This is what separates Ethereum from Bitcoin, as the latter is Turing-incomplete, limiting its functionality.
Bitcoin functions primarily as a "distributed ledger", which specifies rules for the transfer of value. In addition to handling the transfer of value, Ethereum (via the EVM) enables the deployment of smart contracts. As a result, Ethereum is described as a “distributed state machine.”
“State” refers to the information about a system at any point in time. In Ethereum, state refers to the addresses, account balances, and smart contract code existing at a specific moment. Each transaction causes a change in Ethereum's state (state transition) that's reflected throughout the network.
How Does the Ethereum Virtual Machine Work?
Here, we'll explore the basic building blocks of the Ethereum Virtual Machine. This is to provide a basic overview of the system, so some heavy technical details may be left out.
The EVM uses a series of instructions called "opcodes" to execute different tasks. There are 140+ opcodes enabling the execution of different processes in the EVM—hence, Ethereum's description as Turing-complete.
We need opcodes because the EVM cannot interpret instructions written in Solidity, the language used for coding smart contracts. Ergo, smart contract code are converted to opcodes, so they can be executed in the EVM environment.
For example, you can create a smart contract using the
CREATE opcode or halt a running contract with the
STOP opcode. You can check here for a detailed overview of opcodes in Ethereum.
Gas is the resource that enables the execution of code in the EVM environment, measured in wei (a unit of ether). Just like you need gas to power your car in real life, the EVM needs gas to execute operations.
Earlier, we mentioned "opcodes"—specific instructions that can be used to perform different operations in the EVM. Gas is simply the amount of computational resources required to perform a particular operation.
Every code execution carries a gas fee, which varies depending on many factors, such as the complexity of the operation and network-wide demand. Gas fees incentivize individuals to lend their computing power to Ethereum. Without gas fees, the EVM wouldn't function as a decentralized computer.
Gas serves another purpose: preventing the execution of malicious actions, like distributed denial-of-service (DDoS) attacks. While the EVM can run almost any computation, it's hard to predict the runtime for every operation.
A well-designed malicious operation can run infinitely, causing the network to lose scarce computing power and eventually crash. Gas fees prevent this problem by forcing malicious actors to pay for every step performed in the computational process.
Before initiating an operation, you must specify a “gas limit”—the maximum amount of gas you're willing to spend on a computation. Once the gas limit is exceeded, the computation halts immediately. Again, this stops anyone from deploying “infinite loop” computations that could threaten the health of Ethereum.
An overview of some gas fees in Ethereum. [image source]
Smart contracts are pieces of code that execute once a predefined set of parameters are met. A smart contract uses conditional programming (if y, then x) to perform operations.
Smart contracts are immutable, autonomous, and transparent. These features combined make smart contracts particularly appealing, although they are not without flaws.
'Immutability' means a smart contract's code cannot be altered once deployed on the blockchain. Smart contracts are autonomous because they can self-execute without external control. And they are transparent since the rules governing their performance are written in publicly available code.
Some smart contracts are used to create and exchange tokens on the blockchain. ERC-20 tokens, for instance, have a smart contract defining their naming, creation, exchange, total supply, and other attributes.
A transaction is an instruction from an Ethereum account. An example transaction is sending ether to an address from your wallet. Here, you're instructing the EVM to transfer value from your wallet to another location on the blockchain.
Not every transaction on Ethereum involves the transfer of value; some may transfer arbitrary data. The result of a transaction further depends on the recipient.
A regular, externally owned account (EOA) will simply receive the ether sent. However, a contract account may execute code once the transaction is successful. And, as explained earlier, transactions result in a ‘state transition function’, i.e., a change in Ethereum's state.
These elements make up the core of the EVM's infrastructure.
Features of Ethereum Virtual Machine
In programming, determinism is the ability of a program to produce the same output for a specific input at every instance. Determinism ensures that developers can design programs to perform specific operations and produce required results, independently.
The EVM is deterministic, so opcodes provide the same results no matter how many times the computation is performed. This is important as Ethereum's smart contract-powered dApps handle high-value transactions and must perform reliably. Or else, users wouldn't be confident to use them without expecting failures.
The EVM is isolated, meaning the code has no access to the processes on your computer. You can safely deploy programs in the EVM while protecting your hardware/software from potential problems.
Moreover, smart contracts operate in isolated environments within the EVM. Thus, bugs or hacks that affect a particular smart contract are kept from harming the underlying protocol.
The EVM is Turing-complete, so it can theoretically be used to perform any computation if it has the right resources and instructions. However, every computation relies on the gas allocated to it. If the gas runs out, then the operation ceases to run.
In this context, you can see Ethereum as “quasi-Turing complete” or “terminable” since code execution can terminate at specific times. However, this feature is important to ensure programs don't run forever (accidentally or maliciously) and stall the network.
Why is the Ethereum Virtual Machine (EVM) Important?
The Ethereum Virtual Machine is what makes the execution of smart contracts on the Ethereum protocol possible. These smart contracts underpin decentralized applications, tokens, and many of the projects running on the Ethereum blockchain.
Here is a detailed overview of the EVM's functions:
1. Providing a runtime environment for smart contracts
Without the EVM, deploying a smart contract would be futile. The EVM provides a safe, sandboxed environment for the execution of smart contract code.
2. Functioning as a decentralized processing unit
The EVM powers the Ethereum protocol and gives it the programmability necessary to create decentralized applications (dApps) Every transaction or smart contract execution is performed in the EVM.
3. Keeping track of state changes
The EVM monitors changes to Ethereum’s world state. As explained earlier in this article, state in Ethereum-speak refers to a description of the Ethereum protocol at any point in time. Ethereum's state comprises account balances, contract code, contract storage, and more.
Operations, like sending ether, executing code, or creating a contract, ause changes in state. And it is the EVM's job to update the Ethereum network state to reflect those changes.
The Ethereum Virtual Machine is at the heart of Ethereum's operation. Without the EVM, Ethereum cannot become an “Internet computer, powering decentralized applications available to users around the world.
However, the EVM still faces many problems, such as poor scalability, that limit its functionality. But planned upgrades to Ethereum, including sharding, promise to improve the EVM and expand its usage
Cover photo by Choong Deng Xiang on Unsplash.