EIPs Explained

EIP-1884 · Final · Core · 2019-03-28

EIP-1884: Repricing for trie-size-dependent opcodes

Istanbul-era repricing that raised SLOAD, BALANCE and EXTCODEHASH gas costs to match the real cost of reading a growing state trie, and added the cheap SELFBALANCE opcode.

What problem does it solve?

Opcodes that read the state trie got slower as Ethereum's state grew, but their gas prices stayed the same. Measurements from go-ethereum full syncs showed storage and account reads dominating block processing time. SLOAD had become more than twice as slow per unit of gas as it was right after its last repricing in EIP-150.

Underpriced operations are dangerous: an attacker can fill blocks with them and make blocks slow to process. They also make processing time per gas uneven, which limits how high the block gas limit can safely go.

How it works

Activated in the Istanbul hard fork:

Opcode Before After
SLOAD (0x54) 200 800
BALANCE (0x31) 400 700
EXTCODEHASH (0x3f) 400 700
SELFBALANCE (0x47, new) — 5
  • BALANCE was raised to 700 to match EXTCODESIZE, which already cost 700.
  • EXTCODEHASH (EIP-1052) was priced the same as BALANCE because it performs the same account lookup, so it rose too.
  • SELFBALANCE takes no arguments and pushes the current contract's balance. Checking your own balance is cheap because the account is already loaded, so it no longer needs to pay the price of an arbitrary lookup. It sits at 0x47 because the 0x3X range was full, and costs 5 rather than 2 because balance can change during execution and still needs a cached lookup.

Sample code

Solidity compilers targeting Istanbul or later compile address(this).balance to SELFBALANCE automatically. In inline assembly it is available directly:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Vault {
    function myBalance() external view returns (uint256 bal) {
        assembly {
            bal := selfbalance() // 5 gas, vs. balance(address()) for an external lookup
        }
    }
}

The fallback-function pitfall this EIP exposed:

contract Fragile {
    uint256 public counter;
    // Called via transfer()/send(), which forward only a 2300 gas stipend.
    // An SLOAD (and SSTORE) here can exceed the stipend after a repricing -> the transfer reverts.
    receive() external payable { counter += 1; }
}

Things to know

  • Status is Final. It shipped in Istanbul alongside other changes such as EIP-2200 net gas metering.
  • It was a breaking change for contracts that assumed fixed gas costs. Fallback functions that read storage could run out of gas under the 2300 stipend forwarded by transfer/send. This is a main reason those methods are now discouraged in favour of call.
  • The spec checked ERC-165's 30,000-gas limit for supportsInterface and judged that typical implementations would stay well within it.
  • These prices were reworked again in Berlin by EIP-2929, which introduced cold/warm access pricing (for example, a cold SLOAD costs 2100 and a warm one 100).

#gas #opcodes #state #core #istanbul

Details

Authors
Martin Holst Swende (@holiman)
Status
Final
Type
Standards Track / Core
Created
2019-03-28
Requires
Required by

Machine-readable: Markdown · JSON