rings-node 0.39.0

Rings is a browser-native, structured peer-to-peer network: browser tabs and native daemons find each other by DID and talk over WebRTC, routed by a Chord DHT.
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
//! Links as core generations, their ledgers, the table bound, reconciliation, and the epoch reset.

use std::collections::BTreeMap;
use std::collections::BTreeSet;

use rand::rngs::StdRng;
use rand::seq::SliceRandom;
use rand::Rng;
use rand::SeedableRng;
use rings_core::dht::Did;

use super::super::OnionAdmissionLink;
use super::generation;
use super::latest_expiry;
use super::layer;
use super::link;
use super::live_filters;
use super::open;
use super::registry;
use super::send;
use super::send_on;
use super::sender_load;
use super::state;
use super::state_with;
use super::units;
use super::Verdict;
use super::EPOCH;
use super::ORIGIN_MS;
use super::Q;
use crate::onion::circuit::admission::OnionAdmissionLayer;
use crate::onion::circuit::admission::OnionAdmissionRejection;
use crate::onion::circuit::admission::OnionAdmissionState;
use crate::onion::circuit::admission::OnionChargeRejection;
use crate::onion::circuit::admission::OnionEpochNotFresh;
use crate::onion::circuit::admission::OnionLinkTableFull;
use crate::onion::circuit::admission::OnionReplayFilterKey;
use crate::onion::circuit::admission::ADMISSION_WINDOW_QUANTA_WIDE;
use crate::onion::circuit::admission::ONION_ADMISSION_SENDER_UNITS;
use crate::onion::circuit::ONION_ADMISSION_WINDOW_MS;
use crate::onion::OnionProcessEpoch;

/// Law: there is no fixed sender limit and no lockout. With `R = 100`, 150 live links, more than
/// the old 64 partitions, are all admitted.
#[test]
fn test_more_than_64_live_links_are_all_admitted() {
    let mut rng = StdRng::seed_from_u64(0x0841_000a);
    let mut admission = state_with(&mut rng, EPOCH, 100, 0..150);
    let x = latest_expiry(ORIGIN_MS);
    for sender in 0..150_u32 {
        assert_eq!(
            send(
                &mut admission,
                ORIGIN_MS,
                sender,
                1,
                layer(x, u128::from(sender))
            ),
            Verdict::Admitted
        );
    }
    assert_eq!(admission.senders.len(), 150);
}

/// Law: closing a link does not reset its ledger while it carries load, even under table pressure.
/// A DID spends `B` and closes its link. 38 churning DIDs then open, send and close, filling the
/// table: a refused churner never becomes live. When the DID reconnects within the window, as a new
/// generation, it finds its old ledger and is still over budget. Once the window passes, the sweep
/// releases every drained churner.
#[test]
fn test_closed_link_keeps_its_ledger_until_it_drains() {
    const R: usize = 8;
    let mut rng = StdRng::seed_from_u64(0x0841_000b);
    let mut admission = state_with(&mut rng, EPOCH, R, 1..2);
    let x = latest_expiry(ORIGIN_MS);
    let spender = Did::from(1_u32);
    assert_eq!(
        send(
            &mut admission,
            ORIGIN_MS,
            1,
            ONION_ADMISSION_SENDER_UNITS,
            layer(x, 1)
        ),
        Verdict::Admitted
    );
    admission.link_closed(ORIGIN_MS, link(1));
    let mut refused = 0;
    for churner in 2..40_u32 {
        match admission.link_opened(ORIGIN_MS + 1, link(churner)) {
            Ok(()) => {
                assert_eq!(
                    send(
                        &mut admission,
                        ORIGIN_MS + 1,
                        churner,
                        1,
                        layer(x, u128::from(churner))
                    ),
                    Verdict::Admitted
                );
                admission.link_closed(ORIGIN_MS + 1, link(churner));
            }
            Err(OnionLinkTableFull) => refused += 1,
        }
        assert!(admission.senders.len() <= 2 * R);
    }
    assert!(refused > 0);
    assert!(admission.senders.contains_key(&spender));
    open(&mut admission, ORIGIN_MS + 2, generation(1, 1));
    assert_eq!(
        send_on(
            &mut admission,
            ORIGIN_MS + 2,
            generation(1, 1),
            1,
            layer(x, 100)
        ),
        Verdict::Unpaid(OnionChargeRejection::SenderBudget)
    );
    let drained = ORIGIN_MS + ONION_ADMISSION_WINDOW_MS;
    assert_eq!(
        send_on(
            &mut admission,
            drained,
            generation(1, 1),
            1,
            layer(latest_expiry(drained), 101)
        ),
        Verdict::Admitted
    );
    assert_eq!(admission.senders.keys().collect::<Vec<_>>(), vec![&spender]);
}

/// Law: a full table refuses the link, and the link's cells are never charged or decrypted. The
/// shell's close of that refused link is a no-op. The peer's redial, a new generation, succeeds
/// once a closed ledger has drained and been swept.
#[test]
fn test_full_table_refuses_the_link_and_the_peer_redials() {
    let mut rng = StdRng::seed_from_u64(0x0841_000c);
    let mut admission = state_with(&mut rng, EPOCH, 1, 1..3);
    let x = latest_expiry(ORIGIN_MS);
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 1, 1, layer(x, 1)),
        Verdict::Admitted
    );
    assert_eq!(
        admission.link_opened(ORIGIN_MS, link(3)),
        Err(OnionLinkTableFull)
    );
    let before = admission.senders.clone();
    admission.link_closed(ORIGIN_MS, link(3));
    assert_eq!(admission.senders, before);
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 3, 1, layer(x, 3)),
        Verdict::Unpaid(OnionChargeRejection::LinkNotLive)
    );

    admission.link_closed(ORIGIN_MS, link(1));
    assert_eq!(
        admission.link_opened(ORIGIN_MS + 1, generation(3, 1)),
        Err(OnionLinkTableFull)
    );
    let drained = ORIGIN_MS + ONION_ADMISSION_WINDOW_MS;
    open(&mut admission, drained, generation(3, 2));
    assert!(!admission.senders.contains_key(&Did::from(1_u32)));
    assert!(admission.senders.contains_key(&Did::from(2_u32)));
    assert_eq!(
        send_on(
            &mut admission,
            drained,
            generation(3, 2),
            1,
            layer(latest_expiry(drained), 3)
        ),
        Verdict::Admitted
    );
}

/// Law: liveness is per generation, so interleaved generations of one DID
/// (`open(g₀) open(g₁) close(g₀)`) leave `g₁` live and `g₀` not. Only the last close, together with
/// a drained load, releases the DID's ledger.
#[test]
fn test_ledger_counts_interleaved_link_generations() {
    let mut rng = StdRng::seed_from_u64(0x0841_0010);
    let mut admission = state_with(&mut rng, EPOCH, 4, 0..0);
    let peer = Did::from(1_u32);
    open(&mut admission, ORIGIN_MS, generation(1, 0));
    open(&mut admission, ORIGIN_MS, generation(1, 1));
    admission.link_closed(ORIGIN_MS, generation(1, 0));
    let x = latest_expiry(ORIGIN_MS);
    assert_eq!(
        send_on(&mut admission, ORIGIN_MS, generation(1, 0), 1, layer(x, 1)),
        Verdict::Unpaid(OnionChargeRejection::LinkNotLive)
    );
    assert_eq!(
        send_on(&mut admission, ORIGIN_MS, generation(1, 1), 1, layer(x, 1)),
        Verdict::Admitted
    );
    admission.link_closed(ORIGIN_MS, generation(1, 1));
    assert!(admission.senders.contains_key(&peer));
    let drained = ORIGIN_MS + ONION_ADMISSION_WINDOW_MS;
    assert_eq!(
        send_on(
            &mut admission,
            drained,
            generation(1, 1),
            1,
            layer(latest_expiry(drained), 2)
        ),
        Verdict::Unpaid(OnionChargeRejection::LinkNotLive)
    );
    assert!(admission.senders.is_empty());
}

/// Law: a close is idempotent and specific to its generation. A late close of a refused `g₀`, a
/// repeated close, and a close of an unknown generation all leave the live `g₁` live, across
/// later windows, so no stale close can lock a link out.
#[test]
fn test_a_refused_generation_cannot_close_a_live_one() {
    let mut rng = StdRng::seed_from_u64(0x0841_0012);
    let mut admission = state_with(&mut rng, EPOCH, 1, 1..3);
    let x = latest_expiry(ORIGIN_MS);
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 1, 1, layer(x, 1)),
        Verdict::Admitted
    );
    assert_eq!(
        admission.link_opened(ORIGIN_MS, generation(3, 0)),
        Err(OnionLinkTableFull)
    );
    admission.link_closed(ORIGIN_MS, link(1));
    let drained = ORIGIN_MS + ONION_ADMISSION_WINDOW_MS;
    open(&mut admission, drained, generation(3, 1));
    admission.link_closed(drained, generation(3, 0));
    admission.link_closed(drained, generation(3, 0));
    admission.link_closed(drained, generation(3, 7));
    for window in 1..4_u128 {
        let now_ms = drained + window * ONION_ADMISSION_WINDOW_MS;
        assert_eq!(
            send_on(
                &mut admission,
                now_ms,
                generation(3, 1),
                1,
                layer(latest_expiry(now_ms), window)
            ),
            Verdict::Admitted
        );
    }
}

/// Law: the epoch reset keeps the table's own live links, `ρ = reconcile(live_links(S)) ∘ clear`,
/// and refuses none. Every live link starts with a zero ledger. The reset clears the loads, the
/// clock and the replay store, and drops every ledger without a live link. Every layer of the
/// old epoch is then rejected, and so is every token charged before the reset, because its epoch
/// differs; a DID that had no live link is still not live.
#[test]
fn test_renewal_keeps_the_live_links_and_clears_the_rest() {
    let mut rng = StdRng::seed_from_u64(0x0841_0015);
    let mut admission = state_with(&mut rng, EPOCH, 4, 1..3);
    let x = latest_expiry(ORIGIN_MS);
    assert_eq!(
        send(
            &mut admission,
            ORIGIN_MS,
            1,
            ONION_ADMISSION_SENDER_UNITS,
            layer(x, 1)
        ),
        Verdict::Admitted
    );
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 2, 1, layer(x, 2)),
        Verdict::Admitted
    );
    let held = admission
        .charge_units(ORIGIN_MS, link(2), units(1))
        .expect("DID 2 has headroom");
    let renewed_epoch = OnionProcessEpoch::new([8; 16]);
    assert_eq!(
        admission.renew(renewed_epoch, OnionReplayFilterKey::new(rng.gen())),
        Ok(())
    );
    assert_eq!(admission.clock_ms, 0);
    assert_eq!(live_filters(&admission), Vec::new());
    assert_eq!(admission.senders.keys().collect::<Vec<_>>(), vec![
        &Did::from(1_u32),
        &Did::from(2_u32)
    ]);
    assert_eq!(sender_load(&admission, 1, ORIGIN_MS), Some(0));
    assert_eq!(sender_load(&admission, 2, ORIGIN_MS), Some(0));
    assert_eq!(
        admission.admit(ORIGIN_MS, held, OnionAdmissionLayer {
            epoch: renewed_epoch,
            ..layer(x, 4)
        }),
        Err(OnionAdmissionRejection::StaleEpoch)
    );
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 1, 1, layer(x, 1)),
        Verdict::Rejected(OnionAdmissionRejection::StaleEpoch)
    );
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 1, 1, OnionAdmissionLayer {
            epoch: renewed_epoch,
            ..layer(x, 1)
        }),
        Verdict::Admitted
    );
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 3, 1, layer(x, 3)),
        Verdict::Unpaid(OnionChargeRejection::LinkNotLive)
    );
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 2, 1, OnionAdmissionLayer {
            epoch: renewed_epoch,
            ..layer(x, 5)
        }),
        Verdict::Admitted
    );
}

/// Law (M1 of the round-5 review): renewing into the current epoch is refused and changes nothing,
/// so the replay store survives and an admitted `(x, ν)` cannot be admitted again.
#[test]
fn test_a_same_epoch_renewal_is_refused_and_cannot_readmit_a_replay() {
    let mut rng = StdRng::seed_from_u64(0x0841_0017);
    let mut admission = state(&mut rng);
    let x = latest_expiry(ORIGIN_MS);
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 1, 1, layer(x, 1)),
        Verdict::Admitted
    );
    assert_eq!(
        admission.renew(EPOCH, OnionReplayFilterKey::new(rng.gen())),
        Err(OnionEpochNotFresh)
    );
    assert_eq!(admission.clock_ms, ORIGIN_MS);
    assert_eq!(live_filters(&admission), vec![x]);
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 1, 1, layer(x, 1)),
        Verdict::Rejected(OnionAdmissionRejection::Replayed)
    );
}

/// Law (M2 of the round-5 review): lost `Retired` events pin live links until the shell
/// reconciles with core's snapshot. Here every `Retired` is lost, so the table fills and refuses a
/// newcomer. `reconcile` then closes every generation absent from the snapshot, opens the snapshot
/// generation that was never reported (a lost `Admitted`), and returns the links it refuses.
#[test]
fn test_reconcile_recovers_lost_link_events() {
    let mut rng = StdRng::seed_from_u64(0x0841_0018);
    let mut admission = state_with(&mut rng, EPOCH, 2, 1..5);
    assert_eq!(
        admission.link_opened(ORIGIN_MS, link(5)),
        Err(OnionLinkTableFull)
    );
    let refused = admission.reconcile(ORIGIN_MS, [link(1), generation(6, 3)]);
    assert!(refused.links().is_empty());
    assert_eq!(admission.senders.keys().collect::<Vec<_>>(), vec![
        &Did::from(1_u32),
        &Did::from(6_u32)
    ]);
    open(&mut admission, ORIGIN_MS, link(5));
    assert_eq!(
        send_on(
            &mut admission,
            ORIGIN_MS,
            generation(6, 3),
            1,
            layer(latest_expiry(ORIGIN_MS), 1)
        ),
        Verdict::Admitted
    );
    assert_eq!(
        send(
            &mut admission,
            ORIGIN_MS,
            2,
            1,
            layer(latest_expiry(ORIGIN_MS), 2)
        ),
        Verdict::Unpaid(OnionChargeRejection::LinkNotLive)
    );

    // DIDs 1 and 5 are absent and unloaded, so they are released. DID 6 is absent but still
    // loaded, so its closed ledger keeps a slot until it drains. Three of the ten new links fit,
    // and reconcile opens in DID order, so DIDs 10 to 12 win and 13 to 19 are refused.
    let crowded = (10..20).map(link).collect::<Vec<_>>();
    let refused = admission.reconcile(ORIGIN_MS, crowded);
    assert_eq!(
        refused.links(),
        (13..20).map(link).collect::<Vec<_>>().as_slice()
    );
    assert_eq!(admission.senders.len(), 4);
    assert!(admission.senders.contains_key(&Did::from(6_u32)));
}

/// Law (memory bound): the live-link set is capped at `2·R` even for one DID. That can only be
/// reached if the event obligation is broken, so that many generations of one DID stay live.
#[test]
fn test_live_links_are_capped_as_a_memory_bound() {
    let mut rng = StdRng::seed_from_u64(0x0841_0019);
    let mut admission = state_with(&mut rng, EPOCH, 2, 0..0);
    for generation_index in 0..4 {
        open(&mut admission, ORIGIN_MS, generation(1, generation_index));
    }
    assert_eq!(
        admission.link_opened(ORIGIN_MS, generation(1, 4)),
        Err(OnionLinkTableFull)
    );
    assert_eq!(admission.live_link_count(), 4);
}

/// Law (invariant): no ledger is releasable after any step. A closed ledger with load is released
/// by the first step in the quantum in which it drains, whatever that step is, and not only under
/// table pressure.
#[test]
fn test_drained_closed_ledgers_are_swept_on_the_next_quantum() {
    let mut rng = StdRng::seed_from_u64(0x0841_0011);
    let mut admission = state_with(&mut rng, EPOCH, 64, 1..3);
    let x = latest_expiry(ORIGIN_MS);
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 1, 1, layer(x, 1)),
        Verdict::Admitted
    );
    admission.link_closed(ORIGIN_MS, link(1));
    let still_loaded = ORIGIN_MS + ONION_ADMISSION_WINDOW_MS - 1;
    assert_eq!(
        send(
            &mut admission,
            still_loaded,
            2,
            1,
            layer(latest_expiry(still_loaded), 2)
        ),
        Verdict::Admitted
    );
    assert!(admission.senders.contains_key(&Did::from(1_u32)));
    let drained = ORIGIN_MS + ONION_ADMISSION_WINDOW_MS;
    assert_eq!(
        send(
            &mut admission,
            drained,
            2,
            1,
            layer(latest_expiry(drained), 3)
        ),
        Verdict::Admitted
    );
    assert!(!admission.senders.contains_key(&Did::from(1_u32)));
    assert!(admission
        .senders
        .values()
        .all(|sender| !sender.is_releasable_at(drained / Q)));
}

/// Property, seeded: under random link churn (48 DIDs, `R = 16`, two of them heavy), no DID is
/// ever charged more than `B` units in any window of five consecutive quanta, the table never
/// exceeds `2·R`, the live-link count equals the model's, and no ledger is ever releasable between
/// steps. Closes and charges pick a random live generation, or with probability ¼ a random
/// generation that may be stale, refused or unknown. The run hits the sender budget, links that
/// are not live, and the full table, so none of the bounds holds vacuously.
#[test]
fn test_rotation_through_many_dids_never_exceeds_the_sender_budget() {
    const R: usize = 16;
    let mut rng = StdRng::seed_from_u64(0x0841_000d);
    let mut admission = state_with(&mut rng, EPOCH, R, 0..0);
    let mut live_generations = BTreeMap::<u32, Vec<u64>>::new();
    let mut next_generation = 0_u64;
    let mut charged = BTreeMap::<(u32, u128), u32>::new();
    let mut outcomes = [0_u32; 3];
    let mut now_ms = ORIGIN_MS;
    for tag in 0..30_000_u128 {
        now_ms += rng.gen_range(0..=Q / 64);
        let sender = if rng.gen_bool(0.5) {
            rng.gen_range(0..2_u32)
        } else {
            rng.gen_range(2..48_u32)
        };
        match rng.gen_range(0..16) {
            0 => {
                let generations = live_generations.entry(sender).or_default();
                let closed = if generations.is_empty() || rng.gen_ratio(1, 4) {
                    rng.gen_range(0..=next_generation)
                } else {
                    generations.swap_remove(rng.gen_range(0..generations.len()))
                };
                generations.retain(|live| *live != closed);
                admission.link_closed(now_ms, generation(sender, closed));
            }
            1 | 2 => {
                next_generation += 1;
                match admission.link_opened(now_ms, generation(sender, next_generation)) {
                    Ok(()) => live_generations
                        .entry(sender)
                        .or_default()
                        .push(next_generation),
                    Err(OnionLinkTableFull) => outcomes[2] += 1,
                }
            }
            _ => {
                let cost = rng.gen_range(1..=768_u32);
                let on = match live_generations.get(&sender) {
                    Some(generations) if !generations.is_empty() && !rng.gen_ratio(1, 4) => {
                        generations
                            .get(rng.gen_range(0..generations.len()))
                            .copied()
                            .unwrap_or(0)
                    }
                    _ => rng.gen_range(0..=next_generation),
                };
                let modelled_live = live_generations
                    .get(&sender)
                    .is_some_and(|generations| generations.contains(&on));
                let verdict = send_on(
                    &mut admission,
                    now_ms,
                    generation(sender, on),
                    cost,
                    layer(latest_expiry(now_ms), tag),
                );
                assert_eq!(
                    verdict == Verdict::Unpaid(OnionChargeRejection::LinkNotLive),
                    !modelled_live
                );
                match verdict {
                    Verdict::Admitted => {
                        *charged.entry((sender, now_ms / Q)).or_default() += cost;
                    }
                    Verdict::Unpaid(OnionChargeRejection::SenderBudget) => outcomes[0] += 1,
                    Verdict::Unpaid(OnionChargeRejection::LinkNotLive) => outcomes[1] += 1,
                    unexpected => assert_eq!(unexpected, Verdict::Admitted),
                }
            }
        }
        assert!(admission.senders.len() <= 2 * R);
        assert_eq!(
            admission.live_link_count(),
            live_generations.values().map(Vec::len).sum::<usize>()
        );
        assert!(admission
            .senders
            .values()
            .all(|sender| !sender.is_releasable_at(now_ms / Q)));
    }
    assert!(outcomes.iter().all(|count| *count > 0), "{outcomes:?}");
    for (sender, quantum) in charged.keys() {
        let window = charged
            .range(
                (
                    *sender,
                    quantum.saturating_sub(ADMISSION_WINDOW_QUANTA_WIDE - 1),
                )..=(*sender, *quantum),
            )
            .map(|(_, units)| *units)
            .sum::<u32>();
        assert!(window <= ONION_ADMISSION_SENDER_UNITS);
    }
}

/// Whether the table has no room for `link`, given the modelled number of live links: the live-link
/// set is full, or `link`'s DID has no ledger and the ledger table is full. This is the only
/// justification for a refusal.
fn is_full_for(
    admission: &OnionAdmissionState,
    live_links: usize,
    link: &OnionAdmissionLink,
) -> bool {
    live_links >= admission.capacity
        || (!admission.senders.contains_key(&link.peer())
            && admission.senders.len() >= admission.capacity)
}

/// The live links of `admission`, as `(did, generation)` pairs.
fn live_links(admission: &OnionAdmissionState) -> BTreeSet<(Did, u64)> {
    admission
        .senders
        .iter()
        .flat_map(|(&did, sender)| sender.live.iter().map(move |&generation| (did, generation)))
        .collect()
}

/// The model of the lost-events property: core's truth `L`, the live set admission should hold,
/// and two states driven in lockstep, one reconciled with sorted snapshots and one with shuffled
/// snapshots.
struct Lockstep {
    /// The seeded source of every choice.
    rng: StdRng,
    /// The state reconciled with sorted snapshots.
    sorted: OnionAdmissionState,
    /// The state reconciled with shuffled snapshots.
    shuffled: OnionAdmissionState,
    /// Core's registry of live links, `L`.
    truth: BTreeSet<(Did, u64)>,
    /// The live set admission should hold, given the events it received.
    modelled: BTreeSet<(Did, u64)>,
    /// The last generation core issued.
    next_generation: u64,
    /// Reconciliations run.
    reconciliations: u32,
    /// Events lost before reaching admission.
    lost: u32,
    /// Delivered opens, delivered closes, event refusals, and reconciliations with a refusal.
    occurred: [u32; 4],
}

impl Lockstep {
    /// Two fresh states with `R = 8` under one probe key, and empty truth.
    fn new(seed: u64) -> Self {
        let mut rng = StdRng::seed_from_u64(seed);
        let key: [u8; 32] = rng.gen();
        let fresh = || OnionAdmissionState::new(EPOCH, OnionReplayFilterKey::new(key), registry(8));
        Self {
            rng,
            sorted: fresh(),
            shuffled: fresh(),
            truth: BTreeSet::new(),
            modelled: BTreeSet::new(),
            next_generation: 0,
            reconciliations: 0,
            lost: 0,
            occurred: [0; 4],
        }
    }

    /// Core admits a new generation of `did`. The event is lost with probability ⅙. Otherwise both
    /// states open it; a refusal must be justified, and the shell then closes the link in core.
    fn open(&mut self, now_ms: u128, did: Did) {
        self.next_generation += 1;
        let opened = OnionAdmissionLink::new(did, self.next_generation);
        self.truth.insert((did, opened.generation()));
        if self.rng.gen_ratio(1, 6) {
            self.lost += 1;
            return;
        }
        let verdict = self.sorted.link_opened(now_ms, opened);
        assert_eq!(self.shuffled.link_opened(now_ms, opened), verdict);
        match verdict {
            Ok(()) => {
                self.occurred[0] += 1;
                self.modelled.insert((did, opened.generation()));
            }
            Err(OnionLinkTableFull) => {
                self.occurred[2] += 1;
                assert!(is_full_for(&self.sorted, self.modelled.len(), &opened));
                self.truth.remove(&(did, opened.generation()));
            }
        }
    }

    /// Core retires a random live generation. The event is lost with probability ⅙; otherwise both
    /// states close it.
    fn retire(&mut self, now_ms: u128) {
        let index = self.rng.gen_range(0..self.truth.len().max(1));
        let Some((did, generation)) = self.truth.iter().nth(index).copied() else {
            return;
        };
        self.truth.remove(&(did, generation));
        if self.rng.gen_ratio(1, 6) {
            self.lost += 1;
            return;
        }
        let closed = OnionAdmissionLink::new(did, generation);
        self.sorted.link_closed(now_ms, closed);
        self.shuffled.link_closed(now_ms, closed);
        self.occurred[1] += 1;
        self.modelled.remove(&(did, generation));
    }

    /// Reconcile both states against `L`, sorted and shuffled, and check the four reconciliation
    /// laws and the justification of every refusal. The shell then closes the refused links in
    /// core.
    fn reconcile(&mut self, now_ms: u128) {
        self.reconciliations += 1;
        let snapshot = self
            .truth
            .iter()
            .map(|&(did, generation)| OnionAdmissionLink::new(did, generation))
            .collect::<Vec<_>>();
        let mut permuted = snapshot.clone();
        permuted.shuffle(&mut self.rng);
        let before = live_links(&self.sorted);
        let refused = self.sorted.reconcile(now_ms, snapshot.iter().copied());
        assert_eq!(
            self.shuffled.reconcile(now_ms, permuted.iter().copied()),
            refused
        );
        let refused_set = refused
            .links()
            .iter()
            .map(|link| (link.peer(), link.generation()))
            .collect::<BTreeSet<_>>();
        let expected = self
            .truth
            .difference(&refused_set)
            .copied()
            .collect::<BTreeSet<_>>();
        assert_eq!(live_links(&self.sorted), expected);
        assert!(before
            .intersection(&self.truth)
            .all(|link| !refused_set.contains(link)));
        assert!(refused
            .links()
            .iter()
            .all(|link| is_full_for(&self.sorted, expected.len(), link)));
        self.occurred[3] += u32::from(!refused.links().is_empty());
        let ledgers = self.sorted.senders.clone();
        assert_eq!(
            self.sorted.reconcile(now_ms, snapshot.iter().copied()),
            refused
        );
        assert_eq!(
            self.shuffled.reconcile(now_ms, permuted.iter().copied()),
            refused
        );
        assert_eq!(self.sorted.senders, ledgers);
        self.truth = expected;
        self.modelled = self.truth.clone();
    }

    /// Charge a cell on a random modelled link, or with probability ¼ on a possibly stale
    /// generation of `did`. Both states agree, and `LinkNotLive` holds exactly when the model lacks
    /// the link.
    fn charge(&mut self, now_ms: u128, did: Did, tag: u128) {
        let index = self.rng.gen_range(0..self.modelled.len().max(1));
        let on = match self.modelled.iter().nth(index) {
            Some(&(did, generation)) if !self.rng.gen_ratio(1, 4) => {
                OnionAdmissionLink::new(did, generation)
            }
            _ => OnionAdmissionLink::new(did, self.rng.gen_range(0..=self.next_generation)),
        };
        let cost = self.rng.gen_range(1..=64_u32);
        let x = layer(latest_expiry(now_ms), tag);
        let verdict = send_on(&mut self.sorted, now_ms, on, cost, x);
        assert_eq!(send_on(&mut self.shuffled, now_ms, on, cost, x), verdict);
        assert_eq!(
            verdict == Verdict::Unpaid(OnionChargeRejection::LinkNotLive),
            !self.modelled.contains(&(on.peer(), on.generation()))
        );
    }
}

/// Property, seeded (M2 of the round-6 review): with lost `Admitted`/`Retired` events, periodic
/// reconciliation against core's truth `L` restores the laws. Two states are driven in lockstep
/// with the same inputs; one receives every snapshot sorted and the other shuffled. After every
/// reconciliation:
/// * `𝓛ᵢ = L \ refused`;
/// * no link that was live before and is in `L` is refused;
/// * a second reconciliation with the same snapshot changes nothing and refuses the same links;
/// * the shuffled snapshot yields the same state as the sorted one.
///
/// Every refusal is justified: after a refused `link_opened`, or after a reconciliation that
/// refuses a link, the table has no room for that link, counting live links from the model.
/// Throughout, a charge is `LinkNotLive` exactly when the modelled live set lacks the link, and
/// the state's live set equals the model's after every step. The shell closes every refused link
/// in core, so the model removes it from `L`. The run asserts that delivered opens, delivered
/// closes, event refusals and reconcile refusals all occur.
#[test]
fn test_reconcile_restores_core_truth_under_lost_events() {
    let mut model = Lockstep::new(0x0841_001b);
    let mut now_ms = ORIGIN_MS;
    for tag in 0..6_000_u128 {
        now_ms += model.rng.gen_range(0..=Q / 16);
        let did = Did::from(model.rng.gen_range(0..24_u32));
        match model.rng.gen_range(0..16) {
            0 | 1 => model.open(now_ms, did),
            2 => model.retire(now_ms),
            3 => model.reconcile(now_ms),
            _ => model.charge(now_ms, did, tag),
        }
        assert_eq!(live_links(&model.sorted), model.modelled);
        assert_eq!(model.sorted.senders, model.shuffled.senders);
    }
    assert!(
        model.reconciliations > 100 && model.lost > 50,
        "{} {}",
        model.reconciliations,
        model.lost
    );
    assert!(
        model.occurred.iter().all(|count| *count > 0),
        "{:?}",
        model.occurred
    );
}