diff options
Diffstat (limited to 'src/federation')
| -rw-r--r-- | src/federation/fold.rs | 85 |
1 files changed, 71 insertions, 14 deletions
diff --git a/src/federation/fold.rs b/src/federation/fold.rs index 78f5c39..89ce12c 100644 --- a/src/federation/fold.rs +++ b/src/federation/fold.rs @@ -49,6 +49,19 @@ pub(crate) struct Ballot { pub(crate) votes: Vec<Id>, } +/// What the votes on a proposal on a version make of it, as a store reads them. +#[derive(Debug, PartialEq, Eq)] +pub(crate) struct Tally<'a> { + /// The parties voting `true`. + pub(crate) yes: BTreeSet<&'a str>, + /// The parties voting `false`. + pub(crate) no: BTreeSet<&'a str>, + /// Whether it passed: two thirds of the parties of the version voted `true`. + pub(crate) passed: bool, + /// Whether it is dead: so many voted `false` that the rest can't pass it. + pub(crate) dead: bool, +} + /// A party's key set or node list. #[derive(Clone, Copy, PartialEq, Eq)] enum List { @@ -386,19 +399,17 @@ impl State { (yes, no) } - /// Whether a proposal on `on` passed here: two thirds of the parties of `on` voted - /// `true`. - fn passed(&self, on: &Version, proposal: &Id) -> bool { - let (yes, _) = self.votes(&on.hash, proposal); - 3 * yes.len() >= 2 * on.parties.len() - } - - /// Whether a proposal on `on` is dead here: so many voted `false` that the rest can't - /// pass it. - fn dead(&self, on: &Version, proposal: &Id) -> bool { - let (_, no) = self.votes(&on.hash, proposal); + /// What the votes here make of the proposal of hash `proposal` on `on`: who voted what, + /// whether it passed, and whether it is dead. + pub(crate) fn tally(&self, on: &Version, proposal: &Id) -> Tally<'_> { + let (yes, no) = self.votes(&on.hash, proposal); let parties = on.parties.len(); - 3 * (parties - no.len().min(parties)) < 2 * parties + Tally { + passed: 3 * yes.len() >= 2 * parties, + dead: 3 * (parties - no.len().min(parties)) < 2 * parties, + yes, + no, + } } /// Whether a signed event is taken here, at its history, `current` the version current @@ -461,7 +472,7 @@ impl State { _ => None, }) .collect(); - if backed.iter().any(|other| !self.dead(current, other)) { + if backed.iter().any(|other| !self.tally(current, other).dead) { return Err(Refused::new( "its party backs another proposal on that version, not dead", )); @@ -480,7 +491,7 @@ impl State { "its proposal isn't proposed on the version current at its history", )); } - if !self.passed(current, hash) { + if !self.tally(current, hash).passed { return Err(Refused::new("its proposal hasn't passed at its history")); } let next = Version::read(&self.proposed[hash]) @@ -781,6 +792,52 @@ mod tests { assert!(store.state().ballots(&next.hash()).is_empty()); } + /// Who voted what on a proposal, and whether it passed or is dead. + #[test] + fn a_tally_names_the_votes_and_whether_a_proposal_passed_or_is_dead() { + let (tv, lass, makefu) = (tv(), lass(), makefu()); + let spec = Spec::first(&[&tv, &lass, &makefu], &[]); + let mut store = federation(&spec); + let root = store.id(); + let on = spec.hash(); + let one = spec.after(on); + let mut two = spec.after(on); + two.rules.clear(); + let first = taken(store.offer(tv.propose(&[root], &one.json()))); + let second = taken(store.offer(tv.propose(&[root], &two.json()))); + let tally = |store: &Store<Federation>, proposal: Id| { + let state = store.state(); + let tally = state.tally(state.current(), &proposal); + let parties = |set: BTreeSet<&str>| -> Vec<String> { + set.into_iter().map(str::to_owned).collect() + }; + ( + parties(tally.yes), + parties(tally.no), + tally.passed, + tally.dead, + ) + }; + assert_eq!(tally(&store, one.hash()), (vec![], vec![], false, false)); + for (party, proposal, hash, yes) in [ + (&lass, first, one.hash(), true), + (&makefu, first, one.hash(), true), + (&lass, second, two.hash(), false), + (&makefu, second, two.hash(), false), + ] { + taken(store.offer(party.vote(&[proposal], on, hash, yes))); + } + let (lass, makefu) = ("lass".to_owned(), "makefu".to_owned()); + assert_eq!( + tally(&store, one.hash()), + (vec![lass.clone(), makefu.clone()], vec![], true, false) + ); + assert_eq!( + tally(&store, two.hash()), + (vec![], vec![lass, makefu], false, true) + ); + } + #[test] fn a_party_writes_where_a_rule_gives_it_the_place() { let (tv, lass) = (tv(), lass()); |
