Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more networking config options to web-client ClientConfiguration #3143

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions web-client/src/client/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ impl Client {
})
.collect::<Result<Vec<Seed>, JsError>>()?;

config.network.seeds = seed_nodes;
config.network.only_secure_ws_connections = true;
config.network_id = web_config.network_id;
config.network.desired_peer_count = 12;
config.network.peer_count_max = 50;
config.network.peer_count_per_ip_max = 10;
config.network.peer_count_per_subnet_max = 10;
config.network.seeds = seed_nodes;
config.network.only_secure_ws_connections = web_config.only_secure_ws_connections;
config.network.desired_peer_count = web_config.desired_peer_count;
config.network.peer_count_max = web_config.peer_count_max;
config.network.peer_count_per_ip_max = web_config.peer_count_per_ip_max;
config.network.peer_count_per_subnet_max = web_config.peer_count_per_subnet_max;

log::info!(?config, "Final configuration");

Expand Down
85 changes: 85 additions & 0 deletions web-client/src/common/client_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ pub struct ClientConfiguration {
pub seed_nodes: Vec<String>,
#[wasm_bindgen(skip)]
pub log_level: String,
#[wasm_bindgen(skip)]
pub only_secure_ws_connections: bool,
#[wasm_bindgen(skip)]
pub desired_peer_count: usize,
#[wasm_bindgen(skip)]
pub peer_count_max: usize,
#[wasm_bindgen(skip)]
pub peer_count_per_ip_max: usize,
#[wasm_bindgen(skip)]
pub peer_count_per_subnet_max: usize,
}

#[cfg(any(feature = "client", feature = "primitives"))]
Expand All @@ -38,6 +48,16 @@ pub struct PlainClientConfiguration {
pub seed_nodes: Option<Vec<String>>,
#[cfg_attr(feature = "client", serde(skip_serializing_if = "Option::is_none"))]
pub log_level: Option<String>,
#[cfg_attr(feature = "client", serde(skip_serializing_if = "Option::is_none"))]
pub only_secure_ws_connections: Option<bool>,
#[cfg_attr(feature = "client", serde(skip_serializing_if = "Option::is_none"))]
pub desired_peer_count: Option<usize>,
#[cfg_attr(feature = "client", serde(skip_serializing_if = "Option::is_none"))]
pub peer_count_max: Option<usize>,
#[cfg_attr(feature = "client", serde(skip_serializing_if = "Option::is_none"))]
pub peer_count_per_ip_max: Option<usize>,
#[cfg_attr(feature = "client", serde(skip_serializing_if = "Option::is_none"))]
pub peer_count_per_subnet_max: Option<usize>,
}

impl Default for ClientConfiguration {
Expand All @@ -61,6 +81,11 @@ impl Default for ClientConfiguration {
"/dns4/zenith.seed.nimiq.systems/tcp/443/wss".to_string(),
],
log_level: "info".to_string(),
only_secure_ws_connections: true,
desired_peer_count: 12,
peer_count_max: 50,
peer_count_per_ip_max: 10,
peer_count_per_subnet_max: 10,
}
}
}
Expand Down Expand Up @@ -106,6 +131,41 @@ impl ClientConfiguration {
self.log_level = log_level.to_lowercase();
}

/// Sets whether the client should only connect to secure WebSocket connections.
/// Default is `true`.
#[wasm_bindgen(js_name = onlySecureWsConnections)]
pub fn only_secure_ws_connections(&mut self, only_secure_ws_connections: bool) {
self.only_secure_ws_connections = only_secure_ws_connections;
}

/// Sets the desired number of peers the client should try to connect to.
/// Default is `12`.
#[wasm_bindgen(js_name = desiredPeerCount)]
pub fn desired_peer_count(&mut self, desired_peer_count: usize) {
self.desired_peer_count = desired_peer_count;
}

/// Sets the maximum number of peers the client should connect to.
/// Default is `50`.
#[wasm_bindgen(js_name = peerCountMax)]
pub fn peer_count_max(&mut self, peer_count_max: usize) {
self.peer_count_max = peer_count_max;
}

/// Sets the maximum number of peers the client should connect to per IP address.
/// Default is `10`.
#[wasm_bindgen(js_name = peerCountPerIpMax)]
pub fn peer_count_per_ip_max(&mut self, peer_count_per_ip_max: usize) {
self.peer_count_per_ip_max = peer_count_per_ip_max;
}

/// Sets the maximum number of peers the client should connect to per subnet.
/// Default is `10`.
#[wasm_bindgen(js_name = peerCountPerSubnetMax)]
pub fn peer_count_per_subnet_max(&mut self, peer_count_per_subnet_max: usize) {
self.peer_count_per_subnet_max = peer_count_per_subnet_max;
}

// TODO: Find a way to make this method work, maybe by using the synthetic Client from the main thread as an import?
// /// Instantiates a client from this configuration builder.
// #[wasm_bindgen(js_name = instantiateClient)]
Expand All @@ -122,6 +182,11 @@ impl ClientConfiguration {
network_id: Some(self.network_id.to_string()),
seed_nodes: Some(self.seed_nodes.clone()),
log_level: Some(self.log_level.clone()),
only_secure_ws_connections: Some(self.only_secure_ws_connections),
desired_peer_count: Some(self.desired_peer_count),
peer_count_max: Some(self.peer_count_max),
peer_count_per_ip_max: Some(self.peer_count_per_ip_max),
peer_count_per_subnet_max: Some(self.peer_count_per_subnet_max),
})
.unwrap()
.into()
Expand All @@ -148,6 +213,26 @@ impl TryFrom<PlainClientConfiguration> for ClientConfiguration {
client_config.log_level = log_level;
}

if let Some(only_secure_ws_connections) = config.only_secure_ws_connections {
client_config.only_secure_ws_connections = only_secure_ws_connections;
}

if let Some(desired_peer_count) = config.desired_peer_count {
client_config.desired_peer_count = desired_peer_count;
}

if let Some(peer_count_max) = config.peer_count_max {
client_config.peer_count_max = peer_count_max;
}

if let Some(peer_count_per_ip_max) = config.peer_count_per_ip_max {
client_config.peer_count_per_ip_max = peer_count_per_ip_max;
}

if let Some(peer_count_per_subnet_max) = config.peer_count_per_subnet_max {
client_config.peer_count_per_subnet_max = peer_count_per_subnet_max;
}

Ok(client_config)
}
}
Expand Down