Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: simplify p2p subcommand #9265

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ use backon::{ConstantBuilder, Retryable};
use clap::{Parser, Subcommand};
use reth_chainspec::ChainSpec;
use reth_config::Config;
use reth_db::create_db;
use reth_network::NetworkConfigBuilder;
use reth_network_p2p::bodies::client::BodiesClient;
use reth_node_core::args::DatadirArgs;
use reth_primitives::BlockHashOrNumber;
use reth_provider::{providers::StaticFileProvider, ProviderFactory};
use std::{path::PathBuf, sync::Arc};

/// `reth p2p` command
Expand Down Expand Up @@ -75,10 +73,6 @@ pub enum Subcommands {
impl Command {
/// Execute `p2p` command
pub async fn execute(&self) -> eyre::Result<()> {
let tempdir = tempfile::TempDir::new()?;
let noop_db = Arc::new(create_db(tempdir.into_path(), self.db.database_args())?);

// add network name to data dir
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 @@ -101,7 +95,7 @@ impl Command {
let rlpx_socket = (self.network.addr, self.network.port).into();
let boot_nodes = self.chain.bootnodes().unwrap_or_default();

let network = 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)
.chain_spec(self.chain.clone())
Expand All @@ -110,13 +104,11 @@ impl Command {
.apply(|builder| {
self.network.discovery.apply_to_builder(builder, rlpx_socket, boot_nodes)
})
.build(Arc::new(ProviderFactory::new(
noop_db,
self.chain.clone(),
StaticFileProvider::read_write(data_dir.static_files())?,
)))
.start_network()
.build_with_noop_provider()
.manager()
.await?;
let network = net.handle().clone();
tokio::task::spawn(net);

let fetch_client = network.fetch_client().await?;
let retries = self.retries.max(1);
Expand Down
12 changes: 11 additions & 1 deletion crates/net/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use reth_eth_wire::{HelloMessage, HelloMessageWithProtocols, Status};
use reth_network_peers::{mainnet_nodes, pk2id, sepolia_nodes, PeerId, TrustedPeer};
use reth_network_types::{PeersConfig, SessionsConfig};
use reth_primitives::{ForkFilter, Head};
use reth_storage_api::{BlockReader, HeaderProvider};
use reth_storage_api::{BlockNumReader, BlockReader, HeaderProvider};
use reth_tasks::{TaskSpawner, TokioTaskExecutor};
use secp256k1::SECP256K1;
use std::{collections::HashSet, net::SocketAddr, sync::Arc};
Expand Down Expand Up @@ -119,6 +119,16 @@ impl<C> NetworkConfig<C> {
}
}

impl<C> NetworkConfig<C>
where
C: BlockNumReader,
{
/// Convenience method for calling [`NetworkManager::new`].
pub async fn manager(self) -> Result<NetworkManager<C>, NetworkError> {
NetworkManager::new(self).await
}
}

impl<C> NetworkConfig<C>
where
C: BlockReader + HeaderProvider + Clone + Unpin + 'static,
Expand Down
Loading