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. [
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.