Skip to content

chain/ethereum: Reduce unnecessary eth_getBlockByHash and eth_getBlockByNumber RPC calls#6491

Open
lutter wants to merge 2 commits intomasterfrom
lutter/block-by
Open

chain/ethereum: Reduce unnecessary eth_getBlockByHash and eth_getBlockByNumber RPC calls#6491
lutter wants to merge 2 commits intomasterfrom
lutter/block-by

Conversation

@lutter
Copy link
Copy Markdown
Collaborator

@lutter lutter commented Apr 8, 2026

Both eth_getBlockByHash and eth_getBlockByNumber were being called unconditionally in several places, even when the block data was already available in the block cache. With many syncing subgraphs this generates a flood of redundant RPC calls.

This PR makes three methods cache-aware before falling back to RPC:

  • block_pointer_from_number — previously always called eth_getBlockByNumber. Now checks chain_store.block_ptrs_by_numbers first and only falls back to RPC when the block number isn't in the cache or has multiple entries (recorded reorg).
  • is_on_main_chain — same pattern as above. This is called every reconciliation cycle for subgraphs beyond the reorg threshold, so the savings are proportional to the number of syncing subgraphs.
  • fetch_full_block_with_rpc — previously called adapter.block_by_hash() directly, bypassing the cache entirely. Now delegates the light block lookup to fetch_light_block_with_rpc, which checks recent_blocks_cache and the DB before making an RPC call. This matters in two cases: when the block is already in the cache as a light block (receipts missing but block data present), and when the block was just populated by the preceding walk_back_ancestor → parent_ptr chain.

No semantic changes — RPC is still the fallback whenever the cache doesn't have a definitive answer.

lutter added 2 commits April 8, 2026 11:03
…block_pointer_from_number

Both methods previously always made an eth_getBlockByNumber RPC call.
is_on_main_chain is called every reconciliation cycle for subgraphs that
are beyond the reorg threshold, so with many syncing subgraphs this
generated a flood of unnecessary calls for blocks already in the cache.

Both methods now check chain_store.block_ptrs_by_numbers first and only
fall back to RPC when the cache has no entry or an ambiguous result
(multiple blocks at the same number due to a recorded reorg).
…_rpc

fetch_full_block_with_rpc previously called adapter.block_by_hash()
directly, bypassing the block cache and always making an eth_getBlockByHash
RPC call. It is called from ancestor_block when the cached block has no
receipts, or after walking back the chain via parent_ptr (which populates
the cache). In both cases the block may already be available in the cache.

Change it to delegate the light block fetch to fetch_light_block_with_rpc,
which goes through adapter.load_blocks() and chain_store.blocks() —
checking recent_blocks_cache and the DB before falling back to RPC.
@lutter lutter requested a review from incrypto32 April 8, 2026 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants