Skip to content

Commit

Permalink
feat(node-builder): move network setup to node-builder launch/ Builde…
Browse files Browse the repository at this point in the history
…rContext types (#8648)
  • Loading branch information
fgimenez authored Jun 6, 2024
1 parent 7b435e0 commit f80b054
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 153 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions crates/node-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ reth-beacon-consensus.workspace = true
# ethereum
alloy-rpc-types-engine.workspace = true

# ethereum
discv5.workspace = true

# async
tokio.workspace = true
tokio-util.workspace = true
Expand Down
119 changes: 6 additions & 113 deletions crates/node-core/src/node_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,29 @@
use crate::{
args::{
get_secret_key, DatabaseArgs, DatadirArgs, DebugArgs, DevArgs, DiscoveryArgs, NetworkArgs,
PayloadBuilderArgs, PruningArgs, RpcServerArgs, TxPoolArgs,
DatabaseArgs, DatadirArgs, DebugArgs, DevArgs, NetworkArgs, PayloadBuilderArgs,
PruningArgs, RpcServerArgs, TxPoolArgs,
},
dirs::{ChainPath, DataDirPath},
metrics::prometheus_exporter,
utils::get_single_header,
};
use discv5::ListenConfig;
use metrics_exporter_prometheus::PrometheusHandle;
use once_cell::sync::Lazy;
use reth_config::{config::PruneConfig, Config};
use reth_config::config::PruneConfig;
use reth_db_api::{database::Database, database_metrics::DatabaseMetrics};
use reth_network::{NetworkBuilder, NetworkConfig, NetworkManager};
use reth_network_p2p::headers::client::HeadersClient;
use reth_primitives::{
constants::eip4844::MAINNET_KZG_TRUSTED_SETUP, kzg::KzgSettings, stage::StageId,
BlockHashOrNumber, BlockNumber, ChainSpec, Head, SealedHeader, B256, MAINNET,
};
use reth_provider::{
providers::StaticFileProvider, BlockHashReader, BlockNumReader, HeaderProvider,
ProviderFactory, StageCheckpointReader,
providers::StaticFileProvider, BlockHashReader, HeaderProvider, ProviderFactory,
StageCheckpointReader,
};
use reth_storage_errors::provider::ProviderResult;
use reth_tasks::TaskExecutor;
use secp256k1::SecretKey;
use std::{
net::{IpAddr, SocketAddr, SocketAddrV4, SocketAddrV6},
path::PathBuf,
sync::Arc,
};
use std::{net::SocketAddr, path::PathBuf, sync::Arc};
use tracing::*;

/// The default prometheus recorder handle. We use a global static to ensure that it is only
Expand Down Expand Up @@ -245,15 +238,6 @@ impl NodeConfig {
self
}

/// Get the network secret from the given data dir
pub fn network_secret(&self) -> eyre::Result<SecretKey> {
let network_secret_path =
self.network.p2p_secret_key.clone().unwrap_or_else(|| self.datadir().p2p_secret());
debug!(target: "reth::cli", ?network_secret_path, "Loading p2p key file");
let secret_key = get_secret_key(&network_secret_path)?;
Ok(secret_key)
}

/// Returns the initial pipeline target, based on whether or not the node is running in
/// `debug.tip` mode, `debug.continuous` mode, or neither.
///
Expand Down Expand Up @@ -302,38 +286,6 @@ impl NodeConfig {
Ok(max_block)
}

/// Create the [`NetworkConfig`] for the node
pub fn network_config<C>(
&self,
config: &Config,
client: C,
executor: TaskExecutor,
head: Head,
) -> eyre::Result<NetworkConfig<C>> {
info!(target: "reth::cli", "Connecting to P2P network");
let secret_key = self.network_secret()?;
let default_peers_path = self.datadir().known_peers();
Ok(self.load_network_config(config, client, executor, head, secret_key, default_peers_path))
}

/// Create the [`NetworkBuilder`].
///
/// This only configures it and does not spawn it.
pub async fn build_network<C>(
&self,
config: &Config,
client: C,
executor: TaskExecutor,
head: Head,
) -> eyre::Result<NetworkBuilder<C, (), ()>>
where
C: BlockNumReader,
{
let network_config = self.network_config(config, client, executor, head)?;
let builder = NetworkManager::builder(network_config).await?;
Ok(builder)
}

/// Loads '`MAINNET_KZG_TRUSTED_SETUP`'
pub fn kzg_settings(&self) -> eyre::Result<Arc<KzgSettings>> {
Ok(Arc::clone(&MAINNET_KZG_TRUSTED_SETUP))
Expand Down Expand Up @@ -454,65 +406,6 @@ impl NodeConfig {
}
}

/// Builds the [`NetworkConfig`] with the given [`ProviderFactory`].
pub fn load_network_config<C>(
&self,
config: &Config,
client: C,
executor: TaskExecutor,
head: Head,
secret_key: SecretKey,
default_peers_path: PathBuf,
) -> NetworkConfig<C> {
self.network
.network_config(config, self.chain.clone(), secret_key, default_peers_path)
.with_task_executor(Box::new(executor))
.set_head(head)
.listener_addr(SocketAddr::new(
self.network.addr,
// set discovery port based on instance number
self.network.port + self.instance - 1,
))
.disable_discv4_discovery_if(self.chain.chain.is_optimism())
.discovery_addr(SocketAddr::new(
self.network.discovery.addr,
// set discovery port based on instance number
self.network.discovery.port + self.instance - 1,
))
.map_discv5_config_builder(|builder| {
let DiscoveryArgs {
discv5_addr,
discv5_addr_ipv6,
discv5_port,
discv5_port_ipv6,
..
} = self.network.discovery;

// Use rlpx address if none given
let discv5_addr_ipv4 = discv5_addr.or(match self.network.addr {
IpAddr::V4(ip) => Some(ip),
IpAddr::V6(_) => None,
});
let discv5_addr_ipv6 = discv5_addr_ipv6.or(match self.network.addr {
IpAddr::V4(_) => None,
IpAddr::V6(ip) => Some(ip),
});

let discv5_port_ipv4 = discv5_port + self.instance - 1;
let discv5_port_ipv6 = discv5_port_ipv6 + self.instance - 1;

builder.discv5_config(
discv5::ConfigBuilder::new(ListenConfig::from_two_sockets(
discv5_addr_ipv4.map(|addr| SocketAddrV4::new(addr, discv5_port_ipv4)),
discv5_addr_ipv6
.map(|addr| SocketAddrV6::new(addr, discv5_port_ipv6, 0, 0)),
))
.build(),
)
})
.build(client)
}

/// Change rpc port numbers based on the instance number, using the inner
/// [`RpcServerArgs::adjust_instance_ports`] method.
pub fn adjust_instance_ports(&mut self) {
Expand Down
10 changes: 10 additions & 0 deletions crates/node/builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ tokio = { workspace = true, features = [
] }
tokio-stream.workspace = true

# ethereum
discv5.workspace = true

# crypto
secp256k1 = { workspace = true, features = [
"global-context",
"rand-std",
"recovery",
] }

## misc
aquamarine.workspace = true
eyre.workspace = true
Expand Down
Loading

0 comments on commit f80b054

Please sign in to comment.