ipfrs-network 0.2.0

Peer-to-peer networking layer with libp2p and QUIC for IPFRS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Background mode support with pause/resume functionality
//!
//! This module provides functionality to pause and resume network operations
//! when an application goes to the background (e.g., on mobile devices).
//! This helps save battery and network resources while the app is not active.

use parking_lot::RwLock;
use std::sync::Arc;
use std::time::{Duration, Instant};
use thiserror::Error;
use tracing::{debug, info};

/// Background mode state
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackgroundState {
    /// Network is running normally (foreground)
    Active,
    /// Network is paused (background)
    Paused,
    /// Network is in the process of pausing
    Pausing,
    /// Network is in the process of resuming
    Resuming,
}

/// Configuration for background mode behavior
#[derive(Debug, Clone)]
pub struct BackgroundModeConfig {
    /// Whether to pause DHT queries in background
    pub pause_dht_queries: bool,
    /// Whether to pause provider announcements in background
    pub pause_provider_announcements: bool,
    /// Whether to close idle connections when entering background
    pub close_idle_connections: bool,
    /// Minimum connection age to be considered for closing (when close_idle_connections is true)
    pub idle_connection_threshold: Duration,
    /// Whether to maintain a minimal set of connections in background
    pub keep_minimal_connections: bool,
    /// Number of connections to keep when in background mode
    pub minimal_connection_count: usize,
    /// Whether to reduce DHT query frequency in background
    pub reduce_dht_frequency: bool,
    /// DHT query interval when in background (if reduce_dht_frequency is true)
    pub background_dht_interval: Duration,
}

impl Default for BackgroundModeConfig {
    fn default() -> Self {
        Self {
            pause_dht_queries: false,
            pause_provider_announcements: true,
            close_idle_connections: true,
            idle_connection_threshold: Duration::from_secs(300), // 5 minutes
            keep_minimal_connections: true,
            minimal_connection_count: 3,
            reduce_dht_frequency: true,
            background_dht_interval: Duration::from_secs(300), // 5 minutes
        }
    }
}

impl BackgroundModeConfig {
    /// Mobile configuration - aggressive power saving
    pub fn mobile() -> Self {
        Self {
            pause_dht_queries: true, // Pause all DHT queries
            pause_provider_announcements: true,
            close_idle_connections: true,
            idle_connection_threshold: Duration::from_secs(60), // 1 minute
            keep_minimal_connections: true,
            minimal_connection_count: 2, // Keep only 2 connections
            reduce_dht_frequency: true,
            background_dht_interval: Duration::from_secs(600), // 10 minutes
        }
    }

    /// Server configuration - minimal impact
    pub fn server() -> Self {
        Self {
            pause_dht_queries: false,
            pause_provider_announcements: false,
            close_idle_connections: false,
            idle_connection_threshold: Duration::from_secs(3600), // 1 hour
            keep_minimal_connections: false,
            minimal_connection_count: 0,
            reduce_dht_frequency: false,
            background_dht_interval: Duration::from_secs(60),
        }
    }

    /// Balanced configuration
    pub fn balanced() -> Self {
        Self::default()
    }
}

/// Background mode manager
pub struct BackgroundModeManager {
    config: BackgroundModeConfig,
    state: Arc<RwLock<BackgroundState>>,
    stats: Arc<RwLock<BackgroundModeStats>>,
    /// Time when last state transition occurred
    last_transition: Arc<RwLock<Option<Instant>>>,
    /// Time spent in each state
    time_in_active: Arc<RwLock<Duration>>,
    time_in_paused: Arc<RwLock<Duration>>,
}

/// Background mode statistics
#[derive(Debug, Clone, Default)]
pub struct BackgroundModeStats {
    /// Number of times the network was paused
    pub pause_count: usize,
    /// Number of times the network was resumed
    pub resume_count: usize,
    /// Total time spent in background mode
    pub total_background_time: Duration,
    /// Total time spent in foreground mode
    pub total_foreground_time: Duration,
    /// Number of connections closed when entering background
    pub connections_closed_on_pause: usize,
    /// Number of DHT queries skipped in background
    pub dht_queries_skipped: usize,
}

/// Errors that can occur during background mode operations
#[derive(Debug, Error)]
pub enum BackgroundModeError {
    #[error("Invalid state transition from {from:?} to {to:?}")]
    InvalidStateTransition {
        from: BackgroundState,
        to: BackgroundState,
    },

    #[error("Operation not allowed in current state: {0:?}")]
    OperationNotAllowed(BackgroundState),
}

impl BackgroundModeManager {
    /// Create a new background mode manager
    pub fn new(config: BackgroundModeConfig) -> Self {
        Self {
            config,
            state: Arc::new(RwLock::new(BackgroundState::Active)),
            stats: Arc::new(RwLock::new(BackgroundModeStats::default())),
            last_transition: Arc::new(RwLock::new(Some(Instant::now()))),
            time_in_active: Arc::new(RwLock::new(Duration::ZERO)),
            time_in_paused: Arc::new(RwLock::new(Duration::ZERO)),
        }
    }

    /// Get current background state
    pub fn state(&self) -> BackgroundState {
        *self.state.read()
    }

    /// Check if the network is paused
    pub fn is_paused(&self) -> bool {
        matches!(self.state(), BackgroundState::Paused)
    }

    /// Check if the network is active
    pub fn is_active(&self) -> bool {
        matches!(self.state(), BackgroundState::Active)
    }

    /// Pause network operations (enter background mode)
    pub fn pause(&self) -> Result<(), BackgroundModeError> {
        let current_state = *self.state.read();

        match current_state {
            BackgroundState::Active => {
                info!("Pausing network for background mode");
                *self.state.write() = BackgroundState::Pausing;

                // Update time tracking
                self.update_time_tracking(current_state);

                // Perform pause operations
                self.perform_pause_operations();

                *self.state.write() = BackgroundState::Paused;
                *self.last_transition.write() = Some(Instant::now());

                let mut stats = self.stats.write();
                stats.pause_count += 1;

                debug!("Network paused successfully");
                Ok(())
            }
            BackgroundState::Paused => {
                // Already paused, no-op
                Ok(())
            }
            state => Err(BackgroundModeError::InvalidStateTransition {
                from: state,
                to: BackgroundState::Paused,
            }),
        }
    }

    /// Resume network operations (enter foreground mode)
    pub fn resume(&self) -> Result<(), BackgroundModeError> {
        let current_state = *self.state.read();

        match current_state {
            BackgroundState::Paused => {
                info!("Resuming network from background mode");
                *self.state.write() = BackgroundState::Resuming;

                // Update time tracking
                self.update_time_tracking(current_state);

                // Perform resume operations
                self.perform_resume_operations();

                *self.state.write() = BackgroundState::Active;
                *self.last_transition.write() = Some(Instant::now());

                let mut stats = self.stats.write();
                stats.resume_count += 1;

                debug!("Network resumed successfully");
                Ok(())
            }
            BackgroundState::Active => {
                // Already active, no-op
                Ok(())
            }
            state => Err(BackgroundModeError::InvalidStateTransition {
                from: state,
                to: BackgroundState::Active,
            }),
        }
    }

    /// Perform operations when entering background mode
    fn perform_pause_operations(&self) {
        // These are hooks that the NetworkNode can use
        debug!(
            "Background mode config: pause_dht={}, pause_announcements={}, close_idle={}",
            self.config.pause_dht_queries,
            self.config.pause_provider_announcements,
            self.config.close_idle_connections
        );

        // Actual implementation would be in NetworkNode
        // which would call these methods and perform the operations
    }

    /// Perform operations when resuming from background mode
    fn perform_resume_operations(&self) {
        debug!("Resuming network operations");

        // Actual implementation would be in NetworkNode
        // which would call these methods and perform the operations
    }

    /// Update time tracking when state changes
    fn update_time_tracking(&self, old_state: BackgroundState) {
        if let Some(last_transition) = *self.last_transition.read() {
            let elapsed = last_transition.elapsed();

            match old_state {
                BackgroundState::Active => {
                    *self.time_in_active.write() += elapsed;
                    let mut stats = self.stats.write();
                    stats.total_foreground_time += elapsed;
                }
                BackgroundState::Paused => {
                    *self.time_in_paused.write() += elapsed;
                    let mut stats = self.stats.write();
                    stats.total_background_time += elapsed;
                }
                _ => {}
            }
        }
    }

    /// Check if a DHT query should be allowed in current state
    pub fn should_allow_dht_query(&self) -> bool {
        match self.state() {
            BackgroundState::Active | BackgroundState::Resuming => true,
            BackgroundState::Paused | BackgroundState::Pausing => !self.config.pause_dht_queries,
        }
    }

    /// Check if provider announcements should be allowed in current state
    pub fn should_allow_provider_announcements(&self) -> bool {
        match self.state() {
            BackgroundState::Active | BackgroundState::Resuming => true,
            BackgroundState::Paused | BackgroundState::Pausing => {
                !self.config.pause_provider_announcements
            }
        }
    }

    /// Get configuration
    pub fn config(&self) -> &BackgroundModeConfig {
        &self.config
    }

    /// Get statistics
    pub fn stats(&self) -> BackgroundModeStats {
        // Update current state time before returning stats
        let current_state = *self.state.read();
        self.update_time_tracking(current_state);

        self.stats.read().clone()
    }

    /// Reset statistics
    pub fn reset_stats(&self) {
        *self.stats.write() = BackgroundModeStats::default();
        *self.time_in_active.write() = Duration::ZERO;
        *self.time_in_paused.write() = Duration::ZERO;
        *self.last_transition.write() = Some(Instant::now());
    }

    /// Increment DHT queries skipped counter
    pub fn record_dht_query_skipped(&self) {
        self.stats.write().dht_queries_skipped += 1;
    }

    /// Record connections closed on pause
    pub fn record_connections_closed(&self, count: usize) {
        self.stats.write().connections_closed_on_pause += count;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_background_mode_creation() {
        let manager = BackgroundModeManager::new(BackgroundModeConfig::default());
        assert_eq!(manager.state(), BackgroundState::Active);
        assert!(manager.is_active());
        assert!(!manager.is_paused());
    }

    #[test]
    fn test_pause_resume() {
        let manager = BackgroundModeManager::new(BackgroundModeConfig::default());

        // Pause
        assert!(manager.pause().is_ok());
        assert_eq!(manager.state(), BackgroundState::Paused);
        assert!(manager.is_paused());
        assert!(!manager.is_active());

        // Resume
        assert!(manager.resume().is_ok());
        assert_eq!(manager.state(), BackgroundState::Active);
        assert!(manager.is_active());
        assert!(!manager.is_paused());
    }

    #[test]
    fn test_pause_when_already_paused() {
        let manager = BackgroundModeManager::new(BackgroundModeConfig::default());

        assert!(manager.pause().is_ok());
        assert!(manager.pause().is_ok()); // Should be ok to pause when already paused
        assert_eq!(manager.state(), BackgroundState::Paused);
    }

    #[test]
    fn test_resume_when_already_active() {
        let manager = BackgroundModeManager::new(BackgroundModeConfig::default());

        assert!(manager.resume().is_ok()); // Should be ok to resume when already active
        assert_eq!(manager.state(), BackgroundState::Active);
    }

    #[test]
    fn test_dht_query_allowed_in_active_state() {
        let manager = BackgroundModeManager::new(BackgroundModeConfig::default());
        assert!(manager.should_allow_dht_query());
    }

    #[test]
    fn test_dht_query_behavior_in_paused_state() {
        let config = BackgroundModeConfig {
            pause_dht_queries: true,
            ..Default::default()
        };
        let manager = BackgroundModeManager::new(config);

        manager
            .pause()
            .expect("test: pause should succeed from Active state");
        assert!(!manager.should_allow_dht_query());
    }

    #[test]
    fn test_dht_query_allowed_when_not_paused_in_background() {
        let config = BackgroundModeConfig {
            pause_dht_queries: false,
            ..Default::default()
        };
        let manager = BackgroundModeManager::new(config);

        manager
            .pause()
            .expect("test: pause should succeed from Active state");
        assert!(manager.should_allow_dht_query());
    }

    #[test]
    fn test_provider_announcements_in_active_state() {
        let manager = BackgroundModeManager::new(BackgroundModeConfig::default());
        assert!(manager.should_allow_provider_announcements());
    }

    #[test]
    fn test_provider_announcements_in_paused_state() {
        let manager = BackgroundModeManager::new(BackgroundModeConfig::default());
        manager
            .pause()
            .expect("test: pause should succeed from Active state");
        // Default config pauses announcements
        assert!(!manager.should_allow_provider_announcements());
    }

    #[test]
    fn test_statistics_tracking() {
        let manager = BackgroundModeManager::new(BackgroundModeConfig::default());

        manager
            .pause()
            .expect("test: first pause should succeed from Active state");
        manager
            .resume()
            .expect("test: resume should succeed from Paused state");
        manager
            .pause()
            .expect("test: second pause should succeed from Active state");

        let stats = manager.stats();
        assert_eq!(stats.pause_count, 2);
        assert_eq!(stats.resume_count, 1);
    }

    #[test]
    fn test_mobile_config() {
        let config = BackgroundModeConfig::mobile();
        assert!(config.pause_dht_queries);
        assert!(config.pause_provider_announcements);
        assert!(config.close_idle_connections);
        assert_eq!(config.minimal_connection_count, 2);
    }

    #[test]
    fn test_server_config() {
        let config = BackgroundModeConfig::server();
        assert!(!config.pause_dht_queries);
        assert!(!config.pause_provider_announcements);
        assert!(!config.close_idle_connections);
    }

    #[test]
    fn test_record_dht_query_skipped() {
        let manager = BackgroundModeManager::new(BackgroundModeConfig::default());
        manager.record_dht_query_skipped();
        manager.record_dht_query_skipped();

        let stats = manager.stats();
        assert_eq!(stats.dht_queries_skipped, 2);
    }

    #[test]
    fn test_record_connections_closed() {
        let manager = BackgroundModeManager::new(BackgroundModeConfig::default());
        manager.record_connections_closed(5);

        let stats = manager.stats();
        assert_eq!(stats.connections_closed_on_pause, 5);
    }

    #[test]
    fn test_reset_stats() {
        let manager = BackgroundModeManager::new(BackgroundModeConfig::default());

        manager
            .pause()
            .expect("test: pause should succeed from Active state");
        manager
            .resume()
            .expect("test: resume should succeed from Paused state");
        manager.record_dht_query_skipped();

        manager.reset_stats();

        let stats = manager.stats();
        assert_eq!(stats.pause_count, 0);
        assert_eq!(stats.resume_count, 0);
        assert_eq!(stats.dht_queries_skipped, 0);
    }
}