Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
26 changes: 17 additions & 9 deletions substrate/network-libp2p/src/network_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub struct NetworkState {
min_peers: u32,
/// `max_peers` taken from the configuration.
max_peers: u32,
/// `incoming_peers_factor` taken from the configuration.
incoming_peers_factor: u32,
/// `max_incoming_peers` calculated as `max_peers / max_incoming_peers_factor` from the configuration.
max_incoming_peers: u32,

Expand Down Expand Up @@ -171,7 +173,8 @@ impl NetworkState {
peerstore,
min_peers: config.min_peers,
max_peers: config.max_peers,
max_incoming_peers: config.max_peers / config.max_incoming_peers_factor,
incoming_peers_factor: config.incoming_peers_factor,
max_incoming_peers: config.max_peers / config.incoming_peers_factor,
connections: RwLock::new(Connections {
peer_by_nodeid: FnvHashMap::with_capacity_and_hasher(expected_max_peers, Default::default()),
info_by_peer: FnvHashMap::with_capacity_and_hasher(expected_max_peers, Default::default()),
Expand Down Expand Up @@ -433,19 +436,18 @@ impl NetworkState {
}
}

/// Returns the number of open and pending connections with
/// custom protocols.
pub fn num_open_custom_connections(&self) -> u32 {
num_open_custom_connections(&self.connections.read()).total
}

/// Returns the number of new outgoing custom connections to peers to
/// open. This takes into account the number of active peers.
pub fn should_open_outgoing_custom_connections(&self) -> u32 {
use std::cmp::max;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be at top of the file.


if self.reserved_only.load(atomic::Ordering::Relaxed) {
0
} else {
self.min_peers.saturating_sub(self.num_open_custom_connections())
let num_open_custom_connections = num_open_custom_connections(&self.connections.read());
let min_outgoing_peers = num_open_custom_connections.incoming * self.incoming_peers_factor.saturating_sub(1);
max(min_outgoing_peers.saturating_sub(num_open_custom_connections.outgoing),
self.min_peers.saturating_sub(num_open_custom_connections.total))
}
}

Expand Down Expand Up @@ -744,6 +746,8 @@ struct OpenCustomConnectionsNumbers {
pub total: u32,
/// Incoming number of open and pending connections.
pub incoming: u32,
/// Outgoing number of open and pending connections.
pub outgoing: u32,
}

/// Returns the number of open and pending connections with
Expand All @@ -770,7 +774,11 @@ fn num_open_custom_connections(connections: &Connections) -> OpenCustomConnectio
}
}

OpenCustomConnectionsNumbers { total, incoming }
OpenCustomConnectionsNumbers {
total,
incoming,
outgoing: total - incoming,
}
}

/// Parses an address of the form `/ip4/x.x.x.x/tcp/x/p2p/xxxxxx`, and adds it
Expand Down
6 changes: 3 additions & 3 deletions substrate/network-libp2p/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ pub struct NetworkConfiguration {
pub min_peers: u32,
/// Maximum allowed number of peers
pub max_peers: u32,
/// Maximum incoming peers factor. The maximum allowed number of incoming peers is calculated as `max_peers / max_incoming_peers_factor`.
pub max_incoming_peers_factor: u32,
/// At most `1 / incoming_peers_factor` of incoming connections are allowed.
pub incoming_peers_factor: u32,
/// Maximum handshakes
pub max_handshakes: u32,
/// Reserved protocols. Peers with <key> protocol get additional <value> connection slots.
Expand Down Expand Up @@ -190,7 +190,7 @@ impl NetworkConfiguration {
use_secret: None,
min_peers: 25,
max_peers: 50,
max_incoming_peers_factor: 3,
incoming_peers_factor: 3,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like 3 is too high, especially for bootstrap nodes which probably want 90% of incoming connections.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So 2, probably?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And just for information, was choosing 3 because it comes from the eclipse attack paper.

max_handshakes: 64,
reserved_protocols: HashMap::new(),
ip_filter: IpFilter::default(),
Expand Down