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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
//! Peer churn tracking and stability scoring for DHT and routing decisions.
//!
//! This module provides [`PeerChurnManager`] which tracks peer join/leave events,
//! computes churn rate over sliding windows, and maintains stability scores to
//! inform DHT and routing decisions.
use std::collections::HashMap;
/// An event representing a peer joining or leaving the network.
#[derive(Clone, Debug, PartialEq)]
pub enum ChurnEvent {
/// A peer joined at the given tick.
Joined {
/// The peer's identifier.
peer_id: String,
/// The logical clock tick when the peer joined.
tick: u64,
},
/// A peer left at the given tick.
Left {
/// The peer's identifier.
peer_id: String,
/// The logical clock tick when the peer left.
tick: u64,
},
}
impl ChurnEvent {
/// Returns the tick associated with this event.
pub fn tick(&self) -> u64 {
match self {
ChurnEvent::Joined { tick, .. } => *tick,
ChurnEvent::Left { tick, .. } => *tick,
}
}
/// Returns the peer_id associated with this event.
pub fn peer_id(&self) -> &str {
match self {
ChurnEvent::Joined { peer_id, .. } => peer_id,
ChurnEvent::Left { peer_id, .. } => peer_id,
}
}
}
/// Records the lifetime of a single peer in the network.
#[derive(Clone, Debug, PartialEq)]
pub struct PeerLifetime {
/// The peer's unique identifier.
pub peer_id: String,
/// The tick at which this peer joined.
pub joined_tick: u64,
/// The tick at which this peer left, or `None` if still online.
pub left_tick: Option<u64>,
}
impl PeerLifetime {
/// Returns how long this peer has been (or was) connected.
///
/// If the peer is still online, `current_tick` is used as the end boundary.
pub fn duration(&self, current_tick: u64) -> u64 {
self.left_tick
.unwrap_or(current_tick)
.saturating_sub(self.joined_tick)
}
/// Returns `true` if the peer is currently online (has not left).
pub fn is_online(&self) -> bool {
self.left_tick.is_none()
}
}
/// A sliding time window used to compute churn rate.
#[derive(Clone, Debug, PartialEq)]
pub struct ChurnWindow {
/// The inclusive start tick of this window.
pub tick_start: u64,
/// The inclusive end tick of this window.
pub tick_end: u64,
/// Number of join events within the window.
pub joins: u32,
/// Number of leave events within the window.
pub leaves: u32,
}
impl ChurnWindow {
/// Computes the churn rate as `(joins + leaves) / window_width`.
///
/// Returns `0.0` for zero-width windows.
pub fn churn_rate(&self) -> f64 {
if self.tick_end == self.tick_start {
return 0.0;
}
let events = (self.joins as f64) + (self.leaves as f64);
let width = (self.tick_end - self.tick_start + 1) as f64;
events / width
}
}
/// Configuration for the [`PeerChurnManager`].
#[derive(Clone, Debug, PartialEq)]
pub struct ChurnManagerConfig {
/// Width of the sliding window in ticks (default: 100).
pub window_size_ticks: u64,
/// Churn rate below this threshold is considered stable (default: 0.1).
pub stability_threshold: f64,
}
impl Default for ChurnManagerConfig {
fn default() -> Self {
Self {
window_size_ticks: 100,
stability_threshold: 0.1,
}
}
}
/// Aggregated statistics for the churn manager at a given tick.
#[derive(Clone, Debug, PartialEq)]
pub struct ChurnStats {
/// Total number of join events recorded since creation.
pub total_joins: u64,
/// Total number of leave events recorded since creation.
pub total_leaves: u64,
/// Number of peers currently online.
pub current_online: usize,
/// Mean lifetime of all peers (online peers measured to `current_tick`).
pub avg_lifetime_ticks: f64,
/// Churn rate from the most recent sliding window.
pub churn_rate: f64,
/// Whether the churn rate is below the stability threshold.
pub is_stable: bool,
}
/// Manages peer join/leave events, computes churn rate over sliding windows,
/// and provides stability scores for DHT and routing decisions.
pub struct PeerChurnManager {
/// Per-peer lifetime records, keyed by peer ID.
pub lifetimes: HashMap<String, PeerLifetime>,
/// All events recorded in chronological order.
pub events: Vec<ChurnEvent>,
/// Configuration governing window size and stability threshold.
pub config: ChurnManagerConfig,
}
impl PeerChurnManager {
/// Creates a new [`PeerChurnManager`] with the given configuration.
pub fn new(config: ChurnManagerConfig) -> Self {
Self {
lifetimes: HashMap::new(),
events: Vec::new(),
config,
}
}
/// Records a churn event and updates internal state accordingly.
///
/// - `Joined`: Creates (or replaces, if the peer was previously offline) a
/// [`PeerLifetime`] record.
/// - `Left`: If the peer is currently online, marks the peer as offline by
/// setting `left_tick`. If the peer is unknown or already offline, this is
/// a no-op beyond storing the event.
pub fn record_event(&mut self, event: ChurnEvent) {
match &event {
ChurnEvent::Joined { peer_id, tick } => {
let lifetime = PeerLifetime {
peer_id: peer_id.clone(),
joined_tick: *tick,
left_tick: None,
};
self.lifetimes.insert(peer_id.clone(), lifetime);
}
ChurnEvent::Left { peer_id, tick } => {
if let Some(lifetime) = self.lifetimes.get_mut(peer_id) {
if lifetime.is_online() {
lifetime.left_tick = Some(*tick);
}
}
}
}
self.events.push(event);
}
/// Builds the current sliding window ending at `current_tick`.
///
/// The window spans `[current_tick - window_size_ticks, current_tick]`
/// (saturating at 0), and counts join and leave events within that range.
pub fn current_window(&self, current_tick: u64) -> ChurnWindow {
let tick_start = current_tick.saturating_sub(self.config.window_size_ticks);
let tick_end = current_tick;
let mut joins: u32 = 0;
let mut leaves: u32 = 0;
for event in &self.events {
let t = event.tick();
if t >= tick_start && t <= tick_end {
match event {
ChurnEvent::Joined { .. } => joins += 1,
ChurnEvent::Left { .. } => leaves += 1,
}
}
}
ChurnWindow {
tick_start,
tick_end,
joins,
leaves,
}
}
/// Computes a stability score in `[0.0, 1.0]`.
///
/// `1.0` means no churn; `0.0` means maximum churn (rate ≥ 1.0).
pub fn stability_score(&self, current_tick: u64) -> f64 {
let window = self.current_window(current_tick);
let rate = window.churn_rate();
1.0 - rate.min(1.0)
}
/// Returns the peer IDs of all currently online peers, sorted alphabetically.
pub fn online_peers(&self) -> Vec<&str> {
let mut peers: Vec<&str> = self
.lifetimes
.values()
.filter(|l| l.is_online())
.map(|l| l.peer_id.as_str())
.collect();
peers.sort_unstable();
peers
}
/// Returns a reference to the [`PeerLifetime`] for a given peer ID, if any.
pub fn peer_lifetime(&self, peer_id: &str) -> Option<&PeerLifetime> {
self.lifetimes.get(peer_id)
}
/// Computes aggregated [`ChurnStats`] at the given tick.
pub fn stats(&self, current_tick: u64) -> ChurnStats {
let mut total_joins: u64 = 0;
let mut total_leaves: u64 = 0;
for event in &self.events {
match event {
ChurnEvent::Joined { .. } => total_joins += 1,
ChurnEvent::Left { .. } => total_leaves += 1,
}
}
let current_online = self.lifetimes.values().filter(|l| l.is_online()).count();
let avg_lifetime_ticks = if self.lifetimes.is_empty() {
0.0
} else {
let total: u64 = self
.lifetimes
.values()
.map(|l| l.duration(current_tick))
.sum();
total as f64 / self.lifetimes.len() as f64
};
let window = self.current_window(current_tick);
let churn_rate = window.churn_rate();
let is_stable = churn_rate < self.config.stability_threshold;
ChurnStats {
total_joins,
total_leaves,
current_online,
avg_lifetime_ticks,
churn_rate,
is_stable,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn default_manager() -> PeerChurnManager {
PeerChurnManager::new(ChurnManagerConfig::default())
}
// ── ChurnEvent helpers ────────────────────────────────────────────────────
#[test]
fn test_churn_event_tick_joined() {
let e = ChurnEvent::Joined {
peer_id: "peer1".into(),
tick: 42,
};
assert_eq!(e.tick(), 42);
}
#[test]
fn test_churn_event_tick_left() {
let e = ChurnEvent::Left {
peer_id: "peer1".into(),
tick: 77,
};
assert_eq!(e.tick(), 77);
}
#[test]
fn test_churn_event_peer_id() {
let e = ChurnEvent::Joined {
peer_id: "alpha".into(),
tick: 1,
};
assert_eq!(e.peer_id(), "alpha");
}
#[test]
fn test_churn_event_clone_and_eq() {
let e = ChurnEvent::Left {
peer_id: "x".into(),
tick: 5,
};
assert_eq!(e.clone(), e);
}
// ── PeerLifetime ──────────────────────────────────────────────────────────
#[test]
fn test_peer_lifetime_is_online_when_left_tick_none() {
let l = PeerLifetime {
peer_id: "p".into(),
joined_tick: 10,
left_tick: None,
};
assert!(l.is_online());
}
#[test]
fn test_peer_lifetime_is_not_online_when_left_tick_set() {
let l = PeerLifetime {
peer_id: "p".into(),
joined_tick: 10,
left_tick: Some(20),
};
assert!(!l.is_online());
}
#[test]
fn test_peer_lifetime_duration_online() {
let l = PeerLifetime {
peer_id: "p".into(),
joined_tick: 5,
left_tick: None,
};
assert_eq!(l.duration(15), 10);
}
#[test]
fn test_peer_lifetime_duration_offline() {
let l = PeerLifetime {
peer_id: "p".into(),
joined_tick: 5,
left_tick: Some(12),
};
assert_eq!(l.duration(999), 7);
}
#[test]
fn test_peer_lifetime_duration_zero_when_same_tick() {
let l = PeerLifetime {
peer_id: "p".into(),
joined_tick: 10,
left_tick: Some(10),
};
assert_eq!(l.duration(10), 0);
}
// ── ChurnWindow ───────────────────────────────────────────────────────────
#[test]
fn test_churn_window_churn_rate_basic() {
let w = ChurnWindow {
tick_start: 0,
tick_end: 9,
joins: 3,
leaves: 2,
};
// (3+2) / (9-0+1) = 5/10 = 0.5
assert!((w.churn_rate() - 0.5).abs() < f64::EPSILON);
}
#[test]
fn test_churn_window_churn_rate_zero_width() {
let w = ChurnWindow {
tick_start: 5,
tick_end: 5,
joins: 10,
leaves: 10,
};
assert_eq!(w.churn_rate(), 0.0);
}
#[test]
fn test_churn_window_churn_rate_no_events() {
let w = ChurnWindow {
tick_start: 0,
tick_end: 99,
joins: 0,
leaves: 0,
};
assert_eq!(w.churn_rate(), 0.0);
}
// ── ChurnManagerConfig ────────────────────────────────────────────────────
#[test]
fn test_churn_manager_config_default() {
let cfg = ChurnManagerConfig::default();
assert_eq!(cfg.window_size_ticks, 100);
assert!((cfg.stability_threshold - 0.1).abs() < f64::EPSILON);
}
// ── record_event: Joined ──────────────────────────────────────────────────
#[test]
fn test_record_event_joined_creates_lifetime() {
let mut mgr = default_manager();
mgr.record_event(ChurnEvent::Joined {
peer_id: "peer1".into(),
tick: 10,
});
let l = mgr.peer_lifetime("peer1").expect("lifetime should exist");
assert_eq!(l.joined_tick, 10);
assert!(l.is_online());
}
#[test]
fn test_record_event_joined_stores_event() {
let mut mgr = default_manager();
mgr.record_event(ChurnEvent::Joined {
peer_id: "peer1".into(),
tick: 1,
});
assert_eq!(mgr.events.len(), 1);
}
#[test]
fn test_record_event_joined_replaces_offline_peer() {
let mut mgr = default_manager();
mgr.record_event(ChurnEvent::Joined {
peer_id: "peer1".into(),
tick: 5,
});
mgr.record_event(ChurnEvent::Left {
peer_id: "peer1".into(),
tick: 10,
});
// Peer rejoins
mgr.record_event(ChurnEvent::Joined {
peer_id: "peer1".into(),
tick: 20,
});
let l = mgr.peer_lifetime("peer1").expect("lifetime should exist");
assert_eq!(l.joined_tick, 20);
assert!(l.is_online());
}
// ── record_event: Left ────────────────────────────────────────────────────
#[test]
fn test_record_event_left_sets_left_tick() {
let mut mgr = default_manager();
mgr.record_event(ChurnEvent::Joined {
peer_id: "peer1".into(),
tick: 0,
});
mgr.record_event(ChurnEvent::Left {
peer_id: "peer1".into(),
tick: 50,
});
let l = mgr.peer_lifetime("peer1").expect("lifetime should exist");
assert_eq!(l.left_tick, Some(50));
assert!(!l.is_online());
}
#[test]
fn test_record_event_left_unknown_peer_is_noop() {
let mut mgr = default_manager();
// No join first — Left for unknown peer should not panic or insert an entry
mgr.record_event(ChurnEvent::Left {
peer_id: "ghost".into(),
tick: 5,
});
assert!(mgr.peer_lifetime("ghost").is_none());
// Event is still stored
assert_eq!(mgr.events.len(), 1);
}
#[test]
fn test_record_event_left_already_offline_noop() {
let mut mgr = default_manager();
mgr.record_event(ChurnEvent::Joined {
peer_id: "p".into(),
tick: 0,
});
mgr.record_event(ChurnEvent::Left {
peer_id: "p".into(),
tick: 10,
});
// Second leave should not change left_tick
mgr.record_event(ChurnEvent::Left {
peer_id: "p".into(),
tick: 99,
});
let l = mgr.peer_lifetime("p").expect("lifetime should exist");
assert_eq!(l.left_tick, Some(10));
}
// ── current_window ────────────────────────────────────────────────────────
#[test]
fn test_current_window_counts_joins_and_leaves() {
let mut mgr = default_manager();
// Events inside window [900, 1000]
mgr.record_event(ChurnEvent::Joined {
peer_id: "a".into(),
tick: 950,
});
mgr.record_event(ChurnEvent::Joined {
peer_id: "b".into(),
tick: 960,
});
mgr.record_event(ChurnEvent::Left {
peer_id: "a".into(),
tick: 970,
});
// Event outside window (before start)
mgr.record_event(ChurnEvent::Joined {
peer_id: "c".into(),
tick: 899,
});
let w = mgr.current_window(1000);
assert_eq!(w.joins, 2);
assert_eq!(w.leaves, 1);
assert_eq!(w.tick_start, 900);
assert_eq!(w.tick_end, 1000);
}
#[test]
fn test_current_window_includes_boundaries() {
let mut mgr = default_manager();
mgr.record_event(ChurnEvent::Joined {
peer_id: "x".into(),
tick: 0,
});
mgr.record_event(ChurnEvent::Left {
peer_id: "x".into(),
tick: 100,
});
// window_size_ticks=100, so window = [0, 100]
let w = mgr.current_window(100);
assert_eq!(w.joins, 1);
assert_eq!(w.leaves, 1);
}
#[test]
fn test_current_window_saturates_at_zero() {
let mgr = default_manager();
let w = mgr.current_window(50);
assert_eq!(w.tick_start, 0); // 50 - 100 saturates to 0
}
// ── stability_score ───────────────────────────────────────────────────────
#[test]
fn test_stability_score_no_events() {
let mgr = default_manager();
// No events → churn_rate = 0 → stability = 1.0
assert!((mgr.stability_score(100) - 1.0).abs() < f64::EPSILON);
}
#[test]
fn test_stability_score_high_churn_clamped_to_zero() {
let mut mgr = PeerChurnManager::new(ChurnManagerConfig {
window_size_ticks: 1,
stability_threshold: 0.1,
});
// 4 events in a 2-tick window → rate = 4/2 = 2.0, clamped to 1.0 → score = 0.0
mgr.record_event(ChurnEvent::Joined {
peer_id: "a".into(),
tick: 0,
});
mgr.record_event(ChurnEvent::Left {
peer_id: "a".into(),
tick: 0,
});
mgr.record_event(ChurnEvent::Joined {
peer_id: "b".into(),
tick: 1,
});
mgr.record_event(ChurnEvent::Left {
peer_id: "b".into(),
tick: 1,
});
let score = mgr.stability_score(1);
assert!((score - 0.0).abs() < f64::EPSILON);
}
#[test]
fn test_stability_score_partial() {
let cfg = ChurnManagerConfig {
window_size_ticks: 9,
stability_threshold: 0.1,
};
let mut mgr = PeerChurnManager::new(cfg);
// 5 events in window [0, 9] (width=10) → rate = 5/10 = 0.5 → score = 0.5
for i in 0u64..5 {
mgr.record_event(ChurnEvent::Joined {
peer_id: format!("p{i}"),
tick: i,
});
}
let score = mgr.stability_score(9);
assert!((score - 0.5).abs() < 1e-10);
}
// ── online_peers ──────────────────────────────────────────────────────────
#[test]
fn test_online_peers_sorted_alphabetically() {
let mut mgr = default_manager();
mgr.record_event(ChurnEvent::Joined {
peer_id: "charlie".into(),
tick: 1,
});
mgr.record_event(ChurnEvent::Joined {
peer_id: "alice".into(),
tick: 2,
});
mgr.record_event(ChurnEvent::Joined {
peer_id: "bob".into(),
tick: 3,
});
let peers = mgr.online_peers();
assert_eq!(peers, vec!["alice", "bob", "charlie"]);
}
#[test]
fn test_online_peers_excludes_offline() {
let mut mgr = default_manager();
mgr.record_event(ChurnEvent::Joined {
peer_id: "alice".into(),
tick: 0,
});
mgr.record_event(ChurnEvent::Joined {
peer_id: "bob".into(),
tick: 1,
});
mgr.record_event(ChurnEvent::Left {
peer_id: "alice".into(),
tick: 5,
});
let peers = mgr.online_peers();
assert_eq!(peers, vec!["bob"]);
}
#[test]
fn test_online_peers_empty_when_all_left() {
let mut mgr = default_manager();
mgr.record_event(ChurnEvent::Joined {
peer_id: "x".into(),
tick: 0,
});
mgr.record_event(ChurnEvent::Left {
peer_id: "x".into(),
tick: 1,
});
assert!(mgr.online_peers().is_empty());
}
// ── peer_lifetime ─────────────────────────────────────────────────────────
#[test]
fn test_peer_lifetime_returns_some() {
let mut mgr = default_manager();
mgr.record_event(ChurnEvent::Joined {
peer_id: "z".into(),
tick: 7,
});
assert!(mgr.peer_lifetime("z").is_some());
}
#[test]
fn test_peer_lifetime_returns_none_for_unknown() {
let mgr = default_manager();
assert!(mgr.peer_lifetime("nobody").is_none());
}
// ── stats ─────────────────────────────────────────────────────────────────
#[test]
fn test_stats_current_online() {
let mut mgr = default_manager();
mgr.record_event(ChurnEvent::Joined {
peer_id: "a".into(),
tick: 0,
});
mgr.record_event(ChurnEvent::Joined {
peer_id: "b".into(),
tick: 0,
});
mgr.record_event(ChurnEvent::Left {
peer_id: "a".into(),
tick: 5,
});
let s = mgr.stats(10);
assert_eq!(s.current_online, 1);
}
#[test]
fn test_stats_total_joins_and_leaves() {
let mut mgr = default_manager();
mgr.record_event(ChurnEvent::Joined {
peer_id: "a".into(),
tick: 0,
});
mgr.record_event(ChurnEvent::Joined {
peer_id: "b".into(),
tick: 1,
});
mgr.record_event(ChurnEvent::Left {
peer_id: "a".into(),
tick: 5,
});
let s = mgr.stats(10);
assert_eq!(s.total_joins, 2);
assert_eq!(s.total_leaves, 1);
}
#[test]
fn test_stats_avg_lifetime_ticks() {
let mut mgr = default_manager();
// peer_a: online from 0, left at 10 → duration at tick=20 is 10
mgr.record_event(ChurnEvent::Joined {
peer_id: "a".into(),
tick: 0,
});
mgr.record_event(ChurnEvent::Left {
peer_id: "a".into(),
tick: 10,
});
// peer_b: online from 10, still online → duration at tick=20 is 10
mgr.record_event(ChurnEvent::Joined {
peer_id: "b".into(),
tick: 10,
});
let s = mgr.stats(20);
assert!((s.avg_lifetime_ticks - 10.0).abs() < f64::EPSILON);
}
#[test]
fn test_stats_is_stable_true() {
let cfg = ChurnManagerConfig {
window_size_ticks: 100,
stability_threshold: 0.5,
};
let mgr = PeerChurnManager::new(cfg);
// No events → churn_rate = 0 < 0.5 → stable
let s = mgr.stats(100);
assert!(s.is_stable);
}
#[test]
fn test_stats_is_stable_false() {
let cfg = ChurnManagerConfig {
window_size_ticks: 9,
stability_threshold: 0.1,
};
let mut mgr = PeerChurnManager::new(cfg);
// 5 joins in [0..9] window → rate = 5/10 = 0.5 > 0.1 → not stable
for i in 0u64..5 {
mgr.record_event(ChurnEvent::Joined {
peer_id: format!("p{i}"),
tick: i,
});
}
let s = mgr.stats(9);
assert!(!s.is_stable);
}
}