From 973ca0082016691a8a397200fde81f5a978534f2 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sat, 25 Nov 2023 08:42:08 +0100 Subject: [PATCH] feat: add block+receipts iter (#5560) --- crates/rpc/rpc/src/eth/cache/mod.rs | 22 ++++++++-------------- crates/storage/provider/src/chain.rs | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/crates/rpc/rpc/src/eth/cache/mod.rs b/crates/rpc/rpc/src/eth/cache/mod.rs index 2ca9406cb1..80cf92d1b7 100644 --- a/crates/rpc/rpc/src/eth/cache/mod.rs +++ b/crates/rpc/rpc/src/eth/cache/mod.rs @@ -535,20 +535,14 @@ where { while let Some(event) = events.next().await { if let Some(committed) = event.committed() { - // we're only interested in new committed blocks - let (blocks, state) = committed.inner(); - - let blocks = blocks.iter().map(|(_, block)| block.clone()).collect::>(); - - // also cache all receipts of the blocks - let mut receipts = Vec::with_capacity(blocks.len()); - for block in &blocks { - let block_receipts = BlockReceipts { - block_hash: block.block.hash, - receipts: state.receipts_by_block(block.number).to_vec(), - }; - receipts.push(block_receipts); - } + let (blocks, receipts): (Vec<_>, Vec<_>) = committed + .blocks_and_receipts() + .map(|(block, receipts)| { + let block_receipts = + BlockReceipts { block_hash: block.block.hash, receipts: receipts.clone() }; + (block.clone(), block_receipts) + }) + .unzip(); let _ = eth_state_cache .to_service diff --git a/crates/storage/provider/src/chain.rs b/crates/storage/provider/src/chain.rs index 3a6ae3fffc..c970f8f35f 100644 --- a/crates/storage/provider/src/chain.rs +++ b/crates/storage/provider/src/chain.rs @@ -105,6 +105,23 @@ impl Chain { (ChainBlocks { blocks: Cow::Borrowed(&self.blocks) }, &self.state) } + /// Returns an iterator over all the receipts of the blocks in the chain. + pub fn block_receipts_iter(&self) -> impl Iterator>> + '_ { + self.state.receipts().iter() + } + + /// Returns an iterator over all blocks in the chain with increasing block number. + pub fn blocks_iter(&self) -> impl Iterator + '_ { + self.blocks().iter().map(|block| block.1) + } + + /// Returns an iterator over all blocks and their receipts in the chain. + pub fn blocks_and_receipts( + &self, + ) -> impl Iterator>)> + '_ { + self.blocks_iter().zip(self.block_receipts_iter()) + } + /// Get the block at which this chain forked. #[track_caller] pub fn fork_block(&self) -> ForkBlock {