Skip to content

Commit

Permalink
feat: map_chainspec for NodeConfig (paradigmxyz#12068)
Browse files Browse the repository at this point in the history
  • Loading branch information
greged93 authored Oct 26, 2024
1 parent e0ad598 commit cecdf61
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
22 changes: 3 additions & 19 deletions crates/exex/exex/src/dyn_context.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions crates/node/core/src/node_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down

0 comments on commit cecdf61

Please sign in to comment.