From fe5e3bd0c801f9fce30cf160baf7540acf5cc7ce Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Thu, 5 Jan 2023 11:54:25 +0200 Subject: [PATCH] feat(node): allow config'ing trusted nodes from cli (#728) --- bin/reth/src/lib.rs | 20 ++++++++++++++++++++ bin/reth/src/node/mod.rs | 17 ++++++++++++----- bin/reth/src/stage/mod.rs | 18 +----------------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/bin/reth/src/lib.rs b/bin/reth/src/lib.rs index ed456fa19..db76381c4 100644 --- a/bin/reth/src/lib.rs +++ b/bin/reth/src/lib.rs @@ -16,3 +16,23 @@ pub mod prometheus_exporter; pub mod stage; pub mod test_eth_chain; pub mod util; + +use clap::Parser; +use reth_primitives::NodeRecord; + +#[derive(Debug, Parser)] +/// Parameters for configuring the network more granularly via CLI +struct NetworkOpts { + /// Disable the discovery service. + #[arg(short, long)] + disable_discovery: bool, + + /// Target trusted peer enodes + /// --trusted-peers enode://abcd@192.168.0.1:30303 + #[arg(long)] + trusted_peers: Vec, + + /// Connect only to trusted peers + #[arg(long)] + trusted_only: bool, +} diff --git a/bin/reth/src/node/mod.rs b/bin/reth/src/node/mod.rs index 7b3d8eefb..fdc116671 100644 --- a/bin/reth/src/node/mod.rs +++ b/bin/reth/src/node/mod.rs @@ -9,6 +9,7 @@ use crate::{ chainspec::{chain_spec_value_parser, ChainSpecification}, init::{init_db, init_genesis}, }, + NetworkOpts, }; use clap::{crate_version, Parser}; use fdlimit::raise_fd_limit; @@ -73,9 +74,8 @@ pub struct Command { #[arg(long = "debug.tip")] tip: Option, - /// Disable the discovery service. - #[arg(short, long)] - disable_discovery: bool, + #[clap(flatten)] + network: NetworkOpts, } impl Command { @@ -86,7 +86,14 @@ impl Command { // Does not do anything on windows. raise_fd_limit(); - let config: Config = confy::load_path(&self.config).unwrap_or_default(); + let mut config: Config = confy::load_path(&self.config).unwrap_or_default(); + config.peers.connect_trusted_nodes_only = self.network.trusted_only; + if !self.network.trusted_peers.is_empty() { + self.network.trusted_peers.iter().for_each(|peer| { + config.peers.trusted_nodes.insert(*peer); + }); + } + info!("reth {} starting", crate_version!()); info!("Opening database at {}", &self.db); @@ -104,7 +111,7 @@ impl Command { let genesis_hash = init_genesis(db.clone(), self.chain.genesis.clone())?; let network = config - .network_config(db.clone(), chain_id, genesis_hash, self.disable_discovery) + .network_config(db.clone(), chain_id, genesis_hash, self.network.disable_discovery) .start_network() .await?; diff --git a/bin/reth/src/stage/mod.rs b/bin/reth/src/stage/mod.rs index 691ef6dbc..8b1dcda17 100644 --- a/bin/reth/src/stage/mod.rs +++ b/bin/reth/src/stage/mod.rs @@ -9,11 +9,11 @@ use crate::{ chainspec::{chain_spec_value_parser, ChainSpecification}, init::{init_db, init_genesis}, }, + NetworkOpts, }; use reth_consensus::BeaconConsensus; use reth_downloaders::bodies::concurrent::ConcurrentDownloader; use reth_executor::Config as ExecutorConfig; -use reth_primitives::NodeRecord; use reth_stages::{ metrics::HeaderMetrics, stages::{bodies::BodyStage, execution::ExecutionStage, sender_recovery::SenderRecoveryStage}, @@ -101,22 +101,6 @@ enum StageEnum { Execution, } -#[derive(Debug, Parser)] -struct NetworkOpts { - /// Disable the discovery service. - #[arg(short, long)] - disable_discovery: bool, - - /// Target trusted peer enodes - /// --trusted-peers enode://abcd@192.168.0.1:30303 - #[arg(long)] - trusted_peers: Vec, - - /// Connect only to trusted peers - #[arg(long)] - trusted_only: bool, -} - impl Command { /// Execute `stage` command pub async fn execute(&self) -> eyre::Result<()> {