Crypto 101: Ethereum Virtual Machine (EVM)

Crypto Saving Expert
7 min readSep 11, 2022

--

The Ethereum Virtual Machine (EVM) is the core feature that makes Ethereum more than a “distributed ledger” like Bitcoin. EVM allows Ethereum to be a “distributed state machine,” and it’s the solution that enables developers to create smart contracts and DApps (Decentralized Applications) in the Solidity programming language. EVM revolutionised the whole blockchain industry and allowed the creation of new use cases. In this article, we will explore EVM and its features.

Source: Pixabay

What is EVM?

Before going into the EVM explanation, let’s understand what a Virtual Machine (VM) is. Accordingly to VMware, “a Virtual Machine (VM) is a compute resource that uses software instead of a physical computer to run programs and deploy apps.” In other words, VMs are similar to computer machines with storage, memory, and CPU; the main difference is that they operate purely as code. The best solution, so far, to make decentralised networks scale is making use of VMs because VMs can run on different systems and hardware specs from any location in the world.

Source: Internet Policy Review

A real-world example is BitTorrent, classified by Vitalik Buterin as the first truly decentralised application. What BitTorrent presented was a Virtual Machine that allowed file-sharing without geographical restrictions and was censorship-resistant (as to shut down a decentralised network would require shutting down each member of the network).

In a simplistic definition, the EVM is nothing more, nothing less, than a large database for holding all Ethereum accounts and balances (distributed ledger function). Also, a machine state that can execute machine code language and change with each new block is added to the blockchain ledger (distributed machine state function).

So, we can describe the Ethereum Virtual Machine (EVM) as a virtual computer (a piece of software) that works as a decentralised computer. It is used to create DApps, execute and deploy smart contracts, and compute the state of the Ethereum network after each new block is added to the chain.

This way, the Ethereum blockchain can be described as a Peer-To-Peer (P2P) decentralised, open source, and distributed structure of computers (nodes), where each node connects to the next, and every node is responsible for the security and stability of the entire network. Maintaining the consensus through all the different nodes that compose the Ethereum ecosystem requires each node to use the EVM.

A Diagram of the EVM. Source: Ethereum

The Elements of EVM

Now, unlike what a layman would assume, CPUs can’t read programming languages like C+, Python, Solidity, etc. CPUs only understand bytecode language, a computer object code that an interpreter converts into binary machine code; only after this conversion can CPUs understand the commands. Usually, the interpreter implements a virtual machine (VM) that translates the bytecode for the largest platform.

As mentioned before, EVM is a virtual computer, and this virtual CPU runs inside the Go Ethereum (Geth) network. Ethereum uses the Solidity programming language to create computer programs, where the code is compiled into bytecode and then distributed to every computer (node) running in the Geth network. So, whenever a smart contract gets deployed, every node receives a copy of it, runs its bytecode, and gives the code to the deployers, changing the network state (state changes require the consensus of every node). The same will happens with every transaction or interaction with smart codes; this is the distributed state machine function.

In summary: smart contracts are written in Solidity and other programming languages, and these programming languages convert this instruction into bytecodes (CPUs language — binary code) and compile to the EVM. All nodes in the network must agree (a consensus) to execute these instructions. Now, for each instruction, the EVM implements a cost; this cost is known as gas.

1. Opcodes

Currently, EVM is considered Turing Complete; however, a more accurate term would be Quasi-Turing Complete. Turing Complete is a term that means that any real-world general-purpose computer or computer language can simulate the computational aspects of any other general-purpose computer or computer language and, in theory, solve any computational problem required. This only is possible with EVM opcodes, a set of instructions executed at the machine level. A good movie recommendation at this point would be ‘The Imitation Game’, a movie about the first computer and the idea behind its functions.

As we already mentioned, EVM opcodes assist EVM in executing specific tasks of smart contracts or transactions; you can find a list with all EVM opcodes in Ethereum here. Aiming 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.

2. Smart Contracts

A smart contract is a conjunct of self-executing lines of code that allow parties to transact without the presence of a centralised agent (authority). Inside the lines of codes of a smart contract exists a list of operations that are self-executed when certain conditions occur; these conditions can happen on-chain or off-chain. These operations include sending funds to a specific address, exchanging coins in a Decentralized Exchange (DEX), minting an NFT, etc.

The most important feature of smart contracts is that they are a secure method for transacting (if the smart contract was correctly designed and the deployers are not harmful agents); this happens because the lines of code cannot be arbitrarily changed. Also, allow the parties involved in these operations to always expect a specific outcome from the smart contract. Every time a smart contract gets executed, the EVM will compute the new state of the Ethereum network, updating the account balances, data storage of smart contracts, and transactions on the account and contract levels; all these functions are what live inside of the called state.

EVM is the environment where these smart contracts could live and interact with each other, where EVM combines the resources of thousands of CPUs connected to the Ethereum network. Just refreshing, smart contracts are written in the programing language of Solidity in the Ethereum network. EVM cannot directly execute Solidity, so the code is compiled to machine instructions called opcodes, and gas is needed to use opcodes.

3. Gas

The main objective of gas is to act as the fee for computing the operations of a smart contract done by each node in the Ethereum network. The computation fees prevent an attacker from deploying many complex contracts that will require long computation times to halt the network. These fees make this type of DDoS (Distributed Denial-of-Service) attack too expensive to be executed.

In terms of gas costs, the higher the opcode complexity, the higher the base cost will be. Currently, the starting gas cost of every transaction is 21,000 gas units. But the main source of complaint surrounding gas is not about gas cost itself but the gas limit required to complete a transaction. The gas limit is the maximum amount of gas the sender is willing to pay for the transaction to be executed and validated. To discover the gas fee, you can multiply the total gas cost (base) by the gas price (the cost of completing operations).

Gas also serves as compensation for validators responsible for guaranteeing that the information in the transactions is valid. The problem is that when network activity is high, people will raise their gas limit to get their transaction executed and validated; this way, validators will choose from the mempool the transactions with higher gas limits.

The reason for the Ethereum network being considered a Quasi-Turing Complete is gas limits, as the computation is limited to the amount of gas a sender is willing to pay to complete their computation.

Use Cases for EVM

Some of the most common use cases of EVM are:

  • ERC-20: are tokens that can be sent between addresses and have a fixed amount, and their values are the same across the Ethereum network. Smart contracts that follow a specified data structure on the EVM are used to create these tokens. The data structure is responsible for controlling the naming, distributions, supply amount, and monitoring of the token;
  • Decentralised Exchanges (DEX): that deploy smart contracts to users to exchange their ERC-20 tokens. The main innovation behind DEXs is a solution called Automated Market Makers (AMM) they allows users to contribute to liquidity pools of specific tokens without the need for a centralised agent controlling it.
  • ERC-721: these smart contracts are used to mint NFTs, like ERC-20 tokens; ERC-721 has unique value across the blockchain. These smart contracts are optimised to create unique artworks and provide property rights to the original art piece;
  • ERC-1155: is standard for smart contracts that manage multiple token types. A deployed smart contract may include any combination of fungible, non-fungible tokens or other configurations.

To Conclude

Ethereum is the leading platform in the category of the smart contract, but Ethereum is not perfect. Often users face high gas fees and congestion on the network. This has made developers create other blockchains EVM-compatible. In this solution, developers copy certain pieces of the Ethereum network and create DApps (cross-chain bridges) that allow users to transfer assets between EVM-compatible networks. These alternative Layer 1 chains usually offer higher throughput and cheaper fees.

Other chains, besides Ethereum, also offer their virtual machines. Some examples are the Algorand Virtual Machine (AVM), NEO Virtual Machine (NeoVM), and Rootstock Virtual Machine (RSK), which are built on top of the Bitcoin network and allow developers to write smart contract code for decentralised applications on Ethereum. The main explanations about virtual machines are applicable for each of those examples; what will differ is some of their specifications.

~ By Jordano M. Z. ~

--

--

Crypto Saving Expert

Full suite of Crypto information, education, news & exclusive discounts completely FREE!