11// Copyright 2025 - Nym Technologies SA <[email protected] > 22// SPDX-License-Identifier: GPL-3.0-only
3-
3+
44use nym_credential_storage:: persistent_storage:: PersistentStorage ;
55use nym_registration_common:: NymNode ;
66use nym_sdk:: {
@@ -10,24 +10,24 @@ use nym_sdk::{
1010 MixnetClientStorage , OnDiskPersistent , ReplyStorageBackend , StoragePaths , x25519:: KeyPair ,
1111 } ,
1212} ;
13-
13+
1414#[ cfg( unix) ]
1515use std:: os:: fd:: RawFd ;
1616use std:: { path:: PathBuf , sync:: Arc , time:: Duration } ;
1717use tokio_util:: sync:: CancellationToken ;
1818use typed_builder:: TypedBuilder ;
19-
19+
2020use crate :: error:: RegistrationClientError ;
21-
21+
2222const VPN_AVERAGE_PACKET_DELAY : Duration = Duration :: from_millis ( 15 ) ;
2323const MIXNET_CLIENT_STARTUP_TIMEOUT : Duration = Duration :: from_secs ( 30 ) ;
24-
24+
2525#[ derive( Clone ) ]
2626pub struct NymNodeWithKeys {
2727 pub node : NymNode ,
2828 pub keys : Arc < KeyPair > ,
2929}
30-
30+
3131#[ derive( TypedBuilder ) ]
3232pub struct BuilderConfig {
3333 pub entry_node : NymNodeWithKeys ,
@@ -44,27 +44,27 @@ pub struct BuilderConfig {
4444 #[ cfg( unix) ]
4545 pub connection_fd_callback : Arc < dyn Fn ( RawFd ) + Send + Sync > ,
4646}
47-
47+
4848#[ derive( Clone , Default , Debug , Eq , PartialEq ) ]
4949pub struct MixnetClientConfig {
5050 /// Disable Poission process rate limiting of outbound traffic.
5151 pub disable_real_traffic_poisson_process : bool ,
52-
52+
5353 /// Disable constant rate background loop cover traffic
5454 pub disable_background_cover_traffic : bool ,
55-
55+
5656 /// The minimum performance of mixnodes to use.
5757 pub min_mixnode_performance : Option < u8 > ,
58-
58+
5959 /// The minimum performance of gateways to use.
6060 pub min_gateway_performance : Option < u8 > ,
61-
61+
6262 ///Setting optionally the poisson rate for cover traffic stream
6363 pub loop_cover_traffic_average_delay : Option < Duration > ,
64-
64+
6565 /// Average packet delay in milliseconds.
6666 pub average_packet_delay : Option < Duration > ,
67-
67+
6868 /// Average message sending delay in milliseconds.
6969 pub message_sending_average_delay : Option < Duration > ,
7070}
@@ -76,16 +76,16 @@ impl BuilderConfig {
7676 mixnet_debug_config ( & self . mixnet_client_config )
7777 }
7878 }
79-
79+
8080 pub async fn setup_storage (
8181 & self ,
8282 ) -> Result < Option < ( OnDiskPersistent , PersistentStorage ) > , RegistrationClientError > {
8383 if let Some ( path) = & self . data_path {
8484 tracing:: debug!( "Using custom key storage path: {}" , path. display( ) ) ;
85-
85+
8686 let storage_paths = StoragePaths :: new_from_dir ( path)
8787 . map_err ( |err| RegistrationClientError :: BuildMixnetClient ( Box :: new ( err) ) ) ?;
88-
88+
8989 let mixnet_client_storage = storage_paths
9090 . initialise_persistent_storage ( & self . mixnet_client_debug_config ( ) )
9191 . await
@@ -94,13 +94,13 @@ impl BuilderConfig {
9494 . persistent_credential_storage ( )
9595 . await
9696 . map_err ( |err| RegistrationClientError :: BuildMixnetClient ( Box :: new ( err) ) ) ?;
97-
97+
9898 Ok ( Some ( ( mixnet_client_storage, credential_storage) ) )
9999 } else {
100100 Ok ( None )
101101 }
102102 }
103-
103+
104104 pub async fn build_and_connect_mixnet_client < S > (
105105 self ,
106106 builder : MixnetClientBuilder < S > ,
@@ -120,7 +120,7 @@ impl BuilderConfig {
120120 } else {
121121 RememberMe :: new_mixnet ( )
122122 } ;
123-
123+
124124 let builder = builder
125125 . with_user_agent ( self . user_agent )
126126 . request_gateway ( self . entry_node . node . identity . to_string ( ) )
@@ -130,10 +130,10 @@ impl BuilderConfig {
130130 . no_hostname ( true )
131131 . with_remember_me ( remember_me)
132132 . custom_topology_provider ( self . custom_topology_provider ) ;
133-
133+
134134 #[ cfg( unix) ]
135135 let builder = builder. with_connection_fd_callback ( self . connection_fd_callback ) ;
136-
136+
137137 builder
138138 . build ( )
139139 . map_err ( |err| RegistrationClientError :: BuildMixnetClient ( Box :: new ( err) ) ) ?
@@ -142,12 +142,12 @@ impl BuilderConfig {
142142 . map_err ( |err| RegistrationClientError :: ConnectToMixnet ( Box :: new ( err) ) )
143143 }
144144}
145-
145+
146146fn two_hop_debug_config ( mixnet_client_config : & MixnetClientConfig ) -> DebugConfig {
147147 let mut debug_config = DebugConfig :: default ( ) ;
148-
148+
149149 debug_config. traffic . average_packet_delay = VPN_AVERAGE_PACKET_DELAY ;
150-
150+
151151 // We disable mix hops for the mixnet connection.
152152 debug_config. traffic . disable_mix_hops = true ;
153153 // Always disable poisson process for outbound traffic in wireguard.
@@ -156,50 +156,50 @@ fn two_hop_debug_config(mixnet_client_config: &MixnetClientConfig) -> DebugConfi
156156 . disable_main_poisson_packet_distribution = true ;
157157 // Always disable background cover traffic in wireguard.
158158 debug_config. cover_traffic . disable_loop_cover_traffic_stream = true ;
159-
159+
160160 if let Some ( min_mixnode_performance) = mixnet_client_config. min_mixnode_performance {
161161 debug_config. topology . minimum_mixnode_performance = min_mixnode_performance;
162162 }
163-
163+
164164 if let Some ( min_gateway_performance) = mixnet_client_config. min_gateway_performance {
165165 debug_config. topology . minimum_gateway_performance = min_gateway_performance;
166166 }
167167 if let Some ( avg_packet_ms) = mixnet_client_config. average_packet_delay {
168168 debug_config. traffic . average_packet_delay = avg_packet_ms;
169169 debug_config. acknowledgements . average_ack_delay = avg_packet_ms;
170170 }
171-
171+
172172 if let Some ( msg_delay_ms) = mixnet_client_config. message_sending_average_delay {
173173 debug_config. traffic . message_sending_average_delay = msg_delay_ms;
174174 }
175175 log_mixnet_client_config ( & debug_config) ;
176176 debug_config
177177}
178-
178+
179179fn mixnet_debug_config ( mixnet_client_config : & MixnetClientConfig ) -> DebugConfig {
180180 let mut debug_config = DebugConfig :: default ( ) ;
181181 debug_config. traffic . average_packet_delay = VPN_AVERAGE_PACKET_DELAY ;
182-
182+
183183 debug_config
184184 . traffic
185185 . disable_main_poisson_packet_distribution =
186186 mixnet_client_config. disable_real_traffic_poisson_process ;
187-
187+
188188 debug_config. cover_traffic . disable_loop_cover_traffic_stream =
189189 mixnet_client_config. disable_background_cover_traffic ;
190-
190+
191191 if let Some ( min_mixnode_performance) = mixnet_client_config. min_mixnode_performance {
192192 debug_config. topology . minimum_mixnode_performance = min_mixnode_performance;
193193 }
194-
194+
195195 if let Some ( min_gateway_performance) = mixnet_client_config. min_gateway_performance {
196196 debug_config. topology . minimum_gateway_performance = min_gateway_performance;
197197 }
198198 if let Some ( avg_packet_ms) = mixnet_client_config. average_packet_delay {
199199 debug_config. traffic . average_packet_delay = avg_packet_ms;
200200 debug_config. acknowledgements . average_ack_delay = avg_packet_ms;
201201 }
202-
202+
203203 if let Some ( msg_delay_ms) = mixnet_client_config. message_sending_average_delay {
204204 if msg_delay_ms. is_zero ( ) {
205205 debug_config
@@ -222,7 +222,7 @@ fn mixnet_debug_config(mixnet_client_config: &MixnetClientConfig) -> DebugConfig
222222 log_mixnet_client_config ( & debug_config) ;
223223 debug_config
224224}
225-
225+
226226fn log_mixnet_client_config ( debug_config : & DebugConfig ) {
227227 tracing:: info!(
228228 "mixnet client poisson rate limiting: {}" ,
@@ -232,43 +232,43 @@ fn log_mixnet_client_config(debug_config: &DebugConfig) {
232232 . disable_main_poisson_packet_distribution
233233 )
234234 ) ;
235-
235+
236236 tracing:: info!(
237237 "mixnet client background loop cover traffic stream: {}" ,
238238 true_to_disabled( debug_config. cover_traffic. disable_loop_cover_traffic_stream)
239239 ) ;
240-
240+
241241 tracing:: info!(
242242 "mixnet client minimum mixnode performance: {}" ,
243243 debug_config. topology. minimum_mixnode_performance,
244244 ) ;
245-
245+
246246 tracing:: info!(
247247 "mixnet client minimum gateway performance: {}" ,
248248 debug_config. topology. minimum_gateway_performance,
249249 ) ;
250-
250+
251251 tracing:: info!(
252252 "mixnet client loop cover traffic average delay: {} ms" ,
253253 debug_config
254254 . cover_traffic
255255 . loop_cover_traffic_average_delay
256256 . as_millis( )
257257 ) ;
258-
258+
259259 tracing:: info!(
260260 "mixnet client average packet delay: {} ms" ,
261261 debug_config. traffic. average_packet_delay. as_millis( )
262262 ) ;
263-
263+
264264 tracing:: info!(
265265 "mixnet client message sending average delay: {} ms" ,
266266 debug_config
267267 . traffic
268268 . message_sending_average_delay
269269 . as_millis( )
270270 ) ;
271-
271+
272272 tracing:: info!(
273273 "mixnet client disable_loop_cover_traffic_average_delay: {}" ,
274274 true_to_disabled(
@@ -278,15 +278,15 @@ fn log_mixnet_client_config(debug_config: &DebugConfig) {
278278 )
279279 ) ;
280280}
281-
281+
282282fn true_to_disabled ( val : bool ) -> & ' static str {
283283 if val { "disabled" } else { "enabled" }
284284}
285-
285+
286286#[ cfg( test) ]
287287mod tests {
288288 use super :: * ;
289-
289+
290290 #[ test]
291291 fn test_mixnet_client_config_default_values ( ) {
292292 let config = MixnetClientConfig :: default ( ) ;
0 commit comments