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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Routing Table Auditor
//!
//! Audits the DHT routing table for health issues including stale entries,
//! bucket imbalances, and unreachable peers.

/// Severity level of an audit finding.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum AuditSeverity {
    /// Informational finding; no action required.
    Info = 0,
    /// Warning finding; should be investigated.
    Warning = 1,
    /// Error finding; requires immediate attention.
    Error = 2,
}

/// A single finding produced by a routing-table audit run.
#[derive(Debug, Clone)]
pub struct AuditFinding {
    /// How serious this finding is.
    pub severity: AuditSeverity,
    /// Machine-readable category such as `"stale_entry"` or `"empty_buckets"`.
    pub category: String,
    /// Human-readable description of the finding.
    pub description: String,
    /// Peer IDs (or bucket identifiers) involved in this finding.
    pub affected_peers: Vec<String>,
    /// Unix timestamp (seconds) at which the finding was detected.
    pub detected_at_secs: u64,
}

/// Default k-bucket capacity as defined by the Kademlia paper.
pub const DEFAULT_MAX_CAPACITY: usize = 20;

/// Information about a single Kademlia k-bucket.
#[derive(Debug, Clone)]
pub struct BucketInfo {
    /// Kademlia bucket index (0–255).
    pub bucket_index: usize,
    /// Number of peers currently tracked in this bucket.
    pub peer_count: usize,
    /// Maximum number of peers this bucket can hold.
    pub max_capacity: usize,
    /// Unix timestamp (seconds) of the last time this bucket was refreshed.
    pub last_refreshed_secs: u64,
}

impl BucketInfo {
    /// Returns `true` when the bucket has reached its maximum capacity.
    #[must_use]
    pub fn is_full(&self) -> bool {
        self.peer_count >= self.max_capacity
    }

    /// Returns `true` when the bucket contains no peers.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.peer_count == 0
    }

    /// Returns the ratio of current peers to maximum capacity (0.0 – 1.0+).
    #[must_use]
    pub fn fill_ratio(&self) -> f64 {
        if self.max_capacity == 0 {
            return 0.0;
        }
        self.peer_count as f64 / self.max_capacity as f64
    }
}

/// Configuration for the [`RoutingTableAuditor`].
#[derive(Debug, Clone)]
pub struct AuditorConfig {
    /// An entry is considered stale if it has not been refreshed within this
    /// many seconds (default: 3600 = 1 hour).
    pub stale_threshold_secs: u64,
    /// Emit a warning when the number of empty buckets exceeds this value
    /// (default: 10).
    pub max_empty_buckets: usize,
    /// Emit an error when the total number of tracked peers falls below this
    /// value (default: 3).
    pub min_total_peers: usize,
}

impl Default for AuditorConfig {
    fn default() -> Self {
        Self {
            stale_threshold_secs: 3600,
            max_empty_buckets: 10,
            min_total_peers: 3,
        }
    }
}

/// Audits a DHT routing table represented as a collection of [`BucketInfo`]
/// entries and produces [`AuditFinding`] items describing any health issues.
#[derive(Debug, Clone)]
pub struct RoutingTableAuditor {
    /// Up to 256 Kademlia k-buckets tracked by this auditor.
    pub buckets: Vec<BucketInfo>,
    /// Configuration driving audit thresholds.
    pub config: AuditorConfig,
}

impl RoutingTableAuditor {
    /// Creates a new auditor with the given configuration and an empty bucket
    /// list.
    #[must_use]
    pub fn new(config: AuditorConfig) -> Self {
        Self {
            buckets: Vec::new(),
            config,
        }
    }

    /// Appends a bucket to the auditor's bucket list.
    pub fn add_bucket(&mut self, bucket: BucketInfo) {
        self.buckets.push(bucket);
    }

    /// Updates an existing bucket identified by `bucket_index`.  If no bucket
    /// with that index exists a new one is inserted with
    /// [`DEFAULT_MAX_CAPACITY`].
    pub fn update_bucket(&mut self, bucket_index: usize, peer_count: usize, now_secs: u64) {
        if let Some(b) = self
            .buckets
            .iter_mut()
            .find(|b| b.bucket_index == bucket_index)
        {
            b.peer_count = peer_count;
            b.last_refreshed_secs = now_secs;
        } else {
            self.buckets.push(BucketInfo {
                bucket_index,
                peer_count,
                max_capacity: DEFAULT_MAX_CAPACITY,
                last_refreshed_secs: now_secs,
            });
        }
    }

    /// Runs the full audit at time `now_secs` and returns all findings sorted
    /// by severity descending (most severe first).
    ///
    /// Checks performed:
    /// 1. **insufficient_peers** (Error) – total peer count < `min_total_peers`.
    /// 2. **empty_buckets** (Warning) – count of empty buckets > `max_empty_buckets`.
    /// 3. **stale_bucket** (Warning) – each bucket whose `last_refreshed_secs`
    ///    is older than `stale_threshold_secs`.
    /// 4. **full_bucket** (Info) – each bucket at maximum capacity.
    #[must_use]
    pub fn audit(&self, now_secs: u64) -> Vec<AuditFinding> {
        let mut findings: Vec<AuditFinding> = Vec::new();

        // 1. Insufficient peers
        let total = self.total_peers();
        if total < self.config.min_total_peers {
            findings.push(AuditFinding {
                severity: AuditSeverity::Error,
                category: "insufficient_peers".to_string(),
                description: format!(
                    "Routing table has only {} peer(s); minimum required is {}.",
                    total, self.config.min_total_peers
                ),
                affected_peers: Vec::new(),
                detected_at_secs: now_secs,
            });
        }

        // 2. Too many empty buckets
        let empty_count = self.empty_bucket_count();
        if empty_count > self.config.max_empty_buckets {
            findings.push(AuditFinding {
                severity: AuditSeverity::Warning,
                category: "empty_buckets".to_string(),
                description: format!(
                    "{} bucket(s) are empty; threshold is {}.",
                    empty_count, self.config.max_empty_buckets
                ),
                affected_peers: Vec::new(),
                detected_at_secs: now_secs,
            });
        }

        // 3. Stale buckets
        for bucket in &self.buckets {
            let age = now_secs.saturating_sub(bucket.last_refreshed_secs);
            if age > self.config.stale_threshold_secs {
                findings.push(AuditFinding {
                    severity: AuditSeverity::Warning,
                    category: "stale_bucket".to_string(),
                    description: format!(
                        "Bucket {} has not been refreshed for {} second(s) (threshold: {}).",
                        bucket.bucket_index, age, self.config.stale_threshold_secs
                    ),
                    affected_peers: vec![format!("bucket-{}", bucket.bucket_index)],
                    detected_at_secs: now_secs,
                });
            }
        }

        // 4. Full buckets
        for bucket in &self.buckets {
            if bucket.is_full() {
                findings.push(AuditFinding {
                    severity: AuditSeverity::Info,
                    category: "full_bucket".to_string(),
                    description: format!(
                        "Bucket {} is at full capacity ({}/{}).",
                        bucket.bucket_index, bucket.peer_count, bucket.max_capacity
                    ),
                    affected_peers: vec![format!("bucket-{}", bucket.bucket_index)],
                    detected_at_secs: now_secs,
                });
            }
        }

        // Sort by severity descending (Error > Warning > Info)
        findings.sort_by_key(|f| std::cmp::Reverse(f.severity));

        findings
    }

    /// Returns the total number of peers across all tracked buckets.
    #[must_use]
    pub fn total_peers(&self) -> usize {
        self.buckets.iter().map(|b| b.peer_count).sum()
    }

    /// Returns the number of buckets that currently contain no peers.
    #[must_use]
    pub fn empty_bucket_count(&self) -> usize {
        self.buckets.iter().filter(|b| b.is_empty()).count()
    }

    /// Returns the number of buckets that have not been refreshed within the
    /// configured `stale_threshold_secs`.
    #[must_use]
    pub fn stale_bucket_count(&self, now_secs: u64) -> usize {
        self.buckets
            .iter()
            .filter(|b| {
                now_secs.saturating_sub(b.last_refreshed_secs) > self.config.stale_threshold_secs
            })
            .count()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    fn make_bucket(index: usize, peer_count: usize, refreshed: u64) -> BucketInfo {
        BucketInfo {
            bucket_index: index,
            peer_count,
            max_capacity: DEFAULT_MAX_CAPACITY,
            last_refreshed_secs: refreshed,
        }
    }

    fn default_auditor() -> RoutingTableAuditor {
        RoutingTableAuditor::new(AuditorConfig::default())
    }

    // ── add_bucket ───────────────────────────────────────────────────────────

    #[test]
    fn test_add_bucket_increases_count() {
        let mut auditor = default_auditor();
        assert_eq!(auditor.buckets.len(), 0);
        auditor.add_bucket(make_bucket(0, 5, 1000));
        assert_eq!(auditor.buckets.len(), 1);
        auditor.add_bucket(make_bucket(1, 3, 1000));
        assert_eq!(auditor.buckets.len(), 2);
    }

    #[test]
    fn test_add_bucket_stores_correct_data() {
        let mut auditor = default_auditor();
        auditor.add_bucket(make_bucket(42, 7, 999));
        let b = &auditor.buckets[0];
        assert_eq!(b.bucket_index, 42);
        assert_eq!(b.peer_count, 7);
        assert_eq!(b.last_refreshed_secs, 999);
    }

    // ── update_bucket ────────────────────────────────────────────────────────

    #[test]
    fn test_update_bucket_updates_existing() {
        let mut auditor = default_auditor();
        auditor.add_bucket(make_bucket(5, 2, 500));
        auditor.update_bucket(5, 10, 1000);
        assert_eq!(auditor.buckets.len(), 1, "no new bucket should be inserted");
        assert_eq!(auditor.buckets[0].peer_count, 10);
        assert_eq!(auditor.buckets[0].last_refreshed_secs, 1000);
    }

    #[test]
    fn test_update_bucket_inserts_when_missing() {
        let mut auditor = default_auditor();
        auditor.update_bucket(7, 4, 2000);
        assert_eq!(auditor.buckets.len(), 1);
        assert_eq!(auditor.buckets[0].bucket_index, 7);
        assert_eq!(auditor.buckets[0].peer_count, 4);
        assert_eq!(auditor.buckets[0].max_capacity, DEFAULT_MAX_CAPACITY);
    }

    #[test]
    fn test_update_bucket_upsert_multiple() {
        let mut auditor = default_auditor();
        auditor.update_bucket(1, 3, 100);
        auditor.update_bucket(2, 5, 200);
        auditor.update_bucket(1, 8, 300); // update existing
        assert_eq!(auditor.buckets.len(), 2);
        let b1 = auditor
            .buckets
            .iter()
            .find(|b| b.bucket_index == 1)
            .expect("test: bucket with index 1 should exist after upsert");
        assert_eq!(b1.peer_count, 8);
        assert_eq!(b1.last_refreshed_secs, 300);
    }

    // ── total_peers ──────────────────────────────────────────────────────────

    #[test]
    fn test_total_peers_empty_table() {
        let auditor = default_auditor();
        assert_eq!(auditor.total_peers(), 0);
    }

    #[test]
    fn test_total_peers_sum() {
        let mut auditor = default_auditor();
        auditor.add_bucket(make_bucket(0, 5, 1000));
        auditor.add_bucket(make_bucket(1, 3, 1000));
        auditor.add_bucket(make_bucket(2, 0, 1000));
        assert_eq!(auditor.total_peers(), 8);
    }

    // ── empty_bucket_count ───────────────────────────────────────────────────

    #[test]
    fn test_empty_bucket_count() {
        let mut auditor = default_auditor();
        auditor.add_bucket(make_bucket(0, 0, 1000));
        auditor.add_bucket(make_bucket(1, 5, 1000));
        auditor.add_bucket(make_bucket(2, 0, 1000));
        assert_eq!(auditor.empty_bucket_count(), 2);
    }

    // ── stale_bucket_count ───────────────────────────────────────────────────

    #[test]
    fn test_stale_bucket_count() {
        let mut auditor = RoutingTableAuditor::new(AuditorConfig {
            stale_threshold_secs: 600,
            ..AuditorConfig::default()
        });
        let now = 10_000u64;
        // refreshed 500 s ago → not stale
        auditor.add_bucket(make_bucket(0, 2, now - 500));
        // refreshed 601 s ago → stale
        auditor.add_bucket(make_bucket(1, 2, now - 601));
        // refreshed 700 s ago → stale
        auditor.add_bucket(make_bucket(2, 2, now - 700));
        assert_eq!(auditor.stale_bucket_count(now), 2);
    }

    // ── fill_ratio, is_full, is_empty ────────────────────────────────────────

    #[test]
    fn test_fill_ratio_zero_peers() {
        let b = make_bucket(0, 0, 0);
        assert!((b.fill_ratio() - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_fill_ratio_half_full() {
        let b = make_bucket(0, 10, 0); // max_capacity = 20
        assert!((b.fill_ratio() - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn test_fill_ratio_full() {
        let b = make_bucket(0, 20, 0);
        assert!((b.fill_ratio() - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_is_full_and_is_empty() {
        let empty = make_bucket(0, 0, 0);
        let full = make_bucket(1, 20, 0);
        let partial = make_bucket(2, 10, 0);

        assert!(empty.is_empty());
        assert!(!empty.is_full());

        assert!(full.is_full());
        assert!(!full.is_empty());

        assert!(!partial.is_empty());
        assert!(!partial.is_full());
    }

    // ── audit: insufficient_peers → Error ───────────────────────────────────

    #[test]
    fn test_audit_insufficient_peers_error() {
        let mut auditor = RoutingTableAuditor::new(AuditorConfig {
            min_total_peers: 5,
            max_empty_buckets: 100, // suppress empty-bucket warning
            stale_threshold_secs: 9999,
        });
        auditor.add_bucket(make_bucket(0, 2, 1000));
        let findings = auditor.audit(2000);
        let errors: Vec<_> = findings
            .iter()
            .filter(|f| f.severity == AuditSeverity::Error && f.category == "insufficient_peers")
            .collect();
        assert_eq!(errors.len(), 1, "expected one insufficient_peers error");
    }

    // ── audit: many empty buckets → Warning ──────────────────────────────────

    #[test]
    fn test_audit_many_empty_buckets_warning() {
        let mut auditor = RoutingTableAuditor::new(AuditorConfig {
            max_empty_buckets: 2,
            min_total_peers: 0, // suppress peer error
            stale_threshold_secs: 9999,
        });
        // Add 5 peers so insufficient_peers won't fire, but 3 empty buckets
        auditor.add_bucket(make_bucket(0, 5, 1000));
        auditor.add_bucket(make_bucket(1, 0, 1000));
        auditor.add_bucket(make_bucket(2, 0, 1000));
        auditor.add_bucket(make_bucket(3, 0, 1000));
        let findings = auditor.audit(2000);
        let warnings: Vec<_> = findings
            .iter()
            .filter(|f| f.severity == AuditSeverity::Warning && f.category == "empty_buckets")
            .collect();
        assert_eq!(warnings.len(), 1, "expected one empty_buckets warning");
    }

    // ── audit: stale bucket → Warning ───────────────────────────────────────

    #[test]
    fn test_audit_stale_bucket_warning() {
        let now = 10_000u64;
        let mut auditor = RoutingTableAuditor::new(AuditorConfig {
            stale_threshold_secs: 500,
            min_total_peers: 0,
            max_empty_buckets: 100,
        });
        auditor.add_bucket(make_bucket(0, 3, now - 600)); // stale
        auditor.add_bucket(make_bucket(1, 3, now - 100)); // fresh
        let findings = auditor.audit(now);
        let stale_warnings: Vec<_> = findings
            .iter()
            .filter(|f| f.category == "stale_bucket")
            .collect();
        assert_eq!(stale_warnings.len(), 1);
        assert_eq!(stale_warnings[0].affected_peers, vec!["bucket-0"]);
    }

    // ── audit: full bucket → Info ────────────────────────────────────────────

    #[test]
    fn test_audit_full_bucket_info() {
        let now = 5000u64;
        let mut auditor = RoutingTableAuditor::new(AuditorConfig {
            min_total_peers: 0,
            max_empty_buckets: 100,
            stale_threshold_secs: 9999,
        });
        auditor.add_bucket(make_bucket(3, DEFAULT_MAX_CAPACITY, now));
        let findings = auditor.audit(now);
        let info: Vec<_> = findings
            .iter()
            .filter(|f| f.severity == AuditSeverity::Info && f.category == "full_bucket")
            .collect();
        assert_eq!(info.len(), 1);
        assert_eq!(info[0].affected_peers, vec!["bucket-3"]);
    }

    // ── audit: sort by severity descending ───────────────────────────────────

    #[test]
    fn test_audit_sorted_by_severity_descending() {
        let now = 20_000u64;
        let mut auditor = RoutingTableAuditor::new(AuditorConfig {
            min_total_peers: 10,       // will trigger Error (total = 20 + full)
            max_empty_buckets: 0,      // will trigger Warning (1 empty bucket)
            stale_threshold_secs: 500, // will trigger Warning for old bucket
        });
        // 1 empty bucket → empty_buckets warning
        auditor.add_bucket(make_bucket(0, 0, now));
        // 1 stale bucket
        auditor.add_bucket(make_bucket(1, 2, now - 600));
        // 1 full bucket → full_bucket info
        auditor.add_bucket(make_bucket(2, DEFAULT_MAX_CAPACITY, now));
        // Enough peers so insufficient_peers fires: total = 0+2+20 = 22 >= 10, won't fire
        // Let's make total < 10 → use few peers
        let mut auditor2 = RoutingTableAuditor::new(AuditorConfig {
            min_total_peers: 50,
            max_empty_buckets: 0,
            stale_threshold_secs: 500,
        });
        auditor2.add_bucket(make_bucket(0, 0, now)); // empty → warning
        auditor2.add_bucket(make_bucket(1, 2, now - 600)); // stale → warning
        auditor2.add_bucket(make_bucket(2, DEFAULT_MAX_CAPACITY, now)); // full → info
                                                                        // total = 22 < 50 → error

        let findings = auditor2.audit(now);
        assert!(!findings.is_empty());

        // Verify that no finding has a severity higher than the previous one
        for window in findings.windows(2) {
            assert!(
                window[0].severity >= window[1].severity,
                "findings not sorted descending: {:?} < {:?}",
                window[0].severity,
                window[1].severity
            );
        }

        // First finding must be Error
        assert_eq!(findings[0].severity, AuditSeverity::Error);
        // Last finding must be Info
        assert_eq!(
            findings.last().map(|f| f.severity),
            Some(AuditSeverity::Info)
        );
    }

    // ── AuditSeverity ordering ────────────────────────────────────────────────

    #[test]
    fn test_audit_severity_ordering() {
        assert!(AuditSeverity::Error > AuditSeverity::Warning);
        assert!(AuditSeverity::Warning > AuditSeverity::Info);
        assert_eq!(AuditSeverity::Info, AuditSeverity::Info);
    }

    // ── fill_ratio with zero capacity ────────────────────────────────────────

    #[test]
    fn test_fill_ratio_zero_capacity() {
        let b = BucketInfo {
            bucket_index: 0,
            peer_count: 5,
            max_capacity: 0,
            last_refreshed_secs: 0,
        };
        assert!((b.fill_ratio() - 0.0).abs() < f64::EPSILON);
    }

    // ── no stale buckets when all fresh ──────────────────────────────────────

    #[test]
    fn test_no_stale_buckets_all_fresh() {
        let now = 5000u64;
        let mut auditor = RoutingTableAuditor::new(AuditorConfig {
            stale_threshold_secs: 3600,
            ..AuditorConfig::default()
        });
        auditor.add_bucket(make_bucket(0, 3, now - 100));
        auditor.add_bucket(make_bucket(1, 5, now - 200));
        assert_eq!(auditor.stale_bucket_count(now), 0);
    }
}