What is the Ethereum Virtual Machine (EVM)?
Written by Alchemy
Reviewed by Brady Werkheiser
The EVM - A Machine With Many Names
The World's Computer. The Unstoppable Machine. The Heart of Ethereum. The Ethereum Virtual Machine (EVM) has many names which highlights its importance to the Ethereum Network. It is the core feature that makes it not just a 'distributed ledger' like Bitcoin but a 'distributed state machine'.
The EVM allows developers to create smart contracts in a programming language called Solidity. A popular application of smart contracts is to manage the generation and exchanging of tokens. Many applications and protocols use tokens to reward users for actions that are important to its goal. Without the EVM, none of this would be possible.
To get a better understanding of the power of the EVM, let’s look at some of its core components:
Virtual Machines - The Platform of the EVM
Decentralized networks are hard to take down. If you want to do it, you'll need to remove every single node currently online, and find a way to prevent more from popping up.
A good way to get a decentralized network to scale is by using virtual machines (VM). This is because VMs can be run on different operating systems and hardware and from any geographical location. VMs operate as an abstraction layer between the code and the machine executing the code.
VMs function in a similar way to a physical machine with storage, memory, and CPU but they operate purely as code. In theory, anyone can run a VM which makes it a highly portable platform for a decentralized network. With the EVM, it uses a decentralized network of nodes to execute smart contracts.
Smart Contracts - The App of the EVM
Smart contracts are self-executing lines of code that allow for parties to transact with each other without the need for any central authority. Within a smart contract is a list of defined operations that are executed when certain conditions, on or off-chain are met. Some operations could be to transfer funds to a certain address, communicate with another contract or even create a new contract. Rather than a third party executing the transaction, any sender can send funds to the address of the smart contract to trigger these operations.
What makes smart contracts a secure method of transacting is the fact the code cannot be altered or changed. There is always an expected outcome from the smart contract which is the built-in trust of the code. Anytime a contract is executed, it is said to alter the state of the EVM.
The EVM can be described as a "state machine" because it is responsible for computing the state changes that are a result of executing the code of smart contracts. What this means is that it maintains the account ether balances, data storage of smart contracts, and transactions on both account and contract levels as they are completed. All of these actions are what change the 'state' of the network.
Smart contracts are primarily written in the language of Solidity. The EVM cannot directly execute Solidity so first the code must be compiled to lower level machine instructions called opcodes.
Opcodes - The Language of the EVM
The EVM is labeled widely as being Turing Complete or more accurately quasi-Turing Complete. What this means is that the EVM can in theory solve any computational problem. This is done by executing machine-level instructions called EVM opcodes.
EVM opcodes assist the EVM to complete the specific tasks of a smart contract or transaction. Currently, there are roughly 150 opcodes that the EVM can execute. They cover a range of operations including: arithmetic, stopping, logging, duplication, push, memory, comparison, and exchange. As well as for retrieving block and environment information. You can find a list of opcodes here.
For efficiency, EVM converts its bytecode to opcode where each opcode is assigned one or more bytes in bytecode. Each opcode operation requires a specific amount of gas to be executed by the EVM. You have probably heard about gas, but what is it?
Gas - The Fuel of the EVM
Gas is probably the most talked-about and least understood topic regarding the EVM. At the time of writing, gas prices can be extremely high but some there are efforts to improve this. High gas prices can be a major barrier to the mainstream embracing Ethereum. But why do we have to pay gas prices and what causes the price to be so high?
The purpose of gas is to act as a fee for computing the operations of a smart contract done by each Ethereum node. There needs to be a fee for computation in order to prevent an attacker from bringing the network to a halt by deploying a large amount of complex contracts that require long computation times. This type of DDoDS attack is discouraged because it would be so expensive to run.
Each opcode has a gas cost assigned to it with the more complex opcodes having higher costs. For example, simple addition costs 3 gas and every transaction starts at a cost of 21,000 gas. Most of the complaints are not about gas itself but the gas limit to complete a successful transaction.
Gas limit is the maximum amount of gas that the sender is willing to pay for the transaction to be executed and validated. To get the gas fee, you can multiply the total gas cost (the base values of the operations) by the gas price (the cost of completing those operations). Much like when filling your car with actual gas, there is the cost of the gas itself and the amount of gas needed to get to your destination.
Gas Fee = Total Gas Cost x Gas Price
The gas fee is to compensate the validator who is responsible for making sure the information in the transaction is valid, there are no errors/exceptions from the EVM, and that the sender does as the required funds needed to pay for the computation. When a sender sets a high gas limit it indicates that the operation is complex which incentives validators to pick up the transactions for a high reward.
When network activity is high, validators can simply choose from the pool of pending transactions with higher gas limits. Gas fees are thus influenced by supply and demand. The good thing is that any gas not consumed is refunded to the sender.
If the pre-paid gas limit is hit, the validator is still compensated for their work but the transaction is not completed. In this way, the EVM is quasi-Turing Complete as the computations it can complete are limited to the amount a sender is willing to pay to be completed.
Use Cases of the EVM
Now that we have covered each of the elements of the EVM, let’s look at how these parts come together to help power the projects in Ethereum:
ERC-20 Tokens
ERC-20 tokens are tokens that can be transferred between addresses, have a fixed amount and their value is the same across the network. Smart contracts that follow a defined data structure on the EVM are used to create ERC-20 tokens. This data structure controls the naming, distribution, supply amount and monitoring of the token.
Several applications and projects use ERC-20 tokens to incentivize users. Livepeer, a decentralized video streaming network, uses the Livepeer token (LPT) to incentivize those who supply resources to the network. Nexus Mutual, decentralized insurance on smart contracts, uses the NXM token to allow users to purchase coverage and make claims.
Decentralized Exchanges
A decentralized exchange deploys smart contracts to allow users to exchange ERC-20 tokens. These smart contracts are called Automated Market Makers (AMM) because they allow users to contribute to liquidity pools of certain tokens without any third party controlling it. Some of the popular exchanges like Uniswap and SushiSwap are applications of this AMM model.
ERC-721 Tokens
The other widely popular token standard is ERC-20’s non-fungible cousin ERC-721. These smart contracts are used for minting NFT’s (Non-fungible tokens) which are tokens with unique value across the blockchain. Creating unique works of art is the biggest use-case for these types of tokens. Other projects in the gaming space like Axie Infinity and God Unchained use these tokens for collectables in-game.
The EVM and Beyond
The EVM makes Ethereum a platform and not just a blockchain. However, the EVM is not a perfect system. There are many challenges around transaction speed and network throughput. This is an area of focus for the development community and the roadmap for Ethereum. If Ethereum is to fulfill its promises of revolutionizing how we transact amongst each other, it will be on the back of improvements to the EVM.
Related overviews
What is Blockchain Sharding and How Does it Relate to Ethereum?
Explore a New Transaction Type that Guarantees Transactions are Mined and Optimized for Gas
Learn About Safe (Justified), Finalized, and Latest Commitment Levels