Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

EIP-234 proposes adding a block hash filter option to Ethereum's JSON-RPC API, allowing clients to retrieve logs associated with a particular block.

This article translates, summarizes, and expands on the following proposal.

More EIP explainers:

Overview

EIP-234 improves the filter options used by Ethereum JSON-RPC methods such as eth_newFilter and eth_getLogs. It introduces an option that identifies the desired block by its hash.

Existing problems

Traditionally, clients use fromBlock and toBlock to specify a range of blocks when retrieving event logs. This approach presents several problems.

  • Chain reorganizations (reorgs)
    • A reorganization can replace a block, causing previously retrieved logs to no longer belong to the canonical chain.
  • Unreliable network connections
    • Connection failures can make it difficult to retrieve logs for a particular block or track changes reliably.
  • Empty result sets
    • An empty result does not identify the block that was actually queried, making it difficult to determine whether the correct block had no matching logs.

Proposed improvements

The proposal adds a blockHash parameter to filter options.

  • Precise block selection
    • Clients can request logs for a particular block rather than whichever block currently occupies a given height.
  • Handling reorganizations
    • A hash identifies the same block even if the canonical chain changes. Retrieval still depends on the node retaining and serving that block.
  • More robust clients
    • Developers can associate query results with known blocks and implement more reliable event tracking.

Summary

Adding blockHash makes Ethereum event-log retrieval more flexible and reliable. It helps clients identify the block associated with their results, including when reorganizations or connection failures occur.

Motivation

The proposal introduces an optional blockHash field to the filter options of the Ethereum JSON-RPC API, including eth_newFilter and eth_getLogs.

The role of blockHash

A client can specify a particular block hash when filtering event logs. If the node knows that block, it returns results matching the filter for that block alone. If the block cannot be found, the node returns an error.

How it works

The option serves a purpose similar to the existing fromBlock and toBlock filters, except that it restricts the query to one specifically identified block.

This allows developers to retrieve events associated with an exact block rather than relying on its position in the chain.

Benefits

Identifying the block precisely improves the accuracy of the retrieved event data.

Even during a chain reorganization, the block hash continues to identify the intended block. Clients can therefore avoid confusing logs from two different blocks at the same height.

When network or node failures occur, a client can retry the request for the same block hash rather than inadvertently querying a replacement block. Successful retrieval is not guaranteed if the block is no longer available to the node.

These properties support more reliable Ethereum applications.

Additional considerations

Blockchain clients, including DApps, face a common problem: reliably receiving notifications about logs in newly added blocks and about logs removed during chain reorganizations.

A reorganization replaces one or more blocks in the canonical chain. Transactions previously included in those blocks may no longer be included at the same position, or may be absent from the new canonical chain.

Problems

  • Network or node failure
    • A client using a WebSocket log subscription can lose its connection. If a reorganization occurs while disconnected, the client may miss notifications that earlier logs have been removed.
  • Loss of filter state
    • An HTTP client polling for filter updates can encounter a node restart between requests. If the node loses the filter state, the client may not receive information about reorganizations that happened during the interruption.

Solutions

  • Maintaining a local chain model
    • A client maintains the latest n blocks locally and subscribes to or polls for new blocks. This helps it recover from disconnections, reorganizations, and node failures.
    • When a new block arrives, the client reconciles its model with the node's chain, fetching missing parent blocks or rolling back replaced blocks as necessary. Reorganizations beyond the retained history require additional recovery.
  • Fetching logs by block hash
    • Querying by hash rather than block number ensures that a successful result corresponds to the requested block.
    • Even when no logs match, the client knows which block it queried. It can then take appropriate action if the chain changes, such as rolling back that block's local effects and fetching its replacement.

The proposal helps clients handle changes to the canonical chain more reliably. Hash-based retrieval is particularly useful where reorganizations are possible, although clients still need their own reconciliation and finality policies.

Compatibility

Existing filter options

The fromBlock and toBlock fields select a range of block numbers. The new blockHash field selects an individual block by its hash.

Problem

Combining blockHash with fromBlock or toBlock introduces conflicting ways to identify the query's scope.

The hash already selects a single block, while the other fields select a range.

Solution

These options must be mutually exclusive. A request specifying blockHash must not also specify fromBlock or toBlock.

This distinction makes the API unambiguous and lets developers choose between range-based filtering and exact-block filtering according to their application's needs.

Tests

The following JSON-RPC example illustrates a request for logs associated with a particular block hash using eth_getLogs.

bash
{ "jsonrpc": "2.0", "id": 1, "method": "eth_getLogs", params: [{"blockHash": "0xbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0c"}] }

The example is reproduced unchanged from the original article. It is illustrative rather than valid JSON: the params property requires quotation marks, and the placeholder block hash must be replaced with an actual 32-byte hexadecimal hash.

JSON-RPC request structure

  • jsonrpc
    • The JSON-RPC protocol version, here 2.0.
  • id
    • An identifier returned in the response so the client can associate the response with its request.
  • method
    • The JSON-RPC method to invoke, here eth_getLogs.
  • params
    • The array of method parameters. In this example it contains a filter object specifying blockHash.

Request behavior

The request returns all matching logs from the block identified by blockHash. The illustrative placeholder in the original example is 0xbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0cbl0c.

If a topics filter is supplied, only logs matching those topic conditions are returned.

If the specified block does not exist or is unavailable, an error is returned. EIP-234 illustrates this with error code -32000, the message "Block not found.", and the requested block hash as error data. Exact error formatting may vary by client.

This request mechanism enables developers to retrieve log information for a specifically identified Ethereum block. It is useful for tracking smart contract events and analyzing block-level activity.

Citation

Micah Zoltu (@MicahZoltu), "EIP-234: Add blockHash to JSON-RPC filter options.," Ethereum Improvement Proposals, no. 234, March 2017. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-234.

Conclusion

EIP-234 introduces block-hash-based log filtering, allowing Ethereum clients to associate retrieved logs with an exact block and better handle reorganizations.

Questions are welcome via Twitter @cardene777.

Other publications:

https://chaldene.net/