Skip to content

Commit

Permalink
just parameterize commands that need network by NetworkPrimitives
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Dec 13, 2024
1 parent 6aca2ce commit 79261b4
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 27 deletions.
10 changes: 8 additions & 2 deletions bin/reth/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -169,9 +170,14 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>, Ext: clap::Args + fmt::Debug> Cl
runner.run_blocking_until_ctrl_c(command.execute::<EthereumNode>())
}
Commands::Stage(command) => runner.run_command_until_exit(|ctx| {
command.execute::<EthereumNode, _, _>(ctx, EthExecutorProvider::ethereum)
command.execute::<EthereumNode, _, _, EthNetworkPrimitives>(
ctx,
EthExecutorProvider::ethereum,
)
}),
Commands::P2P(command) => runner.run_until_ctrl_c(command.execute()),
Commands::P2P(command) => {
runner.run_until_ctrl_c(command.execute::<EthNetworkPrimitives>())
}
#[cfg(feature = "dev")]
Commands::TestVectors(command) => runner.run_until_ctrl_c(command.execute()),
Commands::Config(command) => runner.run_until_ctrl_c(command.execute()),
Expand Down
6 changes: 3 additions & 3 deletions crates/cli/commands/src/p2p/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -75,7 +75,7 @@ pub enum Subcommands {

impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C> {
/// Execute `p2p` command
pub async fn execute(self) -> eyre::Result<()> {
pub async fn execute<N: NetworkPrimitives>(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());

Expand All @@ -97,7 +97,7 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C>
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::<N>::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())
Expand Down
7 changes: 5 additions & 2 deletions crates/cli/commands/src/stage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,14 +43,15 @@ pub enum Subcommands<C: ChainSpecParser> {

impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C> {
/// Execute `stage` command
pub async fn execute<N, E, F>(self, ctx: CliContext, executor: F) -> eyre::Result<()>
pub async fn execute<N, E, F, P>(self, ctx: CliContext, executor: F) -> eyre::Result<()>
where
N: CliNodeTypes<ChainSpec = C::ChainSpec>,
E: BlockExecutorProvider<Primitives = N::Primitives>,
F: FnOnce(Arc<C::ChainSpec>) -> E,
P: NetworkPrimitives<BlockHeader = HeaderTy<N>, BlockBody = BodyTy<N>, Block = BlockTy<N>>,
{
match self.command {
Subcommands::Run(command) => command.execute::<N, _, _>(ctx, executor).await,
Subcommands::Run(command) => command.execute::<N, _, _, P>(ctx, executor).await,
Subcommands::Drop(command) => command.execute::<N>().await,
Subcommands::Dump(command) => command.execute::<N, _, _>(executor).await,
Subcommands::Unwind(command) => command.execute::<N>().await,
Expand Down
13 changes: 7 additions & 6 deletions crates/cli/commands/src/stage/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -104,11 +105,12 @@ pub struct Command<C: ChainSpecParser> {

impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C> {
/// Execute `stage` command
pub async fn execute<N, E, F>(self, ctx: CliContext, executor: F) -> eyre::Result<()>
pub async fn execute<N, E, F, P>(self, ctx: CliContext, executor: F) -> eyre::Result<()>
where
N: CliNodeTypes<ChainSpec = C::ChainSpec>,
E: BlockExecutorProvider<Primitives = N::Primitives>,
F: FnOnce(Arc<C::ChainSpec>) -> E,
P: NetworkPrimitives<BlockHeader = HeaderTy<N>, BlockBody = BodyTy<N>, Block = BlockTy<N>>,
{
// Raise the fd limit of the process.
// Does not do anything on windows.
Expand Down Expand Up @@ -174,7 +176,7 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C>

let network = self
.network
.network_config(
.network_config::<P>(
&config,
provider_factory.chain_spec(),
p2p_secret_key,
Expand All @@ -186,13 +188,12 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C>
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(),
Expand Down Expand Up @@ -224,7 +225,7 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C>

let network = self
.network
.network_config(
.network_config::<P>(
&config,
provider_factory.chain_spec(),
p2p_secret_key,
Expand Down
12 changes: 6 additions & 6 deletions crates/node/builder/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,8 @@ impl<Node: FullNodeTypes> BuilderContext<Node> {
impl<Node: FullNodeTypes<Types: NodeTypes<ChainSpec: Hardforks>>> BuilderContext<Node> {
/// Creates the [`NetworkBuilder`] for the node.
pub async fn network_builder<N>(&self) -> eyre::Result<NetworkBuilder<(), (), N>>
where
N: NetworkPrimitives,
where
N: NetworkPrimitives,
{
let network_config = self.network_config()?;
let builder = NetworkManager::builder(network_config).await?;
Expand All @@ -765,17 +765,17 @@ impl<Node: FullNodeTypes<Types: NodeTypes<ChainSpec: Hardforks>>> BuilderContext

/// Returns the default network config for the node.
pub fn network_config<N>(&self) -> eyre::Result<NetworkConfig<Node::Provider, N>>
where
N: NetworkPrimitives,
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<N>(&self) -> eyre::Result<NetworkConfigBuilder<N>>
where
N: NetworkPrimitives,
where
N: NetworkPrimitives,
{
let secret_key = self.network_secret(&self.config().datadir())?;
let default_peers_path = self.config().datadir().known_peers();
Expand Down
7 changes: 4 additions & 3 deletions crates/node/core/src/args/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -413,8 +414,8 @@ impl DiscoveryArgs {
rlpx_tcp_socket: SocketAddr,
boot_nodes: impl IntoIterator<Item = NodeRecord>,
) -> NetworkConfigBuilder<N>
where
N: NetworkPrimitives,
where
N: NetworkPrimitives,
{
if self.disable_discovery || self.disable_dns_discovery {
network_config_builder = network_config_builder.disable_dns_discovery();
Expand Down
9 changes: 6 additions & 3 deletions crates/optimism/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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::<OpNode>()),
Commands::Stage(command) => runner.run_command_until_exit(|ctx| {
command.execute::<OpNode, _, _>(ctx, OpExecutorProvider::optimism)
command
.execute::<OpNode, _, _, OpNetworkPrimitives>(ctx, OpExecutorProvider::optimism)
}),
Commands::P2P(command) => runner.run_until_ctrl_c(command.execute()),
Commands::P2P(command) => {
runner.run_until_ctrl_c(command.execute::<OpNetworkPrimitives>())
}
Commands::Config(command) => runner.run_until_ctrl_c(command.execute()),
Commands::Recover(command) => {
runner.run_command_until_exit(|ctx| command.execute::<OpNode>(ctx))
Expand Down
2 changes: 1 addition & 1 deletion crates/optimism/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion crates/optimism/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down

0 comments on commit 79261b4

Please sign in to comment.