Skip to content

Commit

Permalink
Merge pull request #1695 from eqlabs/sistemd/p2p-limits-args
Browse files Browse the repository at this point in the history
Add CLI args for inbound connection limits
  • Loading branch information
sistemd authored Jan 24, 2024
2 parents e187aae + ba3a4ec commit e7dc41a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
8 changes: 4 additions & 4 deletions crates/p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ pub struct LimitsConfig {
pub max_inbound_relay_peers: usize,
}

impl Default for LimitsConfig {
fn default() -> Self {
impl LimitsConfig {
pub fn new(max_inbound_direct_peers: usize, max_inbound_relay_peers: usize) -> Self {
Self {
direct_connection_timeout: Duration::from_secs(30),
relay_connection_timeout: Duration::from_secs(10),
max_inbound_direct_peers: 35,
max_inbound_relay_peers: 15,
max_inbound_direct_peers,
max_inbound_relay_peers,
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions crates/p2p/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Default for TestPeer {
fn default() -> Self {
Self::new(
Default::default(),
Default::default(),
LimitsConfig::new(10, 10),
Keypair::generate_ed25519(),
)
}
Expand Down Expand Up @@ -248,7 +248,8 @@ async fn periodic_bootstrap() {
let limits_cfg = LimitsConfig {
direct_connection_timeout: Duration::from_millis(50),
relay_connection_timeout: Duration::from_millis(50),
..Default::default()
max_inbound_direct_peers: 10,
max_inbound_relay_peers: 10,
};
let mut boot = TestPeer::new(periodic_cfg, limits_cfg, Keypair::generate_ed25519());
let mut peer1 = TestPeer::new(periodic_cfg, limits_cfg, Keypair::generate_ed25519());
Expand Down Expand Up @@ -315,7 +316,8 @@ async fn reconnect_too_quickly() {
let limits_cfg = LimitsConfig {
direct_connection_timeout: CONNECTION_TIMEOUT,
relay_connection_timeout: Duration::from_millis(500),
..Default::default()
max_inbound_direct_peers: 10,
max_inbound_relay_peers: 10,
};

let mut peer1 = TestPeer::new(periodic_cfg, limits_cfg, Keypair::generate_ed25519());
Expand Down Expand Up @@ -410,7 +412,8 @@ async fn duplicate_connection() {
let limits_cfg = LimitsConfig {
direct_connection_timeout: CONNECTION_TIMEOUT,
relay_connection_timeout: Duration::from_millis(500),
..Default::default()
max_inbound_direct_peers: 10,
max_inbound_relay_peers: 10,
};
let keypair = Keypair::generate_ed25519();
let mut peer1 = TestPeer::new(periodic_cfg, limits_cfg, keypair.clone());
Expand Down Expand Up @@ -491,7 +494,7 @@ async fn max_inbound_connections() {
direct_connection_timeout: CONNECTION_TIMEOUT,
relay_connection_timeout: Duration::from_millis(500),
max_inbound_direct_peers: 2,
..Default::default()
max_inbound_relay_peers: 0,
};
let mut peer1 = TestPeer::new(periodic_cfg, limits_cfg, Keypair::generate_ed25519());
let mut peer2 = TestPeer::new(periodic_cfg, limits_cfg, Keypair::generate_ed25519());
Expand Down
25 changes: 25 additions & 0 deletions crates/pathfinder/src/bin/pathfinder/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,24 @@ Example:
env = "PATHFINDER_P2P_PREDEFINED_PEERS"
)]
predefined_peers: Vec<String>,

#[arg(
long = "p2p.max-inbound-direct-connections",
long_help = "The maximum number of inbound direct (non-relayed) connections.",
value_name = "MAX_INBOUND_DIRECT_CONNECTIONS",
env = "PATHFINDER_MAX_INBOUND_DIRECT_CONNECTIONS",
default_value = "35"
)]
max_inbound_direct_connections: u32,

#[arg(
long = "p2p.max-inbound-relayed-connections",
long_help = "The maximum number of inbound relayed connections.",
value_name = "MAX_INBOUND_RELAYED_CONNECTIONS",
env = "PATHFINDER_MAX_INBOUND_RELAYED_CONNECTIONS",
default_value = "15"
)]
max_inbound_relayed_connections: u32,
}

#[cfg(feature = "p2p")]
Expand Down Expand Up @@ -518,6 +536,8 @@ pub struct P2PConfig {
pub listen_on: Multiaddr,
pub bootstrap_addresses: Vec<Multiaddr>,
pub predefined_peers: Vec<Multiaddr>,
pub max_inbound_direct_connections: usize,
pub max_inbound_relayed_connections: usize,
}

#[cfg(not(feature = "p2p"))]
Expand Down Expand Up @@ -599,6 +619,11 @@ impl P2PConfig {
};

Self {
max_inbound_direct_connections: args.max_inbound_direct_connections.try_into().unwrap(),
max_inbound_relayed_connections: args
.max_inbound_relayed_connections
.try_into()
.unwrap(),
proxy: args.proxy,
identity_config_file: args.identity_config_file,
listen_on: args.listen_on,
Expand Down
4 changes: 4 additions & 0 deletions crates/pathfinder/src/bin/pathfinder/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@ async fn start_p2p(
};

let context = P2PContext {
limits_cfg: p2p::LimitsConfig::new(
config.max_inbound_direct_connections,
config.max_inbound_relayed_connections,
),
chain_id,
storage,
proxy: config.proxy,
Expand Down
4 changes: 3 additions & 1 deletion crates/pathfinder/src/p2p_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub type P2PNetworkHandle = (
);

pub struct P2PContext {
pub limits_cfg: p2p::LimitsConfig,
pub chain_id: ChainId,
pub storage: Storage,
pub proxy: bool,
Expand All @@ -35,6 +36,7 @@ pub struct P2PContext {
#[tracing::instrument(name = "p2p", skip_all)]
pub async fn start(context: P2PContext) -> anyhow::Result<P2PNetworkHandle> {
let P2PContext {
limits_cfg,
chain_id,
storage,
proxy,
Expand All @@ -52,7 +54,7 @@ pub async fn start(context: P2PContext) -> anyhow::Result<P2PNetworkHandle> {
keypair,
peers.clone(),
Default::default(),
Default::default(),
limits_cfg,
chain_id,
);

Expand Down

0 comments on commit e7dc41a

Please sign in to comment.