From 1f9291f6b68fac8e154974b29fd44fcab55c8c67 Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Fri, 10 Jan 2025 20:16:59 +0530 Subject: [PATCH] fix: make the limits non-configurable, remove irrelevant semicolons --- bin/fuel-core/src/cli/run/p2p.rs | 24 ++++-------------------- crates/services/p2p/src/behavior.rs | 14 +++++++------- crates/services/p2p/src/config.rs | 12 ------------ crates/services/p2p/src/discovery.rs | 6 +++--- crates/services/p2p/src/heartbeat.rs | 2 +- crates/services/p2p/src/peer_report.rs | 6 +++--- 6 files changed, 18 insertions(+), 46 deletions(-) diff --git a/bin/fuel-core/src/cli/run/p2p.rs b/bin/fuel-core/src/cli/run/p2p.rs index d7492ce6c7f..98a9a33759c 100644 --- a/bin/fuel-core/src/cli/run/p2p.rs +++ b/bin/fuel-core/src/cli/run/p2p.rs @@ -99,19 +99,6 @@ pub struct P2PArgs { #[clap(long = "max-connections-per-peer", default_value = "3", env)] pub max_connections_per_peer: u32, - /// Max number of concurrent pending incoming connections - /// Useful in mitigating against DDoS attacks - #[clap(long = "max-pending-incoming-connections", default_value = "100", env)] - pub max_pending_incoming_connections: u32, - - /// Max number of concurrent pending outgoing connections - #[clap(long = "max-pending-outgoing-connections", default_value = "100", env)] - pub max_pending_outgoing_connections: u32, - - /// Max number of established connections - #[clap(long = "max-established-connections", default_value = "100", env)] - pub max_established_connections: u32, - /// Set the delay between random walks for p2p node discovery in seconds. /// If it's not set the random walk will be disabled. /// Also if `reserved_nodes_only_mode` is set to `true`, @@ -235,16 +222,16 @@ impl KeypairArg { let secret = SecretKey::from_str(s); if let Ok(secret) = secret { - return Ok(KeypairArg::InlineSecret(secret)); + return Ok(KeypairArg::InlineSecret(secret)) } let path = PathBuf::from_str(s); if let Ok(pathbuf) = path { if pathbuf.exists() { - return Ok(KeypairArg::Path(pathbuf)); + return Ok(KeypairArg::Path(pathbuf)) } else { return Err(anyhow!( "path `{pathbuf:?}` does not exist for keypair argument" - )); + )) } } Err(anyhow!( @@ -270,7 +257,7 @@ impl P2PArgs { ) -> anyhow::Result>> { if !self.enable_p2p { tracing::info!("P2P service disabled"); - return Ok(None); + return Ok(None) } let local_keypair = { @@ -336,9 +323,6 @@ impl P2PArgs { enable_mdns: self.enable_mdns, max_peers_connected: self.max_peers_connected, max_connections_per_peer: self.max_connections_per_peer, - max_pending_incoming_connections: self.max_pending_incoming_connections, - max_pending_outgoing_connections: self.max_pending_outgoing_connections, - max_established_connections: self.max_established_connections, allow_private_addresses: self.allow_private_addresses, random_walk, connection_idle_timeout: Some(Duration::from_secs( diff --git a/crates/services/p2p/src/behavior.rs b/crates/services/p2p/src/behavior.rs index 7d1ff4e1e3a..a8b0b8326dd 100644 --- a/crates/services/p2p/src/behavior.rs +++ b/crates/services/p2p/src/behavior.rs @@ -39,6 +39,10 @@ use libp2p::{ PeerId, }; +const MAX_PENDING_INCOMING_CONNECTIONS: u32 = 100; +const MAX_PENDING_OUTGOING_CONNECTIONS: u32 = 100; +const MAX_ESTABLISHED_CONNECTIONS: u32 = 1000; + /// Handles all p2p protocols needed for Fuel. #[derive(NetworkBehaviour)] pub struct FuelBehaviour { @@ -122,13 +126,9 @@ impl FuelBehaviour { let connection_limits = connection_limits::Behaviour::new( ConnectionLimits::default() - .with_max_pending_incoming(Some( - p2p_config.max_pending_incoming_connections, - )) - .with_max_pending_outgoing(Some( - p2p_config.max_pending_outgoing_connections, - )) - .with_max_established(Some(p2p_config.max_established_connections)), + .with_max_pending_incoming(Some(MAX_PENDING_INCOMING_CONNECTIONS)) + .with_max_pending_outgoing(Some(MAX_PENDING_OUTGOING_CONNECTIONS)) + .with_max_established(Some(MAX_ESTABLISHED_CONNECTIONS)), ); let req_res_protocol = codec diff --git a/crates/services/p2p/src/config.rs b/crates/services/p2p/src/config.rs index 136899b2a81..0cfad8c3647 100644 --- a/crates/services/p2p/src/config.rs +++ b/crates/services/p2p/src/config.rs @@ -100,12 +100,6 @@ pub struct Config { /// Max number of connections per single peer /// The total number of connections will be `(max_peers_connected + reserved_nodes.len()) * max_connections_per_peer` pub max_connections_per_peer: u32, - /// Max number of concurrent pending incoming connections - pub max_pending_incoming_connections: u32, - /// Max number of concurrent pending outgoing connections - pub max_pending_outgoing_connections: u32, - /// Max number of established connections - pub max_established_connections: u32, /// The interval at which identification requests are sent to /// the remote on established connections after the first request pub identify_interval: Option, @@ -174,9 +168,6 @@ impl Config { enable_mdns: self.enable_mdns, max_peers_connected: self.max_peers_connected, max_connections_per_peer: self.max_connections_per_peer, - max_pending_incoming_connections: self.max_pending_incoming_connections, - max_pending_outgoing_connections: self.max_pending_outgoing_connections, - max_established_connections: self.max_established_connections, allow_private_addresses: self.allow_private_addresses, random_walk: self.random_walk, connection_idle_timeout: self.connection_idle_timeout, @@ -229,9 +220,6 @@ impl Config { enable_mdns: false, max_peers_connected: 50, max_connections_per_peer: 3, - max_pending_incoming_connections: 100, - max_pending_outgoing_connections: 100, - max_established_connections: 150, allow_private_addresses: true, random_walk: Some(Duration::from_millis(500)), connection_idle_timeout: Some(Duration::from_secs(120)), diff --git a/crates/services/p2p/src/discovery.rs b/crates/services/p2p/src/discovery.rs index b2b29917ef8..db6e594a2a0 100644 --- a/crates/services/p2p/src/discovery.rs +++ b/crates/services/p2p/src/discovery.rs @@ -205,7 +205,7 @@ impl NetworkBehaviour for Behaviour { // poll sub-behaviors if let Poll::Ready(kad_action) = self.kademlia.poll(cx) { - return Poll::Ready(kad_action); + return Poll::Ready(kad_action) }; while let Poll::Ready(mdns_event) = self.mdns.poll(cx) { @@ -377,10 +377,10 @@ mod tests { } _ => {} } - continue 'polling; + continue 'polling } } - break; + break } // if there are no swarms left to discover we are done with the discovery diff --git a/crates/services/p2p/src/heartbeat.rs b/crates/services/p2p/src/heartbeat.rs index 6a9af79929d..e38ea87099d 100644 --- a/crates/services/p2p/src/heartbeat.rs +++ b/crates/services/p2p/src/heartbeat.rs @@ -145,7 +145,7 @@ impl NetworkBehaviour for Behaviour { _: &mut std::task::Context<'_>, ) -> Poll>> { if let Some(action) = self.pending_events.pop_front() { - return Poll::Ready(action.build()); + return Poll::Ready(action.build()) } Poll::Pending diff --git a/crates/services/p2p/src/peer_report.rs b/crates/services/p2p/src/peer_report.rs index c0a39e2d05b..6aa0c1dfaa0 100644 --- a/crates/services/p2p/src/peer_report.rs +++ b/crates/services/p2p/src/peer_report.rs @@ -204,7 +204,7 @@ impl NetworkBehaviour for Behaviour { cx: &mut Context<'_>, ) -> Poll>> { if let Some(event) = self.pending_events.pop_front() { - return Poll::Ready(event); + return Poll::Ready(event) } if let Some((instant, peer_id)) = self.reserved_nodes_to_connect.front() { @@ -225,12 +225,12 @@ impl NetworkBehaviour for Behaviour { .build(); self.pending_connections.insert(opts.connection_id()); - return Poll::Ready(ToSwarm::Dial { opts }); + return Poll::Ready(ToSwarm::Dial { opts }) } } if self.decay_interval.poll_tick(cx).is_ready() { - return Poll::Ready(ToSwarm::GenerateEvent(PeerReportEvent::PerformDecay)); + return Poll::Ready(ToSwarm::GenerateEvent(PeerReportEvent::PerformDecay)) } Poll::Pending