Skip to content

Commit

Permalink
fix: make the limits non-configurable, remove irrelevant semicolons
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Jan 10, 2025
1 parent b622eea commit 1f9291f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 46 deletions.
24 changes: 4 additions & 20 deletions bin/fuel-core/src/cli/run/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down Expand Up @@ -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!(
Expand All @@ -270,7 +257,7 @@ impl P2PArgs {
) -> anyhow::Result<Option<Config<NotInitialized>>> {
if !self.enable_p2p {
tracing::info!("P2P service disabled");
return Ok(None);
return Ok(None)
}

let local_keypair = {
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 7 additions & 7 deletions crates/services/p2p/src/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions crates/services/p2p/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,6 @@ pub struct Config<State = Initialized> {
/// 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<Duration>,
Expand Down Expand Up @@ -174,9 +168,6 @@ impl Config<NotInitialized> {
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,
Expand Down Expand Up @@ -229,9 +220,6 @@ impl Config<NotInitialized> {
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)),
Expand Down
6 changes: 3 additions & 3 deletions crates/services/p2p/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/services/p2p/src/heartbeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl NetworkBehaviour for Behaviour {
_: &mut std::task::Context<'_>,
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
if let Some(action) = self.pending_events.pop_front() {
return Poll::Ready(action.build());
return Poll::Ready(action.build())
}

Poll::Pending
Expand Down
6 changes: 3 additions & 3 deletions crates/services/p2p/src/peer_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl NetworkBehaviour for Behaviour {
cx: &mut Context<'_>,
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
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() {
Expand All @@ -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
Expand Down

0 comments on commit 1f9291f

Please sign in to comment.