EVM

The EVM is a crucial part of the Ethereum network as it executes all the smart contracts on the network. The diagram provides a simplified view of how the EVM processes and stores information.
Here’s a breakdown of its components:
Machine State (Volatile):
This section encapsulates elements that are temporary and exist only during the execution of a contract. It includes:
Program Counter (PC): This is akin to a pointer. It keeps track of where in the contract’s bytecode we currently are during execution. It determines the next instruction to execute.
Gas Available: Every operation in the EVM requires a certain amount of gas to execute. This indicates how much computational effort is left to execute commands. It’s a measure to avoid infinite loops or excessive computations.
Stack: The EVM is a stack-based machine, and this is its temporary storage. The stack is used to perform operations like addition, subtraction, etc. It can hold up to 1024 elements.
Memory: This is another temporary storage area. Unlike the stack, it’s a word-addressable area and isn’t cleared between function calls. It’s also expandable.
Virtual ROM: This immutable section contains the EVM code or the bytecode that is executed by EVM. It’s unchangeable once deployed.
World State (Persistent): This is the persistent state of the EVM and includes:
Account Storage: A permanent storage area where each account’s data resides. Unlike memory, storage is persistent and stored on the blockchain. It’s a key-value store that maps 256-bit words to 256-bit words.
State transition function
The state transition function describes how the state of Ethereum changes from one state to another when a transaction is executed.
In Ethereum, the state is an enormous data structure called a modified Merkle Patricia Trie, which keeps all accounts linked by hashes and reducible to a single root hash stored on the blockchain. The state transition function is a formula that the EVM processes every time it executes a transaction. The purpose of the function is to make sure transactions adhere to the transaction standard and are technically valid (e.g., correct nonce and valid signature).
The state transition function can be formally described as follows:
Y(S,T)=S'
Given an old valid state (S) and a new set of valid transactions (T), the Ethereum state transition function Y(S, T) produces a new valid output state S'.
Here’s a simplified version of how the state transition function works when a transaction is executed1:
Some validity checks are performed.
The sender’s nonce is incremented by 1 and the gas allocated for the transaction is removed from the sender’s balance.
If the transaction specifies a receiver, we are dealing with a Message call. If not, we are dealing with a contract creation.
If the execution of point 3 was successful, the changes to the state are kept, otherwise, we return back to the changes before point 3.
The refund balance is returned to the sender and the used gas is given to the beneficiary address (i.e., the miner’s one).
The state transition function is instrumental in preserving the uniformity and reliability of data across the Ethereum blockchain. Enforcing stringent conditions for changes ensures that all alterations are legitimate and records consistent across all nodes.
Opcodes
In the Ethereum Virtual Machine (EVM), opcodes are the operations that the EVM can perform. Each opcode is a byte of data signifying a specific instruction, and collectively, they form the bytecode — the EVM’s low-level programming language.
The EVM operates on a stack-based architecture. Operands are pushed onto the stack, and operations are executed using these operands. The EVM is Turing-complete as a result of a set of 140 opcodes. That means it should be able to solve any computation problem.
Here are some examples of opcodes:
ADD: Performs addition of two integers.
MUL: Performs multiplication of two integers.
SUB: Performs subtraction of two integers.
Last updated