EIPs Explained

EIP-2935 · Final · Core · 2020-09-03

EIP-2935: Serve historical block hashes from state

Stores the last 8191 block hashes in a system contract's storage, so contracts and stateless clients can read about a day of block-hash history from state instead of relying on BLOCKHASH's 256-block window.

What problem does it solve?

The BLOCKHASH opcode only returns hashes for the most recent 256 blocks, and the EVM silently assumes the client has those hashes at hand. That assumption breaks for stateless clients, which receive just the state they need (a witness) rather than holding the chain locally. If block hashes live in state, they can be included in a witness like any other storage value.

Putting them in state also gives contracts and rollups a much longer window to prove things about recent blocks, without changing what BLOCKHASH itself does.

How it works

Parameter Value
HISTORY_SERVE_WINDOW 8191
BLOCKHASH_SERVE_WINDOW 256 (unchanged)
SYSTEM_ADDRESS 0xfffffffffffffffffffffffffffffffffffffffe
HISTORY_STORAGE_ADDRESS 0x0000F90827F1C53a10cb7A02335B175320002935
  • At the start of every block, before any transaction, the client calls the history contract from SYSTEM_ADDRESS with the parent block hash as calldata. This follows the system-call conventions of EIP-4788: it must complete, does not count against the block gas limit, and transfers no value.
  • The contract's set path stores the hash in a ring buffer: slot (block.number - 1) % 8191.
  • Any other caller hits the get path: send a 32-byte big-endian block number and receive its 32-byte hash. The call reverts if the input is not exactly 32 bytes or the number is outside [block.number - 8191, block.number - 1].
  • BLOCKHASH semantics and costs are untouched.
  • The contract is deployed with a pre-signed transaction from a keyless sender, which is how the fixed address is obtained.

After activation it takes 8191 blocks (about a day at current slot times) to fill the buffer; hashes from before the fork block are never written.

Sample code

Reading an older block hash from Solidity (on a chain where this EIP is active):

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

library BlockHashHistory {
    address constant HISTORY = 0x0000F90827F1C53a10cb7A02335B175320002935;

    function get(uint256 blockNumber) internal view returns (bytes32 h) {
        (bool ok, bytes memory out) = HISTORY.staticcall(abi.encode(blockNumber));
        require(ok && out.length == 32, "out of range");
        h = abi.decode(out, (bytes32));
    }
}

Off-chain with ethers v6:

import { JsonRpcProvider, toBeHex, zeroPadValue } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const n = (await provider.getBlockNumber()) - 1000;
const hash = await provider.call({
  to: "0x0000F90827F1C53a10cb7A02335B175320002935",
  data: zeroPadValue(toBeHex(n), 32),
});
console.log(n, hash);

Things to know

  • The contract is called like any other contract and follows normal EIP-2929 warm/cold gas rules; the system call does not pre-warm it.
  • Calldata must be exactly 32 bytes; ABI-encoding a single uint256 produces that.
  • The window is 8191 blocks, not unlimited. For older history, proofs against beacon block roots (via EIP-4788) are the intended route.
  • EIP-7709 (Draft) proposes making BLOCKHASH itself read from this contract.

#core #state #blockhash #system-contract #stateless

Details

Authors
Vitalik Buterin (@vbuterin), Tomasz Stanczak (@tkstanczak), Guillaume Ballet (@gballet), Gajinder Singh (@g11tech), Tanishq Jasoria (@tanishqjasoria), Ignacio Hagopian (@jsign), Jochem Brouwer (@jochem-brouwer), Sina Mahmoodi (@s1na)
Status
Final
Type
Standards Track / Core
Created
2020-09-03
Required by

Machine-readable: Markdown · JSON