Skip to content

Commit

Permalink
feat: add block_range provider function (paradigmxyz#4819)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Sep 27, 2023
1 parent b994d15 commit 66fc196
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
9 changes: 8 additions & 1 deletion crates/storage/provider/src/providers/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> {
Expand Down
11 changes: 11 additions & 0 deletions crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
6 changes: 5 additions & 1 deletion crates/storage/provider/src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub use state::{
};
use std::{
collections::{BTreeMap, HashSet},
ops::RangeBounds,
ops::{RangeBounds, RangeInclusive},
sync::Arc,
time::Instant,
};
Expand Down Expand Up @@ -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>
Expand Down
6 changes: 5 additions & 1 deletion crates/storage/provider/src/test_utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion crates/storage/provider/src/test_utils/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions crates/storage/provider/src/traits/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 66fc196

Please sign in to comment.