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

Ability to (de)serialize NetworkConfigBuilder #897

Merged
merged 6 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 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 @@ -4,7 +4,7 @@ use std::sync::Arc;
use reth_db::database::Database;
use reth_network::{
config::{mainnet_nodes, rng_secret_key},
NetworkConfig, PeersConfig,
NetworkConfig, NetworkConfigBuilder, PeersConfig,
};
use reth_primitives::{NodeRecord, H256};
use reth_provider::ProviderImpl;
Expand Down Expand Up @@ -34,13 +34,13 @@ impl Config {
let peer_config = reth_network::PeersConfig::default()
.with_trusted_nodes(self.peers.trusted_nodes.clone())
.with_connect_trusted_nodes_only(self.peers.connect_trusted_nodes_only);
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)
.genesis_hash(genesis_hash)
.chain_id(chain_id)
.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(Debug, Default)]
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Discv4ConfigBuilder {
config: Discv4Config,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/net/dns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ lru = "0.9"
thiserror = "1.0"
tracing = "0.1"
parking_lot = "0.12"

serde = "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::{Deserialize, Serialize};
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, Serialize, Deserialize)]
leruaa marked this conversation as resolved.
Show resolved Hide resolved
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 = "1.0"

[dev-dependencies]
reth-tracing = { path = "../../tracing" }
Expand Down
3 changes: 2 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::{Serialize, Deserialize};
use std::{
fmt,
future::{poll_fn, Future},
Expand All @@ -21,7 +22,7 @@ use std::{
use tracing::warn;

/// All builtin resolvers.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default, Hash)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default, Hash, Serialize, Deserialize)]
leruaa marked this conversation as resolved.
Show resolved Hide resolved
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",
] }

[dev-dependencies]
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::{Chain, ForkFilter, Hardfork, NodeRecord, PeerId, H256, MAI
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 @@ -82,12 +83,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 @@ -121,10 +122,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 @@ -145,11 +145,10 @@ pub struct NetworkConfigBuilder<C> {
chain: Chain,
/// Network genesis hash
genesis_hash: H256,
/// 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 @@ -164,10 +163,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 @@ -178,7 +176,6 @@ impl<C> NetworkConfigBuilder<C> {
sessions_config: None,
chain: Chain::Named(reth_primitives::rpc::Chain::Mainnet),
genesis_hash: MAINNET_GENESIS,
block_import: Box::<ProofOfStakeBlockImport>::default(),
network_mode: Default::default(),
executor: None,
status: None,
Expand Down Expand Up @@ -256,12 +253,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 @@ -306,10 +297,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 @@ -320,7 +311,6 @@ impl<C> NetworkConfigBuilder<C> {
sessions_config,
chain,
genesis_hash,
block_import,
network_mode,
executor,
status,
Expand Down Expand Up @@ -369,7 +359,7 @@ impl<C> NetworkConfigBuilder<C> {
sessions_config: sessions_config.unwrap_or_default(),
chain,
genesis_hash,
block_import,
block_import: Box::<ProofOfStakeBlockImport>::default(),
network_mode,
executor,
status: status.unwrap_or_default(),
Expand All @@ -384,7 +374,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 @@ -409,14 +399,14 @@ mod tests {
use reth_dns_discovery::tree::LinkEntry;
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
5 changes: 4 additions & 1 deletion crates/net/network/src/session/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Configuration types for [SessionManager](crate::session::SessionManager).

use serde::{Deserialize, Serialize};

use crate::session::{Direction, ExceedsSessionLimit};
use std::time::Duration;

Expand All @@ -9,6 +11,7 @@ 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, Serialize, Deserialize)]
leruaa marked this conversation as resolved.
Show resolved Hide resolved
pub struct SessionsConfig {
/// Size of the session command buffer (per session task).
pub session_command_buffer: usize,
Expand Down Expand Up @@ -54,7 +57,7 @@ impl SessionsConfig {
/// Limits for sessions.
///
/// By default, no session limits will be enforced
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
leruaa marked this conversation as resolved.
Show resolved Hide resolved
pub struct SessionLimits {
max_pending_inbound: Option<u32>,
max_pending_outbound: Option<u32>,
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