From 79261b4c7facf7e5d5560d6a605ef630e96314f1 Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Fri, 13 Dec 2024 17:03:55 -0500 Subject: [PATCH] just parameterize commands that need network by NetworkPrimitives --- bin/reth/src/cli/mod.rs | 10 ++++++++-- crates/cli/commands/src/p2p/mod.rs | 6 +++--- crates/cli/commands/src/stage/mod.rs | 7 +++++-- crates/cli/commands/src/stage/run.rs | 13 +++++++------ crates/node/builder/src/builder/mod.rs | 12 ++++++------ crates/node/core/src/args/network.rs | 7 ++++--- crates/optimism/cli/src/lib.rs | 9 ++++++--- crates/optimism/node/src/lib.rs | 2 +- crates/optimism/node/src/node.rs | 2 +- 9 files changed, 41 insertions(+), 27 deletions(-) diff --git a/bin/reth/src/cli/mod.rs b/bin/reth/src/cli/mod.rs index 192ab670028a..a146a88b724d 100644 --- a/bin/reth/src/cli/mod.rs +++ b/bin/reth/src/cli/mod.rs @@ -16,6 +16,7 @@ use reth_cli_commands::{ use reth_cli_runner::CliRunner; use reth_db::DatabaseEnv; use reth_ethereum_cli::chainspec::EthereumChainSpecParser; +use reth_network::EthNetworkPrimitives; use reth_node_builder::{NodeBuilder, WithLaunchContext}; use reth_node_ethereum::{EthExecutorProvider, EthereumNode}; use reth_node_metrics::recorder::install_prometheus_recorder; @@ -169,9 +170,14 @@ impl, Ext: clap::Args + fmt::Debug> Cl runner.run_blocking_until_ctrl_c(command.execute::()) } Commands::Stage(command) => runner.run_command_until_exit(|ctx| { - command.execute::(ctx, EthExecutorProvider::ethereum) + command.execute::( + ctx, + EthExecutorProvider::ethereum, + ) }), - Commands::P2P(command) => runner.run_until_ctrl_c(command.execute()), + Commands::P2P(command) => { + runner.run_until_ctrl_c(command.execute::()) + } #[cfg(feature = "dev")] Commands::TestVectors(command) => runner.run_until_ctrl_c(command.execute()), Commands::Config(command) => runner.run_until_ctrl_c(command.execute()), diff --git a/crates/cli/commands/src/p2p/mod.rs b/crates/cli/commands/src/p2p/mod.rs index 898bfbf8e34a..d1821ded826e 100644 --- a/crates/cli/commands/src/p2p/mod.rs +++ b/crates/cli/commands/src/p2p/mod.rs @@ -9,7 +9,7 @@ use reth_chainspec::{EthChainSpec, EthereumHardforks}; use reth_cli::chainspec::ChainSpecParser; use reth_cli_util::{get_secret_key, hash_or_num_value_parser}; use reth_config::Config; -use reth_network::{BlockDownloaderProvider, NetworkConfigBuilder}; +use reth_network::{BlockDownloaderProvider, NetworkConfigBuilder, NetworkPrimitives}; use reth_network_p2p::bodies::client::BodiesClient; use reth_node_core::{ args::{DatabaseArgs, DatadirArgs, NetworkArgs}, @@ -75,7 +75,7 @@ pub enum Subcommands { impl> Command { /// Execute `p2p` command - pub async fn execute(self) -> eyre::Result<()> { + pub async fn execute(self) -> eyre::Result<()> { let data_dir = self.datadir.clone().resolve_datadir(self.chain.chain()); let config_path = self.config.clone().unwrap_or_else(|| data_dir.config()); @@ -97,7 +97,7 @@ impl> Command let rlpx_socket = (self.network.addr, self.network.port).into(); let boot_nodes = self.chain.bootnodes().unwrap_or_default(); - let net = NetworkConfigBuilder::new(p2p_secret_key) + let net = NetworkConfigBuilder::::new(p2p_secret_key) .peer_config(config.peers_config_with_basic_nodes_from_file(None)) .external_ip_resolver(self.network.nat) .disable_discv4_discovery_if(self.chain.chain().is_optimism()) diff --git a/crates/cli/commands/src/stage/mod.rs b/crates/cli/commands/src/stage/mod.rs index 91ab458daf64..fd22da15bb46 100644 --- a/crates/cli/commands/src/stage/mod.rs +++ b/crates/cli/commands/src/stage/mod.rs @@ -8,6 +8,8 @@ use reth_chainspec::{EthChainSpec, EthereumHardforks}; use reth_cli::chainspec::ChainSpecParser; use reth_cli_runner::CliContext; use reth_evm::execute::BlockExecutorProvider; +use reth_network::NetworkPrimitives; +use reth_node_api::{BlockTy, BodyTy, HeaderTy}; pub mod drop; pub mod dump; @@ -41,14 +43,15 @@ pub enum Subcommands { impl> Command { /// Execute `stage` command - pub async fn execute(self, ctx: CliContext, executor: F) -> eyre::Result<()> + pub async fn execute(self, ctx: CliContext, executor: F) -> eyre::Result<()> where N: CliNodeTypes, E: BlockExecutorProvider, F: FnOnce(Arc) -> E, + P: NetworkPrimitives, BlockBody = BodyTy, Block = BlockTy>, { match self.command { - Subcommands::Run(command) => command.execute::(ctx, executor).await, + Subcommands::Run(command) => command.execute::(ctx, executor).await, Subcommands::Drop(command) => command.execute::().await, Subcommands::Dump(command) => command.execute::(executor).await, Subcommands::Unwind(command) => command.execute::().await, diff --git a/crates/cli/commands/src/stage/run.rs b/crates/cli/commands/src/stage/run.rs index 88a5fa6204e8..e7db0f1f15e7 100644 --- a/crates/cli/commands/src/stage/run.rs +++ b/crates/cli/commands/src/stage/run.rs @@ -18,8 +18,9 @@ use reth_downloaders::{ }; use reth_evm::execute::BlockExecutorProvider; use reth_exex::ExExManagerHandle; -use reth_network::BlockDownloaderProvider; +use reth_network::{BlockDownloaderProvider, NetworkPrimitives}; use reth_network_p2p::HeadersClient; +use reth_node_api::{BlockTy, BodyTy, HeaderTy}; use reth_node_core::{ args::{NetworkArgs, StageEnum}, version::{ @@ -104,11 +105,12 @@ pub struct Command { impl> Command { /// Execute `stage` command - pub async fn execute(self, ctx: CliContext, executor: F) -> eyre::Result<()> + pub async fn execute(self, ctx: CliContext, executor: F) -> eyre::Result<()> where N: CliNodeTypes, E: BlockExecutorProvider, F: FnOnce(Arc) -> E, + P: NetworkPrimitives, BlockBody = BodyTy, Block = BlockTy>, { // Raise the fd limit of the process. // Does not do anything on windows. @@ -174,7 +176,7 @@ impl> Command let network = self .network - .network_config( + .network_config::

( &config, provider_factory.chain_spec(), p2p_secret_key, @@ -186,13 +188,12 @@ impl> Command let fetch_client = Arc::new(network.fetch_client().await?); // Use `to` as the tip for the stage - let tip = fetch_client + let tip: P::BlockHeader = fetch_client .get_header(BlockHashOrNumber::Number(self.to)) .await? .into_data() .ok_or(StageError::MissingSyncGap)?; let (_, rx) = watch::channel(tip.hash_slow()); - ( Box::new(HeaderStage::new( provider_factory.clone(), @@ -224,7 +225,7 @@ impl> Command let network = self .network - .network_config( + .network_config::

( &config, provider_factory.chain_spec(), p2p_secret_key, diff --git a/crates/node/builder/src/builder/mod.rs b/crates/node/builder/src/builder/mod.rs index b830bd296d07..a6d32043e418 100644 --- a/crates/node/builder/src/builder/mod.rs +++ b/crates/node/builder/src/builder/mod.rs @@ -755,8 +755,8 @@ impl BuilderContext { impl>> BuilderContext { /// Creates the [`NetworkBuilder`] for the node. pub async fn network_builder(&self) -> eyre::Result> - where - N: NetworkPrimitives, + where + N: NetworkPrimitives, { let network_config = self.network_config()?; let builder = NetworkManager::builder(network_config).await?; @@ -765,8 +765,8 @@ impl>> BuilderContext /// Returns the default network config for the node. pub fn network_config(&self) -> eyre::Result> - where - N: NetworkPrimitives, + where + N: NetworkPrimitives, { let network_builder = self.network_config_builder(); Ok(self.build_network_config(network_builder?)) @@ -774,8 +774,8 @@ impl>> BuilderContext /// Get the [`NetworkConfigBuilder`]. pub fn network_config_builder(&self) -> eyre::Result> - where - N: NetworkPrimitives, + where + N: NetworkPrimitives, { let secret_key = self.network_secret(&self.config().datadir())?; let default_peers_path = self.config().datadir().known_peers(); diff --git a/crates/node/core/src/args/network.rs b/crates/node/core/src/args/network.rs index ced2b044b58d..cb784e5fd76e 100644 --- a/crates/node/core/src/args/network.rs +++ b/crates/node/core/src/args/network.rs @@ -29,7 +29,8 @@ use reth_network::{ TransactionFetcherConfig, TransactionsManagerConfig, DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ, SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE, - }, HelloMessageWithProtocols, NetworkConfigBuilder, NetworkPrimitives, SessionsConfig + }, + HelloMessageWithProtocols, NetworkConfigBuilder, NetworkPrimitives, SessionsConfig, }; use reth_network_peers::{mainnet_nodes, TrustedPeer}; use secp256k1::SecretKey; @@ -413,8 +414,8 @@ impl DiscoveryArgs { rlpx_tcp_socket: SocketAddr, boot_nodes: impl IntoIterator, ) -> NetworkConfigBuilder - where - N: NetworkPrimitives, + where + N: NetworkPrimitives, { if self.disable_discovery || self.disable_dns_discovery { network_config_builder = network_config_builder.disable_dns_discovery(); diff --git a/crates/optimism/cli/src/lib.rs b/crates/optimism/cli/src/lib.rs index 23eaa99b5213..4e1aa9469c14 100644 --- a/crates/optimism/cli/src/lib.rs +++ b/crates/optimism/cli/src/lib.rs @@ -52,7 +52,7 @@ use reth_node_core::{ version::{LONG_VERSION, SHORT_VERSION}, }; use reth_optimism_evm::OpExecutorProvider; -use reth_optimism_node::OpNode; +use reth_optimism_node::{OpNetworkPrimitives, OpNode}; use reth_tracing::FileWorkerGuard; use tracing::info; @@ -164,9 +164,12 @@ where Commands::DumpGenesis(command) => runner.run_blocking_until_ctrl_c(command.execute()), Commands::Db(command) => runner.run_blocking_until_ctrl_c(command.execute::()), Commands::Stage(command) => runner.run_command_until_exit(|ctx| { - command.execute::(ctx, OpExecutorProvider::optimism) + command + .execute::(ctx, OpExecutorProvider::optimism) }), - Commands::P2P(command) => runner.run_until_ctrl_c(command.execute()), + Commands::P2P(command) => { + runner.run_until_ctrl_c(command.execute::()) + } Commands::Config(command) => runner.run_until_ctrl_c(command.execute()), Commands::Recover(command) => { runner.run_command_until_exit(|ctx| command.execute::(ctx)) diff --git a/crates/optimism/node/src/lib.rs b/crates/optimism/node/src/lib.rs index 81db8b2b7fcf..18c5715474ed 100644 --- a/crates/optimism/node/src/lib.rs +++ b/crates/optimism/node/src/lib.rs @@ -21,7 +21,7 @@ pub mod engine; pub use engine::OpEngineTypes; pub mod node; -pub use node::OpNode; +pub use node::{OpNetworkPrimitives, OpNode}; pub mod txpool; diff --git a/crates/optimism/node/src/node.rs b/crates/optimism/node/src/node.rs index 631183de2110..c9826fd253a9 100644 --- a/crates/optimism/node/src/node.rs +++ b/crates/optimism/node/src/node.rs @@ -11,7 +11,7 @@ use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGenera use reth_chainspec::{EthChainSpec, EthereumHardforks, Hardforks}; use reth_db::transaction::{DbTx, DbTxMut}; use reth_evm::{execute::BasicBlockExecutorProvider, ConfigureEvm}; -use reth_network::{NetworkPrimitives, NetworkConfig, NetworkHandle, NetworkManager, PeersInfo}; +use reth_network::{NetworkConfig, NetworkHandle, NetworkManager, NetworkPrimitives, PeersInfo}; use reth_node_api::{AddOnsContext, EngineValidator, FullNodeComponents, NodeAddOns, TxTy}; use reth_node_builder::{ components::{