Skip to content

Commit

Permalink
feat(network): add peer manager and discovery config to network config (
Browse files Browse the repository at this point in the history
#1397)

* feat(network): add peer manager and discovery config to network config

* fix(network): fix CR comments

* fix(network): fix CR comments #2

* fix(network): fix CR comments #3
  • Loading branch information
AlonLStarkWare authored Oct 28, 2024
1 parent 036794e commit 9e9d946
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 8 deletions.
39 changes: 39 additions & 0 deletions config/mempool/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,50 @@
"pointer_target": "chain_id",
"privacy": "Public"
},
"mempool_p2p_config.network_config.discovery_config.bootstrap_dial_retry_config.base_delay_millis": {
"description": "The base delay in milliseconds for the exponential backoff strategy.",
"privacy": "Public",
"value": 2
},
"mempool_p2p_config.network_config.discovery_config.bootstrap_dial_retry_config.factor": {
"description": "The factor for the exponential backoff strategy.",
"privacy": "Public",
"value": 5
},
"mempool_p2p_config.network_config.discovery_config.bootstrap_dial_retry_config.max_delay": {
"description": "The maximum delay for the exponential backoff strategy.",
"privacy": "Public",
"value": {
"nanos": 0,
"secs": 5
}
},
"mempool_p2p_config.network_config.discovery_config.heartbeat_interval": {
"description": "The interval between each discovery (Kademlia) query in milliseconds.",
"privacy": "Public",
"value": 100
},
"mempool_p2p_config.network_config.idle_connection_timeout": {
"description": "Amount of time in seconds that a connection with no active sessions will stay alive.",
"privacy": "Public",
"value": 120
},
"mempool_p2p_config.network_config.peer_manager_config.malicious_timeout": {
"description": "The duration a peer is blacklisted after being marked as malicious.",
"privacy": "Public",
"value": {
"nanos": 0,
"secs": 31536000
}
},
"mempool_p2p_config.network_config.peer_manager_config.unstable_timeout": {
"description": "The duration a peer blacklisted after being reported as unstable.",
"privacy": "Public",
"value": {
"nanos": 0,
"secs": 1
}
},
"mempool_p2p_config.network_config.quic_port": {
"description": "The port that the node listens on for incoming quic connections.",
"privacy": "Public",
Expand Down
39 changes: 39 additions & 0 deletions config/papyrus/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,50 @@
"pointer_target": "chain_id",
"privacy": "Public"
},
"network.discovery_config.bootstrap_dial_retry_config.base_delay_millis": {
"description": "The base delay in milliseconds for the exponential backoff strategy.",
"privacy": "Public",
"value": 2
},
"network.discovery_config.bootstrap_dial_retry_config.factor": {
"description": "The factor for the exponential backoff strategy.",
"privacy": "Public",
"value": 5
},
"network.discovery_config.bootstrap_dial_retry_config.max_delay": {
"description": "The maximum delay for the exponential backoff strategy.",
"privacy": "Public",
"value": {
"nanos": 0,
"secs": 5
}
},
"network.discovery_config.heartbeat_interval": {
"description": "The interval between each discovery (Kademlia) query in milliseconds.",
"privacy": "Public",
"value": 100
},
"network.idle_connection_timeout": {
"description": "Amount of time in seconds that a connection with no active sessions will stay alive.",
"privacy": "Public",
"value": 120
},
"network.peer_manager_config.malicious_timeout": {
"description": "The duration a peer is blacklisted after being marked as malicious.",
"privacy": "Public",
"value": {
"nanos": 0,
"secs": 31536000
}
},
"network.peer_manager_config.unstable_timeout": {
"description": "The duration a peer blacklisted after being reported as unstable.",
"privacy": "Public",
"value": {
"nanos": 0,
"secs": 1
}
},
"network.quic_port": {
"description": "The port that the node listens on for incoming quic connections.",
"privacy": "Public",
Expand Down
5 changes: 4 additions & 1 deletion crates/papyrus_network/src/discovery/flow_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use libp2p::{identify, kad, Multiaddr, Swarm};
use libp2p_swarm_test::SwarmExt;
use starknet_api::core::ChainId;

use super::Behaviour;
use super::{Behaviour, DiscoveryConfig};
use crate::mixed_behaviour;
use crate::mixed_behaviour::{BridgedBehaviour, MixedBehaviour};
use crate::peer_manager::PeerManagerConfig;
use crate::utils::StreamHashMap;

#[derive(NetworkBehaviour)]
Expand All @@ -31,6 +32,8 @@ impl DiscoveryMixedBehaviour {
Default::default(),
ChainId::Mainnet,
None,
DiscoveryConfig::default(),
PeerManagerConfig::default(),
);
Self {
identify: mixed_behaviour.identify,
Expand Down
51 changes: 49 additions & 2 deletions crates/papyrus_network/src/discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod flow_test;
pub mod identify_impl;
pub mod kad_impl;

use std::collections::BTreeMap;
use std::task::{ready, Context, Poll, Waker};
use std::time::Duration;

Expand All @@ -27,6 +28,10 @@ use libp2p::swarm::{
ToSwarm,
};
use libp2p::{Multiaddr, PeerId};
use papyrus_config::converters::deserialize_milliseconds_to_duration;
use papyrus_config::dumping::{append_sub_config_name, ser_param, SerializeConfig};
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use serde::{Deserialize, Serialize};
use tokio_retry::strategy::ExponentialBackoff;

use crate::mixed_behaviour;
Expand Down Expand Up @@ -181,9 +186,10 @@ impl NetworkBehaviour for Behaviour {
}
}

// TODO(alon): add to NetworkConfig
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct DiscoveryConfig {
pub bootstrap_dial_retry_config: RetryConfig,
#[serde(deserialize_with = "deserialize_milliseconds_to_duration")]
pub heartbeat_interval: Duration,
}

Expand All @@ -196,7 +202,23 @@ impl Default for DiscoveryConfig {
}
}

#[derive(Copy, Clone, Debug)]
impl SerializeConfig for DiscoveryConfig {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
let mut dump = BTreeMap::from([ser_param(
"heartbeat_interval",
&self.heartbeat_interval.as_millis(),
"The interval between each discovery (Kademlia) query in milliseconds.",
ParamPrivacyInput::Public,
)]);
dump.append(&mut append_sub_config_name(
self.bootstrap_dial_retry_config.dump(),
"bootstrap_dial_retry_config",
));
dump
}
}

#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct RetryConfig {
pub base_delay_millis: u64,
pub max_delay: Duration,
Expand All @@ -209,6 +231,31 @@ impl Default for RetryConfig {
}
}

impl SerializeConfig for RetryConfig {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
BTreeMap::from([
ser_param(
"base_delay_millis",
&self.base_delay_millis,
"The base delay in milliseconds for the exponential backoff strategy.",
ParamPrivacyInput::Public,
),
ser_param(
"max_delay",
&self.max_delay,
"The maximum delay for the exponential backoff strategy.",
ParamPrivacyInput::Public,
),
ser_param(
"factor",
&self.factor,
"The factor for the exponential backoff strategy.",
ParamPrivacyInput::Public,
),
])
}
}

impl RetryConfig {
fn strategy(&self) -> ExponentialBackoff {
ExponentialBackoff::from_millis(self.base_delay_millis)
Expand Down
4 changes: 4 additions & 0 deletions crates/papyrus_network/src/e2e_broadcast_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ use libp2p::{Multiaddr, Swarm};
use libp2p_swarm_test::SwarmExt;
use starknet_api::core::ChainId;

use crate::discovery::DiscoveryConfig;
use crate::gossipsub_impl::Topic;
use crate::mixed_behaviour::MixedBehaviour;
use crate::network_manager::{BroadcastTopicClientTrait, GenericNetworkManager};
use crate::peer_manager::PeerManagerConfig;
use crate::sqmr;
use crate::sqmr::Bytes;

Expand All @@ -23,6 +25,8 @@ async fn create_swarm(bootstrap_peer_multiaddr: Option<Multiaddr>) -> Swarm<Mixe
sqmr::Config::default(),
ChainId::Mainnet,
None,
DiscoveryConfig::default(),
PeerManagerConfig::default(),
)
});
// Not using SwarmExt::listen because it panics if the swarm emits other events
Expand Down
16 changes: 15 additions & 1 deletion crates/papyrus_network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ mod utils;
use std::collections::BTreeMap;
use std::time::Duration;

use discovery::DiscoveryConfig;
use libp2p::Multiaddr;
use papyrus_config::converters::{
deserialize_optional_vec_u8,
deserialize_seconds_to_duration,
serialize_optional_vec_u8,
};
use papyrus_config::dumping::{ser_optional_param, ser_param, SerializeConfig};
use papyrus_config::dumping::{
append_sub_config_name,
ser_optional_param,
ser_param,
SerializeConfig,
};
use papyrus_config::validators::validate_vec_u256;
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use peer_manager::PeerManagerConfig;
use serde::{Deserialize, Serialize};
use starknet_api::core::ChainId;
use validator::Validate;
Expand All @@ -46,6 +53,8 @@ pub struct NetworkConfig {
pub(crate) secret_key: Option<Vec<u8>>,
pub advertised_multiaddr: Option<Multiaddr>,
pub chain_id: ChainId,
pub discovery_config: DiscoveryConfig,
pub peer_manager_config: PeerManagerConfig,
}

impl SerializeConfig for NetworkConfig {
Expand Down Expand Up @@ -106,6 +115,9 @@ impl SerializeConfig for NetworkConfig {
instead",
ParamPrivacyInput::Public,
));
config.extend(append_sub_config_name(self.discovery_config.dump(), "discovery_config"));
config
.extend(append_sub_config_name(self.peer_manager_config.dump(), "peer_manager_config"));
config
}
}
Expand All @@ -121,6 +133,8 @@ impl Default for NetworkConfig {
secret_key: None,
advertised_multiaddr: None,
chain_id: ChainId::Mainnet,
discovery_config: DiscoveryConfig::default(),
peer_manager_config: PeerManagerConfig::default(),
}
}
}
6 changes: 4 additions & 2 deletions crates/papyrus_network/src/mixed_behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ impl MixedBehaviour {
streamed_bytes_config: sqmr::Config,
chain_id: ChainId,
node_version: Option<String>,
discovery_config: DiscoveryConfig,
peer_manager_config: PeerManagerConfig,
) -> Self {
let public_key = keypair.public();
let local_peer_id = PeerId::from_public_key(&public_key);
Expand All @@ -73,11 +75,11 @@ impl MixedBehaviour {
.expect("Failed to create StreamProtocol from a string that starts with /"),
]);
Self {
peer_manager: peer_manager::PeerManager::new(PeerManagerConfig::default()),
peer_manager: peer_manager::PeerManager::new(peer_manager_config),
discovery: bootstrap_peer_multiaddr
.map(|bootstrap_peer_multiaddr| {
discovery::Behaviour::new(
DiscoveryConfig::default(),
discovery_config,
DialOpts::from(bootstrap_peer_multiaddr.clone())
.get_peer_id()
.expect("bootstrap_peer_multiaddr doesn't have a peer id"),
Expand Down
4 changes: 4 additions & 0 deletions crates/papyrus_network/src/network_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ impl NetworkManager {
advertised_multiaddr,
secret_key,
chain_id,
discovery_config,
peer_manager_config,
} = config;

let listen_addresses = vec![
Expand All @@ -635,6 +637,8 @@ impl NetworkManager {
sqmr::Config { session_timeout },
chain_id,
node_version,
discovery_config,
peer_manager_config,
)
});
let advertised_multiaddr = advertised_multiaddr.map(|address| {
Expand Down
26 changes: 24 additions & 2 deletions crates/papyrus_network/src/peer_manager/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::collections::HashMap;
use std::collections::{BTreeMap, HashMap};
use std::time::Duration;

use futures::future::BoxFuture;
use futures::FutureExt;
use libp2p::swarm::dial_opts::DialOpts;
use libp2p::swarm::ToSwarm;
use libp2p::PeerId;
use papyrus_config::dumping::{ser_param, SerializeConfig};
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use peer::Peer;
use serde::{Deserialize, Serialize};
use tracing::info;

pub use self::behaviour_impl::ToOtherBehaviourEvent;
Expand Down Expand Up @@ -47,7 +50,7 @@ pub struct PeerManager {
sleep_waiting_for_unblocked_peer: Option<BoxFuture<'static, ()>>,
}

#[derive(Clone)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct PeerManagerConfig {
malicious_timeout: Duration,
unstable_timeout: Duration,
Expand All @@ -73,6 +76,25 @@ impl Default for PeerManagerConfig {
}
}

impl SerializeConfig for PeerManagerConfig {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
BTreeMap::from([
ser_param(
"malicious_timeout",
&self.malicious_timeout,
"The duration a peer is blacklisted after being marked as malicious.",
ParamPrivacyInput::Public,
),
ser_param(
"unstable_timeout",
&self.unstable_timeout,
"The duration a peer blacklisted after being reported as unstable.",
ParamPrivacyInput::Public,
),
])
}
}

#[allow(dead_code)]
impl PeerManager {
pub(crate) fn new(config: PeerManagerConfig) -> Self {
Expand Down
Loading

0 comments on commit 9e9d946

Please sign in to comment.