Introduction to the EVM
Learn about the EVM.
We'll cover the following
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
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.
This illustration shows the key components and their relationships in managing gas within the EVM.