feat: add block+receipts iter (#5560)

This commit is contained in:
Matthias Seitz
2023-11-25 08:42:08 +01:00
committed by GitHub
parent 7ef2d49a1f
commit 973ca00820
2 changed files with 25 additions and 14 deletions

View File

@@ -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::<Vec<_>>();
// 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

View File

@@ -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<Item = &Vec<Option<Receipt>>> + '_ {
self.state.receipts().iter()
}
/// Returns an iterator over all blocks in the chain with increasing block number.
pub fn blocks_iter(&self) -> impl Iterator<Item = &SealedBlockWithSenders> + '_ {
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<Item = (&SealedBlockWithSenders, &Vec<Option<Receipt>>)> + '_ {
self.blocks_iter().zip(self.block_receipts_iter())
}
/// Get the block at which this chain forked.
#[track_caller]
pub fn fork_block(&self) -> ForkBlock {