feat(node-builder): add with_rocksdb_provider to NodeBuilder (#22970)

Co-authored-by: Arsenii Kulikov <klkvrr@gmail.com>
This commit is contained in:
Dan Cline
2026-03-12 12:42:59 -04:00
committed by GitHub
parent 9b53c4fa39
commit b37b881074
4 changed files with 45 additions and 15 deletions

View File

@@ -32,7 +32,7 @@ use reth_node_core::{
primitives::Head,
};
use reth_provider::{
providers::{BlockchainProvider, NodeTypesForProvider},
providers::{BlockchainProvider, NodeTypesForProvider, RocksDBProvider},
ChainSpecProvider, FullProvider,
};
use reth_tasks::TaskExecutor;
@@ -154,12 +154,14 @@ pub struct NodeBuilder<DB, ChainSpec> {
config: NodeConfig<ChainSpec>,
/// The configured database for the node.
database: DB,
/// An optional [`RocksDBProvider`] to use instead of creating one during launch.
rocksdb_provider: Option<RocksDBProvider>,
}
impl<ChainSpec> NodeBuilder<(), ChainSpec> {
/// Create a new [`NodeBuilder`].
pub const fn new(config: NodeConfig<ChainSpec>) -> Self {
Self { config, database: () }
Self { config, database: (), rocksdb_provider: None }
}
}
@@ -228,7 +230,13 @@ impl<DB, ChainSpec> NodeBuilder<DB, ChainSpec> {
impl<DB, ChainSpec: EthChainSpec> NodeBuilder<DB, ChainSpec> {
/// Configures the underlying database that the node will use.
pub fn with_database<D>(self, database: D) -> NodeBuilder<D, ChainSpec> {
NodeBuilder { config: self.config, database }
NodeBuilder { config: self.config, database, rocksdb_provider: self.rocksdb_provider }
}
/// Sets the [`RocksDBProvider`] to use instead of creating one during launch.
pub fn with_rocksdb_provider(mut self, rocksdb_provider: RocksDBProvider) -> Self {
self.rocksdb_provider = Some(rocksdb_provider);
self
}
/// Preconfigure the builder with the context to launch the node.
@@ -297,7 +305,7 @@ where
T: NodeTypesForProvider<ChainSpec = ChainSpec>,
P: FullProvider<NodeTypesWithDBAdapter<T, DB>>,
{
NodeBuilderWithTypes::new(self.config, self.database)
NodeBuilderWithTypes::new(self.config, self.database, self.rocksdb_provider)
}
/// Preconfigures the node with a specific node implementation.
@@ -347,6 +355,12 @@ where
DB: Database + DatabaseMetrics + Clone + Unpin + 'static,
ChainSpec: EthChainSpec + EthereumHardforks,
{
/// Sets the [`RocksDBProvider`] to use instead of creating one during launch.
pub fn with_rocksdb_provider(mut self, rocksdb_provider: RocksDBProvider) -> Self {
self.builder.rocksdb_provider = Some(rocksdb_provider);
self
}
/// Configures the types of the node.
pub fn with_types<T>(self) -> WithLaunchContext<NodeBuilderWithTypes<RethFullAdapter<DB, T>>>
where

View File

@@ -16,6 +16,7 @@ use crate::{
use reth_exex::ExExContext;
use reth_node_api::{FullNodeComponents, FullNodeTypes, NodeAddOns, NodeTypes};
use reth_node_core::node_config::NodeConfig;
use reth_provider::providers::RocksDBProvider;
use reth_tasks::TaskExecutor;
use std::{fmt, fmt::Debug, future::Future};
@@ -25,6 +26,8 @@ pub struct NodeBuilderWithTypes<T: FullNodeTypes> {
config: NodeConfig<<T::Types as NodeTypes>::ChainSpec>,
/// The configured database for the node.
adapter: NodeTypesAdapter<T>,
/// An optional [`RocksDBProvider`] to use instead of creating one during launch.
rocksdb_provider: Option<RocksDBProvider>,
}
impl<T: FullNodeTypes> NodeBuilderWithTypes<T> {
@@ -32,8 +35,9 @@ impl<T: FullNodeTypes> NodeBuilderWithTypes<T> {
pub const fn new(
config: NodeConfig<<T::Types as NodeTypes>::ChainSpec>,
database: T::DB,
rocksdb_provider: Option<RocksDBProvider>,
) -> Self {
Self { config, adapter: NodeTypesAdapter::new(database) }
Self { config, adapter: NodeTypesAdapter::new(database), rocksdb_provider }
}
/// Advances the state of the node builder to the next state where all components are configured
@@ -41,11 +45,12 @@ impl<T: FullNodeTypes> NodeBuilderWithTypes<T> {
where
CB: NodeComponentsBuilder<T>,
{
let Self { config, adapter } = self;
let Self { config, adapter, rocksdb_provider } = self;
NodeBuilderWithComponents {
config,
adapter,
rocksdb_provider,
components_builder,
add_ons: AddOns { hooks: NodeHooks::default(), exexs: Vec::new(), add_ons: () },
}
@@ -150,6 +155,8 @@ pub struct NodeBuilderWithComponents<
pub config: NodeConfig<<T::Types as NodeTypes>::ChainSpec>,
/// Adapter for the underlying node types and database
pub adapter: NodeTypesAdapter<T>,
/// An optional [`RocksDBProvider`] to use instead of creating one during launch.
pub rocksdb_provider: Option<RocksDBProvider>,
/// container for type specific components
pub components_builder: CB,
/// Additional node extensions.
@@ -167,11 +174,12 @@ where
where
AO: NodeAddOns<NodeAdapter<T, CB::Components>>,
{
let Self { config, adapter, components_builder, .. } = self;
let Self { config, adapter, rocksdb_provider, components_builder, .. } = self;
NodeBuilderWithComponents {
config,
adapter,
rocksdb_provider,
components_builder,
add_ons: AddOns { hooks: NodeHooks::default(), exexs: Vec::new(), add_ons },
}

View File

@@ -472,6 +472,7 @@ where
pub async fn create_provider_factory<N, Evm>(
&self,
changeset_cache: ChangesetCache,
rocksdb_provider: Option<RocksDBProvider>,
) -> eyre::Result<ProviderFactory<N>>
where
N: ProviderNodeTypes<DB = DB, ChainSpec = ChainSpec>,
@@ -489,12 +490,16 @@ where
.with_genesis_block_number(self.chain_spec().genesis().number.unwrap_or_default())
.build()?;
// Initialize RocksDB provider with metrics, statistics, and default tables
let rocksdb_provider = RocksDBProvider::builder(self.data_dir().rocksdb())
.with_default_tables()
.with_metrics()
.with_statistics()
.build()?;
// Use the provided RocksDB provider or create a new one
let rocksdb_provider = if let Some(provider) = rocksdb_provider {
provider
} else {
RocksDBProvider::builder(self.data_dir().rocksdb())
.with_default_tables()
.with_metrics()
.with_statistics()
.build()?
};
let factory = ProviderFactory::new(
self.right().clone(),
@@ -573,12 +578,14 @@ where
pub async fn with_provider_factory<N, Evm>(
self,
changeset_cache: ChangesetCache,
rocksdb_provider: Option<RocksDBProvider>,
) -> eyre::Result<LaunchContextWith<Attached<WithConfigs<ChainSpec>, ProviderFactory<N>>>>
where
N: ProviderNodeTypes<DB = DB, ChainSpec = ChainSpec>,
Evm: ConfigureEvm<Primitives = N::Primitives> + 'static,
{
let factory = self.create_provider_factory::<N, Evm>(changeset_cache).await?;
let factory =
self.create_provider_factory::<N, Evm>(changeset_cache, rocksdb_provider).await?;
let ctx = LaunchContextWith {
inner: self.inner,
attachment: self.attachment.map_right(|_| factory),

View File

@@ -81,6 +81,7 @@ impl EngineNodeLauncher {
let Self { ctx, engine_tree_config } = self;
let NodeBuilderWithComponents {
adapter: NodeTypesAdapter { database },
rocksdb_provider,
components_builder,
add_ons: AddOns { hooks, exexs: installed_exex, add_ons },
config,
@@ -102,7 +103,7 @@ impl EngineNodeLauncher {
// ensure certain settings take effect
.with_adjusted_configs()
// Create the provider factory with changeset cache
.with_provider_factory::<_, <CB::Components as NodeComponents<T>>::Evm>(changeset_cache.clone()).await?
.with_provider_factory::<_, <CB::Components as NodeComponents<T>>::Evm>(changeset_cache.clone(), rocksdb_provider).await?
.inspect(|_| {
info!(target: "reth::cli", "Database opened");
})