89DEVs

Ethereum Virtual Machine (EVM)

The Ethereum Virtual Machine (EVM) can be described as the heart of the Ethereum network and it is one of the most important inventions in human history. The EVM is a distributed state machine.

Ethereum Virtual Machine can be compared to a CPU but is not a single entity. It is thousands of interconnected computers running an Ethereum client. They are distributed across the globe in different geolocations and use different software and different hardware to create a truly decentralized network that is resistant to censorship and doesn't depend on intermediaries.

The purpose of the Ethereum Virtual Machine is to provide an environment for smart contracts that can run continuously and uninterrupted. It allows running the Ethereum client on computers regardless of their operating system or hardware. Each computer in the Ethereum network is called a node and the nodes are connected to other nodes to make the network decentralized. 

GETH

To run an Ethereum Virtual Machine the command-line interface geth is used. It is the official implementation of the Ethereum protocol in the Golang programming language. It allows running Ethereum on machines with different hardware and different software like windows, ubuntu, mac and so on.

EOAs and smart contracts

There are two kinds of addresses in the Ethereum networks: Smart contracts that can actually run code and regular addresses that are only used for transactions. The regular addresses are called Externally Owned Accounts (EOA) and in general, there is a person behind it, like a bank account belongs to a person or company.
In general, there are two types of accounts: externally owned accounts, controlled by private keys, and contract accounts, controlled by their contract code.
The last two fields of the mapping, the code and storage are only used for smart contracts, while for regular addresses (EOA) they are empty.

solidity compiles to EVM-bytecode

When a smart contract is deployed to the Ethereum network it runs on the Ethereum Virtual Machine. The Solidity programming language is a high-level programming language and before a smart contract can be deployed to the Ethereum network it has to be compiled. The compiler turns the high-level code written in Solidity into EVM-compatible bytecode for the Ethereum Virtual Machine. The EVM bytecode is only readable by machines not by humans and that's the reason why high-level languages like Solidity were invented to enable blockchain developers to write code that can be understood by humans. There are different compiler version and the specific compiler version for the code is declared in the pragma directive. It is best practice to specify the pragma version of the smart contract on the top of the code. The compiler can be installed on your computer like solc or you can use an IDE like remix to compile the smart contracts, this is the preferred method for beginners. The bytecode is machine code that can be understood by the Ethereum Virtual Machine. It consists of two parts: The creation code and run-time code. When a smart contract is deployed to the Ethereum network the creation code is executed and the run-time code is distributed to the nodes in the Ethereum network. The smart contract is therefore not deployed to a single machine, but to different machines (nodes) at the same time. When the contract is called, the call is executed on different machines that all have the smart contract stored. Smart contracts can be written in other programming languages like Vyper or Rust as well but most smart contracts are currently written in Solidity.

ABI: Application Binary Interface

The Application Binary Interface, short ABI, is a way to communicate between the smart contract and code that is not stored on the blockchain. There are libraries like web3 and Ethers.js that allow interacting with the Ethereum blockchain using other programming languages like JavaScript, Python or Rust. These libraries use the ABI to communicate with the smart contract. Essentially the ABI acts as an intermediary between the code written in let's say JavaScript and the EVM bytecode. The ABI allows interaction between applications and contracts as well as the interaction between two contracts. The ABI is represented in JSON format.

gas price and opcodes

The instructions given to the Ethereum Virtual Machine are called Opcodes so when the EVM executes a smart contract it executes the opcodes specified in the smart contract. Each opcode has a specified gas cost associated with it. The executed opcodes are put on the stack in the machine state and the EVM can also access memory. When a smart contract is executed the cost will be the sum of the gas costs of all opcodes. The gas price is flexible and depends on the demand and supply of computation power on the network. The final cost will be the gas times the gas price and is paid in Ether or a smaller denomination of Ether called Wei. You can use our Wei converter to convert Ether to Wei and other denominations. Smart contracts compete with each other for computing power on the Ethereum Virtual Machine. If more Ether is paid for a unit of gas, the gas price will go up. The smart contract willing to pay the highest gas price will be executed faster than a smart contract that is willing to pay a lower gas price. So the execution time can be sped up by paying a higher transaction fee. When interacting with the blockchain to read data from the blockchain is free of charge, but writing to the blockchain requires paying a transaction fee.

state machine

The Ethereum Virtual machine is a state machine. The input is the old state and a transaction to be applied and the output is the new state. The state of the blockchain is a mapping of accounts that are identified by their addresses to four fields.
  1. The current balance of Ether owned by the address
  2. The nonce is an integer that is incremented for each transaction
  3. The hash of the smart contract code
  4. The hash of the smart contract storage
Smart contracts can change the state through transactions and the change is then propagated to different nodes across the Ethereum network.

        

Summary

Click to jump to section