mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: integrate BlockchainProvider2 in EthNodeLauncher (#9754)
This commit is contained in:
@ -11,8 +11,7 @@ use rayon::ThreadPoolBuilder;
|
||||
use reth_auto_seal_consensus::MiningMode;
|
||||
use reth_beacon_consensus::EthBeaconConsensus;
|
||||
use reth_blockchain_tree::{
|
||||
noop::NoopBlockchainTree, BlockchainTree, BlockchainTreeConfig, ShareableBlockchainTree,
|
||||
TreeExternals,
|
||||
BlockchainTree, BlockchainTreeConfig, ShareableBlockchainTree, TreeExternals,
|
||||
};
|
||||
use reth_chainspec::{Chain, ChainSpec};
|
||||
use reth_config::{config::EtlConfig, PruneConfig};
|
||||
@ -29,8 +28,9 @@ use reth_node_core::{
|
||||
};
|
||||
use reth_primitives::{BlockNumber, Head, B256};
|
||||
use reth_provider::{
|
||||
providers::{BlockchainProvider, StaticFileProvider},
|
||||
CanonStateNotificationSender, ProviderFactory, StaticFileProviderFactory,
|
||||
providers::{BlockchainProvider, BlockchainProvider2, StaticFileProvider},
|
||||
CanonStateNotificationSender, FullProvider, ProviderFactory, StaticFileProviderFactory,
|
||||
TreeViewer,
|
||||
};
|
||||
use reth_prune::{PruneModes, PrunerBuilder};
|
||||
use reth_rpc_builder::config::RethRpcServerConfig;
|
||||
@ -45,6 +45,27 @@ use tokio::sync::{
|
||||
oneshot, watch,
|
||||
};
|
||||
|
||||
/// Allows to set a tree viewer for a configured blockchain provider.
|
||||
// TODO: remove this helper trait once the engine revamp is done, the new
|
||||
// blockchain provider won't require a TreeViewer.
|
||||
// https://github.com/paradigmxyz/reth/issues/8742
|
||||
pub trait WithTree {
|
||||
/// Setter for tree viewer.
|
||||
fn set_tree(self, tree: Arc<dyn TreeViewer>) -> Self;
|
||||
}
|
||||
|
||||
impl<DB: Database> WithTree for BlockchainProvider<DB> {
|
||||
fn set_tree(self, tree: Arc<dyn TreeViewer>) -> Self {
|
||||
self.with_tree(tree)
|
||||
}
|
||||
}
|
||||
|
||||
impl<DB: Database> WithTree for BlockchainProvider2<DB> {
|
||||
fn set_tree(self, _tree: Arc<dyn TreeViewer>) -> Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Reusable setup for launching a node.
|
||||
///
|
||||
/// This provides commonly used boilerplate for launching a node.
|
||||
@ -523,24 +544,18 @@ where
|
||||
}
|
||||
|
||||
/// Creates a `BlockchainProvider` and attaches it to the launch context.
|
||||
pub fn with_blockchain_db<T>(
|
||||
pub fn with_blockchain_db<T, F>(
|
||||
self,
|
||||
create_blockchain_provider: F,
|
||||
tree_config: BlockchainTreeConfig,
|
||||
canon_state_notification_sender: CanonStateNotificationSender,
|
||||
) -> eyre::Result<LaunchContextWith<Attached<WithConfigs, WithMeteredProviders<DB, T>>>>
|
||||
where
|
||||
T: FullNodeTypes<Provider = BlockchainProvider<<T as FullNodeTypes>::DB>>,
|
||||
T: FullNodeTypes,
|
||||
T::Provider: FullProvider<DB>,
|
||||
F: FnOnce(ProviderFactory<DB>) -> eyre::Result<T::Provider>,
|
||||
{
|
||||
let tree_config = BlockchainTreeConfig::default();
|
||||
|
||||
// NOTE: This is a temporary workaround to provide the canon state notification sender to the components builder because there's a cyclic dependency between the blockchain provider and the tree component. This will be removed once the Blockchain provider no longer depends on an instance of the tree: <https://github.com/paradigmxyz/reth/issues/7154>
|
||||
let (canon_state_notification_sender, _receiver) =
|
||||
tokio::sync::broadcast::channel(tree_config.max_reorg_depth() as usize * 2);
|
||||
|
||||
let blockchain_db = BlockchainProvider::new(
|
||||
self.provider_factory().clone(),
|
||||
Arc::new(NoopBlockchainTree::with_canon_state_notifications(
|
||||
canon_state_notification_sender.clone(),
|
||||
)),
|
||||
)?;
|
||||
let blockchain_db = create_blockchain_provider(self.provider_factory().clone())?;
|
||||
|
||||
let metered_providers = WithMeteredProviders {
|
||||
db_provider_container: WithMeteredProvider {
|
||||
@ -566,7 +581,8 @@ where
|
||||
impl<DB, T> LaunchContextWith<Attached<WithConfigs, WithMeteredProviders<DB, T>>>
|
||||
where
|
||||
DB: Database + DatabaseMetrics + Send + Sync + Clone + 'static,
|
||||
T: FullNodeTypes<Provider = BlockchainProvider<DB>>,
|
||||
T: FullNodeTypes,
|
||||
T::Provider: FullProvider<DB> + WithTree,
|
||||
{
|
||||
/// Returns access to the underlying database.
|
||||
pub fn database(&self) -> &DB {
|
||||
@ -592,8 +608,8 @@ where
|
||||
self.right().db_provider_container.metrics_sender.clone()
|
||||
}
|
||||
|
||||
/// Returns a reference to the `BlockchainProvider`.
|
||||
pub const fn blockchain_db(&self) -> &BlockchainProvider<DB> {
|
||||
/// Returns a reference to the blockchain provider.
|
||||
pub const fn blockchain_db(&self) -> &T::Provider {
|
||||
&self.right().blockchain_db
|
||||
}
|
||||
|
||||
@ -648,7 +664,7 @@ where
|
||||
let blockchain_tree = Arc::new(ShareableBlockchainTree::new(tree));
|
||||
|
||||
// Replace the tree component with the actual tree
|
||||
let blockchain_db = self.blockchain_db().clone().with_tree(blockchain_tree);
|
||||
let blockchain_db = self.blockchain_db().clone().set_tree(blockchain_tree);
|
||||
|
||||
debug!(target: "reth::cli", "configured blockchain tree");
|
||||
|
||||
@ -685,7 +701,8 @@ where
|
||||
impl<DB, T, CB> LaunchContextWith<Attached<WithConfigs, WithComponents<DB, T, CB>>>
|
||||
where
|
||||
DB: Database + DatabaseMetrics + Send + Sync + Clone + 'static,
|
||||
T: FullNodeTypes<Provider = BlockchainProvider<DB>>,
|
||||
T: FullNodeTypes,
|
||||
T::Provider: FullProvider<DB> + WithTree,
|
||||
CB: NodeComponentsBuilder<T>,
|
||||
{
|
||||
/// Returns the configured `ProviderFactory`.
|
||||
@ -722,8 +739,8 @@ where
|
||||
&self.right().node_adapter
|
||||
}
|
||||
|
||||
/// Returns a reference to the `BlockchainProvider`.
|
||||
pub const fn blockchain_db(&self) -> &BlockchainProvider<DB> {
|
||||
/// Returns a reference to the blockchain provider.
|
||||
pub const fn blockchain_db(&self) -> &T::Provider {
|
||||
&self.right().blockchain_db
|
||||
}
|
||||
|
||||
@ -819,9 +836,14 @@ pub struct WithMeteredProvider<DB> {
|
||||
/// Helper container to bundle the [`ProviderFactory`], [`BlockchainProvider`]
|
||||
/// and a metrics sender.
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct WithMeteredProviders<DB, T> {
|
||||
pub struct WithMeteredProviders<DB, T>
|
||||
where
|
||||
DB: Database,
|
||||
T: FullNodeTypes,
|
||||
T::Provider: FullProvider<DB>,
|
||||
{
|
||||
db_provider_container: WithMeteredProvider<DB>,
|
||||
blockchain_db: BlockchainProvider<DB>,
|
||||
blockchain_db: T::Provider,
|
||||
canon_state_notification_sender: CanonStateNotificationSender,
|
||||
tree_config: BlockchainTreeConfig,
|
||||
// this field is used to store a reference to the FullNodeTypes so that we
|
||||
@ -833,12 +855,14 @@ pub struct WithMeteredProviders<DB, T> {
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct WithComponents<DB, T, CB>
|
||||
where
|
||||
T: FullNodeTypes<Provider = BlockchainProvider<DB>>,
|
||||
DB: Database,
|
||||
T: FullNodeTypes,
|
||||
T::Provider: FullProvider<DB>,
|
||||
CB: NodeComponentsBuilder<T>,
|
||||
{
|
||||
db_provider_container: WithMeteredProvider<DB>,
|
||||
tree_config: BlockchainTreeConfig,
|
||||
blockchain_db: BlockchainProvider<DB>,
|
||||
blockchain_db: T::Provider,
|
||||
node_adapter: NodeAdapter<T, CB::Components>,
|
||||
head: Head,
|
||||
consensus: Arc<dyn Consensus>,
|
||||
|
||||
@ -13,6 +13,7 @@ use reth_beacon_consensus::{
|
||||
hooks::{EngineHooks, PruneHook, StaticFileHook},
|
||||
BeaconConsensusEngine,
|
||||
};
|
||||
use reth_blockchain_tree::{noop::NoopBlockchainTree, BlockchainTreeConfig};
|
||||
use reth_consensus_debug_client::{DebugConsensusClient, EtherscanBlockProvider, RpcBlockProvider};
|
||||
use reth_engine_util::EngineMessageStreamExt;
|
||||
use reth_exex::ExExManagerHandle;
|
||||
@ -119,6 +120,19 @@ where
|
||||
} = target;
|
||||
let NodeHooks { on_component_initialized, on_node_started, .. } = hooks;
|
||||
|
||||
// TODO: remove tree and move tree_config and canon_state_notification_sender
|
||||
// initialization to with_blockchain_db once the engine revamp is done
|
||||
// https://github.com/paradigmxyz/reth/issues/8742
|
||||
let tree_config = BlockchainTreeConfig::default();
|
||||
|
||||
// NOTE: This is a temporary workaround to provide the canon state notification sender to the components builder because there's a cyclic dependency between the blockchain provider and the tree component. This will be removed once the Blockchain provider no longer depends on an instance of the tree: <https://github.com/paradigmxyz/reth/issues/7154>
|
||||
let (canon_state_notification_sender, _receiver) =
|
||||
tokio::sync::broadcast::channel(tree_config.max_reorg_depth() as usize * 2);
|
||||
|
||||
let tree = Arc::new(NoopBlockchainTree::with_canon_state_notifications(
|
||||
canon_state_notification_sender.clone(),
|
||||
));
|
||||
|
||||
// setup the launch context
|
||||
let ctx = ctx
|
||||
.with_configured_globals()
|
||||
@ -146,7 +160,9 @@ where
|
||||
.with_metrics_task()
|
||||
// passing FullNodeTypes as type parameter here so that we can build
|
||||
// later the components.
|
||||
.with_blockchain_db::<T>()?
|
||||
.with_blockchain_db::<T, _>(move |provider_factory| {
|
||||
Ok(BlockchainProvider::new(provider_factory, tree)?)
|
||||
}, tree_config, canon_state_notification_sender)?
|
||||
.with_components(components_builder, on_component_initialized).await?;
|
||||
|
||||
// spawn exexs
|
||||
|
||||
Reference in New Issue
Block a user