The Ethereum Virtual Machine (EVM): What is it and How Does it Work?
A simple machine is generally defined as a physical device that makes work easier by reducing the effort applied by its user. A computer falls under this category as it performs complex processes, calculations, and operations based on inputs provided by its user. It performs complicated tasks on their behalf.
Similarly, a virtual machine (VM) is a computer resource that uses software instead of a physical computer to run programs and deploy applications. A VM generally does all the functions of a hardware computer but exists as software code. A virtual machine uses its own operating system, functioning independently from other virtual machines.
What is the Ethereum Virtual Machine?
With the above general definitions of machines and virtual machines, it is easier to connect the dots on what the Ethereum Virtual Machine (EVM) is.
The Ethereum Virtual Machine is a giant virtual machine that facilitates the deployment and execution of code. The EVM is often described as being Turing complete, which means that given enough time, memory, and necessary instructions, it can solve any computational task, no matter how complex.
The EVM allows developers to create decentralized applications on Ethereum. It also stores all the Ethereum accounts and smart contracts to simplify the retrieval of data and execution of the said contracts. Anyone can access the EVM from anywhere in the world through Ethereum nodes that lend computing power to the Ethereum network at a fee (gas).
What Are Smart Contracts?
Smart contracts are self-executing lines of code that allow entities such as users or other automated code to transact with each other without the need for a central authority. In the case of Ethereum, smart contracts are written using the Solidity programming language.
The software code used to write smart contracts makes them secure because they cannot be altered or changed once deployed on the Ethereum network. Each time a smart contract is executed, it alters the state of the Ethereum Virtual Machine.
This characteristic further makes the EVM a ‘state machine’ as it carries out the heavy work of computing and tracking the state changes resulting from executing code belonging to smart contracts. The EVM does this by monitoring ETH balances on accounts, the data output of smart contracts, and transactions carried out by Ethereum addresses and smart contracts once completed.
Opcodes Assist the EVM To Complete Smart Contract Tasks.
As earlier mentioned, smart contracts on Ethereum are written using the Solidity programming language. However, the EVM cannot directly execute the smart contract code. It has first to be compiled using opcodes which are lower-level machine instructions.
Opcodes assist the EVM in carrying out the specific tasks necessary to execute a smart contract. The EVM can execute an estimated 150 opcodes which perform various operations related to arithmetics, memory, comparison, exchange, duplication, comparison, stopping, logging, pushing data, and retrieving information from blocks.
The EVM Economy Runs on Gas Fees.
As earlier mentioned, Ethereum nodes lend out computing power and are incentivized for doing so using ETH gas. As the name suggests, gas is the fuel that powers smart contract execution and other transitions on Ethereum. The process is similar to how a vehicle owner needs to purchase fuel to power the engine to allow it to move from point A to point B.
Individual opcodes have individual gas costs assigned to them depending on the complexity of the instructions. Furthermore, every opcode execution within a smart contract requires a certain amount of gas to be processed by the Ethereum Virtual Machine. Therefore, opcodes within a smart contract determine the final gas fee for its execution by the EVM.
Gas fees on Ethereum are also designed using a supply and demand mechanism. In situations where there is demand for processing transactions, gas fees usually go up, as witnessed during several NFT mints and periods of high crypto trading activity.
In addition to incentivizing Ethereum node owners to lend out their computing power, gas safeguards the Ethereum network from attacks involving the deployment of large amounts of complex smart contracts in a manner similar to a DDoS attack. Such an attack is prevented as executing the massive smart contracts would require an equally enormous amount of ETH gas fees.
Use Cases of the Ethereum Virtual Machine.
The Ethereum Virtual Machine (EVM), therefore, provides the ideal environment for the following use cases:
- The development of decentralized applications that function purely on software code and without a central authority.
- The EVM allows for the creation of ERC-20 tokens using smart contracts. These tokens are fungible and can be used to represent value. For example, stablecoins such as USDC on Ethereum are ERC-20 tokens pegged to one US dollar.
- The Ethereum Virtual Machine is used to create NFTs, which are ERC-721 tokens.
- Decentralized exchanges such as Uniswap and Sushiswap are made possible through smart contract deployment on the Ethereum Virtual Machine. The smart contracts that govern trading on decentralized exchanges are known as Automated Market Makers (AMM).
- The EVM is consequently the backbone of Decentralized Finance (DeFi) on Ethereum.
Some EVM Pitfalls.
However, the EVM is not without some drawbacks.
To begin with, navigating its complexities requires background knowledge in computer science and programming, limiting its access to developers.
Secondly, ETH gas fees tend to skyrocket during peak periods of demand for its computing power. Although such instances last a few hours, as witnessed during the Yuga Labs Otherside Land Mint, it is a pain point for blockchain users who cannot afford the high fees leading some to look for alternatives such as Solana, the BNB Chain, and Tron.
Thirdly, although the EVM has been designed with decentralization in mind, Ethereum nodes have been observed to be centralized. These nodes are hosted on centralized cloud servers.
Amazon Web Services handles almost 25% of Ethereum nodes. Microsoft Azure, Alibaba Cloud, Google Cloud Platform, DigitalOcean, and Hetzner are also popular options for Ethereum node operators.
This type of centralization can cripple the Ethereum network in case of an outage such as the one experienced on December 8th, 2021, when Amazon Web Services went down for nearly half a day. The outage caused operational interruptions on centralized exchanges such as Binance and Coinbase. It also affected decentralized exchanges such as dYdX.