Skip to content

Commit 3c1fc99

Browse files
rnkrttmattsse
andauthored
perf(rpc): use maybe_cached_block_and_receipts for AtBlockHash (#19910)
Co-authored-by: Matthias Seitz <[email protected]>
1 parent 49059f5 commit 3c1fc99

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

crates/rpc/rpc/src/eth/filter.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -465,22 +465,23 @@ where
465465
) -> Result<Vec<Log>, EthFilterError> {
466466
match filter.block_option {
467467
FilterBlockOption::AtBlockHash(block_hash) => {
468-
// for all matching logs in the block
469-
// get the block header with the hash
470-
let header = self
471-
.provider()
472-
.header_by_hash_or_number(block_hash.into())?
473-
.ok_or_else(|| ProviderError::HeaderNotFound(block_hash.into()))?;
468+
// First try to get cached block and receipts, as it's likely they're already cached
469+
let Some((receipts, maybe_block)) =
470+
self.eth_cache().get_receipts_and_maybe_block(block_hash).await?
471+
else {
472+
return Err(ProviderError::HeaderNotFound(block_hash.into()).into())
473+
};
474474

475-
let block_num_hash = BlockNumHash::new(header.number(), block_hash);
475+
// Get header - from cached block if available, otherwise from provider
476+
let header = if let Some(block) = &maybe_block {
477+
block.header().clone()
478+
} else {
479+
self.provider()
480+
.header_by_hash_or_number(block_hash.into())?
481+
.ok_or_else(|| ProviderError::HeaderNotFound(block_hash.into()))?
482+
};
476483

477-
// we also need to ensure that the receipts are available and return an error if
478-
// not, in case the block hash been reorged
479-
let (receipts, maybe_block) = self
480-
.eth_cache()
481-
.get_receipts_and_maybe_block(block_num_hash.hash)
482-
.await?
483-
.ok_or(EthApiError::HeaderNotFound(block_hash.into()))?;
484+
let block_num_hash = BlockNumHash::new(header.number(), block_hash);
484485

485486
let mut all_logs = Vec::new();
486487
append_matching_block_logs(

0 commit comments

Comments
 (0)