What is the Ethereum Virtual Machine (EVM)?
{{building-on-ethereum}}
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
If you have ever downloaded a movie, song, or software (legally, of course) from BitTorrent then you can understand the benefits of a decentralized network. Even if you are the most powerful authority in the world, taking down a decentralized network is hard as it would require shutting down each member of the network.
![](https://assets-global.website-files.com/5f973c97cf5aea614f93a26c/62200da44bd1c607475eefeb_ehErMV3sb6E5gbTvwOY5Pk-iebEFdqrNi8v4yO-2SQRAqF-SyyzgfxSQBiMGwI9d_V100K7C69V08pk0-RYSfNf0Al8vb9eY9_UzlLavs44Z62R2iNcTbGxIllCyvugOQsqsIz4.png)
Source: https://hub.packtpub.com/are-distributed-networks-decentralized-systems-same/
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.
![](https://assets-global.website-files.com/5f973c97cf5aea614f93a26c/62200da387e8397ff20f36df_0IXTbIIghLpoSstf7S2RBm-q5RLxBuZaBaV505tJYPLeaKCzdcb9MhiCOAgeytvLyrNpDGuv6ayQWBosnLR4jwgeqN8FiyaQrNIHSS54h2l5zhCfsyxhojp0ywl1JlrXj6m7GfY.png)
Source: https://cryptotaxcalculator.io/blog/what-are-smart-contracts/
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.
![](https://assets-global.website-files.com/5f973c97cf5aea614f93a26c/62200da326572a33e55511cc_OC-IGP_NVCE8PffXi7xfjs6f05VJoVhGfq9KPNhHqwNByaBg_ERpZ5RyyHyMxEkFNvdQ_DRprViAHr4m4wilTu_aeY-rX2COit6CYBhwpPLzPX2S0CBt05wOEiM7MGP9WMXeNsw.png)
Source :https://ethereum.org/en/developers/docs/transactions/
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.
![](https://assets-global.website-files.com/5f973c97cf5aea614f93a26c/62200da42c402a5326990b36_rEl7cW1Opu3_7T91NDwUH81oWJJY6M9ksop7shhO_BMVGQ2emHYaAeqL5SKVbvXPDSjpyAziiE40F7smnSF0uw1rIbKGxeq8L2m8T_E7adHoxE7z9wNIxdkTwQuFVY-xmArgqKU.jpeg)
Source: https://hackernoon.com/ethernaut-lvl-0-walkthrough-abis-web3-and-how-to-abuse-them-d92a8842d71b
For efficiency, EVM converts its bytecode to opcode where each opcode is assigned one byte in bytecode. Each opcode operation requires a certain 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.
![](https://assets-global.website-files.com/5f973c97cf5aea614f93a26c/62200da326fa68525ce0a6ce_hWlFy_XIHauStdrsojHHscKVB7pi6deY_D94LNnYFqM8f5hOMxkwStX6IfGHVd1n1EsLvZJoivdAlSwUt-o8xj_uSogz51-8SwnJTGaXxR1r1lShTEVHf3pcDPEogJfQcSLB_cg.png)
Source: https://ethereum.org/en/developers/docs/gas/
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.
![](https://assets-global.website-files.com/5f973c97cf5aea614f93a26c/62200da353d72b76c24c4712_O_IjWmneFOQcctEmX2ZBhxF9AqygBLXt6Ldn1jYZws9iIn2L4as9e1Kt-S7_C-M1wjiGtnfI8YfX1xsfq2WvpBmGZzPvqTOsxSsK2uONc37h3RoUG3q4F1h_bQLozGZ-UxrBNu4.png)
Source: https://changelly.com/blog/automated-market-maker/
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.