feat: make NodeConfig generic over ChainSpec (#11039)

This commit is contained in:
Arsenii Kulikov
2024-09-19 19:05:09 +03:00
committed by GitHub
parent 65fb29c773
commit c3d090adf4
16 changed files with 186 additions and 106 deletions

View File

@ -3,7 +3,7 @@
use crate::args::error::ReceiptsLogError;
use alloy_primitives::{Address, BlockNumber};
use clap::Args;
use reth_chainspec::ChainSpec;
use reth_chainspec::EthChainSpec;
use reth_config::config::PruneConfig;
use reth_prune_types::{PruneMode, PruneModes, ReceiptsLogPruneConfig, MINIMUM_PRUNING_DISTANCE};
use std::collections::BTreeMap;
@ -92,7 +92,7 @@ pub struct PruningArgs {
impl PruningArgs {
/// Returns pruning configuration.
pub fn prune_config(&self, chain_spec: &ChainSpec) -> Option<PruneConfig> {
pub fn prune_config(&self, chain_spec: &impl EthChainSpec) -> Option<PruneConfig> {
// Initialise with a default prune configuration.
let mut config = PruneConfig::default();
@ -106,16 +106,14 @@ impl PruningArgs {
// prune all receipts if chain doesn't have deposit contract specified in chain
// spec
receipts: chain_spec
.deposit_contract
.as_ref()
.deposit_contract()
.map(|contract| PruneMode::Before(contract.block))
.or(Some(PruneMode::Distance(MINIMUM_PRUNING_DISTANCE))),
account_history: Some(PruneMode::Distance(MINIMUM_PRUNING_DISTANCE)),
storage_history: Some(PruneMode::Distance(MINIMUM_PRUNING_DISTANCE)),
receipts_log_filter: ReceiptsLogPruneConfig(
chain_spec
.deposit_contract
.as_ref()
.deposit_contract()
.map(|contract| (contract.address, PruneMode::Before(contract.block)))
.into_iter()
.collect(),

View File

@ -9,7 +9,7 @@ use crate::{
utils::get_single_header,
};
use eyre::eyre;
use reth_chainspec::{ChainSpec, MAINNET};
use reth_chainspec::{ChainSpec, EthChainSpec, MAINNET};
use reth_config::config::PruneConfig;
use reth_network_p2p::headers::client::HeadersClient;
use serde::{de::DeserializeOwned, Serialize};
@ -70,8 +70,8 @@ use tracing::*;
/// let builder = builder.with_rpc(rpc);
/// }
/// ```
#[derive(Debug, Clone)]
pub struct NodeConfig {
#[derive(Debug)]
pub struct NodeConfig<ChainSpec> {
/// All data directory related arguments
pub datadir: DatadirArgs,
@ -129,14 +129,16 @@ pub struct NodeConfig {
pub pruning: PruningArgs,
}
impl NodeConfig {
impl NodeConfig<ChainSpec> {
/// Creates a testing [`NodeConfig`], causing the database to be launched ephemerally.
pub fn test() -> Self {
Self::default()
// set all ports to zero by default for test instances
.with_unused_ports()
}
}
impl<ChainSpec> NodeConfig<ChainSpec> {
/// Sets --dev mode for the node.
///
/// In addition to setting the `--dev` flag, this also:
@ -235,7 +237,10 @@ impl NodeConfig {
}
/// Returns pruning configuration.
pub fn prune_config(&self) -> Option<PruneConfig> {
pub fn prune_config(&self) -> Option<PruneConfig>
where
ChainSpec: EthChainSpec,
{
self.pruning.prune_config(&self.chain)
}
@ -365,8 +370,11 @@ impl NodeConfig {
}
/// Resolve the final datadir path.
pub fn datadir(&self) -> ChainPath<DataDirPath> {
self.datadir.clone().resolve_datadir(self.chain.chain)
pub fn datadir(&self) -> ChainPath<DataDirPath>
where
ChainSpec: EthChainSpec,
{
self.datadir.clone().resolve_datadir(self.chain.chain())
}
/// Load an application configuration from a specified path.
@ -397,7 +405,7 @@ impl NodeConfig {
}
}
impl Default for NodeConfig {
impl Default for NodeConfig<ChainSpec> {
fn default() -> Self {
Self {
config: None,
@ -416,3 +424,23 @@ impl Default for NodeConfig {
}
}
}
impl<ChainSpec> Clone for NodeConfig<ChainSpec> {
fn clone(&self) -> Self {
Self {
chain: self.chain.clone(),
config: self.config.clone(),
metrics: self.metrics,
instance: self.instance,
network: self.network.clone(),
rpc: self.rpc.clone(),
txpool: self.txpool.clone(),
builder: self.builder.clone(),
debug: self.debug.clone(),
db: self.db,
dev: self.dev,
pruning: self.pruning.clone(),
datadir: self.datadir.clone(),
}
}
}