Skip to content

Commit

Permalink
chore: add OmmersProvider trait (#13331)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo authored Dec 12, 2024
1 parent 328d493 commit 7764b9a
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 88 deletions.
15 changes: 9 additions & 6 deletions crates/storage/provider/src/providers/blockchain_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ use reth_primitives_traits::BlockBody as _;
use reth_prune_types::{PruneCheckpoint, PruneSegment};
use reth_stages_types::{StageCheckpoint, StageId};
use reth_storage_api::{
DBProvider, NodePrimitivesProvider, StateCommitmentProvider, StorageChangeSetReader,
DBProvider, NodePrimitivesProvider, OmmersProvider, StateCommitmentProvider,
StorageChangeSetReader,
};
use reth_storage_errors::provider::ProviderResult;
use reth_trie::HashedPostState;
Expand Down Expand Up @@ -314,10 +315,6 @@ impl<N: ProviderNodeTypes> BlockReader for BlockchainProvider2<N> {
Ok(self.canonical_in_memory_state.pending_block_and_receipts())
}

fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Self::Header>>> {
self.consistent_provider()?.ommers(id)
}

fn block_body_indices(
&self,
number: BlockNumber,
Expand Down Expand Up @@ -478,6 +475,12 @@ impl<N: ProviderNodeTypes> WithdrawalsProvider for BlockchainProvider2<N> {
}
}

impl<N: ProviderNodeTypes> OmmersProvider for BlockchainProvider2<N> {
fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Self::Header>>> {
self.consistent_provider()?.ommers(id)
}
}

impl<N: ProviderNodeTypes> StageCheckpointReader for BlockchainProvider2<N> {
fn get_stage_checkpoint(&self, id: StageId) -> ProviderResult<Option<StageCheckpoint>> {
self.consistent_provider()?.get_stage_checkpoint(id)
Expand Down Expand Up @@ -815,7 +818,7 @@ mod tests {
use reth_primitives_traits::{BlockBody as _, SignedTransaction};
use reth_storage_api::{
BlockHashReader, BlockIdReader, BlockNumReader, BlockReader, BlockReaderIdExt, BlockSource,
ChangeSetReader, DatabaseProviderFactory, HeaderProvider, ReceiptProvider,
ChangeSetReader, DatabaseProviderFactory, HeaderProvider, OmmersProvider, ReceiptProvider,
ReceiptProviderIdExt, StateProviderFactory, TransactionVariant, TransactionsProvider,
WithdrawalsProvider,
};
Expand Down
33 changes: 18 additions & 15 deletions crates/storage/provider/src/providers/consistent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use reth_primitives_traits::BlockBody;
use reth_prune_types::{PruneCheckpoint, PruneSegment};
use reth_stages_types::{StageCheckpoint, StageId};
use reth_storage_api::{
DatabaseProviderFactory, NodePrimitivesProvider, StateProvider, StorageChangeSetReader,
DatabaseProviderFactory, NodePrimitivesProvider, OmmersProvider, StateProvider,
StorageChangeSetReader,
};
use reth_storage_errors::provider::ProviderResult;
use revm::{
Expand Down Expand Up @@ -840,20 +841,6 @@ impl<N: ProviderNodeTypes> BlockReader for ConsistentProvider<N> {
Ok(self.canonical_in_memory_state.pending_block_and_receipts())
}

fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<HeaderTy<N>>>> {
self.get_in_memory_or_storage_by_block(
id,
|db_provider| db_provider.ommers(id),
|block_state| {
if self.chain_spec().final_paris_total_difficulty(block_state.number()).is_some() {
return Ok(Some(Vec::new()))
}

Ok(block_state.block_ref().block().body.ommers().map(|o| o.to_vec()))
},
)
}

fn block_body_indices(
&self,
number: BlockNumber,
Expand Down Expand Up @@ -1216,6 +1203,22 @@ impl<N: ProviderNodeTypes> WithdrawalsProvider for ConsistentProvider<N> {
}
}

impl<N: ProviderNodeTypes> OmmersProvider for ConsistentProvider<N> {
fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<HeaderTy<N>>>> {
self.get_in_memory_or_storage_by_block(
id,
|db_provider| db_provider.ommers(id),
|block_state| {
if self.chain_spec().final_paris_total_difficulty(block_state.number()).is_some() {
return Ok(Some(Vec::new()))
}

Ok(block_state.block_ref().block().body.ommers().map(|o| o.to_vec()))
},
)
}
}

impl<N: ProviderNodeTypes> StageCheckpointReader for ConsistentProvider<N> {
fn get_stage_checkpoint(&self, id: StageId) -> ProviderResult<Option<StageCheckpoint>> {
self.storage_provider.get_stage_checkpoint(id)
Expand Down
12 changes: 7 additions & 5 deletions crates/storage/provider/src/providers/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use reth_primitives::{
use reth_prune_types::{PruneCheckpoint, PruneModes, PruneSegment};
use reth_stages_types::{StageCheckpoint, StageId};
use reth_storage_api::{
NodePrimitivesProvider, StateCommitmentProvider, TryIntoHistoricalStateProvider,
NodePrimitivesProvider, OmmersProvider, StateCommitmentProvider, TryIntoHistoricalStateProvider,
};
use reth_storage_errors::provider::ProviderResult;
use reth_trie::HashedPostState;
Expand Down Expand Up @@ -404,10 +404,6 @@ impl<N: ProviderNodeTypes> BlockReader for ProviderFactory<N> {
self.provider()?.pending_block_and_receipts()
}

fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Self::Header>>> {
self.provider()?.ommers(id)
}

fn block_body_indices(
&self,
number: BlockNumber,
Expand Down Expand Up @@ -576,6 +572,12 @@ impl<N: ProviderNodeTypes> WithdrawalsProvider for ProviderFactory<N> {
}
}

impl<N: ProviderNodeTypes> OmmersProvider for ProviderFactory<N> {
fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Self::Header>>> {
self.provider()?.ommers(id)
}
}

impl<N: ProviderNodeTypes> StageCheckpointReader for ProviderFactory<N> {
fn get_stage_checkpoint(&self, id: StageId) -> ProviderResult<Option<StageCheckpoint>> {
self.provider()?.get_stage_checkpoint(id)
Expand Down
44 changes: 23 additions & 21 deletions crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use reth_primitives_traits::{Block as _, BlockBody as _, SignedTransaction};
use reth_prune_types::{PruneCheckpoint, PruneModes, PruneSegment};
use reth_stages_types::{StageCheckpoint, StageId};
use reth_storage_api::{
BlockBodyReader, NodePrimitivesProvider, StateProvider, StorageChangeSetReader,
BlockBodyReader, NodePrimitivesProvider, OmmersProvider, StateProvider, StorageChangeSetReader,
TryIntoHistoricalStateProvider,
};
use reth_storage_errors::provider::{ProviderResult, RootMismatch};
Expand Down Expand Up @@ -1222,26 +1222,6 @@ impl<TX: DbTx + 'static, N: NodeTypesForProvider> BlockReader for DatabaseProvid
Ok(None)
}

/// Returns the ommers for the block with matching id from the database.
///
/// If the block is not found, this returns `None`.
/// If the block exists, but doesn't contain ommers, this returns `None`.
fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Self::Header>>> {
if let Some(number) = self.convert_hash_or_number(id)? {
// If the Paris (Merge) hardfork block is known and block is after it, return empty
// ommers.
if self.chain_spec.final_paris_total_difficulty(number).is_some() {
return Ok(Some(Vec::new()))
}

let ommers =
self.tx.get::<tables::BlockOmmers<Self::Header>>(number)?.map(|o| o.ommers);
return Ok(ommers)
}

Ok(None)
}

fn block_body_indices(&self, num: u64) -> ProviderResult<Option<StoredBlockBodyIndices>> {
Ok(self.tx.get::<tables::BlockBodyIndices>(num)?)
}
Expand Down Expand Up @@ -1635,6 +1615,28 @@ impl<TX: DbTx + 'static, N: NodeTypes<ChainSpec: EthereumHardforks>> Withdrawals
}
}

impl<TX: DbTx + 'static, N: NodeTypesForProvider> OmmersProvider for DatabaseProvider<TX, N> {
/// Returns the ommers for the block with matching id from the database.
///
/// If the block is not found, this returns `None`.
/// If the block exists, but doesn't contain ommers, this returns `None`.
fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Self::Header>>> {
if let Some(number) = self.convert_hash_or_number(id)? {
// If the Paris (Merge) hardfork block is known and block is after it, return empty
// ommers.
if self.chain_spec.final_paris_total_difficulty(number).is_some() {
return Ok(Some(Vec::new()))
}

let ommers =
self.tx.get::<tables::BlockOmmers<Self::Header>>(number)?.map(|o| o.ommers);
return Ok(ommers)
}

Ok(None)
}
}

impl<TX: DbTx + 'static, N: NodeTypesForProvider> EvmEnvProvider<HeaderTy<N>>
for DatabaseProvider<TX, N>
{
Expand Down
12 changes: 7 additions & 5 deletions crates/storage/provider/src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use reth_primitives::{
};
use reth_prune_types::{PruneCheckpoint, PruneSegment};
use reth_stages_types::{StageCheckpoint, StageId};
use reth_storage_api::CanonChainTracker;
use reth_storage_api::{CanonChainTracker, OmmersProvider};
use reth_storage_errors::provider::ProviderResult;
use revm::primitives::{BlockEnv, CfgEnvWithHandlerCfg};
use std::{
Expand Down Expand Up @@ -402,10 +402,6 @@ impl<N: TreeNodeTypes> BlockReader for BlockchainProvider<N> {
Ok(self.tree.pending_block_and_receipts())
}

fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Header>>> {
self.database.ommers(id)
}

fn block_body_indices(
&self,
number: BlockNumber,
Expand Down Expand Up @@ -584,6 +580,12 @@ impl<N: ProviderNodeTypes> WithdrawalsProvider for BlockchainProvider<N> {
}
}

impl<N: TreeNodeTypes> OmmersProvider for BlockchainProvider<N> {
fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Header>>> {
self.database.ommers(id)
}
}

impl<N: ProviderNodeTypes> StageCheckpointReader for BlockchainProvider<N> {
fn get_stage_checkpoint(&self, id: StageId) -> ProviderResult<Option<StageCheckpoint>> {
self.database.provider()?.get_stage_checkpoint(id)
Expand Down
14 changes: 8 additions & 6 deletions crates/storage/provider/src/providers/static_file/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use reth_primitives::{
};
use reth_primitives_traits::SignedTransaction;
use reth_stages_types::{PipelineTarget, StageId};
use reth_storage_api::DBProvider;
use reth_storage_api::{DBProvider, OmmersProvider};
use reth_storage_errors::provider::{ProviderError, ProviderResult};
use std::{
collections::{hash_map::Entry, BTreeMap, HashMap},
Expand Down Expand Up @@ -1628,11 +1628,6 @@ impl<N: FullNodePrimitives<SignedTx: Value, Receipt: Value, BlockHeader: Value>>
Err(ProviderError::UnsupportedProvider)
}

fn ommers(&self, _id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Self::Header>>> {
// Required data not present in static_files
Err(ProviderError::UnsupportedProvider)
}

fn block_body_indices(&self, _num: u64) -> ProviderResult<Option<StoredBlockBodyIndices>> {
// Required data not present in static_files
Err(ProviderError::UnsupportedProvider)
Expand Down Expand Up @@ -1692,6 +1687,13 @@ impl<N: NodePrimitives> WithdrawalsProvider for StaticFileProvider<N> {
}
}

impl<N: FullNodePrimitives<BlockHeader: Value>> OmmersProvider for StaticFileProvider<N> {
fn ommers(&self, _id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Self::Header>>> {
// Required data not present in static_files
Err(ProviderError::UnsupportedProvider)
}
}

impl<N: NodePrimitives> StatsReader for StaticFileProvider<N> {
fn count_entries<T: Table>(&self) -> ProviderResult<usize> {
match T::NAME {
Expand Down
12 changes: 7 additions & 5 deletions crates/storage/provider/src/test_utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use reth_primitives::{
use reth_primitives_traits::SignedTransaction;
use reth_stages_types::{StageCheckpoint, StageId};
use reth_storage_api::{
DatabaseProviderFactory, HashedPostStateProvider, StageCheckpointReader,
DatabaseProviderFactory, HashedPostStateProvider, OmmersProvider, StageCheckpointReader,
StateCommitmentProvider, StateProofProvider, StorageRootProvider,
};
use reth_storage_errors::provider::{ConsistentViewError, ProviderError, ProviderResult};
Expand Down Expand Up @@ -510,10 +510,6 @@ impl BlockReader for MockEthProvider {
Ok(None)
}

fn ommers(&self, _id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Header>>> {
Ok(None)
}

fn block_body_indices(&self, _num: u64) -> ProviderResult<Option<StoredBlockBodyIndices>> {
Ok(None)
}
Expand Down Expand Up @@ -794,6 +790,12 @@ impl WithdrawalsProvider for MockEthProvider {
}
}

impl OmmersProvider for MockEthProvider {
fn ommers(&self, _id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Header>>> {
Ok(None)
}
}

impl ChangeSetReader for MockEthProvider {
fn account_block_changeset(
&self,
Expand Down
13 changes: 8 additions & 5 deletions crates/storage/provider/src/test_utils/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use reth_primitives::{
use reth_prune_types::{PruneCheckpoint, PruneSegment};
use reth_stages_types::{StageCheckpoint, StageId};
use reth_storage_api::{
HashedPostStateProvider, NodePrimitivesProvider, StateProofProvider, StorageRootProvider,
HashedPostStateProvider, NodePrimitivesProvider, OmmersProvider, StateProofProvider,
StorageRootProvider,
};
use reth_storage_errors::provider::ProviderResult;
use reth_trie::{
Expand Down Expand Up @@ -121,10 +122,6 @@ impl BlockReader for NoopProvider {
Ok(None)
}

fn ommers(&self, _id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Header>>> {
Ok(None)
}

fn block_body_indices(&self, _num: u64) -> ProviderResult<Option<StoredBlockBodyIndices>> {
Ok(None)
}
Expand Down Expand Up @@ -528,6 +525,12 @@ impl WithdrawalsProvider for NoopProvider {
}
}

impl OmmersProvider for NoopProvider {
fn ommers(&self, _id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Header>>> {
Ok(None)
}
}

impl PruneCheckpointReader for NoopProvider {
fn get_prune_checkpoint(
&self,
Expand Down
16 changes: 3 additions & 13 deletions crates/storage/storage-api/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
BlockNumReader, HeaderProvider, ReceiptProvider, ReceiptProviderIdExt, TransactionVariant,
TransactionsProvider, WithdrawalsProvider,
BlockNumReader, HeaderProvider, OmmersProvider, ReceiptProvider, ReceiptProviderIdExt,
TransactionVariant, TransactionsProvider, WithdrawalsProvider,
};
use alloy_eips::{BlockHashOrNumber, BlockId, BlockNumberOrTag};
use alloy_primitives::{BlockNumber, B256};
Expand Down Expand Up @@ -53,6 +53,7 @@ pub trait BlockReader:
+ TransactionsProvider
+ ReceiptProvider
+ WithdrawalsProvider
+ OmmersProvider
+ Send
+ Sync
{
Expand Down Expand Up @@ -98,11 +99,6 @@ pub trait BlockReader:
&self,
) -> ProviderResult<Option<(SealedBlockFor<Self::Block>, Vec<Self::Receipt>)>>;

/// Returns the ommers/uncle headers of the given block from the database.
///
/// Returns `None` if block is not found.
fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Self::Header>>>;

/// Returns the block with matching hash from the database.
///
/// Returns `None` if block is not found.
Expand Down Expand Up @@ -190,9 +186,6 @@ impl<T: BlockReader> BlockReader for std::sync::Arc<T> {
) -> ProviderResult<Option<(SealedBlockFor<Self::Block>, Vec<Self::Receipt>)>> {
T::pending_block_and_receipts(self)
}
fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Self::Header>>> {
T::ommers(self, id)
}
fn block_by_hash(&self, hash: B256) -> ProviderResult<Option<Self::Block>> {
T::block_by_hash(self, hash)
}
Expand Down Expand Up @@ -259,9 +252,6 @@ impl<T: BlockReader> BlockReader for &T {
) -> ProviderResult<Option<(SealedBlockFor<Self::Block>, Vec<Self::Receipt>)>> {
T::pending_block_and_receipts(self)
}
fn ommers(&self, id: BlockHashOrNumber) -> ProviderResult<Option<Vec<Self::Header>>> {
T::ommers(self, id)
}
fn block_by_hash(&self, hash: B256) -> ProviderResult<Option<Self::Block>> {
T::block_by_hash(self, hash)
}
Expand Down
3 changes: 3 additions & 0 deletions crates/storage/storage-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pub use chain_info::*;
mod withdrawals;
pub use withdrawals::*;

mod ommers;
pub use ommers::*;

mod database_provider;
pub use database_provider::*;

Expand Down
Loading

0 comments on commit 7764b9a

Please sign in to comment.