Introduction to the EVM

Learn about the EVM.

The EVM is the crucial element of the Ethereum blockchain, particularly important for beginners seeking to understand Solidity. The EVM essentially provides a runtime environment for the execution of smart contracts in Ethereum. EVM is necessary to provide the needed level of security and the ability to run untrusted programs via a global network made up of public nodes. The importance of EVM is further demonstrated by how well it defends against denial-of-service (DoS) attacksA denial-of-service (DoS) attack occurs when a malicious actor aims to make a computer or device inaccessible to its intended users by disrupting its normal operation. This is usually done by flooding the targeted machine with requests, causing legitimate traffic to be unable to be handled, therefore denying service to legitimate users.. EVM is also in charge of preventing a particular program from accessing the states of other programs. The EVM’s functionality emphasizes the importance of establishing communication free from external intervention.

Press + to interact

Turning completeness

Turing completeness is a property that enables a system to perform any computable task, given sufficient resources. A state machine that’s almost Turing complete is an EVM. More importantly, virtual machines create a degree of abstraction between the machine and the code that’s being executed. In order to improve the portability of software, this layer is necessary. Additionally, it has to guarantee that applications are kept apart from one another and from their host.

EVM execution context and architecture

The EVM operates as a stack-based system with a depth of 1,024 items. Each of these items is a 256-bit word, a size specifically chosen for its ease in cryptographic operations like Keccak-256 hashes or secp256k1 signatures. During execution, the EVM has temporary memory that doesn’t persist between transactions. This memory is organized as an array of bytes, accessible by word addresses. Contracts in the EVM also have a more permanent storage structure, known as a Merkle-Patricia trie, which is tied to the corresponding account and is a part of the blockchain’s overall state.

The EVM uses operation codes (opcodes) to execute the compiled bytecode of smart contracts. These opcodes perform standard stack operations such as XOR, AND, ADD, and SUB. Additionally, the EVM supports blockchain-specific stack operations like ADDRESS, BALANCE, and BLOCKHASH.

The following outline provides an overview of the interplay between gas, code, and storage within the EVM:

  • Available gas: This specifies the initial amount of gas set aside for executing a transaction or operation within the EVM.

  • Program counter: This manages the sequence in which the EVM code’s instructions are executed.

  • EVM code: This is the actual bytecode to be run by the EVM, often compiled from languages like Solidity.

  • Operations: This reflects the gas cost for each individual operation or instruction in the EVM code, subtracted from the initial gas available. Message call from operations to more gas indicates that the execution of certain operations might require an additional allocation of gas beyond the initially available amount.

  • Stack: This acts as a recordkeeping mechanism for the cumulative gas costs incurred during the execution of nested or sequential operations.

  • Memory: This represents the EVM’s temporary storage area used for the transaction, where associated gas costs are incurred.

  • Storage: This denotes an additional reserve of gas, possibly for unexpected computational requirements or as part of contract-based or account-based storage.

Press + to interact
EVM execution
EVM execution

This illustration shows the key components and their relationships in managing gas within the EVM.