mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
Ability to (de)serialize NetworkConfigBuilder (#897)
This commit is contained in:
5
Cargo.lock
generated
5
Cargo.lock
generated
@ -4056,6 +4056,7 @@ dependencies = [
|
|||||||
"reth-rlp-derive",
|
"reth-rlp-derive",
|
||||||
"reth-tracing",
|
"reth-tracing",
|
||||||
"secp256k1 0.24.2",
|
"secp256k1 0.24.2",
|
||||||
|
"serde",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-stream",
|
"tokio-stream",
|
||||||
@ -4078,6 +4079,8 @@ dependencies = [
|
|||||||
"reth-rlp",
|
"reth-rlp",
|
||||||
"reth-tracing",
|
"reth-tracing",
|
||||||
"secp256k1 0.24.2",
|
"secp256k1 0.24.2",
|
||||||
|
"serde",
|
||||||
|
"serde_with",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-stream",
|
"tokio-stream",
|
||||||
@ -4302,6 +4305,7 @@ dependencies = [
|
|||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"public-ip",
|
"public-ip",
|
||||||
"reth-tracing",
|
"reth-tracing",
|
||||||
|
"serde_with",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
@ -4919,6 +4923,7 @@ checksum = "d9512ffd81e3a3503ed401f79c33168b9148c75038956039166cd750eaa037c3"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
"secp256k1-sys 0.6.1",
|
"secp256k1-sys 0.6.1",
|
||||||
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@ -5,7 +5,7 @@ use reth_db::database::Database;
|
|||||||
use reth_discv4::Discv4Config;
|
use reth_discv4::Discv4Config;
|
||||||
use reth_network::{
|
use reth_network::{
|
||||||
config::{mainnet_nodes, rng_secret_key},
|
config::{mainnet_nodes, rng_secret_key},
|
||||||
NetworkConfig, PeersConfig,
|
NetworkConfig, NetworkConfigBuilder, PeersConfig,
|
||||||
};
|
};
|
||||||
use reth_primitives::{ChainSpec, NodeRecord};
|
use reth_primitives::{ChainSpec, NodeRecord};
|
||||||
use reth_provider::ProviderImpl;
|
use reth_provider::ProviderImpl;
|
||||||
@ -37,13 +37,13 @@ impl Config {
|
|||||||
.with_connect_trusted_nodes_only(self.peers.connect_trusted_nodes_only);
|
.with_connect_trusted_nodes_only(self.peers.connect_trusted_nodes_only);
|
||||||
let discv4 =
|
let discv4 =
|
||||||
Discv4Config::builder().external_ip_resolver(Some(nat_resolution_method)).clone();
|
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))
|
.boot_nodes(bootnodes.unwrap_or_else(mainnet_nodes))
|
||||||
.peer_config(peer_config)
|
.peer_config(peer_config)
|
||||||
.discovery(discv4)
|
.discovery(discv4)
|
||||||
.chain_spec(chain_spec)
|
.chain_spec(chain_spec)
|
||||||
.set_discovery(disable_discovery)
|
.set_discovery(disable_discovery)
|
||||||
.build()
|
.build(Arc::new(ProviderImpl::new(db)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -24,7 +24,9 @@ secp256k1 = { version = "0.24", features = [
|
|||||||
"rand-std",
|
"rand-std",
|
||||||
"recovery",
|
"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
|
# async/futures
|
||||||
tokio = { version = "1", features = ["io-util", "net", "time"] }
|
tokio = { version = "1", features = ["io-util", "net", "time"] }
|
||||||
@ -37,6 +39,7 @@ thiserror = "1.0"
|
|||||||
hex = "0.4"
|
hex = "0.4"
|
||||||
rand = { version = "0.8", optional = true }
|
rand = { version = "0.8", optional = true }
|
||||||
generic-array = "0.14"
|
generic-array = "0.14"
|
||||||
|
serde = "1.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
|
|||||||
@ -8,13 +8,14 @@ use reth_net_common::ban_list::BanList;
|
|||||||
use reth_net_nat::{NatResolver, ResolveNatInterval};
|
use reth_net_nat::{NatResolver, ResolveNatInterval};
|
||||||
use reth_primitives::NodeRecord;
|
use reth_primitives::NodeRecord;
|
||||||
use reth_rlp::Encodable;
|
use reth_rlp::Encodable;
|
||||||
|
use secp256k1::serde::{Deserialize, Serialize};
|
||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Configuration parameters that define the performance of the discovery network.
|
/// Configuration parameters that define the performance of the discovery network.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
pub struct Discv4Config {
|
pub struct Discv4Config {
|
||||||
/// Whether to enable the incoming packet filter. Default: false.
|
/// Whether to enable the incoming packet filter. Default: false.
|
||||||
pub enable_packet_filter: bool,
|
pub enable_packet_filter: bool,
|
||||||
@ -38,6 +39,7 @@ pub struct Discv4Config {
|
|||||||
/// The duration we set for neighbours responses
|
/// The duration we set for neighbours responses
|
||||||
pub neighbours_expiration: Duration,
|
pub neighbours_expiration: Duration,
|
||||||
/// Provides a way to ban peers and ips.
|
/// Provides a way to ban peers and ips.
|
||||||
|
#[serde(skip)]
|
||||||
pub ban_list: BanList,
|
pub ban_list: BanList,
|
||||||
/// Set the default duration for which nodes are banned for. This timeouts are checked every 5
|
/// 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
|
/// minutes, so the precision will be to the nearest 5 minutes. If set to `None`, bans from
|
||||||
@ -135,7 +137,7 @@ impl Default for Discv4Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Builder type for [`Discv4Config`]
|
/// Builder type for [`Discv4Config`]
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
||||||
pub struct Discv4ConfigBuilder {
|
pub struct Discv4ConfigBuilder {
|
||||||
config: Discv4Config,
|
config: Discv4Config,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,7 +37,8 @@ lru = "0.9"
|
|||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
parking_lot = "0.12"
|
parking_lot = "0.12"
|
||||||
|
serde = "1.0"
|
||||||
|
serde_with = "2.1.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = { version = "1", features = ["sync", "rt", "rt-multi-thread"] }
|
tokio = { version = "1", features = ["sync", "rt", "rt-multi-thread"] }
|
||||||
|
|||||||
@ -1,8 +1,10 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::tree::LinkEntry;
|
use crate::tree::LinkEntry;
|
||||||
use std::{collections::HashSet, num::NonZeroUsize, time::Duration};
|
use std::{collections::HashSet, num::NonZeroUsize, time::Duration};
|
||||||
|
|
||||||
/// Settings for the [DnsDiscoveryClient](crate::DnsDiscoveryClient).
|
/// Settings for the [DnsDiscoveryClient](crate::DnsDiscoveryClient).
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct DnsDiscoveryConfig {
|
pub struct DnsDiscoveryConfig {
|
||||||
/// Timeout for DNS lookups.
|
/// Timeout for DNS lookups.
|
||||||
///
|
///
|
||||||
|
|||||||
@ -28,6 +28,7 @@ use data_encoding::{BASE32_NOPAD, BASE64URL_NOPAD};
|
|||||||
use enr::{Enr, EnrError, EnrKey, EnrKeyUnambiguous, EnrPublicKey};
|
use enr::{Enr, EnrError, EnrKey, EnrKeyUnambiguous, EnrPublicKey};
|
||||||
use reth_primitives::hex;
|
use reth_primitives::hex;
|
||||||
use secp256k1::SecretKey;
|
use secp256k1::SecretKey;
|
||||||
|
use serde_with::{DeserializeFromStr, SerializeDisplay};
|
||||||
use std::{fmt, str::FromStr};
|
use std::{fmt, str::FromStr};
|
||||||
|
|
||||||
const ROOT_V1_PREFIX: &str = "enrtree-root:v1";
|
const ROOT_V1_PREFIX: &str = "enrtree-root:v1";
|
||||||
@ -202,7 +203,7 @@ impl fmt::Display for BranchEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A link entry
|
/// A link entry
|
||||||
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
|
#[derive(Debug, Clone, Hash, Eq, PartialEq, SerializeDisplay, DeserializeFromStr)]
|
||||||
pub struct LinkEntry<K: EnrKeyUnambiguous = SecretKey> {
|
pub struct LinkEntry<K: EnrKeyUnambiguous = SecretKey> {
|
||||||
pub domain: String,
|
pub domain: String,
|
||||||
pub pubkey: K::PublicKey,
|
pub pubkey: K::PublicKey,
|
||||||
|
|||||||
@ -23,6 +23,7 @@ tracing = "0.1"
|
|||||||
pin-project-lite = "0.2.9"
|
pin-project-lite = "0.2.9"
|
||||||
tokio = { version = "1", features = ["time"] }
|
tokio = { version = "1", features = ["time"] }
|
||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
|
serde_with = "2.1.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
reth-tracing = { path = "../../tracing" }
|
reth-tracing = { path = "../../tracing" }
|
||||||
|
|||||||
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
use igd::aio::search_gateway;
|
use igd::aio::search_gateway;
|
||||||
use pin_project_lite::pin_project;
|
use pin_project_lite::pin_project;
|
||||||
|
use serde_with::{DeserializeFromStr, SerializeDisplay};
|
||||||
use std::{
|
use std::{
|
||||||
fmt,
|
fmt,
|
||||||
future::{poll_fn, Future},
|
future::{poll_fn, Future},
|
||||||
@ -21,7 +22,9 @@ use std::{
|
|||||||
use tracing::warn;
|
use tracing::warn;
|
||||||
|
|
||||||
/// All builtin resolvers.
|
/// All builtin resolvers.
|
||||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default, Hash)]
|
#[derive(
|
||||||
|
Debug, Clone, Copy, Eq, PartialEq, Default, Hash, SerializeDisplay, DeserializeFromStr,
|
||||||
|
)]
|
||||||
pub enum NatResolver {
|
pub enum NatResolver {
|
||||||
/// Resolve with any available resolver.
|
/// Resolve with any available resolver.
|
||||||
#[default]
|
#[default]
|
||||||
|
|||||||
@ -61,6 +61,7 @@ secp256k1 = { version = "0.24", features = [
|
|||||||
"global-context",
|
"global-context",
|
||||||
"rand-std",
|
"rand-std",
|
||||||
"recovery",
|
"recovery",
|
||||||
|
"serde",
|
||||||
] }
|
] }
|
||||||
|
|
||||||
enr = { version = "0.7.0", features = ["serde", "rust-secp256k1"], optional = true }
|
enr = { version = "0.7.0", features = ["serde", "rust-secp256k1"], optional = true }
|
||||||
|
|||||||
@ -12,6 +12,7 @@ use reth_primitives::{ChainSpec, ForkFilter, NodeRecord, PeerId, MAINNET};
|
|||||||
use reth_provider::{BlockProvider, HeaderProvider};
|
use reth_provider::{BlockProvider, HeaderProvider};
|
||||||
use reth_tasks::TaskExecutor;
|
use reth_tasks::TaskExecutor;
|
||||||
use secp256k1::{SecretKey, SECP256K1};
|
use secp256k1::{SecretKey, SECP256K1};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashSet,
|
collections::HashSet,
|
||||||
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
|
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
|
||||||
@ -80,12 +81,12 @@ pub struct NetworkConfig<C> {
|
|||||||
impl<C> NetworkConfig<C> {
|
impl<C> NetworkConfig<C> {
|
||||||
/// Create a new instance with all mandatory fields set, rest is field with defaults.
|
/// Create a new instance with all mandatory fields set, rest is field with defaults.
|
||||||
pub fn new(client: Arc<C>, secret_key: SecretKey) -> Self {
|
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
|
/// Convenience method for creating the corresponding builder type
|
||||||
pub fn builder(client: Arc<C>, secret_key: SecretKey) -> NetworkConfigBuilder<C> {
|
pub fn builder(secret_key: SecretKey) -> NetworkConfigBuilder {
|
||||||
NetworkConfigBuilder::new(client, secret_key)
|
NetworkConfigBuilder::new(secret_key)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the config to use for the discovery v4 protocol.
|
/// Sets the config to use for the discovery v4 protocol.
|
||||||
@ -119,10 +120,9 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Builder for [`NetworkConfig`](struct.NetworkConfig.html).
|
/// Builder for [`NetworkConfig`](struct.NetworkConfig.html).
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
pub struct NetworkConfigBuilder<C> {
|
pub struct NetworkConfigBuilder {
|
||||||
/// The client type that can interact with the chain.
|
|
||||||
client: Arc<C>,
|
|
||||||
/// The node's secret key, from which the node's identity is derived.
|
/// The node's secret key, from which the node's identity is derived.
|
||||||
secret_key: SecretKey,
|
secret_key: SecretKey,
|
||||||
/// How to configure discovery over DNS.
|
/// How to configure discovery over DNS.
|
||||||
@ -141,11 +141,10 @@ pub struct NetworkConfigBuilder<C> {
|
|||||||
sessions_config: Option<SessionsConfig>,
|
sessions_config: Option<SessionsConfig>,
|
||||||
/// The network's chain spec
|
/// The network's chain spec
|
||||||
chain_spec: ChainSpec,
|
chain_spec: ChainSpec,
|
||||||
/// The block importer type.
|
|
||||||
block_import: Box<dyn BlockImport>,
|
|
||||||
/// The default mode of the network.
|
/// The default mode of the network.
|
||||||
network_mode: NetworkMode,
|
network_mode: NetworkMode,
|
||||||
/// The executor to use for spawning tasks.
|
/// The executor to use for spawning tasks.
|
||||||
|
#[serde(skip)]
|
||||||
executor: Option<TaskExecutor>,
|
executor: Option<TaskExecutor>,
|
||||||
/// The `Status` message to send to peers at the beginning.
|
/// The `Status` message to send to peers at the beginning.
|
||||||
status: Option<Status>,
|
status: Option<Status>,
|
||||||
@ -160,10 +159,9 @@ pub struct NetworkConfigBuilder<C> {
|
|||||||
// === impl NetworkConfigBuilder ===
|
// === impl NetworkConfigBuilder ===
|
||||||
|
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
impl<C> NetworkConfigBuilder<C> {
|
impl NetworkConfigBuilder {
|
||||||
pub fn new(client: Arc<C>, secret_key: SecretKey) -> Self {
|
pub fn new(secret_key: SecretKey) -> Self {
|
||||||
Self {
|
Self {
|
||||||
client,
|
|
||||||
secret_key,
|
secret_key,
|
||||||
dns_discovery_config: Some(Default::default()),
|
dns_discovery_config: Some(Default::default()),
|
||||||
discovery_v4_builder: Some(Default::default()),
|
discovery_v4_builder: Some(Default::default()),
|
||||||
@ -173,7 +171,6 @@ impl<C> NetworkConfigBuilder<C> {
|
|||||||
peers_config: None,
|
peers_config: None,
|
||||||
sessions_config: None,
|
sessions_config: None,
|
||||||
chain_spec: MAINNET.clone(),
|
chain_spec: MAINNET.clone(),
|
||||||
block_import: Box::<ProofOfStakeBlockImport>::default(),
|
|
||||||
network_mode: Default::default(),
|
network_mode: Default::default(),
|
||||||
executor: None,
|
executor: None,
|
||||||
status: None,
|
status: None,
|
||||||
@ -245,12 +242,6 @@ impl<C> NetworkConfigBuilder<C> {
|
|||||||
self
|
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
|
/// Sets the socket address the network will listen on
|
||||||
pub fn listener_addr(mut self, listener_addr: SocketAddr) -> Self {
|
pub fn listener_addr(mut self, listener_addr: SocketAddr) -> Self {
|
||||||
self.listener_addr = Some(listener_addr);
|
self.listener_addr = Some(listener_addr);
|
||||||
@ -295,10 +286,10 @@ impl<C> NetworkConfigBuilder<C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Consumes the type and creates the actual [`NetworkConfig`]
|
/// 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 peer_id = self.get_peer_id();
|
||||||
let Self {
|
let Self {
|
||||||
client,
|
|
||||||
secret_key,
|
secret_key,
|
||||||
mut dns_discovery_config,
|
mut dns_discovery_config,
|
||||||
discovery_v4_builder,
|
discovery_v4_builder,
|
||||||
@ -308,7 +299,6 @@ impl<C> NetworkConfigBuilder<C> {
|
|||||||
peers_config,
|
peers_config,
|
||||||
sessions_config,
|
sessions_config,
|
||||||
chain_spec,
|
chain_spec,
|
||||||
block_import,
|
|
||||||
network_mode,
|
network_mode,
|
||||||
executor,
|
executor,
|
||||||
status,
|
status,
|
||||||
@ -355,7 +345,7 @@ impl<C> NetworkConfigBuilder<C> {
|
|||||||
peers_config: peers_config.unwrap_or_default(),
|
peers_config: peers_config.unwrap_or_default(),
|
||||||
sessions_config: sessions_config.unwrap_or_default(),
|
sessions_config: sessions_config.unwrap_or_default(),
|
||||||
chain_spec,
|
chain_spec,
|
||||||
block_import,
|
block_import: Box::<ProofOfStakeBlockImport>::default(),
|
||||||
network_mode,
|
network_mode,
|
||||||
executor,
|
executor,
|
||||||
status: status.unwrap_or_default(),
|
status: status.unwrap_or_default(),
|
||||||
@ -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)
|
/// 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.
|
/// 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 {
|
pub enum NetworkMode {
|
||||||
/// Network is in proof-of-work mode.
|
/// Network is in proof-of-work mode.
|
||||||
Work,
|
Work,
|
||||||
@ -396,14 +386,14 @@ mod tests {
|
|||||||
use reth_primitives::Chain;
|
use reth_primitives::Chain;
|
||||||
use reth_provider::test_utils::NoopProvider;
|
use reth_provider::test_utils::NoopProvider;
|
||||||
|
|
||||||
fn builder() -> NetworkConfigBuilder<NoopProvider> {
|
fn builder() -> NetworkConfigBuilder {
|
||||||
let secret_key = SecretKey::new(&mut thread_rng());
|
let secret_key = SecretKey::new(&mut thread_rng());
|
||||||
NetworkConfig::builder(Arc::new(NoopProvider::default()), secret_key)
|
NetworkConfigBuilder::new(secret_key)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_network_dns_defaults() {
|
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 dns = config.dns_discovery_config.unwrap();
|
||||||
let bootstrap_nodes = dns.bootstrap_dns_networks.unwrap();
|
let bootstrap_nodes = dns.bootstrap_dns_networks.unwrap();
|
||||||
|
|||||||
@ -9,6 +9,8 @@ use std::time::Duration;
|
|||||||
pub const INITIAL_REQUEST_TIMEOUT: Duration = Duration::from_secs(20);
|
pub const INITIAL_REQUEST_TIMEOUT: Duration = Duration::from_secs(20);
|
||||||
|
|
||||||
/// Configuration options when creating a [SessionManager](crate::session::SessionManager).
|
/// Configuration options when creating a [SessionManager](crate::session::SessionManager).
|
||||||
|
#[derive(Debug)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct SessionsConfig {
|
pub struct SessionsConfig {
|
||||||
/// Size of the session command buffer (per session task).
|
/// Size of the session command buffer (per session task).
|
||||||
pub session_command_buffer: usize,
|
pub session_command_buffer: usize,
|
||||||
@ -55,6 +57,7 @@ impl SessionsConfig {
|
|||||||
///
|
///
|
||||||
/// By default, no session limits will be enforced
|
/// By default, no session limits will be enforced
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct SessionLimits {
|
pub struct SessionLimits {
|
||||||
max_pending_inbound: Option<u32>,
|
max_pending_inbound: Option<u32>,
|
||||||
max_pending_outbound: Option<u32>,
|
max_pending_outbound: Option<u32>,
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
//! A network implementation for testing purposes.
|
//! A network implementation for testing purposes.
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
error::NetworkError, eth_requests::EthRequestHandler, NetworkConfig, NetworkEvent,
|
error::NetworkError, eth_requests::EthRequestHandler, NetworkConfig, NetworkConfigBuilder,
|
||||||
NetworkHandle, NetworkManager,
|
NetworkEvent, NetworkHandle, NetworkManager,
|
||||||
};
|
};
|
||||||
use futures::{FutureExt, StreamExt};
|
use futures::{FutureExt, StreamExt};
|
||||||
use pin_project::pin_project;
|
use pin_project::pin_project;
|
||||||
@ -287,10 +287,10 @@ where
|
|||||||
/// Initialize the network with a given secret key, allowing devp2p and discovery to bind any
|
/// Initialize the network with a given secret key, allowing devp2p and discovery to bind any
|
||||||
/// available IP and port.
|
/// available IP and port.
|
||||||
pub fn with_secret_key(client: Arc<C>, secret_key: SecretKey) -> Self {
|
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)))
|
.listener_addr(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0)))
|
||||||
.discovery_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 }
|
Self { config, client, secret_key }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -558,7 +558,7 @@ pub enum NetworkTransactionEvent {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::{NetworkConfig, NetworkManager};
|
use crate::{NetworkConfigBuilder, NetworkManager};
|
||||||
use reth_interfaces::sync::{SyncState, SyncStateUpdater};
|
use reth_interfaces::sync::{SyncState, SyncStateUpdater};
|
||||||
use reth_provider::test_utils::NoopProvider;
|
use reth_provider::test_utils::NoopProvider;
|
||||||
use reth_transaction_pool::test_utils::testing_pool;
|
use reth_transaction_pool::test_utils::testing_pool;
|
||||||
@ -572,7 +572,7 @@ mod tests {
|
|||||||
|
|
||||||
let client = Arc::new(NoopProvider::default());
|
let client = Arc::new(NoopProvider::default());
|
||||||
let pool = testing_pool();
|
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)
|
let (handle, network, mut transactions, _) = NetworkManager::new(config)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|||||||
@ -16,7 +16,7 @@ use reth_network::{
|
|||||||
test_utils::{
|
test_utils::{
|
||||||
enr_to_peer_id, unused_tcp_udp, NetworkEventStream, PeerConfig, Testnet, GETH_TIMEOUT,
|
enr_to_peer_id, unused_tcp_udp, NetworkEventStream, PeerConfig, Testnet, GETH_TIMEOUT,
|
||||||
},
|
},
|
||||||
NetworkConfig, NetworkEvent, NetworkManager, PeersConfig,
|
NetworkConfigBuilder, NetworkEvent, NetworkManager, PeersConfig,
|
||||||
};
|
};
|
||||||
use reth_network_api::{NetworkInfo, PeersInfo};
|
use reth_network_api::{NetworkInfo, PeersInfo};
|
||||||
use reth_primitives::{HeadersDirection, NodeRecord, PeerId};
|
use reth_primitives::{HeadersDirection, NodeRecord, PeerId};
|
||||||
@ -207,9 +207,9 @@ async fn test_connect_with_boot_nodes() {
|
|||||||
let mut discv4 = Discv4Config::builder();
|
let mut discv4 = Discv4Config::builder();
|
||||||
discv4.add_boot_nodes(mainnet_nodes());
|
discv4.add_boot_nodes(mainnet_nodes());
|
||||||
|
|
||||||
let config = NetworkConfig::builder(Arc::new(NoopProvider::default()), secret_key)
|
let config = NetworkConfigBuilder::new(secret_key)
|
||||||
.discovery(discv4)
|
.discovery(discv4)
|
||||||
.build();
|
.build(Arc::new(NoopProvider::default()));
|
||||||
let network = NetworkManager::new(config).await.unwrap();
|
let network = NetworkManager::new(config).await.unwrap();
|
||||||
|
|
||||||
let handle = network.handle().clone();
|
let handle = network.handle().clone();
|
||||||
@ -230,7 +230,7 @@ async fn test_connect_with_builder() {
|
|||||||
discv4.add_boot_nodes(mainnet_nodes());
|
discv4.add_boot_nodes(mainnet_nodes());
|
||||||
|
|
||||||
let client = Arc::new(NoopProvider::default());
|
let client = Arc::new(NoopProvider::default());
|
||||||
let config = NetworkConfig::builder(Arc::clone(&client), secret_key).discovery(discv4).build();
|
let config = NetworkConfigBuilder::new(secret_key).discovery(discv4).build(Arc::clone(&client));
|
||||||
let (handle, network, _, requests) = NetworkManager::new(config)
|
let (handle, network, _, requests) = NetworkManager::new(config)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -266,7 +266,7 @@ async fn test_connect_to_trusted_peer() {
|
|||||||
let discv4 = Discv4Config::builder();
|
let discv4 = Discv4Config::builder();
|
||||||
|
|
||||||
let client = Arc::new(NoopProvider::default());
|
let client = Arc::new(NoopProvider::default());
|
||||||
let config = NetworkConfig::builder(Arc::clone(&client), secret_key).discovery(discv4).build();
|
let config = NetworkConfigBuilder::new(secret_key).discovery(discv4).build(Arc::clone(&client));
|
||||||
let (handle, network, transactions, requests) = NetworkManager::new(config)
|
let (handle, network, transactions, requests) = NetworkManager::new(config)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -331,11 +331,11 @@ async fn test_incoming_node_id_blacklist() {
|
|||||||
let peer_config = PeersConfig::default().with_ban_list(ban_list);
|
let peer_config = PeersConfig::default().with_ban_list(ban_list);
|
||||||
|
|
||||||
let (reth_p2p, reth_disc) = unused_tcp_udp();
|
let (reth_p2p, reth_disc) = unused_tcp_udp();
|
||||||
let config = NetworkConfig::builder(Arc::new(NoopProvider::default()), secret_key)
|
let config = NetworkConfigBuilder::new(secret_key)
|
||||||
.listener_addr(reth_p2p)
|
.listener_addr(reth_p2p)
|
||||||
.discovery_addr(reth_disc)
|
.discovery_addr(reth_disc)
|
||||||
.peer_config(peer_config)
|
.peer_config(peer_config)
|
||||||
.build();
|
.build(Arc::new(NoopProvider::default()));
|
||||||
|
|
||||||
let network = NetworkManager::new(config).await.unwrap();
|
let network = NetworkManager::new(config).await.unwrap();
|
||||||
|
|
||||||
@ -380,10 +380,10 @@ async fn test_incoming_connect_with_single_geth() {
|
|||||||
let geth_peer_id = enr_to_peer_id(provider.node_info().await.unwrap().enr);
|
let geth_peer_id = enr_to_peer_id(provider.node_info().await.unwrap().enr);
|
||||||
|
|
||||||
let (reth_p2p, reth_disc) = unused_tcp_udp();
|
let (reth_p2p, reth_disc) = unused_tcp_udp();
|
||||||
let config = NetworkConfig::builder(Arc::new(NoopProvider::default()), secret_key)
|
let config = NetworkConfigBuilder::new(secret_key)
|
||||||
.listener_addr(reth_p2p)
|
.listener_addr(reth_p2p)
|
||||||
.discovery_addr(reth_disc)
|
.discovery_addr(reth_disc)
|
||||||
.build();
|
.build(Arc::new(NoopProvider::default()));
|
||||||
|
|
||||||
let network = NetworkManager::new(config).await.unwrap();
|
let network = NetworkManager::new(config).await.unwrap();
|
||||||
|
|
||||||
@ -414,10 +414,10 @@ async fn test_outgoing_connect_with_single_geth() {
|
|||||||
let secret_key = SecretKey::new(&mut rand::thread_rng());
|
let secret_key = SecretKey::new(&mut rand::thread_rng());
|
||||||
|
|
||||||
let (reth_p2p, reth_disc) = unused_tcp_udp();
|
let (reth_p2p, reth_disc) = unused_tcp_udp();
|
||||||
let config = NetworkConfig::builder(Arc::new(NoopProvider::default()), secret_key)
|
let config = NetworkConfigBuilder::new(secret_key)
|
||||||
.listener_addr(reth_p2p)
|
.listener_addr(reth_p2p)
|
||||||
.discovery_addr(reth_disc)
|
.discovery_addr(reth_disc)
|
||||||
.build();
|
.build(Arc::new(NoopProvider::default()));
|
||||||
let network = NetworkManager::new(config).await.unwrap();
|
let network = NetworkManager::new(config).await.unwrap();
|
||||||
|
|
||||||
let handle = network.handle().clone();
|
let handle = network.handle().clone();
|
||||||
@ -459,10 +459,10 @@ async fn test_geth_disconnect() {
|
|||||||
let secret_key = SecretKey::new(&mut rand::thread_rng());
|
let secret_key = SecretKey::new(&mut rand::thread_rng());
|
||||||
|
|
||||||
let (reth_p2p, reth_disc) = unused_tcp_udp();
|
let (reth_p2p, reth_disc) = unused_tcp_udp();
|
||||||
let config = NetworkConfig::builder(Arc::new(NoopProvider::default()), secret_key)
|
let config = NetworkConfigBuilder::new(secret_key)
|
||||||
.listener_addr(reth_p2p)
|
.listener_addr(reth_p2p)
|
||||||
.discovery_addr(reth_disc)
|
.discovery_addr(reth_disc)
|
||||||
.build();
|
.build(Arc::new(NoopProvider::default()));
|
||||||
let network = NetworkManager::new(config).await.unwrap();
|
let network = NetworkManager::new(config).await.unwrap();
|
||||||
|
|
||||||
let handle = network.handle().clone();
|
let handle = network.handle().clone();
|
||||||
|
|||||||
@ -106,7 +106,7 @@ pub enum ValidationError {
|
|||||||
|
|
||||||
/// Filter that describes the state of blockchain and can be used to check incoming `ForkId`s for
|
/// Filter that describes the state of blockchain and can be used to check incoming `ForkId`s for
|
||||||
/// compatibility.
|
/// compatibility.
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub struct ForkFilter {
|
pub struct ForkFilter {
|
||||||
forks: BTreeMap<BlockNumber, ForkHash>,
|
forks: BTreeMap<BlockNumber, ForkHash>,
|
||||||
|
|
||||||
@ -256,7 +256,7 @@ pub struct ForkTransition {
|
|||||||
pub past: ForkId,
|
pub past: ForkId,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
struct Cache {
|
struct Cache {
|
||||||
// An epoch is a period between forks.
|
// An epoch is a period between forks.
|
||||||
// When we progress from one fork to the next one we move to the next epoch.
|
// When we progress from one fork to the next one we move to the next epoch.
|
||||||
|
|||||||
Reference in New Issue
Block a user