feat: allow access to db via NodeBuilder (#17021)

This commit is contained in:
Matthias Seitz
2025-06-23 19:11:14 +02:00
committed by GitHub
parent dd5501336c
commit eefbc953a0
2 changed files with 78 additions and 13 deletions

View File

@@ -50,15 +50,20 @@ async fn test_eth_launcher() {
let _builder =
NodeBuilder::new(config)
.with_database(db)
.with_launch_context(tasks.executor())
.with_types_and_provider::<EthereumNode, BlockchainProvider<
NodeTypesWithDBAdapter<EthereumNode, Arc<TempDatabase<DatabaseEnv>>>,
>>()
.with_components(EthereumNode::components())
.with_add_ons(EthereumAddOns::default())
.apply(|builder| {
let _ = builder.db();
builder
})
.launch_with_fn(|builder| {
let launcher = EngineNodeLauncher::new(
tasks.executor(),
builder.config.datadir(),
builder.config().datadir(),
Default::default(),
);
builder.launch_with(launcher)

View File

@@ -160,6 +160,48 @@ impl<ChainSpec> NodeBuilder<(), ChainSpec> {
pub const fn new(config: NodeConfig<ChainSpec>) -> Self {
Self { config, database: () }
}
}
impl<DB, ChainSpec> NodeBuilder<DB, ChainSpec> {
/// Returns a reference to the node builder's config.
pub const fn config(&self) -> &NodeConfig<ChainSpec> {
&self.config
}
/// Returns a mutable reference to the node builder's config.
pub const fn config_mut(&mut self) -> &mut NodeConfig<ChainSpec> {
&mut self.config
}
/// Returns a reference to the node's database
pub const fn db(&self) -> &DB {
&self.database
}
/// Returns a mutable reference to the node's database
pub const fn db_mut(&mut self) -> &mut DB {
&mut self.database
}
/// Applies a fallible function to the builder.
pub fn try_apply<F, R>(self, f: F) -> Result<Self, R>
where
F: FnOnce(Self) -> Result<Self, R>,
{
f(self)
}
/// Applies a fallible function to the builder, if the condition is `true`.
pub fn try_apply_if<F, R>(self, cond: bool, f: F) -> Result<Self, R>
where
F: FnOnce(Self) -> Result<Self, R>,
{
if cond {
f(self)
} else {
Ok(self)
}
}
/// Apply a function to the builder
pub fn apply<F>(self, f: F) -> Self
@@ -182,18 +224,6 @@ impl<ChainSpec> NodeBuilder<(), ChainSpec> {
}
}
impl<DB, ChainSpec> NodeBuilder<DB, ChainSpec> {
/// Returns a reference to the node builder's config.
pub const fn config(&self) -> &NodeConfig<ChainSpec> {
&self.config
}
/// Returns a mutable reference to the node builder's config.
pub const fn config_mut(&mut self) -> &mut NodeConfig<ChainSpec> {
&mut self.config
}
}
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> {
@@ -413,6 +443,36 @@ where
&self.builder.config
}
/// Returns a reference to node's database.
pub const fn db(&self) -> &T::DB {
&self.builder.adapter.database
}
/// Returns a mutable reference to node's database.
pub const fn db_mut(&mut self) -> &mut T::DB {
&mut self.builder.adapter.database
}
/// Applies a fallible function to the builder.
pub fn try_apply<F, R>(self, f: F) -> Result<Self, R>
where
F: FnOnce(Self) -> Result<Self, R>,
{
f(self)
}
/// Applies a fallible function to the builder, if the condition is `true`.
pub fn try_apply_if<F, R>(self, cond: bool, f: F) -> Result<Self, R>
where
F: FnOnce(Self) -> Result<Self, R>,
{
if cond {
f(self)
} else {
Ok(self)
}
}
/// Apply a function to the builder
pub fn apply<F>(self, f: F) -> Self
where