Skip to content

Commit

Permalink
network-libp2p: Expose a configuration for the desired number of peers
Browse files Browse the repository at this point in the history
Expose a configuration for the desired number of peers for the
connection pool to realize when to try to connect to more peers.
This configuration is exposed in the network and is set in the client
to be `consensus.min_peers`.
  • Loading branch information
jsdanielh committed Dec 8, 2023
1 parent 7530c88 commit 1af0b21
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ impl ClientInner {
false,
required_services,
tls_config,
config.consensus.min_peers,
);

log::debug!(
Expand Down
1 change: 1 addition & 0 deletions network-libp2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl Behaviour {
peer_id,
config.seeds,
config.discovery.required_services,
config.peer_count_desired,
);

// Request Response behaviour
Expand Down
3 changes: 3 additions & 0 deletions network-libp2p/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct Config {
pub memory_transport: bool,
pub required_services: Services,
pub tls: Option<TlsConfig>,
pub peer_count_desired: usize,
}

impl Config {
Expand All @@ -41,6 +42,7 @@ impl Config {
memory_transport: bool,
required_services: Services,
tls_settings: Option<TlsConfig>,
peer_count_desired: usize,
) -> Self {
// Hardcoding the minimum number of peers in mesh network before adding more
// TODO: Maybe change this to a mesh limits configuration argument of this function
Expand Down Expand Up @@ -80,6 +82,7 @@ impl Config {
memory_transport,
required_services,
tls: tls_settings,
peer_count_desired,
}
}
}
4 changes: 3 additions & 1 deletion network-libp2p/src/connection_pool/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,15 @@ impl Behaviour {
own_peer_id: PeerId,
seeds: Vec<Multiaddr>,
required_services: Services,
peer_count_desired: usize,
) -> Self {
let limits = Limits {
ip_count: HashMap::new(),
ip_subnet_count: HashMap::new(),
peer_count: 0,
};
let config = Config::default();
let mut config = Config::default();
config.peer_count_desired = peer_count_desired;

Check warning on line 321 in network-libp2p/src/connection_pool/behaviour.rs

View workflow job for this annotation

GitHub Actions / Clippy Report

field assignment outside of initializer for an instance created with Default::default()

warning: field assignment outside of initializer for an instance created with Default::default() --> network-libp2p/src/connection_pool/behaviour.rs:321:9 | 321 | config.peer_count_desired = peer_count_desired; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: consider initializing the variable with `connection_pool::behaviour::Config { peer_count_desired: peer_count_desired, ..Default::default() }` and removing relevant reassignments --> network-libp2p/src/connection_pool/behaviour.rs:320:9 | 320 | let mut config = Config::default(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#field_reassign_with_default = note: `#[warn(clippy::field_reassign_with_default)]` on by default
let housekeeping_timer = wasm_timer::Interval::new(config.housekeeping_interval);

Self {
Expand Down
1 change: 1 addition & 0 deletions network-libp2p/tests/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn network_config(address: Multiaddr) -> Config {
memory_transport: true,
required_services: Services::all(),
tls: None,
peer_count_desired: 3,
}
}

Expand Down
1 change: 1 addition & 0 deletions network-libp2p/tests/request_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ fn network_config(address: Multiaddr) -> Config {
memory_transport: true,
required_services: Services::all(),
tls: None,
peer_count_desired: 3,
}
}

Expand Down
1 change: 1 addition & 0 deletions test-utils/src/test_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl TestNetwork for Network {
true,
Services::all(),
None,
3,
);
let network = Arc::new(
Network::new(
Expand Down

0 comments on commit 1af0b21

Please sign in to comment.