diff options
| author | tv <tv@krebsco.de> | 2026-11-17 00:09:59 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-17 01:45:53 +0100 |
| commit | 41e290716c9ce517597d4b2dec29c8882c36afdb (patch) | |
| tree | 41ac19e6f9c9ba23448576c906e7a56b32a07517 | |
| parent | cd47b1fc79893507096d4b6b7e25f47ee8c7cdbb (diff) | |
federation: a rule voting `false` on a checkpoint proposed beside one a node backs
`--store-nay-beside`, for a node voting by itself: where its party backs a proposal on
the current version that can still pass, by its record or by what it votes now, it
votes `false` on every other proposal there that only moves to a checkpoint and can
still pass, once. So, of two checkpoints proposed at once, the one it doesn't back
dies, and its backers are free (`docs/federation.md`, Votes a node casts by itself).
| -rw-r--r-- | docs/example.toml | 6 | ||||
| -rw-r--r-- | src/config/mod.rs | 12 | ||||
| -rw-r--r-- | src/federation/client.rs | 9 | ||||
| -rw-r--r-- | src/federation/vote.rs | 141 | ||||
| -rw-r--r-- | src/lib.rs | 1 |
5 files changed, 167 insertions, 2 deletions
diff --git a/docs/example.toml b/docs/example.toml index 36a4ae2..7f01df7 100644 --- a/docs/example.toml +++ b/docs/example.toml @@ -152,12 +152,14 @@ htpasswd = "/etc/continuum/htpasswd" # (docs/federation.md, Votes a node casts by itself), with a key its party's version # names, kept here, and its party's one record of its votes; its party then votes by # hand only through it (POST /admin/events/vote). It follows the parties named (by -# default only on versions that only move to a checkpoint), and proposes a checkpoint -# once it holds so many events beyond its checkpoint, or so long after it moved there. +# default only on versions that only move to a checkpoint), votes `false` on a +# checkpoint proposed beside one it backs, if told, and proposes a checkpoint once it +# holds so many events beyond its checkpoint, or so long after it moved there. # store-vote-key = "/var/lib/continuum/party.key" # store-vote-record = "/var/lib/continuum/party-record.json" # store-follow = ["lass"] # store-follow-any = false +# store-nay-beside = false # store-propose-events = 10000 # store-propose-every = "7d" # store-held-for = "10m" diff --git a/src/config/mod.rs b/src/config/mod.rs index 7889d7c..ce82ba9 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -282,6 +282,14 @@ pub(crate) const OPTIONS: &[OptionSpec] = &[ ), #[cfg(feature = "federation")] option( + "store-nay-beside", + Kind::Flag, + "BOOL", + Some("false"), + "With store-vote-key: where it backs a proposal that can still pass, vote `false` on every other one on the current version that only moves to a checkpoint, so that, of two checkpoints proposed at once, the other dies and its backers are free.", + ), + #[cfg(feature = "federation")] + option( "store-propose-events", Kind::Integer, "COUNT", @@ -759,6 +767,9 @@ pub(crate) struct Settings { #[serde(default)] pub(crate) store_follow_any: bool, #[cfg(feature = "federation")] + #[serde(default)] + pub(crate) store_nay_beside: bool, + #[cfg(feature = "federation")] pub(crate) store_propose_events: Option<usize>, #[cfg(feature = "federation")] #[serde( @@ -1294,6 +1305,7 @@ impl Settings { ("store-vote-record", self.store_vote_record.is_some()), ("store-follow", !self.store_follow.is_empty()), ("store-follow-any", self.store_follow_any), + ("store-nay-beside", self.store_nay_beside), ("store-propose-events", self.store_propose_events.is_some()), ("store-propose-every", self.store_propose_every.is_some()), ]; diff --git a/src/federation/client.rs b/src/federation/client.rs index ef6e116..85411fe 100644 --- a/src/federation/client.rs +++ b/src/federation/client.rs @@ -242,6 +242,15 @@ impl Record { .map_err(|error| format!("{}: {error}", path.display())) } + /// The proposals on `on` its party voted `true` on, by their hashes. + pub(crate) fn backs(&self, on: &Id) -> Vec<Id> { + self.votes_on(on) + .into_iter() + .filter(|vote| vote.yes) + .map(|vote| vote.proposal) + .collect() + } + /// Its votes on `on`, as it cast them. fn votes_on(&self, on: &Id) -> Vec<Vote> { self.votes 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(¤t, &all, &acts, record, &nays)); + } if !backed && let Some(version) = checkpoint(seen, rules, ¤t, &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(¤t.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()); + } } @@ -66,6 +66,7 @@ fn voter(args: &config::Settings) -> Result<Option<federation::held::Voter>> { rules: federation::vote::Rules { follow: args.store_follow.clone(), follow_any: args.store_follow_any, + nay_beside: args.store_nay_beside, propose_events: args.store_propose_events, propose_every: args.store_propose_every, held_for: args.store_held_for, |
