Skip to content

Commit 411a14e

Browse files
committed
chain/ethereum: Check block cache before RPC in is_on_main_chain and 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).
1 parent 3fba7f9 commit 411a14e

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

chain/ethereum/src/chain.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,18 @@ impl Blockchain for Chain {
582582
.await
583583
.map_err(IngestorError::Unknown),
584584
ChainClient::Rpc(adapters) => {
585+
let cached = self
586+
.chain_store
587+
.cheap_clone()
588+
.block_ptrs_by_numbers(vec![number])
589+
.await
590+
.unwrap_or_default();
591+
if let Some(ptrs) = cached.get(&number) {
592+
if ptrs.len() == 1 {
593+
return Ok(BlockPtr::new(ptrs[0].hash.clone(), ptrs[0].number));
594+
}
595+
}
596+
585597
let adapter = adapters
586598
.cheapest()
587599
.await
@@ -1076,6 +1088,18 @@ impl TriggersAdapterTrait<Chain> for TriggersAdapter {
10761088
Ok(block.hash() == ptr.hash)
10771089
}
10781090
ChainClient::Rpc(adapter) => {
1091+
let cached = self
1092+
.chain_store
1093+
.cheap_clone()
1094+
.block_ptrs_by_numbers(vec![ptr.number])
1095+
.await
1096+
.unwrap_or_default();
1097+
if let Some(ptrs) = cached.get(&ptr.number) {
1098+
if ptrs.len() == 1 {
1099+
return Ok(ptrs[0].hash == ptr.hash);
1100+
}
1101+
}
1102+
10791103
let adapter = adapter
10801104
.cheapest()
10811105
.await

0 commit comments

Comments
 (0)