feat: map_chainspec for NodeConfig (#12068)

This commit is contained in:
greged93
2024-10-26 08:11:27 +02:00
committed by GitHub
parent e0ad59834d
commit cecdf611e9
2 changed files with 26 additions and 19 deletions

View File

@@ -1,7 +1,7 @@
//! Mirrored version of [`ExExContext`](`crate::ExExContext`)
//! without generic abstraction over [Node](`reth_node_api::FullNodeComponents`)
use std::{fmt::Debug, sync::Arc};
use std::fmt::Debug;
use reth_chainspec::{EthChainSpec, Head};
use reth_node_api::FullNodeComponents;
@@ -55,24 +55,8 @@ where
Node::Executor: Debug,
{
fn from(ctx: ExExContext<Node>) -> Self {
// convert `NodeConfig` with generic over chainspec into `NodeConfig<Box<dyn EthChainSpec>`
let chain: Arc<Box<dyn EthChainSpec + 'static>> =
Arc::new(Box::new(ctx.config.chain) as Box<dyn EthChainSpec>);
let config = NodeConfig {
chain,
datadir: ctx.config.datadir,
config: ctx.config.config,
metrics: ctx.config.metrics,
instance: ctx.config.instance,
network: ctx.config.network,
rpc: ctx.config.rpc,
txpool: ctx.config.txpool,
builder: ctx.config.builder,
debug: ctx.config.debug,
db: ctx.config.db,
dev: ctx.config.dev,
pruning: ctx.config.pruning,
};
let config =
ctx.config.map_chainspec(|chainspec| Box::new(chainspec) as Box<dyn EthChainSpec>);
let notifications = Box::new(ctx.notifications) as Box<dyn ExExNotificationsStream>;
Self {

View File

@@ -422,6 +422,29 @@ impl<ChainSpec> NodeConfig<ChainSpec> {
Err(e) => Err(eyre!("Failed to load configuration: {e}")),
}
}
/// Modifies the [`ChainSpec`] generic of the config using the provided closure.
pub fn map_chainspec<F, C>(self, f: F) -> NodeConfig<C>
where
F: FnOnce(Arc<ChainSpec>) -> C,
{
let chain = Arc::new(f(self.chain));
NodeConfig {
chain,
datadir: self.datadir,
config: self.config,
metrics: self.metrics,
instance: self.instance,
network: self.network,
rpc: self.rpc,
txpool: self.txpool,
builder: self.builder,
debug: self.debug,
db: self.db,
dev: self.dev,
pruning: self.pruning,
}
}
}
impl Default for NodeConfig<ChainSpec> {