Understanding Ethereum Gas, Blocks and the Fee Market
Gas is one of the most fundamental concepts on Ethereum but it also seems to be one of the most misunderstood. This isn’t surprising because while on the surface the concept is easy to grasp, there are many layers. The goal of this article is to cover the essential concepts when it comes to gas, blocks and the fee market.
It’s first important to understand vital terminology for transactions.
The EVM — the Ethereum Virtual Machine (EVM) running on each Ethereum node — is an emulation of a computer system. One example of a regular, non-blockchain virtual machine is the VirtualBox software, which allows you to emulate computer systems (guests) on your physical hardware (hosts). Any operation in the EVM consumes CPU cycles, disk access, or memory on the hosting machine. We must somehow measure this work and that is called gas.
Each operation on the EVM consumes a certain amount of gas and not all transactions are created equally. Accessing memory or writing to disk have differing costs with each EVM during execution of a contract. Essentially the more complex the contract and the more operations it performs, the more expensive it is to run it. For example, a simple send of ETH is 21,000 gas but a bet on Augur can cost 1,000,000 gas. In order to cover these costs, the sender of a transaction specifies how much they are willing to pay per unit of gas, called the gas price, which is denominated in gwei (1 gwei = 0.000000001 ETH). We can now calculate a transaction cost which is ‘gas consumed x gas price’.
If a malicious operator crafted a smart contract that went into an infinite loop, each loop would consume some gas and have an infinite cost to the user. Therefore there is a gas limit on every transaction, at which point the EVM would abort the execution of this contract if hit but still charge the malicious transaction the full fee.
Eventually, transactions get batched together and put into blocks by miners. Every transaction that occurs changes the state of Ethereum and occupies a small amount of space in a block, contributing to both the state size and block size.
Another factor to consider when thinking of blocks is the uncle rate. Uncle blocks are an entire article on their own but in very short, if two blocks are found simultaneously by miners, they race across the network to see which propagates first. The “winner” gets the main block with a 2 ETH reward and the loser gets an uncle block with a smaller reward. Due to optimizations in the Geth and Parity clients recently, uncle rates have fallen and are steady around 7%.
In order to keep state size and uncle rates in check, Ethereum has the concept of a block gas limit. Since every transaction has an associated total gas consumed, this limit determines how much computation can be done on the Ethereum network per block.
A recap on the above:
Now that we understand the key concepts around transactions and blocks, it’s time to discuss the Ethereum gas market. Since we have a limit on how much computation can be done per block, we have a cap on the “supply” available for Ethereum transactions a day. This means that a market is created by those on the demand side to get their transactions included in blocks.
At this point, users determine how much they are willing to pay per unit of gas (gas price). This will fluctuate depending on the current demand of the network and there are plans to overhaul the current system through EIP-1559. Due to the gas block limit, the fee market almost always determines what order transactions are mined because miners looking to profit will select the transactions with the highest fees.
After sitting at a block gas limit of 8,000,000 for over a year, miners on Ethereum have recently voted it up to 10,000,000, giving the network 25% more capacity. What’s interesting about this is that so far the block size has not followed it up. One explanation relates to what was discussed earlier in that every opcode in the EVM has an associated gas cost for complexity. A more complex transaction can cost more to execute but that does not mean it costs more to store in a block.
Transactions per day is not the best way to measure usage of the Ethereum network. The proper metric would be total gas spent per day. Over time, as usage of smart contracts have become more popular, the gas/tx on the network has gone up. So while transaction counts are not at an all time high, gas used is.
Hopefully this primer has helped in your understanding of how the Ethereum network functions.