8.4 Gas, Ether, and the Ethereum Virtual Machine (EVM)

3 min readaugust 6, 2024

, , and the EVM are crucial components of Ethereum's ecosystem. Gas measures computational effort, with transactions and smart contracts consuming gas. Ether, the network's native currency, is used to pay for gas, with and as smaller denominations.

The is the heart of . It provides a consistent environment across all nodes, using a -based architecture with its own instruction set. The EVM's model includes stack, memory, and for efficient contract operations.

Gas and Fees

Gas as a Unit of Computation

Top images from around the web for Gas as a Unit of Computation
Top images from around the web for Gas as a Unit of Computation
  • Gas represents the computational effort required to execute operations on the Ethereum network
  • Every transaction and smart contract execution consumes a certain amount of gas
  • Gas is used to measure and allocate the computational resources needed for each operation
  • Complex operations (storage writes) consume more gas compared to simpler ones (additions)

Denominations of Ether

  • Wei is the smallest denomination of Ether, where 1 Ether = 10^18 Wei
    • Allows for precise calculations and avoids dealing with decimal points
  • Gwei (Gigawei) is another commonly used denomination, where 1 Gwei = 10^9 Wei
    • Often used to express gas prices in a more readable format (20 Gwei instead of 20,000,000,000 Wei)

Transaction Fees and Gas Management

  • represents the amount of Ether per unit of gas the sender is willing to pay for the transaction
    • Higher gas prices incentivize miners to prioritize the transaction
  • sets the maximum amount of gas the sender is willing to consume for the transaction
    • Prevents infinite loops or excessive resource consumption
  • is refunded to the sender, while the consumed gas is paid to the miner as a transaction fee
  • Senders must carefully set the gas price and limit to ensure successful and cost-effective transaction execution

Ethereum Virtual Machine (EVM)

EVM Architecture and Execution Environment

  • The Ethereum Virtual Machine (EVM) is a stack-based, isolated execution environment for smart contracts
  • Provides a runtime environment for executing Ethereum smart contracts across all nodes in the network
  • Ensures deterministic and consistent execution of smart contracts, regardless of the underlying hardware or operating system
  • Maintains a stack, memory, and storage for each contract execution

EVM Instruction Set and Opcodes

  • EVM has its own instruction set, consisting of that perform specific operations
  • Opcodes include arithmetic operations (, ), stack manipulations (, ), and storage access (, )
  • Each opcode has an associated gas cost, reflecting its computational complexity and resource usage
  • Smart contract code is compiled into EVM , which is a sequence of opcodes executed by the EVM

EVM Memory and Storage Model

  • EVM has three main data locations: stack, memory, and storage
  • The stack is a last-in-first-out (LIFO) data structure used for holding temporary values and managing execution flow
    • Most opcodes operate on the stack, pushing and popping values as needed
  • Memory is a byte-addressable, volatile storage area used for temporary data during contract execution
    • Provides a linear memory model for storing and accessing data within a single transaction
  • Storage is a persistent key-value store for long-term data that persists across transactions and contract invocations
    • Allows contracts to store and retrieve data across different function calls and transactions

Key Terms to Review (22)

Add: In the context of blockchain and cryptocurrency, 'add' typically refers to the process of incorporating or including data, transactions, or operations into a blockchain network. This action is crucial for maintaining an accurate and updated ledger, which is foundational to the functionality of smart contracts and decentralized applications running on platforms like Ethereum.
Bytecode: Bytecode is an intermediate representation of code that is executed by a virtual machine, specifically in the context of blockchain, it is the code generated from smart contracts written in high-level programming languages like Solidity. This format allows for efficient execution on the Ethereum Virtual Machine (EVM), enabling smart contracts to be deployed and executed in a decentralized environment. Bytecode interacts with gas and Ether for transaction fees and execution costs, forming a fundamental part of decentralized application (DApp) architecture.
Ether: Ether is the native cryptocurrency of the Ethereum network, primarily used to pay for transaction fees and computational services on the platform. It serves as a fuel for the Ethereum blockchain, enabling users to execute smart contracts and run decentralized applications (dApps) within the Ethereum ecosystem, making it essential for operations involving the Ethereum Virtual Machine (EVM).
Ethereum Virtual Machine (EVM): The Ethereum Virtual Machine (EVM) is a decentralized computing environment that enables the execution of smart contracts and decentralized applications (dApps) on the Ethereum blockchain. It acts as a runtime environment for these applications, allowing them to run consistently across all nodes in the network. The EVM is crucial to Ethereum’s functionality, as it ensures that all transactions and computations are executed securely and in a trustless manner, directly linking to key features of the Ethereum platform, its programming language Solidity, and the concepts of gas and Ether used for transactions.
EVM Architecture: EVM architecture refers to the structure and design of the Ethereum Virtual Machine (EVM), which is the runtime environment for executing smart contracts on the Ethereum blockchain. This architecture is critical as it provides the necessary components, such as the stack, memory, storage, and opcode system, that enable the decentralized execution of code, ensuring security and determinism across all nodes in the network.
EVM Instruction Set: The EVM instruction set refers to the collection of low-level operations that the Ethereum Virtual Machine (EVM) can execute to carry out smart contracts on the Ethereum blockchain. This instruction set is essential for defining how the EVM processes transactions and manages state changes, allowing developers to create complex decentralized applications (dApps) using smart contracts that are written in languages like Solidity. The efficiency and capability of the instruction set are crucial for the functioning of Ethereum, as it directly affects gas consumption and overall performance.
Gas: Gas is a unit of measurement used in Ethereum to quantify the amount of computational work required to execute operations or transactions on the network. It plays a critical role in the functioning of smart contracts and the Ethereum ecosystem by determining how much Ether a user needs to pay for processing transactions on the Ethereum Virtual Machine (EVM). Understanding gas is essential for optimizing contract performance and managing transaction costs effectively.
Gas limit: The gas limit is the maximum amount of gas that a user is willing to spend on executing a transaction or running a smart contract on the Ethereum network. This term is crucial because it defines the scope of work that can be performed within a transaction, impacting execution efficiency and the overall cost of using the Ethereum blockchain.
Gas Price: Gas price refers to the cost associated with executing transactions and smart contracts on the Ethereum network. This price is measured in gwei, which is a denomination of Ether, and it represents the fee that users are willing to pay miners for processing their requests. A higher gas price often leads to faster transaction confirmation times, as miners prioritize transactions that offer higher fees.
Gwei: Gwei is a denomination of ether, the native cryptocurrency of the Ethereum network, specifically used to measure gas prices in transactions. Since ether is typically a high-value asset, gwei serves as a more practical unit for everyday transactions, allowing users to specify how much they are willing to pay for computational resources on the Ethereum platform. This measurement is crucial for users interacting with smart contracts and decentralized applications, as it impacts transaction speed and cost.
Memory: In the context of blockchain technology, particularly the Ethereum Virtual Machine (EVM), memory refers to a temporary storage space used for data that is needed during the execution of smart contracts. Unlike storage, which is more permanent and costly, memory is volatile and is erased when a transaction is completed, making it crucial for short-term data handling while maintaining efficiency in contract execution.
Opcodes: Opcodes, short for operation codes, are the fundamental instructions used by the Ethereum Virtual Machine (EVM) to perform specific operations during smart contract execution. They represent a variety of functions, such as mathematical calculations, data manipulation, and control flow, making them essential for executing transactions on the Ethereum blockchain. Each opcode corresponds to a unique bytecode that is processed by the EVM, which ensures that smart contracts can function properly and efficiently.
Pop: In the context of Ethereum, 'pop' refers to the concept of removing or executing an operation within the Ethereum Virtual Machine (EVM), particularly related to managing the stack of data during the execution of smart contracts. It indicates the action of taking data off the top of the stack, which is crucial for executing functions and operations effectively in the EVM environment. Understanding how 'pop' interacts with gas and Ether is key to optimizing smart contract performance and resource management.
Push: In the context of blockchain, particularly within Ethereum, 'push' refers to an operation that adds data onto the stack of the Ethereum Virtual Machine (EVM). This operation is essential for executing smart contracts, as it allows various data types, including addresses and numbers, to be loaded onto the stack for processing. The push operation plays a crucial role in how the EVM processes instructions and manages state changes in a decentralized environment.
Sload: The `sload` opcode in Ethereum is used to read a value from the storage of a smart contract. This operation retrieves data stored at a specific location in the contract's state and is essential for maintaining and accessing persistent data across transactions. Understanding how `sload` interacts with gas consumption, state variables, and the Ethereum Virtual Machine (EVM) is crucial for optimizing smart contract performance and costs.
Smart contract execution: Smart contract execution refers to the process of automatically carrying out the terms and conditions of a smart contract once specific predefined criteria are met. This process is integral to blockchain technology, particularly on platforms like Ethereum, where the execution happens within the Ethereum Virtual Machine (EVM) and utilizes gas as a means to measure computational effort. The reliability and transparency of smart contracts streamline transactions by eliminating the need for intermediaries.
Sstore: sstore is an instruction used in the Ethereum Virtual Machine (EVM) to store a value in a specific location of the smart contract's storage. This operation is crucial for managing state variables within a contract, as it allows for data persistence across transactions and enables contracts to maintain information over time.
Stack: In the context of blockchain and the Ethereum ecosystem, a stack refers to a data structure used by the Ethereum Virtual Machine (EVM) to manage and manipulate data during the execution of smart contracts. The stack operates in a Last In First Out (LIFO) manner, allowing the EVM to efficiently store temporary values and manage computations necessary for processing transactions and executing code within smart contracts.
Storage: In the context of blockchain and cryptocurrency, storage refers to the method of saving and maintaining data securely within a distributed ledger or a network. It plays a crucial role in ensuring the integrity and accessibility of transactions and smart contracts within systems like Ethereum, where data is stored in a decentralized manner across multiple nodes to prevent tampering and loss.
Sub: In the context of Ethereum, 'sub' often refers to the concept of 'subsidy,' which is an incentive provided to participants in a network for their contributions. This term connects closely with Gas, Ether, and the Ethereum Virtual Machine (EVM) as it relates to how users are incentivized to validate transactions and execute smart contracts, ultimately ensuring the proper functioning of the network.
Unspent Gas: Unspent gas refers to the amount of computational power and resources that have been allocated for a transaction or smart contract execution in Ethereum but remain unused. This leftover gas can occur when a transaction does not consume all the gas it was given, leading to potential refunds to the sender. Understanding unspent gas is essential for optimizing transaction costs and ensuring efficient use of Ether in the Ethereum network.
Wei: Wei is the smallest denomination of Ether, the native cryptocurrency of the Ethereum network. Just like cents are to dollars, wei represents a fraction of Ether and is crucial for calculating transaction fees, known as gas, in the Ethereum ecosystem. Understanding wei helps users grasp how much they are spending or receiving in Ether, especially when dealing with small amounts or microtransactions.
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.