feat: add block_range provider function (#4819)

This commit is contained in:
Matthias Seitz
2023-09-27 12:33:01 +02:00
committed by GitHub
parent b994d159b4
commit 66fc1964d7
6 changed files with 42 additions and 4 deletions

View File

@ -15,7 +15,10 @@ use reth_primitives::{
H256, U256,
};
use reth_revm_primitives::primitives::{BlockEnv, CfgEnv};
use std::{ops::RangeBounds, sync::Arc};
use std::{
ops::{RangeBounds, RangeInclusive},
sync::Arc,
};
use tracing::trace;
mod provider;
@ -247,6 +250,10 @@ impl<DB: Database> BlockReader for ProviderFactory<DB> {
fn block_with_senders(&self, number: BlockNumber) -> RethResult<Option<BlockWithSenders>> {
self.provider()?.block_with_senders(number)
}
fn block_range(&self, range: RangeInclusive<BlockNumber>) -> RethResult<Vec<Block>> {
self.provider()?.block_range(range)
}
}
impl<DB: Database> TransactionsProvider for ProviderFactory<DB> {

View File

@ -1086,6 +1086,17 @@ impl<'this, TX: DbTx<'this>> BlockReader for DatabaseProvider<'this, TX> {
Ok(Some(Block { header, body, ommers, withdrawals }.with_senders(senders)))
}
fn block_range(&self, range: RangeInclusive<BlockNumber>) -> RethResult<Vec<Block>> {
let mut blocks = Vec::with_capacity(range.end().saturating_sub(*range.start()) as usize);
// TODO: this can be optimized by using cursors
for num in range {
if let Some(block) = self.block_by_number(num)? {
blocks.push(block);
}
}
Ok(blocks)
}
}
impl<'this, TX: DbTx<'this>> TransactionsProvider for DatabaseProvider<'this, TX> {

View File

@ -26,7 +26,7 @@ pub use state::{
};
use std::{
collections::{BTreeMap, HashSet},
ops::RangeBounds,
ops::{RangeBounds, RangeInclusive},
sync::Arc,
time::Instant,
};
@ -266,6 +266,10 @@ where
fn block_with_senders(&self, number: BlockNumber) -> RethResult<Option<BlockWithSenders>> {
self.database.provider()?.block_with_senders(number)
}
fn block_range(&self, range: RangeInclusive<BlockNumber>) -> RethResult<Vec<Block>> {
self.database.provider()?.block_range(range)
}
}
impl<DB, Tree> TransactionsProvider for BlockchainProvider<DB, Tree>

View File

@ -18,7 +18,7 @@ use reth_primitives::{
use reth_revm_primitives::primitives::{BlockEnv, CfgEnv};
use std::{
collections::{BTreeMap, HashMap},
ops::RangeBounds,
ops::{RangeBounds, RangeInclusive},
sync::Arc,
};
@ -440,6 +440,10 @@ impl BlockReader for MockEthProvider {
fn block_with_senders(&self, _number: BlockNumber) -> RethResult<Option<BlockWithSenders>> {
Ok(None)
}
fn block_range(&self, _range: RangeInclusive<BlockNumber>) -> RethResult<Vec<Block>> {
Ok(vec![])
}
}
impl BlockReaderIdExt for MockEthProvider {

View File

@ -16,7 +16,10 @@ use reth_primitives::{
TxNumber, H256, KECCAK_EMPTY, MAINNET, U256,
};
use reth_revm_primitives::primitives::{BlockEnv, CfgEnv};
use std::{ops::RangeBounds, sync::Arc};
use std::{
ops::{RangeBounds, RangeInclusive},
sync::Arc,
};
/// Supports various api interfaces for testing purposes.
#[derive(Debug, Clone, Default, Copy)]
@ -93,6 +96,10 @@ impl BlockReader for NoopProvider {
) -> RethResult<Option<reth_primitives::BlockWithSenders>> {
Ok(None)
}
fn block_range(&self, _range: RangeInclusive<BlockNumber>) -> RethResult<Vec<Block>> {
Ok(vec![])
}
}
impl BlockReaderIdExt for NoopProvider {

View File

@ -106,6 +106,11 @@ pub trait BlockReader:
///
/// Returns `None` if block is not found.
fn block_with_senders(&self, number: BlockNumber) -> RethResult<Option<BlockWithSenders>>;
/// Returns all blocks in the given inclusive range.
///
/// Note: returns only available blocks
fn block_range(&self, range: RangeInclusive<BlockNumber>) -> RethResult<Vec<Block>>;
}
/// Trait extension for `BlockReader`, for types that implement `BlockId` conversion.