aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/vote.rs
blob: baf8dd0f42f517c045f928aacb3fa715e7b2abfe (plain)
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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
//! Votes a node casts by itself, by its operator's rules (`docs/federation.md`, Votes a
//! node casts by itself), written nowhere in the swarm: every node judges what it signs as
//! any vote or proposal.
//!
//! * **Following**: it votes `true` on a proposal on the current version where a party it
//!   follows voted `true`, never `false`; by default only on a version that only moves to a
//!   checkpoint (`Version::only_moves_on`), and then only where no proposal that changes
//!   more holds it back (`holds_back`), as such a version lapses it; never on one changing
//!   the revision.
//! * **Proposing a checkpoint**: once so many events are held beyond its checkpoint, so
//!   many versions were written since, or so long after it moved there by its own clock, it
//!   proposes a version that only moves to a checkpoint, at the heads its store holds, no
//!   more than an event may name, where no proposal on the current version holds it back: one it would back that
//!   can still pass, one that can still pass that a third of the parties back, someone but
//!   its proposer among them, or one new, held less than a while, whose proposer has no
//!   older one on the version (`holds_back`). It backs what it proposes.
//! * **Voting `false` beside**: where it backs a proposal that can still pass, it votes
//!   `false` on every other proposal on the current version that only moves to a
//!   checkpoint, so that, of two checkpoints proposed at once, the one it doesn't back
//!   dies and its backers are free.
//!
//! It keeps to what a party's client keeps to (`client::Record::may_vote`), its party's
//! one record of its votes kept on it; and, by the voters' rule, backs a version naming a
//! checkpoint only where it changes nothing of the current one but its number, what it
//! changes and its checkpoint, and its heads leave out nothing it held for a while before
//! it was proposed (`Store::left_out`), and only once its store held its checkpoint, or
//! ran, for a while (`Rules::back_after`): a party it follows moves the store once that
//! long at most, and a node just started backs none, as it holds nothing for a while yet.

use std::collections::BTreeSet;
use std::time::Duration;

use super::client::Record;
use super::event::{Does, Event, Signed};
use super::fold::Federation;
use super::jcs::Json;
use super::store::Id;
use super::store::memory::Store;
use super::version::Version;

/// A node's rules for voting by itself, its operator's.
#[derive(Clone, Debug, Default)]
pub(crate) struct Rules {
    /// The parties it follows.
    pub(crate) follow: Vec<String>,
    /// Following on any version, not only one that only moves to a checkpoint.
    pub(crate) follow_any: bool,
    /// Proposing a checkpoint once so many events are held beyond the checkpoint...
    pub(crate) propose_events: Option<usize>,
    /// ...or so long after the store moved to it, by its own clock...
    pub(crate) propose_every: Option<Duration>,
    /// ...or once so many versions were written since the one it holds, of those changing
    /// more than moving to a checkpoint.
    pub(crate) propose_versions: Option<u64>,
    /// How long it holds an event before it holds it for a while: the voters' rule, and how
    /// long a new proposal holds back a checkpoint.
    pub(crate) held_for: Duration,
    /// Voting `false` on a checkpoint proposed beside one it backs.
    pub(crate) nay_beside: bool,
    /// How long its store holds its checkpoint, or runs, before it backs or proposes a
    /// later one.
    pub(crate) back_after: Duration,
}

/// What the node sees: its store, how long it has held each event it took, which of them
/// it held when it started (for how long before, it can't tell: its clock began then), and
/// how long since its store moved to its checkpoint.
pub(crate) struct Seen<'a> {
    pub(crate) store: &'a Store<Federation>,
    pub(crate) held: &'a dyn Fn(&Id) -> Option<Duration>,
    pub(crate) from_start: &'a dyn Fn(&Id) -> bool,
    pub(crate) since_move: Duration,
}

/// What a node voting by itself signs.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Act {
    /// A vote, with the nays that freed its party, to keep.
    Vote {
        proposal: Id,
        yes: bool,
        nays: Vec<Vec<u8>>,
    },
    /// A proposal of this version.
    Propose(Json),
}

/// A proposal on the current version, as the node holds it.
struct Proposed {
    hash: Id,
    /// What it proposes, and whether that is a version.
    json: Json,
    version: Option<Version>,
    /// Its proposing events' parties, how long the node has held the first, and that one's
    /// party (of several held as long, the one of the lowest ID): its proposer, whoever
    /// proposes it again.
    proposers: BTreeSet<String>,
    first: String,
    held: Duration,
    /// Its votes: each party's, and the events, as bytes.
    yes: BTreeSet<String>,
    no: BTreeSet<String>,
    votes: Vec<Vec<u8>>,
}

fn proposed(seen: &Seen<'_>, current: &Version) -> Vec<Proposed> {
    let store = seen.store;
    let mut all = Vec::new();
    for (hash, ballot) in store.state().ballots(&current.hash) {
        let mut proposers = BTreeSet::new();
        let mut json = None;
        let mut held = Duration::ZERO;
        let mut first: Option<(Duration, std::cmp::Reverse<Id>, String)> = None;
        for id in &ballot.proposals {
            if let Some(Event::Signed(Signed {
                party,
                does: Does::Proposal { version, .. },
                ..
            })) = store.get(id).and_then(Event::read_taken)
            {
                proposers.insert(party.clone());
                let since = (seen.held)(id).unwrap_or_default();
                held = held.max(since);
                let this = (since, std::cmp::Reverse(*id), party);
                if first
                    .as_ref()
                    .is_none_or(|first| (&this.0, &this.1) > (&first.0, &first.1))
                {
                    first = Some(this);
                }
                json = Some(version);
            }
        }
        let (Some(json), Some((_, _, first))) = (json, first) else {
            continue;
        };
        let (mut yes, mut no, mut votes) = (BTreeSet::new(), BTreeSet::new(), Vec::new());
        for id in &ballot.votes {
            let Some(bytes) = store.get(id) else {
                continue;
            };
            if let Some(Event::Signed(Signed {
                party,
                does: Does::Vote { yes: vote, .. },
                ..
            })) = Event::read_taken(bytes)
            {
                if vote {
                    yes.insert(party);
                } else {
                    no.insert(party);
                }
                votes.push(bytes.to_vec());
            }
        }
        all.push(Proposed {
            hash,
            version: Version::read(&json).ok(),
            json,
            proposers,
            first,
            held,
            yes,
            no,
            votes,
        });
    }
    all
}

/// Whether the node backs `version`, proposed `proposed_held` ago, by the voters' rule: one
/// naming a checkpoint only where it changes nothing of `current` but its number, what it
/// changes and its checkpoint, and its heads leave out nothing the node held for a while
/// before it was proposed, or when it started, proposals and votes aside, which lapse
/// with the move, and are
/// heads a version may name (`State::may_name`); and only once its store held its
/// checkpoint, or ran, for a while (`Rules::back_after`).
pub(crate) fn backs_checkpoint(
    seen: &Seen<'_>,
    rules: &Rules,
    current: &Version,
    version: &Version,
    proposed_held: Duration,
) -> Result<(), String> {
    let Some(heads) = &version.checkpoint else {
        return Ok(());
    };
    if !version.only_moves_on(current) {
        return Err(
            "a checkpoint that changes more than its number and what it changes".to_owned(),
        );
    }
    // Heads no version may name: votes on it are taken, and no version event on them.
    if let Err(why) = seen.store.state().may_name(heads) {
        return Err(format!(
            "no version event may name its heads ({}): a `true` on it is never written on",
            why.0
        ));
    }
    if seen.since_move < rules.back_after {
        return Err(format!(
            "its store held its checkpoint, or ran, {}s: it backs a later one after {}s",
            seen.since_move.as_secs(),
            rules.back_after.as_secs()
        ));
    }
    let before = proposed_held + rules.held_for;
    // Proposals and votes lapse with the move: they count no more on the version after.
    let lapsing = |id: &Id| {
        matches!(
            seen.store.get(id).and_then(Event::read_taken),
            Some(Event::Signed(Signed {
                does: Does::Proposal { .. } | Does::Vote { .. },
                ..
            }))
        )
    };
    let held: Vec<Id> = seen
        .store
        .taken()
        .iter()
        .filter(|id| {
            // Held when it started: before the proposal, whenever that came.
            ((seen.from_start)(id) || (seen.held)(id).is_some_and(|held| held >= before))
                && !lapsing(id)
        })
        .copied()
        .collect();
    let left_out = seen.store.left_out(heads, &held);
    match left_out.first() {
        None => Ok(()),
        Some(id) => Err(format!(
            "its heads leave out {id}, held for a while before it was proposed, and {} more",
            left_out.len() - 1
        )),
    }
}

/// What a vote of `party` on the proposal of hash `proposal` builds on in `store`: an
/// event proposing it, of a checkpoint one above the heads it names, so that a version
/// event may be written on the vote; and, but for a checkpoint, the nays that freed its
/// party (`client::building`). Why not, where there is none, or where the store refuses a
/// `true` built so: the party backs another proposal that isn't dead at that history, its
/// nays cast beside a checkpoint's heads, say. Such a vote counts nowhere, so a node
/// casts and records none.
pub(crate) fn builds_on(
    store: &Store<Federation>,
    party: &str,
    proposal: &Id,
    yes: bool,
    nays: &[Vec<u8>],
) -> Result<Vec<Id>, String> {
    let state = store.state();
    let current = state.current();
    let proposing = state
        .proposing(&current.hash, proposal)
        .first()
        .copied()
        .ok_or_else(|| {
            format!(
                "no event proposing {proposal} is above the heads it names: no version \
                 event is written on a vote on it"
            )
        })?;
    let checkpoint = state
        .proposal(proposal)
        .and_then(|json| Version::read(json).ok())
        .and_then(|version| version.checkpoint)
        .is_some();
    let on = super::client::building(proposing, checkpoint, nays)?;
    if yes {
        store
            .state_at(&on)
            .may_back(party, proposal, current)
            .map_err(|why| format!("no store takes that vote: {}", why.0))?;
    }
    Ok(on)
}

/// Whether a proposal can still pass: not dead by the votes cast.
fn alive(current: &Version, proposed: &Proposed) -> bool {
    let parties = current.parties.len();
    3 * (parties - proposed.no.len().min(parties)) >= 2 * parties
}

/// Whether `proposed`, of `all`, holds back a version that only moves to a checkpoint,
/// which would lapse it: it can still pass, and a third of the parties back it, someone
/// but its proposer among them; or it came less than `grace` ago, and its proposer, the
/// party of the first event proposing it, has no older proposal on the version. So two
/// parties keep no proposal alive to hold back checkpoints, and one new is lapsed by none
/// before its proposer is heard, once: another party proposing it again changes nothing.
fn holds_back(current: &Version, proposed: &Proposed, all: &[Proposed], grace: Duration) -> bool {
    let backed = !proposed.yes.is_subset(&proposed.proposers)
        && 3 * proposed.yes.len() >= current.parties.len();
    let fresh = proposed.held < grace
        && !all.iter().any(|other| {
            other.hash != proposed.hash
                && other.held >= grace
                && other.proposers.contains(&proposed.first)
        });
    alive(current, proposed) && (backed || fresh)
}

/// What a node voting by itself by `rules` signs now, as it sees its store, by
/// its party's `record`: its votes, at most one `true`, and a proposal of a checkpoint.
pub(crate) fn decide(seen: &Seen<'_>, rules: &Rules, record: &Record) -> Vec<Act> {
    let current = seen.store.state().current().clone();
    let all = proposed(seen, &current);
    let mut acts = Vec::new();
    let mut backed = false;
    // Its own proposals it backs; others as the parties it follows voted.
    let own: BTreeSet<Id> = record
        .signed
        .iter()
        .filter_map(|text| match Event::read_taken(text.as_bytes()) {
            Some(Event::Signed(Signed {
                does: Does::Proposal { hash, .. },
                ..
            })) => Some(hash),
            _ => None,
        })
        .collect();
    let nays: Vec<Vec<u8>> = all
        .iter()
        .flat_map(|proposed| proposed.votes.iter().cloned())
        .collect();
    for proposed in &all {
        let own_one = own.contains(&proposed.hash);
        // A `false` it copies not: one party's would free bound followers.
        let follows = rules
            .follow
            .iter()
            .any(|party| proposed.yes.contains(party));
        if !own_one && !follows {
            continue;
        }
        let moves_on = proposed
            .version
            .as_ref()
            .is_some_and(|version| version.only_moves_on(&current));
        if !own_one && !rules.follow_any && !moves_on {
            continue;
        }
        // A revision its operators move to by hand: its party's nodes run a continuum
        // that knows it first.
        let revised = proposed
            .version
            .as_ref()
            .is_some_and(|version| version.revision != current.revision);
        if !own_one && revised {
            continue;
        }
        // A checkpoint lapses what else is proposed: the party it follows may not end so
        // a proposal that changes more.
        let lapses = all.iter().any(|other| {
            other.hash != proposed.hash
                && !other
                    .version
                    .as_ref()
                    .is_some_and(|version| version.only_moves_on(&current))
                && holds_back(&current, other, &all, rules.held_for)
        });
        if !own_one && moves_on && lapses {
            continue;
        }
        // One `true` at a time, for a version, by the voters' rule.
        if backed
            || proposed.version.as_ref().is_none_or(|version| {
                backs_checkpoint(seen, rules, &current, version, proposed.held).is_err()
            })
        {
            continue;
        }
        if let Ok(kept) = record.may_vote(&current, proposed.hash, &proposed.json, true, &nays)
            && builds_on(seen.store, &record.party, &proposed.hash, true, &kept).is_ok()
        {
            backed = true;
            acts.push(Act::Vote {
                proposal: proposed.hash,
                yes: true,
                nays: kept,
            });
        }
    }
    if rules.nay_beside {
        acts.extend(beside(&current, &all, &acts, record, &nays));
    }
    if !backed && let Some(version) = checkpoint(seen, rules, &current, &all) {
        let hash = Id::of(&version.bytes());
        if record
            .may_vote(&current, hash, &version, true, &nays)
            .is_ok()
        {
            acts.push(Act::Propose(version));
        }
    }
    acts
}

/// The votes `false` a node casts beside what it backs: where its party backs a proposal on
/// `current` that can still pass, by its `record` or by `acts`, on every other proposal
/// that only moves to a checkpoint and can still pass, but where it votes already.
fn beside(
    current: &Version,
    all: &[Proposed],
    acts: &[Act],
    record: &Record,
    nays: &[Vec<u8>],
) -> Vec<Act> {
    let voting: BTreeSet<Id> = acts
        .iter()
        .filter_map(|act| match act {
            Act::Vote { proposal, .. } => Some(*proposal),
            Act::Propose(_) => None,
        })
        .collect();
    let backs: BTreeSet<Id> = record
        .backs(&current.hash)
        .into_iter()
        .chain(acts.iter().filter_map(|act| match act {
            Act::Vote {
                proposal,
                yes: true,
                ..
            } => Some(*proposal),
            _ => None,
        }))
        .collect();
    let backing = all
        .iter()
        .any(|proposed| backs.contains(&proposed.hash) && alive(current, proposed));
    if !backing {
        return Vec::new();
    }
    all.iter()
        .filter(|proposed| {
            !backs.contains(&proposed.hash)
                && !voting.contains(&proposed.hash)
                && alive(current, proposed)
                && proposed
                    .version
                    .as_ref()
                    .is_some_and(|version| version.only_moves_on(current))
        })
        .filter(|proposed| {
            record
                .may_vote(current, proposed.hash, &proposed.json, false, nays)
                .is_ok()
        })
        .map(|proposed| Act::Vote {
            proposal: proposed.hash,
            yes: false,
            nays: Vec::new(),
        })
        .collect()
}

/// A vote by hand, cast through the node that votes for its party, which keeps its
/// party's one record: `yes` on the proposal of hash `proposal` on the current version, by
/// the record, and with a `true` by the voters' rule; or why not.
pub(crate) fn by_hand(
    seen: &Seen<'_>,
    rules: &Rules,
    record: &Record,
    proposal: Id,
    yes: bool,
) -> Result<Act, String> {
    let current = seen.store.state().current().clone();
    let all = proposed(seen, &current);
    let proposed = all
        .iter()
        .find(|proposed| proposed.hash == proposal)
        .ok_or_else(|| format!("no proposal of hash {proposal} on the current version"))?;
    if yes {
        let version = proposed.version.as_ref().ok_or("it proposes no version")?;
        backs_checkpoint(seen, rules, &current, version, proposed.held)?;
    }
    let nays: Vec<Vec<u8>> = all
        .iter()
        .flat_map(|proposed| proposed.votes.iter().cloned())
        .collect();
    let nays = record.may_vote(&current, proposal, &proposed.json, yes, &nays)?;
    builds_on(seen.store, &record.party, &proposal, yes, &nays)?;
    Ok(Act::Vote {
        proposal,
        yes,
        nays,
    })
}

/// The version that only moves to a checkpoint the node proposes now, if its rules have it
/// propose one: at the heads its store holds, at which the current version is current,
/// where no proposal on it holds it back. None where they are more than an event may name:
/// the node merges them first, under a revision with merge events (`Holding::merge`), or
/// what is written after does.
fn checkpoint(seen: &Seen<'_>, rules: &Rules, current: &Version, all: &[Proposed]) -> Option<Json> {
    let store = seen.store;
    let since = store.state().changed_since_checkpoint();
    let due = rules
        .propose_events
        .is_some_and(|events| store.taken().len() >= events)
        || rules
            .propose_every
            .is_some_and(|every| seen.since_move >= every)
        || rules
            .propose_versions
            .is_some_and(|versions| since >= versions);
    if !due || seen.since_move < rules.back_after {
        return None;
    }
    let blocked = all.iter().any(|proposed| {
        let backs = proposed.version.as_ref().is_some_and(|version| {
            version.only_moves_on(current)
                && backs_checkpoint(seen, rules, current, version, proposed.held).is_ok()
        });
        (alive(current, proposed) && backs) || holds_back(current, proposed, all, rules.held_for)
    });
    if blocked {
        return None;
    }
    // Its heads, which nothing taken builds on: the checkpoint's own, where nothing came
    // after it, which a later checkpoint names not.
    let mut heads = store.heads();
    heads.sort_unstable();
    if store.taken().is_empty()
        || heads.len() > super::event::DEPENDENCIES
        || store.state_at(&heads).current().hash != current.hash
    {
        return None;
    }
    let Json::Object(mut members) = current
        .json
        .without("number")
        .without("changes")
        .without("checkpoint")
    else {
        return None;
    };
    members.push(("number".to_owned(), Json::Int(current.number as i64 + 1)));
    members.push(("changes".to_owned(), Json::Str(current.hash.to_string())));
    members.push((
        "checkpoint".to_owned(),
        Json::Array(heads.iter().map(|id| Json::Str(id.to_string())).collect()),
    ));
    Json::object(members)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::federation::event::Checkpoint;
    use crate::federation::harness::{self, Party, Spec};
    use crate::federation::store::memory::Offered;

    fn taken(store: &mut Store<Federation>, bytes: Vec<u8>) -> Id {
        match store.offer(bytes) {
            Offered::Taken(ids) => ids[0],
            other => panic!("not taken: {other:?}"),
        }
    }

    /// tv, lass and makefu, and a store of their federation.
    fn three() -> (Party, Party, Party, Spec, Store<Federation>) {
        let (tv, lass, makefu) = (
            Party::new("tv", 1),
            Party::new("lass", 2),
            Party::new("makefu", 3),
        );
        let first = Spec::first(&[&tv, &lass, &makefu], &[]);
        let root = Checkpoint {
            at: Vec::new(),
            keeps: Vec::new(),
            version: Version::read(&first.json()).unwrap(),
        };
        let store = Store::new(root.bytes(), Vec::new()).unwrap();
        (tv, lass, makefu, first, store)
    }

    fn record(store: &Store<Federation>, party: &str) -> Record {
        let mut record = Record::begin(store.id(), party, store.state().current());
        record.began = Id::of(b"earlier").to_string();
        record
    }

    fn seen<'a>(
        store: &'a Store<Federation>,
        held: &'a dyn Fn(&Id) -> Option<Duration>,
    ) -> Seen<'a> {
        Seen {
            store,
            held,
            from_start: &|_| false,
            since_move: Duration::ZERO,
        }
    }

    /// The voters' rule backs no checkpoint whose move refuses what the node held for a
    /// while: a write built on the heads and on something beside them, a proposal say, as
    /// clients build on every head they hold, has the heads in its history, and the move
    /// refuses it all the same.
    #[test]
    fn federation_attack_68() {
        let (tv, lass, _, first, mut store) = three();
        let root = store.id();
        let head = taken(
            &mut store,
            tv.write(&[root], "home/tv/a", harness::str("1")),
        );
        let mut junk = first.after(first.hash());
        junk.rules.push(("names/x".to_owned(), harness::str("tv")));
        let beside = taken(&mut store, tv.propose(&[root], &junk.json()));
        let write = taken(
            &mut store,
            lass.write(&[head, beside], "home/lass/b", harness::str("2")),
        );
        let mut checkpoint = first.after(first.hash());
        checkpoint.checkpoint = Some(vec![head]);
        let version = Version::read(&checkpoint.json()).unwrap();
        let rules = Rules {
            held_for: Duration::from_secs(60),
            ..Rules::default()
        };
        let long = |_: &Id| Some(Duration::from_secs(3600));
        let seen = Seen {
            store: &store,
            held: &long,
            from_start: &|_| false,
            since_move: Duration::from_secs(86400),
        };
        let current = store.state().current().clone();
        let why = backs_checkpoint(&seen, &rules, &current, &version, Duration::ZERO)
            .expect_err("its move refuses a write held for a while");
        assert!(why.contains(&write.to_string()), "{why}");
        assert_eq!(store.beside(&[head]), vec![beside, write]);
    }

    /// The voters' rule backs no checkpoint at heads no version may name, one in
    /// another's history say: votes on it are taken, no version event on them is, and a
    /// `true` there would bind the party to what is never written.
    #[test]
    fn a_node_backs_no_checkpoint_at_heads_no_version_may_name() {
        let (tv, _, _, first, mut store) = three();
        let root = store.id();
        let under = taken(
            &mut store,
            tv.write(&[root], "home/tv/a", harness::str("1")),
        );
        let over = taken(
            &mut store,
            tv.write(&[under], "home/tv/a", harness::str("2")),
        );
        let mut checkpoint = first.after(first.hash());
        checkpoint.checkpoint = Some(vec![under, over]);
        let version = Version::read(&checkpoint.json()).unwrap();
        let rules = Rules {
            held_for: Duration::from_secs(60),
            ..Rules::default()
        };
        let long = |_: &Id| Some(Duration::from_secs(3600));
        let seen = Seen {
            store: &store,
            held: &long,
            from_start: &|_| false,
            since_move: Duration::from_secs(86400),
        };
        let current = store.state().current().clone();
        let why = backs_checkpoint(&seen, &rules, &current, &version, Duration::ZERO)
            .expect_err("no version event names those heads");
        assert!(why.contains("one in another's history"), "{why}");
        // The votes are taken, and no version event on them is.
        let proposal = taken(&mut store, tv.propose(&[over], &checkpoint.json()));
        let (lass, makefu) = (Party::new("lass", 2), Party::new("makefu", 3));
        let votes: Vec<Id> = [&tv, &lass, &makefu]
            .iter()
            .map(|party| {
                taken(
                    &mut store,
                    party.vote(&[proposal], first.hash(), checkpoint.hash(), true),
                )
            })
            .collect();
        assert!(matches!(
            store.offer(harness::version_event(&votes, checkpoint.hash())),
            Offered::Refused(_)
        ));
    }

    /// What a node held when it started, it held before any proposal: its clock began at
    /// its start, so a write and a checkpoint's proposal both read then look as old as each
    /// other, and without this nothing was held for a while before the proposal.
    #[test]
    fn a_node_backs_no_checkpoint_leaving_out_what_it_held_when_it_started() {
        let (tv, lass, _, first, mut store) = three();
        let root = store.id();
        let head = taken(
            &mut store,
            tv.write(&[root], "home/tv/a", harness::str("1")),
        );
        let write = taken(
            &mut store,
            lass.write(&[root], "home/lass/b", harness::str("2")),
        );
        let mut checkpoint = first.after(first.hash());
        checkpoint.checkpoint = Some(vec![head]);
        let version = Version::read(&checkpoint.json()).unwrap();
        let rules = Rules {
            held_for: Duration::from_secs(60),
            ..Rules::default()
        };
        // Everything read at the start, an hour ago: the proposal as long as the write.
        let hour = Duration::from_secs(3600);
        let since_start = |_: &Id| Some(hour);
        let current = store.state().current().clone();
        let started = Seen {
            store: &store,
            held: &since_start,
            from_start: &|_| true,
            since_move: hour,
        };
        let why = backs_checkpoint(&started, &rules, &current, &version, hour)
            .expect_err("it leaves out a write held at the start");
        assert!(why.contains(&write.to_string()), "{why}");
        // Taken since the start, as late as the proposal: not held for a while before it.
        let running = Seen {
            from_start: &|_| false,
            ..started
        };
        assert!(backs_checkpoint(&running, &rules, &current, &version, hour).is_ok());
    }

    /// A node casts no `true` its own store refuses. Its party's earlier `true` is in the
    /// history of a checkpoint's heads, and the nays that freed it came after they were
    /// named, beside them: a vote on the checkpoint builds on its proposal alone, where
    /// the earlier proposal isn't dead, so every store refuses it. Cast and recorded, it
    /// would bind the party by a vote no node holds.
    #[test]
    fn a_node_casts_no_true_its_store_refuses() {
        let (tv, lass, makefu, first, mut store) = three();
        let root = store.id();
        let on = first.hash();
        let mut change = first.after(on);
        change
            .rules
            .push(("names/x".to_owned(), harness::str("tv")));
        let asked = taken(&mut store, tv.propose(&[root], &change.json()));
        let earlier = makefu.vote(&[asked], on, change.hash(), true);
        let mut record = record(&store, "makefu");
        record.cast(store.state().current(), &earlier, Vec::new());
        let earlier = taken(&mut store, earlier);
        let head = taken(
            &mut store,
            tv.write(&[earlier], "home/tv/a", harness::str("1")),
        );
        let mut checkpoint = first.after(on);
        checkpoint.checkpoint = Some(vec![head]);
        let proposal = taken(&mut store, tv.propose(&[head], &checkpoint.json()));
        taken(
            &mut store,
            tv.vote(&[proposal], on, checkpoint.hash(), true),
        );
        for party in [&tv, &lass] {
            taken(&mut store, party.vote(&[asked], on, change.hash(), false));
        }
        let rules = Rules {
            follow: vec!["tv".to_owned()],
            held_for: Duration::from_secs(60),
            ..Rules::default()
        };
        let long = |_: &Id| Some(Duration::from_secs(3600));
        let acts = decide(&seen(&store, &long), &rules, &record);
        assert!(
            !acts
                .iter()
                .any(|act| matches!(act, Act::Vote { yes: true, .. })),
            "{acts:?}"
        );
        // What it would have cast, its store refuses.
        assert!(matches!(
            store.offer(makefu.vote(&[proposal], on, checkpoint.hash(), true)),
            Offered::Refused(_)
        ));
    }

    #[test]
    fn a_node_follows_a_party_on_checkpoints() {
        let (tv, lass, _, first, mut store) = three();
        let root = store.id();
        let write = taken(
            &mut store,
            tv.write(&[root], "home/tv/a", harness::str("1")),
        );
        let mut checkpoint = first.after(first.hash());
        checkpoint.checkpoint = Some(vec![write]);
        let mut other = first.after(first.hash());
        other.rules.push(("names/x".to_owned(), harness::str("tv")));
        let proposal = taken(&mut store, tv.propose(&[write], &checkpoint.json()));
        let elsewhere = taken(&mut store, tv.propose(&[write], &other.json()));
        let rules = Rules {
            follow: vec!["tv".to_owned()],
            held_for: Duration::from_secs(60),
            ..Rules::default()
        };
        let long = |_: &Id| Some(Duration::from_secs(3600));
        let record = record(&store, "makefu");
        assert!(decide(&seen(&store, &long), &rules, &record).is_empty());
        taken(
            &mut store,
            tv.vote(&[proposal], first.hash(), checkpoint.hash(), true),
        );
        taken(
            &mut store,
            tv.vote(&[elsewhere], first.hash(), other.hash(), false),
        );
        // The checkpoint, followed; the other changes more, and isn't, by default.
        let acts = decide(&seen(&store, &long), &rules, &record);
        assert_eq!(acts.len(), 1);
        assert!(matches!(
            &acts[0],
            Act::Vote { proposal, yes: true, .. } if *proposal == checkpoint.hash()
        ));
        // On any version, the checkpoint still; the other's `false` it copies not.
        let any = Rules {
            follow_any: true,
            ..rules.clone()
        };
        assert_eq!(decide(&seen(&store, &long), &any, &record).len(), 1);
        // Heads leaving out what it held for a while before it was proposed: not backed.
        let later = taken(
            &mut store,
            lass.write(&[root], "home/lass/b", harness::str("1")),
        );
        let held = |id: &Id| {
            Some(if *id == later {
                Duration::from_secs(7200)
            } else {
                Duration::from_secs(60)
            })
        };
        assert!(decide(&seen(&store, &held), &rules, &record).is_empty());
    }

    /// Row 58 (`docs/federation.md`): a party the node follows backs, or votes `false` on,
    /// a version that changes more than moving to a checkpoint: by default the node follows
    /// neither; one following on any version, as its operator chose, backs the one, and
    /// copies the `false` not.
    #[test]
    fn federation_attack_58() {
        let (tv, _, _, first, mut store) = three();
        let root = store.id();
        let mut backed = first.after(first.hash());
        backed
            .rules
            .push(("names/x".to_owned(), harness::str("tv")));
        let mut opposed = first.after(first.hash());
        opposed
            .rules
            .push(("names/y".to_owned(), harness::str("tv")));
        let one = taken(&mut store, tv.propose(&[root], &backed.json()));
        let two = taken(&mut store, tv.propose(&[root], &opposed.json()));
        taken(
            &mut store,
            tv.vote(&[one], first.hash(), backed.hash(), true),
        );
        taken(
            &mut store,
            tv.vote(&[two], first.hash(), opposed.hash(), false),
        );
        let rules = Rules {
            follow: vec!["tv".to_owned()],
            ..Rules::default()
        };
        let long = |_: &Id| Some(Duration::from_secs(3600));
        let record = record(&store, "makefu");
        assert!(decide(&seen(&store, &long), &rules, &record).is_empty());
        let any = Rules {
            follow_any: true,
            ..rules
        };
        let acts = decide(&seen(&store, &long), &any, &record);
        assert_eq!(acts.len(), 1);
        assert!(matches!(
            &acts[0],
            Act::Vote { proposal, yes: true, .. } if *proposal == backed.hash()
        ));
    }

    /// A party the node follows backs a version moving to a later revision: a node
    /// following on any version backs it not, as its operators move to a revision by hand.
    #[test]
    fn a_follower_backs_no_change_of_the_revision() {
        let (tv, _, _, first, mut store) = three();
        let root = store.id();
        let mut revised = first.after(first.hash());
        revised.revision = crate::federation::revision::Feature::MergeEvents.revision();
        let proposal = taken(&mut store, tv.propose(&[root], &revised.json()));
        taken(
            &mut store,
            tv.vote(&[proposal], first.hash(), revised.hash(), true),
        );
        let any = Rules {
            follow: vec!["tv".to_owned()],
            follow_any: true,
            ..Rules::default()
        };
        let long = |_: &Id| Some(Duration::from_secs(3600));
        let record = record(&store, "makefu");
        assert!(decide(&seen(&store, &long), &any, &record).is_empty());
        // By hand, it does.
        assert!(by_hand(&seen(&store, &long), &any, &record, revised.hash(), true).is_ok());
    }

    /// A party the node follows backs a checkpoint while a proposal that changes more,
    /// which another party backs, can still pass: the node backs it not, as it would lapse
    /// that one; while nobody but its proposer backs that one, it does.
    #[test]
    fn a_follower_lets_no_checkpoint_lapse_what_others_back() {
        let (tv, lass, _, first, mut store) = three();
        let root = store.id();
        let on = first.hash();
        let write = taken(
            &mut store,
            tv.write(&[root], "home/tv/a", harness::str("1")),
        );
        let mut checkpoint = first.after(on);
        checkpoint.checkpoint = Some(vec![write]);
        let mut other = first.after(on);
        other
            .rules
            .push(("names/x".to_owned(), harness::str("lass")));
        let proposal = taken(&mut store, tv.propose(&[write], &checkpoint.json()));
        let elsewhere = taken(&mut store, lass.propose(&[root], &other.json()));
        taken(
            &mut store,
            tv.vote(&[proposal], on, checkpoint.hash(), true),
        );
        let rules = Rules {
            follow: vec!["tv".to_owned()],
            ..Rules::default()
        };
        let long = |_: &Id| Some(Duration::from_secs(3600));
        let record = record(&store, "makefu");
        // lass proposed the other, and backs it alone: the checkpoint, followed.
        taken(&mut store, lass.vote(&[elsewhere], on, other.hash(), true));
        assert_eq!(decide(&seen(&store, &long), &rules, &record).len(), 1);
        // Backed by another too: not.
        taken(&mut store, tv.vote(&[elsewhere], on, other.hash(), true));
        assert!(decide(&seen(&store, &long), &rules, &record).is_empty());
    }

    /// Of nine parties, two keep a proposal alive: it holds back no checkpoint a node
    /// proposes, as fewer than a third back it. A new one holds back for a while, but not
    /// one from a proposer whose earlier one is past that while.
    #[test]
    fn two_parties_hold_back_no_checkpoint_but_a_new_proposal_does_for_a_while() {
        let parties: Vec<Party> = (1..=9).map(|n| Party::new(&format!("p{n}"), n)).collect();
        let named: Vec<&Party> = parties.iter().collect();
        let first = Spec::first(&named, &[]);
        let mut store = Store::new(first.root(), Vec::new()).expect("a root");
        let root = store.id();
        let on = first.hash();
        let write = taken(
            &mut store,
            parties[2].write(&[root], "home/p3/a", harness::str("1")),
        );
        let junk = |n: usize| {
            let mut junk = first.after(on);
            junk.rules.push((format!("names/x{n}"), harness::str("p1")));
            junk
        };
        let old = taken(&mut store, parties[0].propose(&[root], &junk(1).json()));
        taken(
            &mut store,
            parties[0].vote(&[old], on, junk(1).hash(), true),
        );
        taken(
            &mut store,
            parties[1].vote(&[old], on, junk(1).hash(), true),
        );
        let rules = Rules {
            propose_events: Some(1),
            held_for: Duration::from_secs(60),
            ..Rules::default()
        };
        let record = record(&store, "p3");
        let proposes = |acts: Vec<Act>| matches!(acts.as_slice(), [Act::Propose(_)]);
        let long = |_: &Id| Some(Duration::from_secs(3600));
        assert!(proposes(decide(&seen(&store, &long), &rules, &record)));
        // One by p4, new: held back, for a while.
        let new = taken(&mut store, parties[3].propose(&[write], &junk(2).json()));
        let fresh = |id: &Id| {
            Some(if *id == new {
                Duration::from_secs(1)
            } else {
                Duration::from_secs(3600)
            })
        };
        assert!(decide(&seen(&store, &fresh), &rules, &record).is_empty());
        assert!(proposes(decide(&seen(&store, &long), &rules, &record)));
        // p4's, proposed again by p1 a moment later: p4's still, and still held back.
        let echo = taken(&mut store, parties[0].propose(&[write], &junk(2).json()));
        let echoed = |id: &Id| {
            Some(if *id == new {
                Duration::from_secs(2)
            } else if *id == echo {
                Duration::from_secs(1)
            } else {
                Duration::from_secs(3600)
            })
        };
        assert!(decide(&seen(&store, &echoed), &rules, &record).is_empty());
        // One by p1 again, new, while its old one is past that while: not.
        let again = taken(&mut store, parties[0].propose(&[write], &junk(3).json()));
        let fresh_again = |id: &Id| {
            Some(if *id == again {
                Duration::from_secs(1)
            } else {
                Duration::from_secs(3600)
            })
        };
        assert!(proposes(decide(
            &seen(&store, &fresh_again),
            &rules,
            &record
        )));
    }

    /// A node backs no checkpoint, and proposes none, before its store held its checkpoint,
    /// or ran, for a while: a party it follows moves the store once that long at most, and
    /// just started it holds nothing for a while yet.
    #[test]
    fn a_node_backs_no_checkpoint_soon_after_a_move_or_its_start() {
        let (tv, _, _, first, mut store) = three();
        let root = store.id();
        let on = first.hash();
        let write = taken(
            &mut store,
            tv.write(&[root], "home/tv/a", harness::str("1")),
        );
        let mut checkpoint = first.after(on);
        checkpoint.checkpoint = Some(vec![write]);
        let proposal = taken(&mut store, tv.propose(&[write], &checkpoint.json()));
        taken(
            &mut store,
            tv.vote(&[proposal], on, checkpoint.hash(), true),
        );
        let rules = Rules {
            follow: vec!["tv".to_owned()],
            propose_events: Some(1),
            back_after: Duration::from_secs(600),
            ..Rules::default()
        };
        let long = |_: &Id| Some(Duration::from_secs(3600));
        let record = record(&store, "makefu");
        let soon = Seen {
            store: &store,
            held: &long,
            from_start: &|_| false,
            since_move: Duration::from_secs(60),
        };
        assert!(decide(&soon, &rules, &record).is_empty());
        let later = Seen {
            since_move: Duration::from_secs(700),
            ..soon
        };
        assert_eq!(decide(&later, &rules, &record).len(), 1);
    }

    #[test]
    fn a_vote_by_hand_keeps_to_the_record() {
        let (tv, _, _, first, mut store) = three();
        let root = store.id();
        let next = first.after(first.hash());
        taken(&mut store, tv.propose(&[root], &next.json()));
        let long = |_: &Id| Some(Duration::from_secs(3600));
        let rules = Rules::default();
        let mut record = record(&store, "tv");
        assert!(by_hand(&seen(&store, &long), &rules, &record, Id::of(b"none"), true).is_err());
        let act = by_hand(&seen(&store, &long), &rules, &record, next.hash(), true).unwrap();
        assert!(matches!(act, Act::Vote { yes: true, .. }));
        record.cast(
            store.state().current(),
            &tv.vote(&[root], first.hash(), next.hash(), true),
            Vec::new(),
        );
        assert!(by_hand(&seen(&store, &long), &rules, &record, next.hash(), false).is_err());
    }

    /// A node proposes a checkpoint once so many versions were written since its store's:
    /// with one, after every version, at heads above it.
    #[test]
    fn a_node_proposes_a_checkpoint_after_a_version() {
        let (tv, lass, _, first, mut store) = three();
        let root = store.id();
        let on = first.hash();
        let next = first.after(on);
        let proposal = taken(&mut store, tv.propose(&[root], &next.json()));
        let votes = [&tv, &lass]
            .map(|party| taken(&mut store, party.vote(&[proposal], on, next.hash(), true)));
        let rules = Rules {
            propose_versions: Some(1),
            ..Rules::default()
        };
        let long = |_: &Id| Some(Duration::from_secs(3600));
        let record = record(&store, "makefu");
        assert!(decide(&seen(&store, &long), &rules, &record).is_empty());
        let written = taken(&mut store, harness::version_event(&votes, next.hash()));
        let acts = decide(&seen(&store, &long), &rules, &record);
        let [Act::Propose(json)] = acts.as_slice() else {
            panic!("not one proposal: {acts:?}");
        };
        let version = Version::read(json).unwrap();
        assert_eq!(version.checkpoint, Some(vec![written]));
        assert_eq!(version.changes, Some(next.hash()));
        // Once the store moved there, the version that moved it counts not: none again.
        let on = next.hash();
        let proposal = taken(&mut store, tv.propose(&[written], json));
        let votes = [&tv, &lass]
            .map(|party| taken(&mut store, party.vote(&[proposal], on, version.hash, true)));
        taken(&mut store, harness::version_event(&votes, version.hash));
        assert_eq!(store.state().current().hash, version.hash);
        assert_eq!(store.state().changed_since_checkpoint(), 0);
        assert!(decide(&seen(&store, &long), &rules, &record).is_empty());
    }

    /// A node proposes a checkpoint at the heads its store holds, the one it took a moment
    /// ago among them; none where they are more than an event may name.
    #[test]
    fn a_node_proposes_a_checkpoint_at_the_heads_it_holds() {
        let (tv, _, _, first, mut store) = three();
        let root = store.id();
        let old = taken(
            &mut store,
            tv.write(&[root], "home/tv/a", harness::str("1")),
        );
        let new = taken(&mut store, tv.write(&[old], "home/tv/a", harness::str("2")));
        let rules = Rules {
            propose_events: Some(2),
            held_for: Duration::from_secs(60),
            ..Rules::default()
        };
        let held = |id: &Id| {
            Some(if *id == new {
                Duration::from_secs(1)
            } else {
                Duration::from_secs(3600)
            })
        };
        let record = record(&store, "tv");
        let acts = decide(&seen(&store, &held), &rules, &record);
        let [Act::Propose(json)] = acts.as_slice() else {
            panic!("not one proposal: {acts:?}");
        };
        let version = Version::read(json).unwrap();
        assert_eq!(version.checkpoint, Some(vec![new]));
        assert!(version.only_moves_on(store.state().current()));
        assert_eq!(version.changes, Some(first.hash()));
        // Not due: none.
        let quiet = Rules {
            propose_events: Some(3),
            ..rules.clone()
        };
        assert!(decide(&seen(&store, &held), &quiet, &record).is_empty());
        // Proposed, it backs it, and proposes none beside it.
        let mut record = record;
        let proposing = tv.propose(&[new], json);
        record.signed(&proposing);
        taken(&mut store, proposing);
        let acts = decide(&seen(&store, &held), &rules, &record);
        assert_eq!(acts.len(), 1);
        assert!(matches!(&acts[0], Act::Vote { yes: true, .. }));
        // Heads apart, more than an event may name.
        let (tv, _, _, _, mut apart) = three();
        let root = apart.id();
        for n in 0..=crate::federation::event::DEPENDENCIES {
            taken(
                &mut apart,
                tv.write(&[root], &format!("home/tv/k{n}"), harness::str("1")),
            );
        }
        let record = super::tests::record(&apart, "tv");
        assert!(decide(&seen(&apart, &held), &rules, &record).is_empty());
    }

    /// Two checkpoints proposed at once: a node backing one votes `false` on the other by
    /// its rule, and on nothing else; without the rule, on nothing.
    #[test]
    fn a_node_votes_false_on_a_checkpoint_beside_the_one_it_backs() {
        let (tv, lass, _, first, mut store) = three();
        let root = store.id();
        let on = first.hash();
        let here = taken(
            &mut store,
            tv.write(&[root], "home/tv/a", harness::str("1")),
        );
        let there = taken(
            &mut store,
            lass.write(&[root], "home/lass/b", harness::str("1")),
        );
        let (mut one, mut two) = (first.after(on), first.after(on));
        one.checkpoint = Some(vec![here]);
        two.checkpoint = Some(vec![there]);
        let mut other = first.after(on);
        other.rules.push(("names/x".to_owned(), harness::str("tv")));
        let proposing = tv.propose(&[here], &one.json());
        let mut record = record(&store, "tv");
        record.signed(&proposing);
        let proposal = taken(&mut store, proposing);
        let by_lass = [
            taken(&mut store, lass.propose(&[there], &two.json())),
            taken(&mut store, lass.propose(&[root], &other.json())),
        ];
        // lass's, held since its proposals came, after tv proposed.
        let long = |id: &Id| {
            Some(if *id == there || by_lass.contains(id) {
                Duration::from_secs(60)
            } else {
                Duration::from_secs(3600)
            })
        };
        let rules = Rules {
            nay_beside: true,
            ..Rules::default()
        };
        // Backing its own, this round: `false` on the other checkpoint, not on the version
        // that changes more.
        let acts = decide(&seen(&store, &long), &rules, &record);
        assert_eq!(acts.len(), 2, "{acts:?}");
        assert!(acts.contains(&Act::Vote {
            proposal: one.hash(),
            yes: true,
            nays: Vec::new()
        }));
        assert!(acts.contains(&Act::Vote {
            proposal: two.hash(),
            yes: false,
            nays: Vec::new()
        }));
        let quiet = Rules::default();
        assert_eq!(decide(&seen(&store, &long), &quiet, &record).len(), 1);
        // Its `true` recorded and taken: `false` on the other, once.
        let yes = tv.vote(&[proposal], on, one.hash(), true);
        record.cast(store.state().current(), &yes, Vec::new());
        taken(&mut store, yes);
        let acts = decide(&seen(&store, &long), &rules, &record);
        assert_eq!(
            acts,
            vec![Act::Vote {
                proposal: two.hash(),
                yes: false,
                nays: Vec::new()
            }]
        );
        let no = tv.vote(&[proposal], on, two.hash(), false);
        record.cast(store.state().current(), &no, Vec::new());
        assert!(decide(&seen(&store, &long), &rules, &record).is_empty());
    }
}