From 237c79efc5d25f438d024522fd89ea7fc600cbd1 Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 19 Nov 2026 21:24:09 +0000 Subject: continuum-party: a record held against the node's votes, a wait, restore A party votes from one record; a second record voting beside it, or one begun anew after a loss, can make the party vote twice. The client votes by its record, never by what a node shows, and still should look. Before a vote (`consistent`, `unrecorded`), the party's votes on the current version that the node shows (each proposal's yes and no) are held against the record's (Record::votes_cast): one the node shows that the record lacks refuses the vote, naming it and `restore`; one the record holds that the node doesn't show is said, as on its way or held back. Where they agree, nothing is said. What may keep a vote from the node is said too (`cut_off`, from counts' `heard`): the party's own nodes the node hasn't heard from in five minutes, through which another vote of it may have gone; and the parties none of whose nodes it heard from lately, where they are a third of those with nodes or more: a part of the swarm cut off. Then it waits (--wait SECONDS, 5 unless given, on vote and propose) and asks the node again: a vote cast elsewhere just before arrives first. A version passing meanwhile ends it, as the proposal lapsed. `restore` takes the party's votes on the current version, as the node holds them, into its record, or a new one of --party (Record::restore, checking each vote's signature, its party and version, none twice); a record begun on the current version casts on it then. Best effort: a vote on its way from elsewhere isn't among them, and what `again` signs anew, and the nays that freed an earlier `true`, it doesn't learn. --- src/federation/client.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'src/federation/client.rs') diff --git a/src/federation/client.rs b/src/federation/client.rs index 846d29c..b95cb3c 100644 --- a/src/federation/client.rs +++ b/src/federation/client.rs @@ -264,6 +264,42 @@ impl Record { .collect() } + /// Its votes on `on`, each by its proposal's hash and whether it is `true`. + pub(crate) fn votes_cast(&self, on: &Id) -> Vec<(Id, bool)> { + self.votes_on(on) + .into_iter() + .map(|vote| (vote.proposal, vote.yes)) + .collect() + } + + /// Takes in its party's votes on `current` among `votes`, each as signed, as a node + /// holds them, that it lacks (`continuum-party restore`): best effort, as one on its + /// way from elsewhere isn't among them. A record begun on `current` casts on it then, + /// by them. How many it took. + pub(crate) fn restore(&mut self, current: &Version, votes: &[Vec]) -> usize { + let mut taken = 0; + for bytes in votes { + let Some(vote) = Vote::read(bytes, true) else { + continue; + }; + if vote.party != self.party || vote.on != current.hash { + continue; + } + if self + .votes + .contains(&String::from_utf8_lossy(bytes).into_owned()) + { + continue; + } + self.cast(current, bytes, Vec::new()); + taken += 1; + } + if self.began == current.hash.to_string() { + self.began = String::new(); + } + taken + } + /// Its votes on `on`, as it cast them. fn votes_on(&self, on: &Id) -> Vec { self.votes @@ -980,6 +1016,35 @@ mod tests { assert!(json(&serde_json::json!(1u64 << 60)).is_err()); } + /// A record restored from a node's votes takes its party's on the current version that + /// it lacks, none of another party's or version, none twice; one begun on the current + /// version casts on it then, by them. + #[test] + fn a_record_restored_from_the_votes_a_node_holds() { + let (tv, lass, _, first, current, one, two) = three(); + let root = Id::of(b"root"); + let mut record = Record::begin(root, "tv", ¤t); + let mine = tv.vote(&[root], first.hash(), one.hash(), true); + let theirs = lass.vote(&[root], first.hash(), one.hash(), true); + let elsewhere = tv.vote(&[root], one.hash(), two.hash(), true); + let votes = [mine.clone(), theirs, elsewhere, mine]; + assert_eq!(record.restore(¤t, &votes), 1); + assert_eq!(record.votes_cast(¤t.hash), [(one.hash(), true)]); + assert_eq!(record.began, ""); + assert_eq!(record.restore(¤t, &votes), 0); + // By what it took: never `false` beside that `true`, nor another `true` beside it. + assert!( + record + .may_vote(¤t, one.hash(), &one.json(), false, &[]) + .is_err() + ); + assert!( + record + .may_vote(¤t, two.hash(), &two.json(), true, &[]) + .is_err() + ); + } + #[test] fn a_record_votes_one_true_at_a_time_and_never_both() { let (tv, lass, makefu, first, current, one, two) = three(); -- cgit v1.3.1