EIP-2929 · Final · Core · 2020-09-01
EIP-2929: Gas cost increases for state access opcodes
Introduced warm and cold state access: the first touch of an address or storage slot in a transaction costs 2600 or 2100 gas, later touches cost 100, closing a long-standing DoS vector.
What problem does it solve?
Opcodes that read state, such as SLOAD, BALANCE, EXTCODESIZE and the CALL family, may force a node to fetch data from disk. They had been underpriced for years. The authors cite research in which deliberately IO-heavy transactions took 20 to 80 seconds to process, compared with milliseconds for an average transaction.
Simply raising every price would have punished contracts that read the same data repeatedly, even though the second read hits a cache. EIP-2929 charges the expensive price only the first time something is touched in a transaction. It went live in the Berlin upgrade (April 2021).
How it works
During each transaction the client keeps two sets:
accessed_addresses, pre-filled with the sender,tx.to(or the created contract) and all precompiles;accessed_storage_keys, a set of(address, slot)pairs, starting empty.
| Constant | Gas |
|---|---|
COLD_ACCOUNT_ACCESS_COST |
2600 |
COLD_SLOAD_COST |
2100 |
WARM_STORAGE_READ_COST |
100 |
- Account access (
BALANCE,EXTCODESIZE,EXTCODECOPY,EXTCODEHASH,CALL,CALLCODE,DELEGATECALL,STATICCALL): 2600 if the address is cold (then mark it warm), otherwise 100. For calls this is charged before the 63/64 gas forwarding rule. SLOAD: 2100 if the slot is cold, otherwise 100.SSTORE: adds 2100 if the slot is cold. EIP-2200 parameters are adjusted (SLOAD_GASbecomes 100,SSTORE_RESET_GASbecomes5000 - 2100), so a cold read-modify-write costs about the same as before.SELFDESTRUCT: adds 2600 if the beneficiary is cold.CREATE/CREATE2warm the new address immediately.- If a call frame reverts, the warm sets roll back to their state before that frame.
Sample code
Gas difference between a cold and a warm read in one transaction:
contract WarmCold {
uint256 public x = 1;
function readTwice() external view returns (uint256 cold, uint256 warm) {
uint256 g = gasleft();
uint256 a = x; // first SLOAD of this slot: cold (2100)
cold = g - gasleft();
g = gasleft();
uint256 b = x; // same slot again: warm (100)
warm = g - gasleft();
require(a == b);
}
}
(The measured numbers include a little overhead, and the optimiser may merge the two reads.)
Pre-warming with an access list (EIP-2930), ethers v6:
const tx = await wallet.sendTransaction({
to: token,
data,
accessList: [{ address: token, storageKeys: [balanceSlot] }],
});
Things to know
- Status: Final, activated in Berlin alongside EIP-2930 access lists, which let a transaction declare addresses and slots up front to pre-pay them at a slightly lower rate and avoid breaking contracts with hard-coded gas.
- Contracts that forward a fixed amount of gas, or rely on the 2300 gas stipend of
transfer/send, can break when a callee's first storage read now costs 2100. Avoid hard-coded gas amounts. - The warm/cold model is the basis for later changes: EIP-3651 pre-warms the block's
COINBASE, and refund rules were revised in EIP-3529. - Reading the same slot repeatedly is now cheap; reading many distinct slots or contracts is what costs gas.