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..38edcc9ac5bc 100644 --- a/crates/cli/commands/src/stage/mod.rs +++ b/crates/cli/commands/src/stage/mod.rs @@ -7,6 +7,7 @@ use clap::{Parser, Subcommand}; use reth_chainspec::{EthChainSpec, EthereumHardforks}; use reth_cli::chainspec::ChainSpecParser; use reth_cli_runner::CliContext; +use reth_eth_wire::NetPrimitivesFor; use reth_evm::execute::BlockExecutorProvider; pub mod drop; @@ -41,14 +42,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: NetPrimitivesFor, { 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..aba0c30bf1aa 100644 --- a/crates/cli/commands/src/stage/run.rs +++ b/crates/cli/commands/src/stage/run.rs @@ -16,6 +16,7 @@ use reth_downloaders::{ bodies::bodies::BodiesDownloaderBuilder, headers::reverse_headers::ReverseHeadersDownloaderBuilder, }; +use reth_eth_wire::NetPrimitivesFor; use reth_evm::execute::BlockExecutorProvider; use reth_exex::ExExManagerHandle; use reth_network::BlockDownloaderProvider; @@ -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: NetPrimitivesFor, { // 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/net/eth-wire-types/src/primitives.rs b/crates/net/eth-wire-types/src/primitives.rs index 4595ac15ab6f..295f6f161f34 100644 --- a/crates/net/eth-wire-types/src/primitives.rs +++ b/crates/net/eth-wire-types/src/primitives.rs @@ -2,6 +2,7 @@ use alloy_consensus::{RlpDecodableReceipt, RlpEncodableReceipt, TxReceipt}; use alloy_rlp::{Decodable, Encodable}; +use reth_primitives::NodePrimitives; use reth_primitives_traits::{Block, BlockBody, BlockHeader, SignedTransaction}; use std::fmt::Debug; @@ -40,7 +41,31 @@ pub trait NetworkPrimitives: + 'static; } -/// Primitive types used by Ethereum network. +/// This is a helper trait for use in bounds, where some of the [`NetworkPrimitives`] associated +/// types must be the same as the [`NodePrimitives`] associated types. +pub trait NetPrimitivesFor: + NetworkPrimitives< + BlockHeader = N::BlockHeader, + BlockBody = N::BlockBody, + Block = N::Block, + Receipt = N::Receipt, +> +{ +} + +impl NetPrimitivesFor for T +where + N: NodePrimitives, + T: NetworkPrimitives< + BlockHeader = N::BlockHeader, + BlockBody = N::BlockBody, + Block = N::Block, + Receipt = N::Receipt, + >, +{ +} + +/// Network primitive types used by Ethereum networks. #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] #[non_exhaustive] pub struct EthNetworkPrimitives; diff --git a/crates/node/builder/src/builder/mod.rs b/crates/node/builder/src/builder/mod.rs index 2413609d032c..44311f34e3c4 100644 --- a/crates/node/builder/src/builder/mod.rs +++ b/crates/node/builder/src/builder/mod.rs @@ -734,11 +734,12 @@ impl BuilderContext { } /// Builds the [`NetworkConfig`]. - pub fn build_network_config( + pub fn build_network_config( &self, - network_builder: NetworkConfigBuilder, - ) -> NetworkConfig + network_builder: NetworkConfigBuilder, + ) -> NetworkConfig where + N: NetworkPrimitives, Node::Types: NodeTypes, { network_builder.build(self.provider.clone()) @@ -747,20 +748,29 @@ impl BuilderContext { impl>> BuilderContext { /// Creates the [`NetworkBuilder`] for the node. - pub async fn network_builder(&self) -> eyre::Result> { + pub async fn network_builder(&self) -> eyre::Result> + where + N: NetworkPrimitives, + { let network_config = self.network_config()?; let builder = NetworkManager::builder(network_config).await?; Ok(builder) } /// Returns the default network config for the node. - pub fn network_config(&self) -> eyre::Result> { + pub fn network_config(&self) -> eyre::Result> + where + N: NetworkPrimitives, + { let network_builder = self.network_config_builder(); Ok(self.build_network_config(network_builder?)) } /// Get the [`NetworkConfigBuilder`]. - pub fn network_config_builder(&self) -> eyre::Result { + pub fn network_config_builder(&self) -> eyre::Result> + where + N: NetworkPrimitives, + { let secret_key = self.network_secret(&self.config().datadir())?; let default_peers_path = self.config().datadir().known_peers(); let builder = self diff --git a/crates/node/core/src/args/network.rs b/crates/node/core/src/args/network.rs index d359cfb3f1af..cb784e5fd76e 100644 --- a/crates/node/core/src/args/network.rs +++ b/crates/node/core/src/args/network.rs @@ -30,7 +30,7 @@ use reth_network::{ DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ, SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE, }, - HelloMessageWithProtocols, NetworkConfigBuilder, SessionsConfig, + HelloMessageWithProtocols, NetworkConfigBuilder, NetworkPrimitives, SessionsConfig, }; use reth_network_peers::{mainnet_nodes, TrustedPeer}; use secp256k1::SecretKey; @@ -196,13 +196,13 @@ impl NetworkArgs { /// 1. --bootnodes flag /// 2. Network preset flags (e.g. --holesky) /// 3. default to mainnet nodes - pub fn network_config( + pub fn network_config( &self, config: &Config, chain_spec: impl EthChainSpec, secret_key: SecretKey, default_peers_file: PathBuf, - ) -> NetworkConfigBuilder { + ) -> NetworkConfigBuilder { let addr = self.resolved_addr(); let chain_bootnodes = self .resolved_bootnodes() @@ -230,7 +230,7 @@ impl NetworkArgs { }; // Configure basic network stack - NetworkConfigBuilder::new(secret_key) + NetworkConfigBuilder::::new(secret_key) .peer_config(config.peers_config_with_basic_nodes_from_file( self.persistent_peers_file(peers_file).as_deref(), )) @@ -408,12 +408,15 @@ pub struct DiscoveryArgs { impl DiscoveryArgs { /// Apply the discovery settings to the given [`NetworkConfigBuilder`] - pub fn apply_to_builder( + pub fn apply_to_builder( &self, - mut network_config_builder: NetworkConfigBuilder, + mut network_config_builder: NetworkConfigBuilder, rlpx_tcp_socket: SocketAddr, boot_nodes: impl IntoIterator, - ) -> NetworkConfigBuilder { + ) -> NetworkConfigBuilder + 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 6f01c757088d..f8428b795bd9 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::{EthNetworkPrimitives, 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::{ @@ -621,7 +621,7 @@ impl OpNetworkBuilder { pub fn network_config( &self, ctx: &BuilderContext, - ) -> eyre::Result::Provider>> + ) -> eyre::Result::Provider, OpNetworkPrimitives>> where Node: FullNodeTypes>, { @@ -670,13 +670,13 @@ where > + Unpin + 'static, { - type Primitives = EthNetworkPrimitives; + type Primitives = OpNetworkPrimitives; async fn build_network( self, ctx: &BuilderContext, pool: Pool, - ) -> eyre::Result { + ) -> eyre::Result> { let network_config = self.network_config(ctx)?; let network = NetworkManager::builder(network_config).await?; let handle = ctx.start_network(network, pool); @@ -722,3 +722,17 @@ where Ok(OpEngineValidator::new(ctx.config.chain.clone())) } } + +/// Network primitive types used by Optimism networks. +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub struct OpNetworkPrimitives; + +impl NetworkPrimitives for OpNetworkPrimitives { + type BlockHeader = alloy_consensus::Header; + type BlockBody = reth_primitives::BlockBody; + type Block = reth_primitives::Block; + type BroadcastedTransaction = reth_primitives::TransactionSigned; + type PooledTransaction = reth_primitives::PooledTransaction; + type Receipt = reth_primitives::Receipt; +}