feat(storage): replace Tree generic with Arc<dyn TreeViewer> (#7810)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Kyrylo Riabov
2024-04-23 22:10:45 +03:00
committed by GitHub
parent a8a434d2c4
commit c659e28aa0
9 changed files with 102 additions and 113 deletions

View File

@ -174,7 +174,7 @@ impl Command {
EvmProcessorFactory::new(self.chain.clone(), evm_config), EvmProcessorFactory::new(self.chain.clone(), evm_config),
); );
let tree = BlockchainTree::new(tree_externals, BlockchainTreeConfig::default(), None)?; let tree = BlockchainTree::new(tree_externals, BlockchainTreeConfig::default(), None)?;
let blockchain_tree = ShareableBlockchainTree::new(tree); let blockchain_tree = Arc::new(ShareableBlockchainTree::new(tree));
// fetch the best block from the database // fetch the best block from the database
let best_block = let best_block =

View File

@ -136,11 +136,10 @@ impl Command {
EvmProcessorFactory::new(self.chain.clone(), evm_config), EvmProcessorFactory::new(self.chain.clone(), evm_config),
); );
let tree = BlockchainTree::new(tree_externals, BlockchainTreeConfig::default(), None)?; let tree = BlockchainTree::new(tree_externals, BlockchainTreeConfig::default(), None)?;
let blockchain_tree = ShareableBlockchainTree::new(tree); let blockchain_tree = Arc::new(ShareableBlockchainTree::new(tree));
// Set up the blockchain provider // Set up the blockchain provider
let blockchain_db = let blockchain_db = BlockchainProvider::new(provider_factory.clone(), blockchain_tree)?;
BlockchainProvider::new(provider_factory.clone(), blockchain_tree.clone())?;
// Set up network // Set up network
let network_secret_path = let network_secret_path =

View File

@ -43,13 +43,7 @@ type DatabaseEnv = TempDatabase<DE>;
type TestBeaconConsensusEngine<Client> = BeaconConsensusEngine< type TestBeaconConsensusEngine<Client> = BeaconConsensusEngine<
Arc<DatabaseEnv>, Arc<DatabaseEnv>,
BlockchainProvider< BlockchainProvider<Arc<DatabaseEnv>>,
Arc<DatabaseEnv>,
ShareableBlockchainTree<
Arc<DatabaseEnv>,
EitherExecutorFactory<TestExecutorFactory, EvmProcessorFactory<EthEvmConfig>>,
>,
>,
Arc<EitherDownloader<Client, NoopFullBlockClient>>, Arc<EitherDownloader<Client, NoopFullBlockClient>>,
EthEngineTypes, EthEngineTypes,
>; >;
@ -423,9 +417,9 @@ where
// Setup blockchain tree // Setup blockchain tree
let externals = TreeExternals::new(provider_factory.clone(), consensus, executor_factory); let externals = TreeExternals::new(provider_factory.clone(), consensus, executor_factory);
let config = BlockchainTreeConfig::new(1, 2, 3, 2); let config = BlockchainTreeConfig::new(1, 2, 3, 2);
let tree = ShareableBlockchainTree::new( let tree = Arc::new(ShareableBlockchainTree::new(
BlockchainTree::new(externals, config, None).expect("failed to create tree"), BlockchainTree::new(externals, config, None).expect("failed to create tree"),
); ));
let latest = self.base_config.chain_spec.genesis_header().seal_slow(); let latest = self.base_config.chain_spec.genesis_header().seal_slow();
let blockchain_provider = let blockchain_provider =
BlockchainProvider::with_latest(provider_factory.clone(), tree, latest); BlockchainProvider::with_latest(provider_factory.clone(), tree, latest);

View File

@ -1,15 +1,13 @@
use node::NodeHelper; use node::NodeHelper;
use reth::{ use reth::{
args::{DiscoveryArgs, NetworkArgs, RpcServerArgs}, args::{DiscoveryArgs, NetworkArgs, RpcServerArgs},
blockchain_tree::ShareableBlockchainTree,
builder::{NodeBuilder, NodeConfig, NodeHandle}, builder::{NodeBuilder, NodeConfig, NodeHandle},
revm::EvmProcessorFactory,
tasks::TaskManager, tasks::TaskManager,
}; };
use reth_db::{test_utils::TempDatabase, DatabaseEnv}; use reth_db::{test_utils::TempDatabase, DatabaseEnv};
use reth_node_builder::{ use reth_node_builder::{
components::{NetworkBuilder, PayloadServiceBuilder, PoolBuilder}, components::{NetworkBuilder, PayloadServiceBuilder, PoolBuilder},
FullNodeComponentsAdapter, FullNodeTypesAdapter, NodeTypes, FullNodeComponentsAdapter, FullNodeTypesAdapter,
}; };
use reth_primitives::ChainSpec; use reth_primitives::ChainSpec;
use reth_provider::providers::BlockchainProvider; use reth_provider::providers::BlockchainProvider;
@ -100,12 +98,10 @@ where
// Type aliases // Type aliases
type TmpDB = Arc<TempDatabase<DatabaseEnv>>; type TmpDB = Arc<TempDatabase<DatabaseEnv>>;
type EvmType<N> = EvmProcessorFactory<<N as NodeTypes>::Evm>;
type RethProvider<N> = BlockchainProvider<TmpDB, ShareableBlockchainTree<TmpDB, EvmType<N>>>;
type TmpPool<N> = <<N as reth_node_builder::Node<TmpNodeAdapter<N>>>::PoolBuilder as PoolBuilder< type TmpPool<N> = <<N as reth_node_builder::Node<TmpNodeAdapter<N>>>::PoolBuilder as PoolBuilder<
TmpNodeAdapter<N>, TmpNodeAdapter<N>,
>>::Pool; >>::Pool;
type TmpNodeAdapter<N> = FullNodeTypesAdapter<N, TmpDB, RethProvider<N>>; type TmpNodeAdapter<N> = FullNodeTypesAdapter<N, TmpDB, BlockchainProvider<TmpDB>>;
/// Type alias for a type of NodeHelper /// Type alias for a type of NodeHelper
pub type NodeHelperType<N> = NodeHelper<FullNodeComponentsAdapter<TmpNodeAdapter<N>, TmpPool<N>>>; pub type NodeHelperType<N> = NodeHelper<FullNodeComponentsAdapter<TmpNodeAdapter<N>, TmpPool<N>>>;

View File

@ -63,11 +63,9 @@ use tokio::sync::{mpsc::unbounded_channel, oneshot};
/// The builtin provider type of the reth node. /// The builtin provider type of the reth node.
// Note: we need to hardcode this because custom components might depend on it in associated types. // Note: we need to hardcode this because custom components might depend on it in associated types.
type RethFullProviderType<DB, Evm> = type RethFullProviderType<DB> = BlockchainProvider<DB>;
BlockchainProvider<DB, ShareableBlockchainTree<DB, EvmProcessorFactory<Evm>>>;
type RethFullAdapter<DB, N> = type RethFullAdapter<DB, N> = FullNodeTypesAdapter<N, DB, RethFullProviderType<DB>>;
FullNodeTypesAdapter<N, DB, RethFullProviderType<DB, <N as NodeTypes>::Evm>>;
#[cfg_attr(doc, aquamarine::aquamarine)] #[cfg_attr(doc, aquamarine::aquamarine)]
/// Declaratively construct a node. /// Declaratively construct a node.
@ -278,7 +276,7 @@ where
>, >,
> >
where where
N: Node<FullNodeTypesAdapter<N, DB, RethFullProviderType<DB, <N as NodeTypes>::Evm>>>, N: Node<FullNodeTypesAdapter<N, DB, RethFullProviderType<DB>>>,
N::PoolBuilder: PoolBuilder<RethFullAdapter<DB, N>>, N::PoolBuilder: PoolBuilder<RethFullAdapter<DB, N>>,
N::NetworkBuilder: crate::components::NetworkBuilder< N::NetworkBuilder: crate::components::NetworkBuilder<
RethFullAdapter<DB, N>, RethFullAdapter<DB, N>,
@ -308,15 +306,14 @@ where
Types, Types,
Components, Components,
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
>, >,
> >
where where
Components: NodeComponentsBuilder< Components:
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, NodeComponentsBuilder<FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>>,
>,
{ {
NodeBuilder { NodeBuilder {
config: self.config, config: self.config,
@ -339,7 +336,7 @@ impl<DB, Types, Components>
Types, Types,
Components, Components,
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
>, >,
@ -347,9 +344,7 @@ impl<DB, Types, Components>
where where
DB: Database + DatabaseMetrics + DatabaseMetadata + Clone + Unpin + 'static, DB: Database + DatabaseMetrics + DatabaseMetadata + Clone + Unpin + 'static,
Types: NodeTypes, Types: NodeTypes,
Components: NodeComponentsBuilder< Components: NodeComponentsBuilder<FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>>,
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>,
>,
{ {
/// Apply a function to the components builder. /// Apply a function to the components builder.
pub fn map_components(self, f: impl FnOnce(Components) -> Components) -> Self { pub fn map_components(self, f: impl FnOnce(Components) -> Components) -> Self {
@ -371,7 +366,7 @@ where
where where
F: Fn( F: Fn(
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
) -> eyre::Result<()> ) -> eyre::Result<()>
@ -388,7 +383,7 @@ where
F: Fn( F: Fn(
FullNode< FullNode<
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
>, >,
@ -407,7 +402,7 @@ where
RpcContext< RpcContext<
'_, '_,
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
>, >,
@ -427,7 +422,7 @@ where
RpcContext< RpcContext<
'_, '_,
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
>, >,
@ -449,7 +444,7 @@ where
F: Fn( F: Fn(
ExExContext< ExExContext<
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
>, >,
@ -476,7 +471,7 @@ where
) -> eyre::Result< ) -> eyre::Result<
NodeHandle< NodeHandle<
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
>, >,
@ -556,7 +551,7 @@ where
.with_sync_metrics_tx(sync_metrics_tx.clone()); .with_sync_metrics_tx(sync_metrics_tx.clone());
let canon_state_notification_sender = tree.canon_state_notification_sender(); let canon_state_notification_sender = tree.canon_state_notification_sender();
let blockchain_tree = ShareableBlockchainTree::new(tree); let blockchain_tree = Arc::new(ShareableBlockchainTree::new(tree));
debug!(target: "reth::cli", "configured blockchain tree"); debug!(target: "reth::cli", "configured blockchain tree");
// fetch the head block from the database // fetch the head block from the database
@ -995,7 +990,7 @@ where
>, >,
> >
where where
N: Node<FullNodeTypesAdapter<N, DB, RethFullProviderType<DB, <N as NodeTypes>::Evm>>>, N: Node<FullNodeTypesAdapter<N, DB, RethFullProviderType<DB>>>,
N::PoolBuilder: PoolBuilder<RethFullAdapter<DB, N>>, N::PoolBuilder: PoolBuilder<RethFullAdapter<DB, N>>,
N::NetworkBuilder: crate::components::NetworkBuilder< N::NetworkBuilder: crate::components::NetworkBuilder<
RethFullAdapter<DB, N>, RethFullAdapter<DB, N>,
@ -1032,7 +1027,7 @@ where
>, >,
> >
where where
N: Node<FullNodeTypesAdapter<N, DB, RethFullProviderType<DB, <N as NodeTypes>::Evm>>>, N: Node<FullNodeTypesAdapter<N, DB, RethFullProviderType<DB>>>,
N::PoolBuilder: PoolBuilder<RethFullAdapter<DB, N>>, N::PoolBuilder: PoolBuilder<RethFullAdapter<DB, N>>,
N::NetworkBuilder: crate::components::NetworkBuilder< N::NetworkBuilder: crate::components::NetworkBuilder<
RethFullAdapter<DB, N>, RethFullAdapter<DB, N>,
@ -1065,15 +1060,14 @@ where
Types, Types,
Components, Components,
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
>, >,
> >
where where
Components: NodeComponentsBuilder< Components:
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, NodeComponentsBuilder<FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>>,
>,
{ {
WithLaunchContext { WithLaunchContext {
builder: self.builder.with_components(components_builder), builder: self.builder.with_components(components_builder),
@ -1090,7 +1084,7 @@ impl<DB, Types, Components>
Types, Types,
Components, Components,
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
>, >,
@ -1098,9 +1092,7 @@ impl<DB, Types, Components>
where where
DB: Database + DatabaseMetrics + DatabaseMetadata + Clone + Unpin + 'static, DB: Database + DatabaseMetrics + DatabaseMetadata + Clone + Unpin + 'static,
Types: NodeTypes, Types: NodeTypes,
Components: NodeComponentsBuilder< Components: NodeComponentsBuilder<FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>>,
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>,
>,
{ {
/// Apply a function to the components builder. /// Apply a function to the components builder.
pub fn map_components(self, f: impl FnOnce(Components) -> Components) -> Self { pub fn map_components(self, f: impl FnOnce(Components) -> Components) -> Self {
@ -1116,7 +1108,7 @@ where
where where
F: Fn( F: Fn(
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
) -> eyre::Result<()> ) -> eyre::Result<()>
@ -1133,7 +1125,7 @@ where
F: Fn( F: Fn(
FullNode< FullNode<
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
>, >,
@ -1152,7 +1144,7 @@ where
RpcContext< RpcContext<
'_, '_,
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
>, >,
@ -1172,7 +1164,7 @@ where
RpcContext< RpcContext<
'_, '_,
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
>, >,
@ -1190,7 +1182,7 @@ where
F: Fn( F: Fn(
ExExContext< ExExContext<
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
>, >,
@ -1210,7 +1202,7 @@ where
) -> eyre::Result< ) -> eyre::Result<
NodeHandle< NodeHandle<
FullNodeComponentsAdapter< FullNodeComponentsAdapter<
FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
Components::Pool, Components::Pool,
>, >,
>, >,
@ -1391,13 +1383,12 @@ impl<Node: FullNodeTypes> std::fmt::Debug for BuilderContext<Node> {
pub struct InitState; pub struct InitState;
/// The state after all types of the node have been configured. /// The state after all types of the node have been configured.
#[derive(Debug)]
pub struct TypesState<Types, DB> pub struct TypesState<Types, DB>
where where
DB: Database + Clone + 'static, DB: Database + Clone + 'static,
Types: NodeTypes, Types: NodeTypes,
{ {
adapter: FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB, Types::Evm>>, adapter: FullNodeTypesAdapter<Types, DB, RethFullProviderType<DB>>,
} }
/// The state of the node builder process after the node's components have been configured. /// The state of the node builder process after the node's components have been configured.

View File

@ -4,7 +4,8 @@ use crate::{
CanonStateNotifications, CanonStateSubscriptions, ChainSpecProvider, ChangeSetReader, CanonStateNotifications, CanonStateSubscriptions, ChainSpecProvider, ChangeSetReader,
DatabaseProviderFactory, EvmEnvProvider, HeaderProvider, ProviderError, PruneCheckpointReader, DatabaseProviderFactory, EvmEnvProvider, HeaderProvider, ProviderError, PruneCheckpointReader,
ReceiptProvider, ReceiptProviderIdExt, StageCheckpointReader, StateProviderBox, ReceiptProvider, ReceiptProviderIdExt, StageCheckpointReader, StateProviderBox,
StateProviderFactory, TransactionVariant, TransactionsProvider, WithdrawalsProvider, StateProviderFactory, TransactionVariant, TransactionsProvider, TreeViewer,
WithdrawalsProvider,
}; };
use reth_db::{ use reth_db::{
database::Database, database::Database,
@ -67,31 +68,36 @@ use reth_rpc_types::engine::ForkchoiceState;
/// This type serves as the main entry point for interacting with the blockchain and provides data /// This type serves as the main entry point for interacting with the blockchain and provides data
/// from database storage and from the blockchain tree (pending state etc.) It is a simple wrapper /// from database storage and from the blockchain tree (pending state etc.) It is a simple wrapper
/// type that holds an instance of the database and the blockchain tree. /// type that holds an instance of the database and the blockchain tree.
#[derive(Clone, Debug)] #[derive(Clone)]
pub struct BlockchainProvider<DB, Tree> { #[allow(missing_debug_implementations)]
pub struct BlockchainProvider<DB> {
/// Provider type used to access the database. /// Provider type used to access the database.
database: ProviderFactory<DB>, database: ProviderFactory<DB>,
/// The blockchain tree instance. /// The blockchain tree instance.
tree: Tree, tree: Arc<dyn TreeViewer>,
/// Tracks the chain info wrt forkchoice updates /// Tracks the chain info wrt forkchoice updates
chain_info: ChainInfoTracker, chain_info: ChainInfoTracker,
} }
impl<DB, Tree> BlockchainProvider<DB, Tree> { impl<DB> BlockchainProvider<DB> {
/// Create new provider instance that wraps the database and the blockchain tree, using the /// Create new provider instance that wraps the database and the blockchain tree, using the
/// provided latest header to initialize the chain info tracker. /// provided latest header to initialize the chain info tracker.
pub fn with_latest(database: ProviderFactory<DB>, tree: Tree, latest: SealedHeader) -> Self { pub fn with_latest(
database: ProviderFactory<DB>,
tree: Arc<dyn TreeViewer>,
latest: SealedHeader,
) -> Self {
Self { database, tree, chain_info: ChainInfoTracker::new(latest) } Self { database, tree, chain_info: ChainInfoTracker::new(latest) }
} }
} }
impl<DB, Tree> BlockchainProvider<DB, Tree> impl<DB> BlockchainProvider<DB>
where where
DB: Database, DB: Database,
{ {
/// Create a new provider using only the database and the tree, fetching the latest header from /// Create a new provider using only the database and the tree, fetching the latest header from
/// the database to initialize the provider. /// the database to initialize the provider.
pub fn new(database: ProviderFactory<DB>, tree: Tree) -> ProviderResult<Self> { pub fn new(database: ProviderFactory<DB>, tree: Arc<dyn TreeViewer>) -> ProviderResult<Self> {
let provider = database.provider()?; let provider = database.provider()?;
let best: ChainInfo = provider.chain_info()?; let best: ChainInfo = provider.chain_info()?;
match provider.header_by_number(best.best_number)? { match provider.header_by_number(best.best_number)? {
@ -104,10 +110,9 @@ where
} }
} }
impl<DB, Tree> BlockchainProvider<DB, Tree> impl<DB> BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: BlockchainTreeViewer,
{ {
/// Ensures that the given block number is canonical (synced) /// Ensures that the given block number is canonical (synced)
/// ///
@ -128,7 +133,7 @@ where
} }
} }
impl<DB, Tree> DatabaseProviderFactory<DB> for BlockchainProvider<DB, Tree> impl<DB> DatabaseProviderFactory<DB> for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
{ {
@ -137,10 +142,9 @@ where
} }
} }
impl<DB, Tree> HeaderProvider for BlockchainProvider<DB, Tree> impl<DB> HeaderProvider for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: Send + Sync,
{ {
fn header(&self, block_hash: &BlockHash) -> ProviderResult<Option<Header>> { fn header(&self, block_hash: &BlockHash) -> ProviderResult<Option<Header>> {
self.database.header(block_hash) self.database.header(block_hash)
@ -182,10 +186,9 @@ where
} }
} }
impl<DB, Tree> BlockHashReader for BlockchainProvider<DB, Tree> impl<DB> BlockHashReader for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: Send + Sync,
{ {
fn block_hash(&self, number: u64) -> ProviderResult<Option<B256>> { fn block_hash(&self, number: u64) -> ProviderResult<Option<B256>> {
self.database.block_hash(number) self.database.block_hash(number)
@ -200,10 +203,9 @@ where
} }
} }
impl<DB, Tree> BlockNumReader for BlockchainProvider<DB, Tree> impl<DB> BlockNumReader for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: BlockchainTreeViewer + Send + Sync,
{ {
fn chain_info(&self) -> ProviderResult<ChainInfo> { fn chain_info(&self) -> ProviderResult<ChainInfo> {
Ok(self.chain_info.chain_info()) Ok(self.chain_info.chain_info())
@ -222,10 +224,9 @@ where
} }
} }
impl<DB, Tree> BlockIdReader for BlockchainProvider<DB, Tree> impl<DB> BlockIdReader for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: BlockchainTreeViewer + Send + Sync,
{ {
fn pending_block_num_hash(&self) -> ProviderResult<Option<BlockNumHash>> { fn pending_block_num_hash(&self) -> ProviderResult<Option<BlockNumHash>> {
Ok(self.tree.pending_block_num_hash()) Ok(self.tree.pending_block_num_hash())
@ -240,10 +241,9 @@ where
} }
} }
impl<DB, Tree> BlockReader for BlockchainProvider<DB, Tree> impl<DB> BlockReader for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: BlockchainTreeViewer + Send + Sync,
{ {
fn find_block_by_hash(&self, hash: B256, source: BlockSource) -> ProviderResult<Option<Block>> { fn find_block_by_hash(&self, hash: B256, source: BlockSource) -> ProviderResult<Option<Block>> {
let block = match source { let block = match source {
@ -320,10 +320,9 @@ where
} }
} }
impl<DB, Tree> TransactionsProvider for BlockchainProvider<DB, Tree> impl<DB> TransactionsProvider for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: BlockchainTreeViewer + Send + Sync,
{ {
fn transaction_id(&self, tx_hash: TxHash) -> ProviderResult<Option<TxNumber>> { fn transaction_id(&self, tx_hash: TxHash) -> ProviderResult<Option<TxNumber>> {
self.database.transaction_id(tx_hash) self.database.transaction_id(tx_hash)
@ -388,10 +387,9 @@ where
} }
} }
impl<DB, Tree> ReceiptProvider for BlockchainProvider<DB, Tree> impl<DB> ReceiptProvider for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: Send + Sync,
{ {
fn receipt(&self, id: TxNumber) -> ProviderResult<Option<Receipt>> { fn receipt(&self, id: TxNumber) -> ProviderResult<Option<Receipt>> {
self.database.receipt(id) self.database.receipt(id)
@ -412,10 +410,10 @@ where
self.database.receipts_by_tx_range(range) self.database.receipts_by_tx_range(range)
} }
} }
impl<DB, Tree> ReceiptProviderIdExt for BlockchainProvider<DB, Tree>
impl<DB> ReceiptProviderIdExt for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: BlockchainTreeViewer + Send + Sync,
{ {
fn receipts_by_block_id(&self, block: BlockId) -> ProviderResult<Option<Vec<Receipt>>> { fn receipts_by_block_id(&self, block: BlockId) -> ProviderResult<Option<Vec<Receipt>>> {
match block { match block {
@ -440,10 +438,9 @@ where
} }
} }
impl<DB, Tree> WithdrawalsProvider for BlockchainProvider<DB, Tree> impl<DB> WithdrawalsProvider for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: Send + Sync,
{ {
fn withdrawals_by_block( fn withdrawals_by_block(
&self, &self,
@ -458,10 +455,9 @@ where
} }
} }
impl<DB, Tree> StageCheckpointReader for BlockchainProvider<DB, Tree> impl<DB> StageCheckpointReader for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: Send + Sync,
{ {
fn get_stage_checkpoint(&self, id: StageId) -> ProviderResult<Option<StageCheckpoint>> { fn get_stage_checkpoint(&self, id: StageId) -> ProviderResult<Option<StageCheckpoint>> {
self.database.provider()?.get_stage_checkpoint(id) self.database.provider()?.get_stage_checkpoint(id)
@ -472,10 +468,9 @@ where
} }
} }
impl<DB, Tree> EvmEnvProvider for BlockchainProvider<DB, Tree> impl<DB> EvmEnvProvider for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: Send + Sync,
{ {
fn fill_env_at<EvmConfig>( fn fill_env_at<EvmConfig>(
&self, &self,
@ -544,10 +539,9 @@ where
} }
} }
impl<DB, Tree> PruneCheckpointReader for BlockchainProvider<DB, Tree> impl<DB> PruneCheckpointReader for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: Send + Sync,
{ {
fn get_prune_checkpoint( fn get_prune_checkpoint(
&self, &self,
@ -557,20 +551,18 @@ where
} }
} }
impl<DB, Tree> ChainSpecProvider for BlockchainProvider<DB, Tree> impl<DB> ChainSpecProvider for BlockchainProvider<DB>
where where
DB: Send + Sync, DB: Send + Sync,
Tree: Send + Sync,
{ {
fn chain_spec(&self) -> Arc<ChainSpec> { fn chain_spec(&self) -> Arc<ChainSpec> {
self.database.chain_spec() self.database.chain_spec()
} }
} }
impl<DB, Tree> StateProviderFactory for BlockchainProvider<DB, Tree> impl<DB> StateProviderFactory for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: BlockchainTreePendingStateProvider + BlockchainTreeViewer,
{ {
/// Storage provider for latest block /// Storage provider for latest block
fn latest(&self) -> ProviderResult<StateProviderBox> { fn latest(&self) -> ProviderResult<StateProviderBox> {
@ -644,10 +636,9 @@ where
} }
} }
impl<DB, Tree> BlockchainTreeEngine for BlockchainProvider<DB, Tree> impl<DB> BlockchainTreeEngine for BlockchainProvider<DB>
where where
DB: Send + Sync, DB: Send + Sync,
Tree: BlockchainTreeEngine,
{ {
fn buffer_block(&self, block: SealedBlockWithSenders) -> Result<(), InsertBlockError> { fn buffer_block(&self, block: SealedBlockWithSenders) -> Result<(), InsertBlockError> {
self.tree.buffer_block(block) self.tree.buffer_block(block)
@ -681,10 +672,9 @@ where
} }
} }
impl<DB, Tree> BlockchainTreeViewer for BlockchainProvider<DB, Tree> impl<DB> BlockchainTreeViewer for BlockchainProvider<DB>
where where
DB: Send + Sync, DB: Send + Sync,
Tree: BlockchainTreeViewer,
{ {
fn blocks(&self) -> BTreeMap<BlockNumber, HashSet<BlockHash>> { fn blocks(&self) -> BTreeMap<BlockNumber, HashSet<BlockHash>> {
self.tree.blocks() self.tree.blocks()
@ -743,10 +733,9 @@ where
} }
} }
impl<DB, Tree> CanonChainTracker for BlockchainProvider<DB, Tree> impl<DB> CanonChainTracker for BlockchainProvider<DB>
where where
DB: Send + Sync, DB: Send + Sync,
Tree: Send + Sync,
Self: BlockReader, Self: BlockReader,
{ {
fn on_forkchoice_update_received(&self, _update: &ForkchoiceState) { fn on_forkchoice_update_received(&self, _update: &ForkchoiceState) {
@ -779,10 +768,9 @@ where
} }
} }
impl<DB, Tree> BlockReaderIdExt for BlockchainProvider<DB, Tree> impl<DB> BlockReaderIdExt for BlockchainProvider<DB>
where where
Self: BlockReader + BlockIdReader + ReceiptProviderIdExt, Self: BlockReader + BlockIdReader + ReceiptProviderIdExt,
Tree: BlockchainTreeEngine,
{ {
fn block_by_id(&self, id: BlockId) -> ProviderResult<Option<Block>> { fn block_by_id(&self, id: BlockId) -> ProviderResult<Option<Block>> {
match id { match id {
@ -859,10 +847,9 @@ where
} }
} }
impl<DB, Tree> BlockchainTreePendingStateProvider for BlockchainProvider<DB, Tree> impl<DB> BlockchainTreePendingStateProvider for BlockchainProvider<DB>
where where
DB: Send + Sync, DB: Send + Sync,
Tree: BlockchainTreePendingStateProvider,
{ {
fn find_pending_state_provider( fn find_pending_state_provider(
&self, &self,
@ -872,20 +859,18 @@ where
} }
} }
impl<DB, Tree> CanonStateSubscriptions for BlockchainProvider<DB, Tree> impl<DB> CanonStateSubscriptions for BlockchainProvider<DB>
where where
DB: Send + Sync, DB: Send + Sync,
Tree: CanonStateSubscriptions,
{ {
fn subscribe_to_canonical_state(&self) -> CanonStateNotifications { fn subscribe_to_canonical_state(&self) -> CanonStateNotifications {
self.tree.subscribe_to_canonical_state() self.tree.subscribe_to_canonical_state()
} }
} }
impl<DB, Tree> ChangeSetReader for BlockchainProvider<DB, Tree> impl<DB> ChangeSetReader for BlockchainProvider<DB>
where where
DB: Database, DB: Database,
Tree: Sync + Send,
{ {
fn account_block_changeset( fn account_block_changeset(
&self, &self,
@ -895,10 +880,9 @@ where
} }
} }
impl<DB, Tree> AccountReader for BlockchainProvider<DB, Tree> impl<DB> AccountReader for BlockchainProvider<DB>
where where
DB: Database + Sync + Send, DB: Database + Sync + Send,
Tree: Sync + Send,
{ {
/// Get basic account information. /// Get basic account information.
fn basic_account(&self, address: Address) -> ProviderResult<Option<Account>> { fn basic_account(&self, address: Address) -> ProviderResult<Option<Account>> {

View File

@ -80,3 +80,6 @@ pub use stats::StatsReader;
mod full; mod full;
pub use full::FullProvider; pub use full::FullProvider;
mod tree_viewer;
pub use tree_viewer::TreeViewer;

View File

@ -0,0 +1,22 @@
use crate::{BlockchainTreePendingStateProvider, CanonStateSubscriptions};
use reth_interfaces::blockchain_tree::{BlockchainTreeEngine, BlockchainTreeViewer};
/// Helper trait to combine all the traits we need for the BlockchainProvider
///
/// This is a temporary solution
pub trait TreeViewer:
BlockchainTreeViewer
+ BlockchainTreePendingStateProvider
+ CanonStateSubscriptions
+ BlockchainTreeEngine
{
}
impl<T> TreeViewer for T where
T: BlockchainTreeViewer
+ BlockchainTreePendingStateProvider
+ CanonStateSubscriptions
+ BlockchainTreeEngine
{
}

View File

@ -49,7 +49,7 @@ async fn main() -> eyre::Result<()> {
// 2. Setup the blockchain provider using only the database provider and a noop for the tree to // 2. Setup the blockchain provider using only the database provider and a noop for the tree to
// satisfy trait bounds. Tree is not used in this example since we are only operating on the // satisfy trait bounds. Tree is not used in this example since we are only operating on the
// disk and don't handle new blocks/live sync etc, which is done by the blockchain tree. // disk and don't handle new blocks/live sync etc, which is done by the blockchain tree.
let provider = BlockchainProvider::new(factory, NoopBlockchainTree::default())?; let provider = BlockchainProvider::new(factory, Arc::new(NoopBlockchainTree::default()))?;
let rpc_builder = RpcModuleBuilder::default() let rpc_builder = RpcModuleBuilder::default()
.with_provider(provider.clone()) .with_provider(provider.clone())