What Is the Ethereum Virtual Machine & How Does It Work?

The EVM is a Turing complete virtual machine that can be accessed globally through a participating network node. The EVM’s Turing completeness is measured by the fact it can run any program. Without the EVM, developers wouldn’t be able to implement the numerous dApps (decentralized applications) for which Ethereum is known.

What you will learn

  • What is Ethereum Virtual Machine

  • How it works

  • Using the Ethereum Learning Machine

What you will learn

  • What is Ethereum Virtual Machine

  • How it works

  • Using the Ethereum Learning Machine

Bitcoin introduced the world to cryptocurrency in 2008. Still, most crypto-enthusiasts believe Ethereum’s seamless integration of smart contracts in 2015 was the first step towards perfecting Bitcoin’s formula. Ethereum’s smart contracts gave developers an easy way to store on-chain transaction protocols using a programming language. To understand smart contracts, you have to understand the Ethereum Virtual Machine (EVM). It is the state machine that enables smart contract deployment and execution in Ethereum’s decentralized ecosystem. The EVM is also the virtual machine used to power the Hedera Smart Contract service, and optimized for blazing-fast transactions, low fees, and incredible scalability.

What is EVM?

The EVM is a virtual machine

The EVM is a Turing complete virtual machine that can be accessed globally through a participating network node. The EVM’s Turing completeness is measured by the fact it can run any program. Without the EVM, developers wouldn’t be able to implement the numerous dApps (decentralized applications) for which Ethereum is known.

Virtual machines aren’t tied to a specific physical device and have no system interface or hardware. A virtual machine uses the computing power of many participants to provide a runtime environment similar to a physical computer. Unlike physical computers, virtual machines aren’t bound to a single operating system or location. People worldwide can use the machine regardless of where they live or what kind of computer they use.

The EVM is a state machine

State machines are computation engines that can switch between various states. When a transaction causes a smart contract to execute, the EVM changes Ethereum’s state to meet the needs of this contract call.

The EVM’s ability to interpret and execute smart contracts during transactions separates Ethereum from simpler blockchains like Bitcoin. The Bitcoin blockchain is simply a distributed ledger. Alternatively, the EVM’s state transition function allows Ethereum to update to a new valid state from block to block in response to a smart contract’s input data.

Ethereum’s state changes allow developers to create custom currencies and NFTs, represent ownership of underlying physical assets, create non-fungible domain names, and make fully-functioning decentralized finance applications or autonomous organizations (DAOs).

The EVM can run any program written into a smart contract. However, more complex smart contracts use more gas, resulting in a higher gas cost. Although gas costs have become a point of contention for many crypto-enthusiasts, they’re an essential part of the EVM. According to Ethereum’s yellow paper, “the computation is intrinsically bounded through a parameter, gas, which limits the total amount of computation done.” The gas, and gas limits, enable the EVM to avoid network abuse.

Is the EVM decentralized?

The EVM could be described as “mostly decentralized.” Countless individuals worldwide run nodes, but the majority of Ethereum nodes are hosted on centralized servers like Amazon Web Services. If the owners shut down the nodes, it could damage the network. Still, over time, as new computers participate in the Ethereum network by running nodes, the ecosystem becomes more decentralized.

How does the Ethereum Virtual Machine work?

EVM functionality

The EVM uses a stack-based architecture and a word size of 256 bits. The 256-bit word size allows the EVM to facilitate native hashing and elliptic curve operations that ensure funds can be spent only by their rightful owners. The EVM supports various programming languages such as Vyper and Solidity, with Solidity being the most popular programming language for smart contract source code. These programming languages are used to write smart contracts, which are converted into the bytecode needed to be utilized by the EVM.

The bytecode stored on-chain, known as the runtime bytecode, is then converted into an opcode that the EVM computation engine interprets to carry out those actions.

When an Ethereum transaction executes a smart contract, an EVM is loaded with the information for the transaction being processed. For example, one variable needed for a smart contract execution is the gas supply, which is set to the amount of gas paid by the sender. The gas supply is reduced as the transaction progresses, and if, at any point, the gas supply reaches zero, the transaction is abandoned. Although abandoned transactions don’t result in changes to the Ethereum state and are not considered valid transactions, the block’s beneficiary is paid for providing resources up to the halting point.

Smart contracts can initiate transactions and call other contracts on their own. In this case, each call results in another EVM being loaded with specific information for the new transaction. This new information is initialized from the EVM one level above. If there isn’t enough gas to complete the execution, the state is discarded, and the transaction execution is reset to the EVM one level above.

How is data stored?

The Ethereum protocol uses two distinct data types – permanent data and ephemeral data. Permanent data, such as a transaction, is recorded in Ethereum’s tree-like data structure and will never be altered. Ephemeral data, such as a wallet’s balance, is recorded and changed in response to new transactions.

EVM’s opcodes use contract memory to retrieve data. Contract state memory is stored at the contract address and is not persistent. A variable’s position in a smart contract’s storage array is determined by its order in the code. If a given variable is 256 bits or less, the EVM will try to fit multiple variables in the space. When a contract inherits another contract, the storage variables of the base contract are stored in the first slots in the order of inheritance.

Whereas contract memory is impermanent, contract storage is stored indefinitely. Contract storage is like a public database where values can be read externally without the need to send a transaction. Still, contract storage is expensive compared to contract state memory.

Using the Ethereum Learning Machine

Ethereum’s EVM has allowed the project to achieve its goal of “decentralizing everything.”

The EVM is complex in its functionality but accessible to new developers, leading to many decentralized applications. Still, newer projects and blockchains continue to improve upon the EVM model. For example, Hedera recently announced smart contract 2.0 availability on the Hedera mainnet.

Hedera’s investment in the EVM will lead to numerous benefits for developers, including low gas fees, fast transaction finality, carbon-negative operations, and leaderless architecture. Additionally, because new blockchain developers use the EVM 30 percent of the time, Hedera’s EVM compatibility will give entry-level Solidity developers a new, less expensive way to develop and deploy dApps.