What is the Ethereum Virtual Machine (EVM)
When developing a smart contract on the Ethereum blockchain, you may have come across the term “Ethereum Virtual Machine”. It could also be that you have been in this space for some time and have come across the term serendipitously, but still don’t quite understand it. The Ethereum Virtual Machine is essentially a computation engine that allows developers to make Ethereum-based decentralized applications. The function of the virtual machine is to effectively improve the portability of software, making sure applications are separated from each other and their host. In brief, EVM is the underlying foundation of Ethereum’s structure.
What are the Components of the Ethereum Virtual Machine (EVM)?
The EVM is considered to be a complex, dedicated software virtual stack, executing contract bytecodes on the entire Ethereum node. Its role is to deploy a number of extra functionalities to the blockchain to ensure users face limited issues on the distributed ledger. Furthermore, the Ethereum Virtual Machine is considered to be Turing Complete. What this means is that the EVM is capable of programming any computational method or language possible. For example, it is possible to write the equivalent code in another Turing complete language for each EVM programme. However, to do this you will need a compiler that allows you to convert it into code that EVM understands. The compiler required for this is made for the solidity language.
How does EVM work?
Due to its complex nature, we will explain the different parts of the EVM in as much depth as possible. There are 7 components to consider:
- Creating a Smart Contract
- Contract State
- Deployment of Smart Contract
- Decompiling of bytecode
Creating a Smart Contract
This is the first step in the process. Smart contracts can be written in a variety of programmes, but the most common one used is Solidity. However, smart contract languages like these can’t be performed by the EVM, and instead, they are compiled into what is known as opcodes. Opcodes are essentially low-level machine instructions that tell the EVM what to do.
In total, there are currently 140 unique opcodes that allow the machine to compute anything if given sufficient resources (Turing completeness). Opcodes are considered to be 1 byte, which essentially means that they are each an individual instruction that the EVM can process. There are various ways in which to categorise opcodes, but because there are 140 we are going to only show you a couple of them. This will help you get an idea of a few of the functions available.
1. Stack-manipulating opcodes
a. POP, PUSH, SWAP
2. Memory manipulating opcodes
a. MLOAD, MSTORE, MSIZE
3. Halting Opcodes
a. STOP, RETURN, REVERT, INVALID
In order for the EVM to store opcodes, they have to be encoded to bytecode. In essence, bytecode allows computer object code to be converted by an interpreter (e.g. Java interpreter) into binary code that computer hardware can read.
The contract state is an important feature to take into account with the EVM. This can be categorised into: stack, contract memory, or contract storage.
Lower-level programming languages don’t allow users to pass arguments to functions directly, and instead, use what is known as a stack to pass values to functions. Stack is fundamentally an abstract data type that comprises a predefined capacity, which allows adding and removing elements in a specific order. The issue with this is that the stack has a limited number of items it can hold.
Due to these limitations, complicated opcodes tend to be stored in contract memory so that data can be passed on or retrieved. If one wishes to store data indefinitely and make it accessible in the future then one must use storage. This way, values can be read externally and send transactions to the contract with no fees. The only issue with this method is that it is thousands of times more expensive than stack or contract memory.
Interacting with the smart contract has costs attached to it and there are ways attackers on the Ethereum network can exploit this. To avoid this from happening, every opcode has a base gas cost assigned to it. The more complicated the instruction for the machine, the higher the gas fee.
Benefits & Limitations of EVM
The Ethereum Virtual Machine is a beautiful creation, and it certainly comes with many beneficial functionalities. Although this is the case, there are also several limitations that must be taken into consideration. Here is a little overview of the benefits and drawbacks of the EVM:
- Allows developers to create their own decentralized apps
- Enables deployment of extra functionalities within a blockchain
- More secure and private
- Fully decentralized
- It can be expensive and slow when the network is busy.
- Although Ethereum is technically fully decentralized, 60% – 70% of the nodes are run on central cloud facilities. This is not necessarily an issue as the majority of blockchains run like this, and most cloud servers don’t have all their servers in one location.
- Requires technical expertise to use → Need to know how to code