Skip to content

Commit 545cd70

Browse files
committed
chain/ethereum: Check block cache before RPC in fetch_full_block_with_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.
1 parent 411a14e commit 545cd70

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

chain/ethereum/src/chain.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,10 @@ impl Blockchain for Chain {
588588
.block_ptrs_by_numbers(vec![number])
589589
.await
590590
.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-
}
591+
if let Some(ptrs) = cached.get(&number)
592+
&& ptrs.len() == 1
593+
{
594+
return Ok(BlockPtr::new(ptrs[0].hash.clone(), ptrs[0].number));
595595
}
596596

597597
let adapter = adapters
@@ -1094,10 +1094,10 @@ impl TriggersAdapterTrait<Chain> for TriggersAdapter {
10941094
.block_ptrs_by_numbers(vec![ptr.number])
10951095
.await
10961096
.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-
}
1097+
if let Some(ptrs) = cached.get(&ptr.number)
1098+
&& ptrs.len() == 1
1099+
{
1100+
return Ok(ptrs[0].hash == ptr.hash);
11011101
}
11021102

11031103
let adapter = adapter
@@ -1285,16 +1285,14 @@ impl TriggersAdapter {
12851285
adapters: &EthereumNetworkAdapters,
12861286
block_ptr: &BlockPtr,
12871287
) -> Result<Option<EthereumBlock>, Error> {
1288-
let adapter = adapters.cheapest_with(&self.capabilities).await?;
1289-
1290-
let block = adapter
1291-
.block_by_hash(&self.logger, block_ptr.hash.as_b256())
1292-
.await?;
1293-
1294-
match block {
1295-
Some(block) => {
1288+
// Use the cache-aware light block fetch first; it checks recent_blocks_cache
1289+
// and the DB before falling back to eth_getBlockByHash.
1290+
let light_block = self.fetch_light_block_with_rpc(adapters, block_ptr).await?;
1291+
match light_block {
1292+
Some(light_block) => {
1293+
let adapter = adapters.cheapest_with(&self.capabilities).await?;
12961294
let ethereum_block = adapter
1297-
.load_full_block(&self.logger, block)
1295+
.load_full_block(&self.logger, light_block.inner().clone())
12981296
.await
12991297
.map_err(|e| anyhow!("Failed to load full block: {}", e))?;
13001298
Ok(Some(ethereum_block))

0 commit comments

Comments
 (0)