mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-29 00:58:11 -05:00
feat: add block+receipts iter (#5560)
This commit is contained in:
22
crates/rpc/rpc/src/eth/cache/mod.rs
vendored
22
crates/rpc/rpc/src/eth/cache/mod.rs
vendored
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user