Skip to content

Commit

Permalink
Ability to (de)serialize NetworkConfigBuilder (paradigmxyz#897)
Browse files Browse the repository at this point in the history
  • Loading branch information
leruaa authored Jan 18, 2023
1 parent e9792c1 commit 115e623
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 57 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions bin/reth/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use reth_db::database::Database;
use reth_discv4::Discv4Config;
use reth_network::{
config::{mainnet_nodes, rng_secret_key},
NetworkConfig, PeersConfig,
NetworkConfig, NetworkConfigBuilder, PeersConfig,
};
use reth_primitives::{ChainSpec, NodeRecord};
use reth_provider::ProviderImpl;
Expand Down Expand Up @@ -37,13 +37,13 @@ impl Config {
.with_connect_trusted_nodes_only(self.peers.connect_trusted_nodes_only);
let discv4 =
Discv4Config::builder().external_ip_resolver(Some(nat_resolution_method)).clone();
NetworkConfig::builder(Arc::new(ProviderImpl::new(db)), rng_secret_key())
NetworkConfigBuilder::new(rng_secret_key())
.boot_nodes(bootnodes.unwrap_or_else(mainnet_nodes))
.peer_config(peer_config)
.discovery(discv4)
.chain_spec(chain_spec)
.set_discovery(disable_discovery)
.build()
.build(Arc::new(ProviderImpl::new(db)))
}
}

Expand Down
5 changes: 4 additions & 1 deletion crates/net/discv4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ secp256k1 = { version = "0.24", features = [
"rand-std",
"recovery",
] }
enr = { version = "0.7.0", default-features = false, features = ["rust-secp256k1"] }
enr = { version = "0.7.0", default-features = false, features = [
"rust-secp256k1",
] }

# async/futures
tokio = { version = "1", features = ["io-util", "net", "time"] }
Expand All @@ -37,6 +39,7 @@ thiserror = "1.0"
hex = "0.4"
rand = { version = "0.8", optional = true }
generic-array = "0.14"
serde = "1.0"

[dev-dependencies]
rand = "0.8"
Expand Down
6 changes: 4 additions & 2 deletions crates/net/discv4/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use reth_net_common::ban_list::BanList;
use reth_net_nat::{NatResolver, ResolveNatInterval};
use reth_primitives::NodeRecord;
use reth_rlp::Encodable;
use secp256k1::serde::{Deserialize, Serialize};
use std::{
collections::{HashMap, HashSet},
time::Duration,
};

/// Configuration parameters that define the performance of the discovery network.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Discv4Config {
/// Whether to enable the incoming packet filter. Default: false.
pub enable_packet_filter: bool,
Expand All @@ -38,6 +39,7 @@ pub struct Discv4Config {
/// The duration we set for neighbours responses
pub neighbours_expiration: Duration,
/// Provides a way to ban peers and ips.
#[serde(skip)]
pub ban_list: BanList,
/// Set the default duration for which nodes are banned for. This timeouts are checked every 5
/// minutes, so the precision will be to the nearest 5 minutes. If set to `None`, bans from
Expand Down Expand Up @@ -135,7 +137,7 @@ impl Default for Discv4Config {
}

/// Builder type for [`Discv4Config`]
#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Discv4ConfigBuilder {
config: Discv4Config,
}
Expand Down
3 changes: 2 additions & 1 deletion crates/net/dns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ lru = "0.9"
thiserror = "1.0"
tracing = "0.1"
parking_lot = "0.12"

serde = "1.0"
serde_with = "2.1.0"

[dev-dependencies]
tokio = { version = "1", features = ["sync", "rt", "rt-multi-thread"] }
Expand Down
4 changes: 3 additions & 1 deletion crates/net/dns/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use serde::{Deserialize, Serialize};

use crate::tree::LinkEntry;
use std::{collections::HashSet, num::NonZeroUsize, time::Duration};

/// Settings for the [DnsDiscoveryClient](crate::DnsDiscoveryClient).
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DnsDiscoveryConfig {
/// Timeout for DNS lookups.
///
Expand Down
3 changes: 2 additions & 1 deletion crates/net/dns/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use data_encoding::{BASE32_NOPAD, BASE64URL_NOPAD};
use enr::{Enr, EnrError, EnrKey, EnrKeyUnambiguous, EnrPublicKey};
use reth_primitives::hex;
use secp256k1::SecretKey;
use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::{fmt, str::FromStr};

const ROOT_V1_PREFIX: &str = "enrtree-root:v1";
Expand Down Expand Up @@ -202,7 +203,7 @@ impl fmt::Display for BranchEntry {
}

/// A link entry
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
#[derive(Debug, Clone, Hash, Eq, PartialEq, SerializeDisplay, DeserializeFromStr)]
pub struct LinkEntry<K: EnrKeyUnambiguous = SecretKey> {
pub domain: String,
pub pubkey: K::PublicKey,
Expand Down
1 change: 1 addition & 0 deletions crates/net/nat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tracing = "0.1"
pin-project-lite = "0.2.9"
tokio = { version = "1", features = ["time"] }
thiserror = "1.0"
serde_with = "2.1.0"

[dev-dependencies]
reth-tracing = { path = "../../tracing" }
Expand Down
5 changes: 4 additions & 1 deletion crates/net/nat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use igd::aio::search_gateway;
use pin_project_lite::pin_project;
use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::{
fmt,
future::{poll_fn, Future},
Expand All @@ -21,7 +22,9 @@ use std::{
use tracing::warn;

/// All builtin resolvers.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default, Hash)]
#[derive(
Debug, Clone, Copy, Eq, PartialEq, Default, Hash, SerializeDisplay, DeserializeFromStr,
)]
pub enum NatResolver {
/// Resolve with any available resolver.
#[default]
Expand Down
1 change: 1 addition & 0 deletions crates/net/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ secp256k1 = { version = "0.24", features = [
"global-context",
"rand-std",
"recovery",
"serde",
] }

enr = { version = "0.7.0", features = ["serde", "rust-secp256k1"], optional = true }
Expand Down
42 changes: 16 additions & 26 deletions crates/net/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use reth_primitives::{ChainSpec, ForkFilter, NodeRecord, PeerId, MAINNET};
use reth_provider::{BlockProvider, HeaderProvider};
use reth_tasks::TaskExecutor;
use secp256k1::{SecretKey, SECP256K1};
use serde::{Deserialize, Serialize};
use std::{
collections::HashSet,
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
Expand Down Expand Up @@ -80,12 +81,12 @@ pub struct NetworkConfig<C> {
impl<C> NetworkConfig<C> {
/// Create a new instance with all mandatory fields set, rest is field with defaults.
pub fn new(client: Arc<C>, secret_key: SecretKey) -> Self {
Self::builder(client, secret_key).build()
Self::builder(secret_key).build(client)
}

/// Convenience method for creating the corresponding builder type
pub fn builder(client: Arc<C>, secret_key: SecretKey) -> NetworkConfigBuilder<C> {
NetworkConfigBuilder::new(client, secret_key)
pub fn builder(secret_key: SecretKey) -> NetworkConfigBuilder {
NetworkConfigBuilder::new(secret_key)
}

/// Sets the config to use for the discovery v4 protocol.
Expand Down Expand Up @@ -119,10 +120,9 @@ where
}

/// Builder for [`NetworkConfig`](struct.NetworkConfig.html).
#[derive(Debug, Serialize, Deserialize)]
#[allow(missing_docs)]
pub struct NetworkConfigBuilder<C> {
/// The client type that can interact with the chain.
client: Arc<C>,
pub struct NetworkConfigBuilder {
/// The node's secret key, from which the node's identity is derived.
secret_key: SecretKey,
/// How to configure discovery over DNS.
Expand All @@ -141,11 +141,10 @@ pub struct NetworkConfigBuilder<C> {
sessions_config: Option<SessionsConfig>,
/// The network's chain spec
chain_spec: ChainSpec,
/// The block importer type.
block_import: Box<dyn BlockImport>,
/// The default mode of the network.
network_mode: NetworkMode,
/// The executor to use for spawning tasks.
#[serde(skip)]
executor: Option<TaskExecutor>,
/// The `Status` message to send to peers at the beginning.
status: Option<Status>,
Expand All @@ -160,10 +159,9 @@ pub struct NetworkConfigBuilder<C> {
// === impl NetworkConfigBuilder ===

#[allow(missing_docs)]
impl<C> NetworkConfigBuilder<C> {
pub fn new(client: Arc<C>, secret_key: SecretKey) -> Self {
impl NetworkConfigBuilder {
pub fn new(secret_key: SecretKey) -> Self {
Self {
client,
secret_key,
dns_discovery_config: Some(Default::default()),
discovery_v4_builder: Some(Default::default()),
Expand All @@ -173,7 +171,6 @@ impl<C> NetworkConfigBuilder<C> {
peers_config: None,
sessions_config: None,
chain_spec: MAINNET.clone(),
block_import: Box::<ProofOfStakeBlockImport>::default(),
network_mode: Default::default(),
executor: None,
status: None,
Expand Down Expand Up @@ -245,12 +242,6 @@ impl<C> NetworkConfigBuilder<C> {
self
}

/// Sets the [`BlockImport`] type to configure.
pub fn block_import<T: BlockImport + 'static>(mut self, block_import: T) -> Self {
self.block_import = Box::new(block_import);
self
}

/// Sets the socket address the network will listen on
pub fn listener_addr(mut self, listener_addr: SocketAddr) -> Self {
self.listener_addr = Some(listener_addr);
Expand Down Expand Up @@ -295,10 +286,10 @@ impl<C> NetworkConfigBuilder<C> {
}

/// Consumes the type and creates the actual [`NetworkConfig`]
pub fn build(self) -> NetworkConfig<C> {
/// for the given client type that can interact with the chain.
pub fn build<C>(self, client: Arc<C>) -> NetworkConfig<C> {
let peer_id = self.get_peer_id();
let Self {
client,
secret_key,
mut dns_discovery_config,
discovery_v4_builder,
Expand All @@ -308,7 +299,6 @@ impl<C> NetworkConfigBuilder<C> {
peers_config,
sessions_config,
chain_spec,
block_import,
network_mode,
executor,
status,
Expand Down Expand Up @@ -355,7 +345,7 @@ impl<C> NetworkConfigBuilder<C> {
peers_config: peers_config.unwrap_or_default(),
sessions_config: sessions_config.unwrap_or_default(),
chain_spec,
block_import,
block_import: Box::<ProofOfStakeBlockImport>::default(),
network_mode,
executor,
status: status.unwrap_or_default(),
Expand All @@ -370,7 +360,7 @@ impl<C> NetworkConfigBuilder<C> {
/// This affects block propagation in the `eth` sub-protocol [EIP-3675](https://eips.ethereum.org/EIPS/eip-3675#devp2p)
///
/// In POS `NewBlockHashes` and `NewBlock` messages become invalid.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default, Serialize, Deserialize)]
pub enum NetworkMode {
/// Network is in proof-of-work mode.
Work,
Expand All @@ -396,14 +386,14 @@ mod tests {
use reth_primitives::Chain;
use reth_provider::test_utils::NoopProvider;

fn builder() -> NetworkConfigBuilder<NoopProvider> {
fn builder() -> NetworkConfigBuilder {
let secret_key = SecretKey::new(&mut thread_rng());
NetworkConfig::builder(Arc::new(NoopProvider::default()), secret_key)
NetworkConfigBuilder::new(secret_key)
}

#[test]
fn test_network_dns_defaults() {
let config = builder().build();
let config = builder().build(Arc::new(NoopProvider::default()));

let dns = config.dns_discovery_config.unwrap();
let bootstrap_nodes = dns.bootstrap_dns_networks.unwrap();
Expand Down
3 changes: 3 additions & 0 deletions crates/net/network/src/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::time::Duration;
pub const INITIAL_REQUEST_TIMEOUT: Duration = Duration::from_secs(20);

/// Configuration options when creating a [SessionManager](crate::session::SessionManager).
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SessionsConfig {
/// Size of the session command buffer (per session task).
pub session_command_buffer: usize,
Expand Down Expand Up @@ -55,6 +57,7 @@ impl SessionsConfig {
///
/// By default, no session limits will be enforced
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SessionLimits {
max_pending_inbound: Option<u32>,
max_pending_outbound: Option<u32>,
Expand Down
8 changes: 4 additions & 4 deletions crates/net/network/src/test_utils/testnet.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! A network implementation for testing purposes.
use crate::{
error::NetworkError, eth_requests::EthRequestHandler, NetworkConfig, NetworkEvent,
NetworkHandle, NetworkManager,
error::NetworkError, eth_requests::EthRequestHandler, NetworkConfig, NetworkConfigBuilder,
NetworkEvent, NetworkHandle, NetworkManager,
};
use futures::{FutureExt, StreamExt};
use pin_project::pin_project;
Expand Down Expand Up @@ -287,10 +287,10 @@ where
/// Initialize the network with a given secret key, allowing devp2p and discovery to bind any
/// available IP and port.
pub fn with_secret_key(client: Arc<C>, secret_key: SecretKey) -> Self {
let config = NetworkConfig::builder(Arc::clone(&client), secret_key)
let config = NetworkConfigBuilder::new(secret_key)
.listener_addr(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0)))
.discovery_addr(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0)))
.build();
.build(Arc::clone(&client));
Self { config, client, secret_key }
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/net/network/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ pub enum NetworkTransactionEvent {
#[cfg(test)]
mod tests {
use super::*;
use crate::{NetworkConfig, NetworkManager};
use crate::{NetworkConfigBuilder, NetworkManager};
use reth_interfaces::sync::{SyncState, SyncStateUpdater};
use reth_provider::test_utils::NoopProvider;
use reth_transaction_pool::test_utils::testing_pool;
Expand All @@ -572,7 +572,7 @@ mod tests {

let client = Arc::new(NoopProvider::default());
let pool = testing_pool();
let config = NetworkConfig::builder(Arc::clone(&client), secret_key).build();
let config = NetworkConfigBuilder::new(secret_key).build(Arc::clone(&client));
let (handle, network, mut transactions, _) = NetworkManager::new(config)
.await
.unwrap()
Expand Down
Loading

0 comments on commit 115e623

Please sign in to comment.