The blockchain ecosystem has evolved very much ever since the introduction of Bitcoin in 2008. The pace of innovation in this ecosystem is much difficult to keep track of. Despite the launch of different blockchain networks with various advantages and interesting approaches, the TVL and the number of DeFi protocols on EVM based chains (Ethereum, Polygon, BSC, Avalanche, etc…) are continually reaching new heights. This clearly indicates that the concept of EVM is a great breakthrough.
In this article, we are going to have a look into the Ethereum Virtual Machine (EVM) to get an overview of how it works under-the-hood.
Before diving into the EVM, we should rewind a bit backwards to understand why EVM is considered as a major leap in the world of blockchain and cryptocurrencies.
EVM was first introduced as the core component of Ethereum. This made Ethereum to standout from the other blockchains. As Bitcoin is considered as a ledger, Ethereum is considered to be a state machine. To be specific: a quasi-turing complete state machine as it is limited by the number of computational steps.
When it was launched, Ethereum was referred to as the "World Computer". It is because of the following reasons:
- A Global singleton: One computer for the whole planet
- Cannot be shutdown: No single power button
- Programmable: Ability to run smart-contracts
- Ubiquitous: Wherever there's internet there's Ethereum.
As Ethereum is a state machine, it would make state transitions based on certain events. At any given point in time, the state of the Ethereum is called the "world state" as it is not local to a single node. It is shared globally across all the nodes in the Ethereum network. The world state transitions from State S(t) to S(t+1) whenever a new "block" is added. A block is nothing but a set of transactions bundled together.
The world state is currently stored in a data-structure called the "Merkle Practicia Trie" in Ethereum, where each of its leaf node is an Account, either an EOA or a Smart contract account. In the future, it is estimated that Ethereum will be using Verkle tries to store world state. To learn more about Verkle and Merkle tries checkout my article here. The world state is nothing but the current state of Ethereum shared by all the nodes in the network.
So in order for the state transition to take place, there should be a set of rules right? The state transition cannot take place without any validations. This is where EVM comes into action. At a higher-level, EVM contains a set of rules and it validates each transaction against the rules.
There aren't any formal definition of EVMs, so I would describe it as follows:
An EVM is a stack based, quasi-turing complete, globally shared virtual machine that has the ability to execute the incoming instructions and validates the state transition of Ethereum.
In our context, a virtual machine (VM) is a virtual environment that functions as a virtual computer system with its own CPU, memory, and storage, created on a physical hardware system that's able to execute programs irrespective of the underlying hardware. Virtual machines are purely software processes. It is used to generate platform specific machine code independent of the underlying platform/architecture.
So the Virtual machines that runs on the Ethereum nodes (globally shared) to execute Ethereum compatible instructions (opcodes) and validate state transitions are called the EVMs.
There are many characteristics for the EVM, but I listed out the three important ones below...
- Atomic : Executes a transaction fully or reverts completely, no intermediate state.
- Synchrony : No interference in execution
- Provenance : The origin of each tx/message can be easily verified/identified.
Also it is important to note that EVM has a very limited execution context. For ex, it can access the function params, storage of the account (if its a contract), the block info (timestamp, etc) and some other limited info.
To get an idea on how the state transitions works (at a very higher level), let's breakdown how a simple transfer of a token works:
- The EVM receives an incoming message 'X',which is a simple transfer of token transaction.
- The EVM forks the world state in a sandbox env (a local copy).
- It loads the token contract 'A' from the world state.
- It executes the "SUB" operation for the Senders account if the "Value" is greater than the available balance.
- It performs the "ADD" operation for the Receivers account.
- If every thing goes well the EVM updates the real world state with its local copy (the consensus comes into play here)
- If any of the validations fail, then the local copy of the world state will be discarded entirely.
This is how the EVM works at a very higher level. We should explore further into the EVM architecture, instruction set and the execution model to understand the mechanics behind the EVM at a lower level, which we'll be covering in the upcoming posts!
Until next time... 👋👋