EIPs Explained

EIP-1898 · Final · Interface · 2019-04-01

EIP-1898: Add `blockHash` to defaultBlock methods

Lets state-reading JSON-RPC calls such as eth_call and eth_getBalance target a block by its hash, so a series of queries is guaranteed to read the same state even across reorgs.

What problem does it solve?

Many RPC methods take a "default block" parameter: a block number or a tag like "latest". Neither pins down a single block. If a chain reorganisation happens between two calls, block number 100 may refer to a different block the second time, and "latest" moves anyway.

That breaks apps that need a consistent view across several calls: a wallet showing sender and recipient balances after a transfer, a DEX UI walking an on-chain order list with many eth_calls, or logic that depends on two pieces of state at once. Workarounds (re-checking block hashes after each call, relying on logs, vendor-specific subscriptions) are clumsy or incomplete.

EIP-1898 generalises what EIP-234 did for log filters: let callers name the exact block by hash.

How it works

For these methods:

eth_getBalance, eth_getStorageAt, eth_getTransactionCount, eth_getCode, eth_call, eth_getProof

the block parameter may now also be an object:

  • { "blockNumber": "0x..." }, or
  • { "blockHash": "0x...", "requireCanonical": false }

Rules:

  • If the block is not found, the node should return an error (recommended code -32001, Resource not found).
  • requireCanonical defaults to false, so a block that has been reorged out can still be queried if the node has it. If true, the node should also error when the block is not on the canonical chain (recommended -32000), with "not found" taking precedence.
  • The old forms (hex number, "latest", "earliest", "pending", "safe", "finalized") keep working.

Sample code

Raw JSON-RPC request:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "eth_getBalance",
  "params": [
    "0x0000000000000000000000000000000000000000",
    { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3", "requireCanonical": true }
  ]
}

(The hash above is mainnet's genesis block, the example the spec uses.)

Pinning several reads to one block with ethers v6:

import { JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const head = await provider.getBlock("latest");
const at = { blockHash: head!.hash, requireCanonical: false };

const [balA, balB] = await Promise.all([
  provider.send("eth_getBalance", [alice, at]),
  provider.send("eth_getBalance", [bob, at]),
]);
console.log(BigInt(balA), BigInt(balB)); // both read from exactly the same state

Things to know

  • Status is Final. Support can still vary between node providers, so test against yours.
  • Querying an old or non-canonical block only works if the node still has that state; non-archive nodes prune old state, so very old block hashes may return errors.
  • The method list in the spec does not cover every RPC call; check client docs for others.
  • The full method catalogue lives in the execution-apis spec; EIP-1474 is the older RPC spec this extends.

#json-rpc #reorg #eth_call #node #interface

Details

Authors
Charles Cooper (@charles-cooper)
Status
Final
Type
Standards Track / Interface
Created
2019-04-01
Requires

Machine-readable: Markdown · JSON