aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/vote.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation/vote.rs')
-rw-r--r--src/federation/vote.rs141
1 files changed, 141 insertions, 0 deletions
diff --git a/src/federation/vote.rs b/src/federation/vote.rs
index 4b11c32..9bf7a1c 100644
--- a/src/federation/vote.rs
+++ b/src/federation/vote.rs
@@ -11,6 +11,10 @@
//! a checkpoint, at the heads of what it held for a while, where nothing that can still
//! pass is proposed on the current version, but a proposal it wouldn't back that nobody
//! backs but its proposer. 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
@@ -42,6 +46,8 @@ pub(crate) struct Rules {
pub(crate) propose_every: Option<Duration>,
/// How long it holds an event before it holds it for a while.
pub(crate) held_for: Duration,
+ /// Voting `false` on a checkpoint proposed beside one it backs.
+ pub(crate) nay_beside: bool,
}
/// What the node sees: its store, how long it has held each event it took, and how long
@@ -236,6 +242,9 @@ pub(crate) fn decide(seen: &Seen<'_>, rules: &Rules, record: &Record) -> Vec<Act
});
}
}
+ 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
@@ -248,6 +257,64 @@ pub(crate) fn decide(seen: &Seen<'_>, rules: &Rules, record: &Record) -> Vec<Act
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.
@@ -510,4 +577,78 @@ mod tests {
assert_eq!(acts.len(), 1);
assert!(matches!(&acts[0], Act::Vote { yes: true, .. }));
}
+
+ /// 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());
+ }
}