If youāve tried developing a smart contract on the Ethereum blockchain, you might have come across the concept of an Ethereum Virtual Machine.
EVMs essentially create a layer of abstraction between the executing code and the executing machine. This is the basic definition of an EVM that most people are familiar with and just the most fundamental definition that seems to provide enough context to just input into your smart contracts without a second thought.
This post on the Ethereum virtual machine was inspired by this tweet which made me realize that to actually be able to understand the specificity and complexity to which the Ethereum blockchain operates, getting a fundamental understanding of how the EVM works is important.
Normally, when you write some kind of software, youāre using a programming language. You could use a language like C++ which involves a lot of English meaning that your CPU can not understand it. There is a compiler that takes C++ code and translates it into instructions known as byte code which the CPU can then execute.
Ethereum is not a computer. It doesnāt have a CPU. Itās distributed, so there are technically millions of computers running the Ethereum Virtual Machine at any given moment.
The CPU is a piece of hardware. The EVM is just a virtual version of the CPU - it executes byte code. The Ethereum virtual machine is part of a program called Geth which is an implementation of the Ethereum protocol (there are other implementations like Open Ethereum which has its own EVM). When you run Geth, you also run the EVM which means that youāre part of the Ethereum network. Geth runs on a computer (windows, ubuntu, OSX) - it runs on a server or some kind of machine.
A developer writes a program in solidity which gets compiled into bytecode which is then distributed to all of the different machines that are running Geth.
The EVM is Turing-complete. What exactly does that mean? The concept came from renowned computer scientist Alan Turing. He developed ideas around what a hypothetical computer or thinking machine can do. He argued that computers do not think or process thoughts like human beings, but go by a set of data processing rules to solve problems.
A Turing machine has a thinking process that runs by way of algorithms. In Turingās paper, he mentioned that such a machine would have to process a reel of tape with a line of symbols or functions that can be moved backward and forward - like a read/write head that would move between these functions. A machine like this could only direct its attention to one āstateā at a time.
The best part is thereās no limit to this tape - itās hypothetically infinite. The tape corresponds to a computerās memory which can be infinitely added to by adding to the length of the reel of tape. When a computer has to follow a set of instructions, the data applied to the instructions is only limited by physical constraints.
So what happens when someone calls a smart contract? All of the different machines pull up their copy of their smart contracts and they run the bytecode and then execute the instructions.
This results in a state change. A state is the state of the blockchain - like a snapshot of the blockchain that includes accounts, smart contracts, and anything else that might be on the blockchain.
All of the different machines that run Geth keep track of the current state. When someone runs a transaction, all of the different Geth machines run the contract which might change the state of the blockchain.
You essentially have a distributed state machine. This is really unique because it adds neutral compute infrastructure. The only way to change the state of the blockchain is to use your private keys and do a transaction. You can have transparency and reduce the times it takes for transactions to go through.
The EVM allows smart contracts to be deterministic and run in isolated environments. One of the most important features of a smart contract is that itās deterministic which means it provides the same output to the same set of inputs. It doesnāt matter how many times the code is executed. This is important becauseĀ decentralized apps or dApps on EthereumĀ may handle financial transactions involving large funds.
Smart contracts being able to be run in Isolation is facilitated by two systems: Virtual machines and docker containers. Since the contract designs in Docker are not deterministic, EVMs can enable both the deterministic aspect while allowing it to be run in isolation.
Isolation ensures that the system can contain hacks or bugs within a smart contract. This allows bugs in a smart contract to not affect the underlying Ethereum protocol.
The EVM is Turing-complete, which means its smart contracts can solve any type of problem, at least hypothetically. But there is no way to tell whether such smart contracts can finish all the given operations within a specific time frame.
A terminating mechanism helps to create exact limits. In Ethereum, the concept of āgasā is used to facilitate traffic. Gas is also part of the incentive mechanism of the network - gas fees are used to selectively determine which functions should be run or prioritized. Gas limits are set at the beginning such that when the limit is reached - the machine simply stops operations or halts its processing.
The Ethereum Virtual Machine effectively creates a new economy in which through gas incentives, it creates a peer-to-peer Turing-completeness, making use of the resources of the world to run programs. How is the EVM essential to the protocol? It allows anyone who joins the network to execute their code in a trustless manner where the outcome of any execution is guaranteed (because it is deterministic).
With a way to measure gas costs to execute a smart contract, the protocol guarantees that fees are received before running the program, protecting the incentives and priority system. Ethereum has validators that ensure that all the information on each network transaction is valid. It ensures that the funds held by the sender are sufficient enough to pay for the execution of the smart contract and that the EVM did not run into exceptions.
While I never thought twice about using the EVM, understanding the concepts behind one of the most fundamental parts of the Ethereum blockchain helps developers build intuition, experiment with code, and actually understand the technology that powers what theyāre building.
Learn more š
a series on a technical explanation of the evm
Ethereum Explained: the EVM
Iām learning by writing about ideas in web3, blockchain, crypto, etc. If you find errors in my understanding or would just like to talk, reach out @_anyasingh