From 57653b1a5805458f758de486c7ed6ffc0e6dd022 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 27 Nov 2023 11:41:19 +0100 Subject: [PATCH] chore: limited node command cleanup (#5539) --- bin/reth/src/cli/ext.rs | 6 ++-- bin/reth/src/node/mod.rs | 60 +++++++++++++++++++++++----------------- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/bin/reth/src/cli/ext.rs b/bin/reth/src/cli/ext.rs index 3529975273..4bfbeb64d1 100644 --- a/bin/reth/src/cli/ext.rs +++ b/bin/reth/src/cli/ext.rs @@ -1,8 +1,8 @@ //! Support for integrating customizations into the CLI. use crate::cli::{ - components::{RethNodeComponents, RethRpcComponents}, - config::{PayloadBuilderConfig, RethRpcConfig}, + components::{RethNodeComponents, RethRpcComponents, RethRpcServerHandles}, + config::{PayloadBuilderConfig, RethNetworkConfig, RethRpcConfig}, }; use clap::Args; use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig}; @@ -10,8 +10,6 @@ use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService}; use reth_tasks::TaskSpawner; use std::{fmt, marker::PhantomData}; -use crate::cli::{components::RethRpcServerHandles, config::RethNetworkConfig}; - /// A trait that allows for extending parts of the CLI with additional functionality. /// /// This is intended as a way to allow to _extend_ the node command. For example, to register diff --git a/bin/reth/src/node/mod.rs b/bin/reth/src/node/mod.rs index 42d3e0136f..aa0ee115de 100644 --- a/bin/reth/src/node/mod.rs +++ b/bin/reth/src/node/mod.rs @@ -13,7 +13,7 @@ use crate::{ config::RethRpcConfig, ext::{RethCliExt, RethNodeCommandConfig}, }, - dirs::{DataDirPath, MaybePlatformPath}, + dirs::{ChainPath, DataDirPath, MaybePlatformPath}, init::init_genesis, node::cl_events::ConsensusLayerHealthEvents, prometheus_exporter, @@ -241,18 +241,14 @@ impl NodeCommand { // Does not do anything on windows. raise_fd_limit(); - // add network name to data dir - let data_dir = self.datadir.unwrap_or_chain_default(self.chain.chain); - let config_path = self.config.clone().unwrap_or(data_dir.config_path()); - - let mut config: Config = self.load_config(config_path.clone())?; - - // always store reth.toml in the data dir, not the chain specific data dir - info!(target: "reth::cli", path = ?config_path, "Configuration loaded"); + // get config + let config = self.load_config()?; let prometheus_handle = self.install_prometheus_recorder()?; + let data_dir = self.data_dir(); let db_path = data_dir.db_path(); + info!(target: "reth::cli", path = ?db_path, "Opening database"); let db = Arc::new(init_db(&db_path, self.db.log_level)?.with_metrics()); info!(target: "reth::cli", "Database opened"); @@ -273,14 +269,12 @@ impl NodeCommand { debug!(target: "reth::cli", chain=%self.chain.chain, genesis=?self.chain.genesis_hash(), "Initializing genesis"); - let genesis_hash = init_genesis(db.clone(), self.chain.clone())?; + let genesis_hash = init_genesis(Arc::clone(&db), self.chain.clone())?; info!(target: "reth::cli", "{}", DisplayHardforks::new(self.chain.hardforks())); let consensus = self.consensus(); - self.init_trusted_nodes(&mut config); - debug!(target: "reth::cli", "Spawning stages metrics listener task"); let (sync_metrics_tx, sync_metrics_rx) = unbounded_channel(); let sync_metrics_listener = reth_stages::MetricsListener::new(sync_metrics_rx); @@ -647,10 +641,35 @@ impl NodeCommand { Ok(pipeline) } + /// Returns the chain specific path to the data dir. + fn data_dir(&self) -> ChainPath { + self.datadir.unwrap_or_chain_default(self.chain.chain) + } + + /// Returns the path to the config file. + fn config_path(&self) -> PathBuf { + self.config.clone().unwrap_or_else(|| self.data_dir().config_path()) + } + /// Loads the reth config with the given datadir root - fn load_config(&self, config_path: PathBuf) -> eyre::Result { - confy::load_path::(config_path.clone()) - .wrap_err_with(|| format!("Could not load config file {:?}", config_path)) + fn load_config(&self) -> eyre::Result { + let config_path = self.config_path(); + let mut config = confy::load_path::(&config_path) + .wrap_err_with(|| format!("Could not load config file {:?}", config_path))?; + + info!(target: "reth::cli", path = ?config_path, "Configuration loaded"); + + // Update the config with the command line arguments + config.peers.connect_trusted_nodes_only = self.network.trusted_only; + + if !self.network.trusted_peers.is_empty() { + info!(target: "reth::cli", "Adding trusted nodes"); + self.network.trusted_peers.iter().for_each(|peer| { + config.peers.trusted_nodes.insert(*peer); + }); + } + + Ok(config) } /// Loads the trusted setup params from a given file path or falls back to @@ -665,17 +684,6 @@ impl NodeCommand { } } - fn init_trusted_nodes(&self, config: &mut Config) { - config.peers.connect_trusted_nodes_only = self.network.trusted_only; - - if !self.network.trusted_peers.is_empty() { - info!(target: "reth::cli", "Adding trusted nodes"); - self.network.trusted_peers.iter().for_each(|peer| { - config.peers.trusted_nodes.insert(*peer); - }); - } - } - fn install_prometheus_recorder(&self) -> eyre::Result { prometheus_exporter::install_recorder() }