diff options
Diffstat (limited to 'src/federation/client.rs')
| -rw-r--r-- | src/federation/client.rs | 65 |
1 files changed, 65 insertions, 0 deletions
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<u8>]) -> 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<Vote> { 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(); |
