diff --git a/nym-registration-client/src/builder/config.rs b/nym-registration-client/src/builder/config.rs index ed752a1d68..3d0895c2a6 100644 --- a/nym-registration-client/src/builder/config.rs +++ b/nym-registration-client/src/builder/config.rs @@ -1,6 +1,6 @@ // Copyright 2025 - Nym Technologies SA // SPDX-License-Identifier: GPL-3.0-only - + use nym_credential_storage::persistent_storage::PersistentStorage; use nym_registration_common::NymNode; use nym_sdk::{ @@ -10,24 +10,24 @@ use nym_sdk::{ MixnetClientStorage, OnDiskPersistent, ReplyStorageBackend, StoragePaths, x25519::KeyPair, }, }; - + #[cfg(unix)] use std::os::fd::RawFd; use std::{path::PathBuf, sync::Arc, time::Duration}; use tokio_util::sync::CancellationToken; use typed_builder::TypedBuilder; - + use crate::error::RegistrationClientError; - + const VPN_AVERAGE_PACKET_DELAY: Duration = Duration::from_millis(15); const MIXNET_CLIENT_STARTUP_TIMEOUT: Duration = Duration::from_secs(30); - + #[derive(Clone)] pub struct NymNodeWithKeys { pub node: NymNode, pub keys: Arc, } - + #[derive(TypedBuilder)] pub struct BuilderConfig { pub entry_node: NymNodeWithKeys, @@ -44,27 +44,29 @@ pub struct BuilderConfig { #[cfg(unix)] pub connection_fd_callback: Arc, } - + #[derive(Clone, Default, Debug, Eq, PartialEq)] pub struct MixnetClientConfig { /// Disable Poission process rate limiting of outbound traffic. - pub disable_poisson_rate: bool, - + pub disable_real_traffic_poisson_process: bool, + /// Disable constant rate background loop cover traffic pub disable_background_cover_traffic: bool, - + /// The minimum performance of mixnodes to use. pub min_mixnode_performance: Option, - + /// The minimum performance of gateways to use. pub min_gateway_performance: Option, + ///Setting optionally the poisson rate for cover traffic stream - pub poisson_rate: Option, + pub loop_cover_traffic_average_delay: Option, + /// Average packet delay in milliseconds. - pub average_packet_delay: Option, - + pub average_packet_delay: Option, + /// Average message sending delay in milliseconds. - pub message_sending_average_delay: Option, + pub message_sending_average_delay: Option, } impl BuilderConfig { pub fn mixnet_client_debug_config(&self) -> DebugConfig { @@ -74,16 +76,16 @@ impl BuilderConfig { mixnet_debug_config(&self.mixnet_client_config) } } - + pub async fn setup_storage( &self, ) -> Result, RegistrationClientError> { if let Some(path) = &self.data_path { tracing::debug!("Using custom key storage path: {}", path.display()); - + let storage_paths = StoragePaths::new_from_dir(path) .map_err(|err| RegistrationClientError::BuildMixnetClient(Box::new(err)))?; - + let mixnet_client_storage = storage_paths .initialise_persistent_storage(&self.mixnet_client_debug_config()) .await @@ -92,13 +94,13 @@ impl BuilderConfig { .persistent_credential_storage() .await .map_err(|err| RegistrationClientError::BuildMixnetClient(Box::new(err)))?; - + Ok(Some((mixnet_client_storage, credential_storage))) } else { Ok(None) } } - + pub async fn build_and_connect_mixnet_client( self, builder: MixnetClientBuilder, @@ -118,7 +120,7 @@ impl BuilderConfig { } else { RememberMe::new_mixnet() }; - + let builder = builder .with_user_agent(self.user_agent) .request_gateway(self.entry_node.node.identity.to_string()) @@ -128,10 +130,10 @@ impl BuilderConfig { .no_hostname(true) .with_remember_me(remember_me) .custom_topology_provider(self.custom_topology_provider); - + #[cfg(unix)] let builder = builder.with_connection_fd_callback(self.connection_fd_callback); - + builder .build() .map_err(|err| RegistrationClientError::BuildMixnetClient(Box::new(err)))? @@ -140,12 +142,12 @@ impl BuilderConfig { .map_err(|err| RegistrationClientError::ConnectToMixnet(Box::new(err))) } } - + fn two_hop_debug_config(mixnet_client_config: &MixnetClientConfig) -> DebugConfig { let mut debug_config = DebugConfig::default(); - + debug_config.traffic.average_packet_delay = VPN_AVERAGE_PACKET_DELAY; - + // We disable mix hops for the mixnet connection. debug_config.traffic.disable_mix_hops = true; // Always disable poisson process for outbound traffic in wireguard. @@ -154,60 +156,73 @@ fn two_hop_debug_config(mixnet_client_config: &MixnetClientConfig) -> DebugConfi .disable_main_poisson_packet_distribution = true; // Always disable background cover traffic in wireguard. debug_config.cover_traffic.disable_loop_cover_traffic_stream = true; - + if let Some(min_mixnode_performance) = mixnet_client_config.min_mixnode_performance { debug_config.topology.minimum_mixnode_performance = min_mixnode_performance; } - + if let Some(min_gateway_performance) = mixnet_client_config.min_gateway_performance { debug_config.topology.minimum_gateway_performance = min_gateway_performance; } if let Some(avg_packet_ms) = mixnet_client_config.average_packet_delay { - debug_config.traffic.average_packet_delay = Duration::from_millis(avg_packet_ms as u64); - debug_config.acknowledgements.average_ack_delay = Duration::from_millis(avg_packet_ms as u64); - + debug_config.traffic.average_packet_delay = avg_packet_ms; + debug_config.acknowledgements.average_ack_delay = avg_packet_ms; } - + if let Some(msg_delay_ms) = mixnet_client_config.message_sending_average_delay { - debug_config.traffic.message_sending_average_delay = Duration::from_millis(msg_delay_ms as u64); - } log_mixnet_client_config(&debug_config); + debug_config.traffic.message_sending_average_delay = msg_delay_ms; + } + log_mixnet_client_config(&debug_config); debug_config } - + fn mixnet_debug_config(mixnet_client_config: &MixnetClientConfig) -> DebugConfig { let mut debug_config = DebugConfig::default(); debug_config.traffic.average_packet_delay = VPN_AVERAGE_PACKET_DELAY; - + debug_config .traffic - .disable_main_poisson_packet_distribution = mixnet_client_config.disable_poisson_rate; - + .disable_main_poisson_packet_distribution = + mixnet_client_config.disable_real_traffic_poisson_process; + debug_config.cover_traffic.disable_loop_cover_traffic_stream = mixnet_client_config.disable_background_cover_traffic; - + if let Some(min_mixnode_performance) = mixnet_client_config.min_mixnode_performance { debug_config.topology.minimum_mixnode_performance = min_mixnode_performance; } - + if let Some(min_gateway_performance) = mixnet_client_config.min_gateway_performance { debug_config.topology.minimum_gateway_performance = min_gateway_performance; } if let Some(avg_packet_ms) = mixnet_client_config.average_packet_delay { - debug_config.traffic.average_packet_delay = Duration::from_millis(avg_packet_ms as u64); - debug_config.acknowledgements.average_ack_delay=Duration::from_millis(avg_packet_ms as u64); + debug_config.traffic.average_packet_delay = avg_packet_ms; + debug_config.acknowledgements.average_ack_delay = avg_packet_ms; } - + if let Some(msg_delay_ms) = mixnet_client_config.message_sending_average_delay { - debug_config.traffic.message_sending_average_delay = Duration::from_millis(msg_delay_ms as u64); + if msg_delay_ms.is_zero() { + debug_config + .traffic + .disable_main_poisson_packet_distribution = true; + } else { + debug_config.traffic.message_sending_average_delay = msg_delay_ms; + } } - if let Some(poisson_rate) = mixnet_client_config.poisson_rate { - let duration = std::time::Duration::from_millis((poisson_rate as f64).round() as u64); - debug_config.cover_traffic.loop_cover_traffic_average_delay = duration; - - } log_mixnet_client_config(&debug_config); + if let Some(loop_cover_traffic_average_delay) = + mixnet_client_config.loop_cover_traffic_average_delay + { + if loop_cover_traffic_average_delay.is_zero() { + debug_config.cover_traffic.disable_loop_cover_traffic_stream = true; + } else { + debug_config.cover_traffic.loop_cover_traffic_average_delay = + loop_cover_traffic_average_delay; + } + } + log_mixnet_client_config(&debug_config); debug_config } - + fn log_mixnet_client_config(debug_config: &DebugConfig) { tracing::info!( "mixnet client poisson rate limiting: {}", @@ -217,35 +232,65 @@ fn log_mixnet_client_config(debug_config: &DebugConfig) { .disable_main_poisson_packet_distribution ) ); - + tracing::info!( "mixnet client background loop cover traffic stream: {}", true_to_disabled(debug_config.cover_traffic.disable_loop_cover_traffic_stream) ); - + tracing::info!( "mixnet client minimum mixnode performance: {}", debug_config.topology.minimum_mixnode_performance, ); - + tracing::info!( "mixnet client minimum gateway performance: {}", debug_config.topology.minimum_gateway_performance, ); + + tracing::info!( + "mixnet client loop cover traffic average delay: {} ms", + debug_config + .cover_traffic + .loop_cover_traffic_average_delay + .as_millis() + ); + + tracing::info!( + "mixnet client average packet delay: {} ms", + debug_config.traffic.average_packet_delay.as_millis() + ); + + tracing::info!( + "mixnet client message sending average delay: {} ms", + debug_config + .traffic + .message_sending_average_delay + .as_millis() + ); + + tracing::info!( + "mixnet client disable_loop_cover_traffic_average_delay: {}", + true_to_disabled( + debug_config + .traffic + .disable_main_poisson_packet_distribution + ) + ); } - + fn true_to_disabled(val: bool) -> &'static str { if val { "disabled" } else { "enabled" } } - + #[cfg(test)] mod tests { use super::*; - + #[test] fn test_mixnet_client_config_default_values() { let config = MixnetClientConfig::default(); - assert!(!config.disable_poisson_rate); + assert!(!config.disable_real_traffic_poisson_process); assert!(!config.disable_background_cover_traffic); assert_eq!(config.min_mixnode_performance, None); assert_eq!(config.min_gateway_performance, None);