From 041e29347b2a01b9e971ebea988561f4dae13f30 Mon Sep 17 00:00:00 2001 From: cairo <101215230+cairoeth@users.noreply.github.com> Date: Fri, 12 Apr 2024 22:17:02 +0200 Subject: [PATCH] feat(rpc): add block timestamp to logs (#7606) --- crates/rpc/rpc/src/eth/filter.rs | 19 +++++++++++++------ crates/rpc/rpc/src/eth/logs_utils.rs | 3 ++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/crates/rpc/rpc/src/eth/filter.rs b/crates/rpc/rpc/src/eth/filter.rs index 8ae7f6c55c..b14c20451c 100644 --- a/crates/rpc/rpc/src/eth/filter.rs +++ b/crates/rpc/rpc/src/eth/filter.rs @@ -349,11 +349,12 @@ where async fn logs_for_filter(&self, filter: Filter) -> Result, FilterError> { match filter.block_option { FilterBlockOption::AtBlockHash(block_hash) => { - // all matching logs in the block - let block_number = self + // for all matching logs in the block + // get the block header with the hash + let block = self .provider - .block_number_for_id(block_hash.into())? - .ok_or_else(|| EthApiError::UnknownBlockNumber)?; + .header_by_hash_or_number(block_hash.into())? + .ok_or(ProviderError::HeaderNotFound(block_hash.into()))?; // we also need to ensure that the receipts are available and return an error if // not, in case the block hash been reorged @@ -369,9 +370,10 @@ where &mut all_logs, &self.provider, &filter, - (block_hash, block_number).into(), + (block_hash, block.number).into(), &receipts, false, + block.timestamp, )?; Ok(all_logs) @@ -440,7 +442,10 @@ where // only one block to check and it's the current best block which we can fetch directly // Note: In case of a reorg, the best block's hash might have changed, hence we only // return early of we were able to fetch the best block's receipts - if let Some(receipts) = self.eth_cache.get_receipts(chain_info.best_hash).await? { + // perf: we're fetching the best block here which is expected to be cached + if let Some((block, receipts)) = + self.eth_cache.get_block_and_receipts(chain_info.best_hash).await? + { logs_utils::append_matching_block_logs( &mut all_logs, &self.provider, @@ -448,6 +453,7 @@ where chain_info.into(), &receipts, false, + block.header.timestamp, )?; } return Ok(all_logs) @@ -487,6 +493,7 @@ where BlockNumHash::new(header.number, block_hash), &receipts, false, + header.timestamp, )?; // size check but only if range is multiple blocks, so we always return all diff --git a/crates/rpc/rpc/src/eth/logs_utils.rs b/crates/rpc/rpc/src/eth/logs_utils.rs index 6e58040f0b..4a7a0b6ae0 100644 --- a/crates/rpc/rpc/src/eth/logs_utils.rs +++ b/crates/rpc/rpc/src/eth/logs_utils.rs @@ -49,6 +49,7 @@ pub(crate) fn append_matching_block_logs( block_num_hash: BlockNumHash, receipts: &[Receipt], removed: bool, + block_timestamp: u64, ) -> Result<(), FilterError> { // Tracks the index of a log in the entire block. let mut log_index: u64 = 0; @@ -97,7 +98,7 @@ pub(crate) fn append_matching_block_logs( transaction_index: Some(receipt_idx as u64), log_index: Some(log_index), removed, - block_timestamp: None, + block_timestamp: Some(block_timestamp), }; all_logs.push(log); }