In this article, we will be studying the topic “What Is Ethereum Virtual Machine(EVM)?”. Also, we will look at the topic’s relationship with Ethereum, contracts, blockchain , byte , etc.
This is a machine with long tape memory through which codes are run without manipulation. Every smart contract runs on its basis. A device can also run with a byte.
Definition of an Ethereum Virtual Machine (EVM)
Ethereum Virtual Machine (EVM) is a hypothetic computer that has the ability to carry out a lot of operations. It acts in a decentralized manner.
All the operations run on Ethereum blockchain depends on this virtual machine.
One can think of the EVM as the aspect of Ethereum that distributes smart contracts, as well as executing them.
It distributes additional softwares that helps to reduce the problems encountered on its blockchain.
In order to stick to agreements made across the blockchain, all the Ethereum-based system needs EVM as a priority.
Smart contracts are codes run on Ethereum blockchain. Their utility is made easier by Ethereum too.
The code found in the EVM is does not have access to file systems, network and so on. It stands alone.
There are two types of accounts associated with Ethereum. They include Externally Owned Accounts (EOA) and Contract Accounts. Under EVM, both of them share equal value.
However, there has been an attempt merge these two accounts to function as one.
The private keys are in charge of externally owned accounts. In contrast, smart wallets are the storage medium for contract accounts.
A bytecode is a term used for the result gotten from converting a contract written with the smart-contract coding method.
A programming language Solidity produces most of the source codes required for the use of smart contracts. EVM later interprets the opcodes gotten from those source codes.
In order to complete some tasks, the EVM further adopts the operation codes.
Elaboration of ETH Virtual Machine
Physically, thousands of computers linked together control the EVM( in form a single node) in an Ethereum network. But don’t see it as the same thing with an ocean wave or cloud.
The main function of Ethereum network is to keep operations of EVM moving without interruption or manipulation. The two Ethereum accounts as well as smart contracts depends on EVM. This same EVM governs the affairs of each block in terms of validation of new blocks. It occurs within the blockchain and there can only be one leader.
To understand the concept of EVM, one is required to have a basic knowledge of terms like bytes, memory, and a stack. You are also required to be familiar with hash functions, proof-of-work and the Merkle tree.
Conversion from a Blockchain to a State Machine
The idea of blockchain is often compared to that of Bitcoin. With the aid of cryptography this Bitcoin adopts decentralization in every aspect. There are regulations made which is why a cryptocurrency acts normal. These regulations covers all transactions done on both Bitcoin and other blockchains. For example, one cannot spend more than the amount of a specific cryptocurrency he or she has gotten before.
Furthermore, Ethereum adds extra effort by enabling smart contracts. However, it abides by the same regulation with Ether as its natural currency. A more complex idea is needed because of this extra powerful feature. Instead of a blockchain, it becomes a state machine. The data plan is widespread and there is machine state attached. According to a regulation already established, the machine state varies in different blocks. They also carry out machine codes individually. The EVM defines the above protocols of change in state.
The Mathematical Illustration of Ethereum’s Change of State
The operations of EVM can be expressed mathematically. Let’s say an input is given, there would an inevitable result. The expression is:
1Y(S, T)= S’ 2
S represents an old legal state. T represents a new set of legal transaction. Y(S, T) represents the Ethereum state transition function and S’ represents a new legal result.
An Ethereum state is a large data structure known as a modified Merkle Patricia Trie. It stores all accounts linked by hashes and those that can be made a single root hash.
Concept of Transactions
The set of instructions approved via cryptography is simply the meaning of transactions. They are of two types, namely:
- transactions with message call as output and
- transactions with contract creation as output.
In the second type, a new contract account is invented which has smart contract bytecode as a component. Once a message call is made by a separate account, the previous account runs its bytecode.
Guidelines Outlined by the EVM
The EVM operates as a stack machine that contains 1024 items. Each item contains a word of 256 bits which facilitates the 256-bit cryptography.
The retains its short term memory (as a word-addressed byte array) during operation. This particular memory is a byte array that easily identifies words of memory which is absent during transactions.
Contracts usually contains an Ethereum state storage. This is connected with a specific account and some units of general state.
Opcodes executes standard stack operations like XOR, AND, ADD, SUB and so on. Compiled smart contract bytecode are run as series of these EVM opcodes. However, these stack operations extends to those special to blockchain. Examples are ADDRESS, BALANCE, BLOCKHASH, etc.
Executions of EVM
There are detailed descriptions in the Ethereum Yellow paper. All the EVM executions must follow those descriptions.
For the past 5 years, the EVM has been checked many times and most of its operations are done in various programming languages.
An EVM execution is present in all Ethereum clients. Also, there are several isolated executions yet to be established. They are listed below
The aim of the Ethereum Virtual Machine
It is the duty of the EVM to find out the general condition of Ethereum in each block in the system.
Just like every other network, Ethereum has a native digital currency of its own called Ether. It also decides the way operations should be carried out on its network. It also records transactions with a distributed ledger. However, reading smart contracts keeps Ethereum on a different level from other network.
This level records transactions with a distributed state machine rather. A state in Ethereum is simply a large database which contains all ETH accounts and balances.
It is also described as a machine state that can vary with change in block, and follow instructions that can translate machine codes. The EVM creates the instructions on change of state at each fresh block.
Introduction to Smart Contracts
A collection of data and code found in an Ethereum-based wallet is referred to as contract. While smart contracts are programs that run on the Ethereum blockchain.
They can also serve as a type of Ethereum account. The difference in the way that Ethereum send transactions is the distribution of smart contracts as controllers. These smart contracts have been programmed in a special way and they only follow the command given to them.
It is left for the user accounts to communicate with these smart contracts. How would they do that? Read along. User accounts sends transactions that carry out specific functions described by the contract. We have unique characteristics which associates with smart contracts.
- Also, there is enforcement of rules that were previously found via smart contract code,
- Subsequently, there is no room for deletion, and
- There is no going back once a transaction is executed.
Relationship between Gas Fees and the Performance of EVM
Gas is known as the fees that a network charges a user for sending and receiving transactions. They sustain the decentralized applications.
This gas fee depends on the nature of network activities. They usually increase with increase in number of operations performed. They are paid in Ethereum too.
Storage of Data in Ethereum
As earlier mentioned, a trie data structure stores data on Ethereum. It appears in form of a tree. The type of data kept in blocks are root node hashes of transactions, states, etc. Account balances are rather indirectly stored and receipts are also held on the blockchain.
There are two types of data stored in the Ethereum network. They include:
- Permanent Data
Transactions fall under this category. They are validated and stored in the trie where they cannot be manipulated.
- Ephemeral Data
Account balance falls under this category. Just that transactions can be manipulated after storage. This occurs when transactions are either sent or received via the wallet address used for storage. For this, permanent data and ephemeral data are stored separately.
The way Ethereum keeps record is similar to that of the bank or financial institutions. For instance, customers use debit cards and they need to validate their funds before approving any withdrawal request. These banks also monitor customer’s funds.
Advantages of the Ethereum Virtual Machine
- The EVM grants users the permission to create their own DApp. The issue of the rich having the final say doesn’t apply. There is no room for inequality as there are a lot of utilities associate with DApps.
- Smart contracts are highly beneficial in various ways. With the introduction of NFTs, anybody can equally create a digital art and sell it off to make profits. They usually do this on a decentralized marketplace. This paves way for equality of rights in the marketplace, first of its kind.
Disadvantages of the Ethereum Virtual Machine
- Decentralization of the EVM network experiences limitations. Companies like Amazon are centralized in nature and they adopt Ethereum. If they choose to abandon Ethereum operations, there will be total shutdown which affects the EVM network. A similar incident has occurred before.
- Technical knowledge is necessary for EVM operation. You have to inclined in the area of coding to use EVM comfortably. Interfaces that are easy to use are still undergoing development.
There are softwares that have the graphic user interface that makes it easy to create NFTs and have access to similar marketplaces.
- Higher gas fees with increase in population of users on a network. This is so detrimental to Ethereum holders. Especially those with smaller amount of the coin, they might end up paying half of their portfolio as gas fee. Although, it slightly affects those with large quantity of Ethereum. This personally affects DApps because the rate of their operations will be very slow.
Operation of The Ethereum Virtual Machine
We use the term EVM severally in this article. It stands for Ethereum Virtual Machine. It creates confusion between the codes being run and the device that runs the code. This layer makes a software more portable and separated from their users.
How Do One Create Smart Contracts
Example Solidity contract (The Greeter)
The EVM executes smart contracts indirectly. They so this by translating them to Opcodes which are low level languages.
These are set of commands that EVM uses to carry out specific operations. Recently, there are 140 unique opcodes present and they give the EVM a retentive memory. This enables wide range of operations with sufficient resources. Opcodes represent 1 byte. Thus, there can only be a maximum number of 256 opcodes. To make this more understandable, they divide the opcodes into the following categories:
- Stack-manipulating opcodes (POP, PUSH, DUP, SWAP)
- Arithmetic/comparison/bitwise opcodes (ADD, SUB, GT, LT, AND, OR)
- Environmental opcodes (CALLER, CALLVALUE, NUMBER)
- Memory-manipulating opcodes (MLOAD, MSTORE, MSTORE8, MSIZE)
- Storage-manipulating opcodes (SLOAD, SSTORE)
- Program counter related opcodes (JUMP, JUMPI, PC, JUMPDEST)
- Halting opcodes (STOP, RETURN, REVERT, INVALID, SELFDESTRUCT)
Complete list of opcodes as of the Byzantium fork, including everything planned for Constantinople
One can convert opcodes to bytecode for easy storage. One can attach a byte to each opcode. Let’s take a look at an illustration below: 0x6001600101
Bytecode, split up into bytes, decoded to opcodes (execution flow), and current stack
From the diagram above, bytes within PUSH1 and PUSH32 needs to merge with the opcode. For this, it receives a special treatment. A bytecode can divide into its bytes while carrying out operations. Note that 1 byte represents 2 hexadecimals.
From the illustration above, 0x60 translates to PUSH1 which is 1byte. The next byte one encounters will add to it. One can carry out another PUSH1 command with 0x01 because they are both PUSH commands. There currently W similar items stacked together. Here comes the last command 0x01, but this time it interprets to an ADD command. One can remove and send back 2 similar items after adding them together. The stack will now have a total of one item which is 0x02.
Low level programming languages assigns value to variables via a stack. Unlike the other higher level languages that enables direct transfer. A stack contains only 1024 items.
The EVM operates as a stack machine that contains 1024 items. Out of this, one can only access 16 at once. Each item contains a word of 256 bits which facilitates the 256-bit cryptography.
Due to this, complex opcodes receives or sends data through adopts contract memory. However, the memory doesn’t last long. They don’t save memory details after executing contracts. Stack is to command reference as memory is to variable declaration.
They use contract storage to store unlimited data. It also makes these data available for future use. Contract storage is also a public ledger that allows users access the data without transaction fees. However, writing to memory is very much easier and less costly than writing to storage.
Smart Contract Transaction Costs
Hackers also attempts to create contracts that tends to slow down operation. This comes with high costs of transactions. Each opcode has its own transaction fees to help stop this malicious attack. Atimes, a lot of these opcodes charge different fees. The opcode KECCAK256 is a good example. It has a base cost of 30 gas, and a dynamic cost of 6 gas per word. The higher the complexity, the higher the gas fee and vice versa. However, 21000 gas is the starting value.
They can return gas fees to users during execution of functions which increases the state size. For instance, when you set a storage value to zero when it is not zero, they return 15,000 gas. But when you totally remove a contract, they return 24000 gas. However, this only happens at the end of a contract execution. It is at a limit of 50% of the gas which is part of the recent contract call. Contracts cannot make self payments.
Strategic Distribution of a Smart Contract
One can make normal transaction without an address during smart contract distribution. Furthermore, some bytecode serves as a constructor and as well added as input data. This constructor is necessary when writing previous variables to storage. It should occur before the copy the runtime bytecode. During this process, they run the bytecode for creation once. On the other hand, runtime bytecode runs on every contract call made.
Another example Solidity contract, along with its bytecode needed to distribute strategically.
We can divide the bytecode into 3 different parts, namely:
The work of the constructor is to write all initial values to storage and also copy the runtime bytecode to the contracts memory
This is the piece of bytecode which they write to memory while generating contracts.
The Solidity creates a Swarm hash of a metadata file which supplements at the end of the bytecode. We can simply define swarm as a decentralized storage medium for files. We can also describe it as distributed storage platform and a service that shares content. The EVM cannot translate swarm hash(although present in the runtime bytecode). This is because it has infinite location. In recent times, Solidity utilizes the following format:
0xa1 0x65 ‘b’ ‘z’ ‘z’ ‘r’ ‘0’ 0x58 0x20 [32 bytes swarm hash] 0x00 0x29. Thus, we can extract the following Swarm hash:
The metadata file contains details like the compiler version or the contract’s commands. Unfortunately, few contracts have uploaded their metadata to the public via the Swarm network. Recently, they made a check to test it.
How to translate machine readable codes to bytecode
There have been attempts to translate bytecode to machine readable codes. One can translate contracts into bytecode as a form of test- run using eveem.org or ethervm.io. However, this process keeps failing because of the compiler’s optimization. Nonetheless, we may still guess most command names with the hope of getting a correct result. They usually compare command signatures to large datasets with renowned command and event names.
An Application Binary Interface is a piece of data that records all commands and events. This ABI is necessary for contract calls. When they make the calls, hashing the name of the command and cutting off everything except the first 4 bytes determines the command signature.
Example Solidity contract, along with its ABI
From the illustration above, the command HelloWorld() clearly shows the signature hash 0x7fffb7bd. The input data needs to start with 0x7fffb7bd if at all this command is to considered. We can add in words, the reference or value that we can pass to a command. However, they are absent but these words come in form of 32-byte. They carry out this process after they enter into a transaction, the signature hash in a data.
But if those words are more than 32 bytes, we can further divided it into various words with reference. They also follow suit after the add the references. Moreover, we consider all the words first before array words in terms of addition. We rather include the starting point of the array words at the expense of the reference.
Solidity and the Ethereum Visual Machine are very important in the Ethereum ecosystem. They help developers to create wonderful and interesting products. It is costs less to execute operations through local means. Whereas, accessing the EVM via smart contracts might not be affordable by all. Apart from high transaction fees, there are lots of benefits we can get from decentralization.