diff options
Diffstat (limited to 'src/federation/vote.rs')
| -rw-r--r-- | src/federation/vote.rs | 99 |
1 files changed, 98 insertions, 1 deletions
diff --git a/src/federation/vote.rs b/src/federation/vote.rs index e96cb22..baf8dd0 100644 --- a/src/federation/vote.rs +++ b/src/federation/vote.rs @@ -236,6 +236,47 @@ pub(crate) fn backs_checkpoint( } } +/// What a vote of `party` on the proposal of hash `proposal` builds on in `store`: an +/// event proposing it, of a checkpoint one above the heads it names, so that a version +/// event may be written on the vote; and, but for a checkpoint, the nays that freed its +/// party (`client::building`). Why not, where there is none, or where the store refuses a +/// `true` built so: the party backs another proposal that isn't dead at that history, its +/// nays cast beside a checkpoint's heads, say. Such a vote counts nowhere, so a node +/// casts and records none. +pub(crate) fn builds_on( + store: &Store<Federation>, + party: &str, + proposal: &Id, + yes: bool, + nays: &[Vec<u8>], +) -> Result<Vec<Id>, String> { + let state = store.state(); + let current = state.current(); + let proposing = state + .proposing(¤t.hash, proposal) + .first() + .copied() + .ok_or_else(|| { + format!( + "no event proposing {proposal} is above the heads it names: no version \ + event is written on a vote on it" + ) + })?; + let checkpoint = state + .proposal(proposal) + .and_then(|json| Version::read(json).ok()) + .and_then(|version| version.checkpoint) + .is_some(); + let on = super::client::building(proposing, checkpoint, nays)?; + if yes { + store + .state_at(&on) + .may_back(party, proposal, current) + .map_err(|why| format!("no store takes that vote: {}", why.0))?; + } + Ok(on) +} + /// Whether a proposal can still pass: not dead by the votes cast. fn alive(current: &Version, proposed: &Proposed) -> bool { let parties = current.parties.len(); @@ -330,7 +371,9 @@ pub(crate) fn decide(seen: &Seen<'_>, rules: &Rules, record: &Record) -> Vec<Act { continue; } - if let Ok(kept) = record.may_vote(¤t, proposed.hash, &proposed.json, true, &nays) { + if let Ok(kept) = record.may_vote(¤t, proposed.hash, &proposed.json, true, &nays) + && builds_on(seen.store, &record.party, &proposed.hash, true, &kept).is_ok() + { backed = true; acts.push(Act::Vote { proposal: proposed.hash, @@ -437,6 +480,7 @@ pub(crate) fn by_hand( .flat_map(|proposed| proposed.votes.iter().cloned()) .collect(); let nays = record.may_vote(¤t, proposal, &proposed.json, yes, &nays)?; + builds_on(seen.store, &record.party, &proposal, yes, &nays)?; Ok(Act::Vote { proposal, yes, @@ -684,6 +728,59 @@ mod tests { assert!(backs_checkpoint(&running, &rules, ¤t, &version, hour).is_ok()); } + /// A node casts no `true` its own store refuses. Its party's earlier `true` is in the + /// history of a checkpoint's heads, and the nays that freed it came after they were + /// named, beside them: a vote on the checkpoint builds on its proposal alone, where + /// the earlier proposal isn't dead, so every store refuses it. Cast and recorded, it + /// would bind the party by a vote no node holds. + #[test] + fn a_node_casts_no_true_its_store_refuses() { + let (tv, lass, makefu, first, mut store) = three(); + let root = store.id(); + let on = first.hash(); + let mut change = first.after(on); + change + .rules + .push(("names/x".to_owned(), harness::str("tv"))); + let asked = taken(&mut store, tv.propose(&[root], &change.json())); + let earlier = makefu.vote(&[asked], on, change.hash(), true); + let mut record = record(&store, "makefu"); + record.cast(store.state().current(), &earlier, Vec::new()); + let earlier = taken(&mut store, earlier); + let head = taken( + &mut store, + tv.write(&[earlier], "home/tv/a", harness::str("1")), + ); + let mut checkpoint = first.after(on); + checkpoint.checkpoint = Some(vec![head]); + let proposal = taken(&mut store, tv.propose(&[head], &checkpoint.json())); + taken( + &mut store, + tv.vote(&[proposal], on, checkpoint.hash(), true), + ); + for party in [&tv, &lass] { + taken(&mut store, party.vote(&[asked], on, change.hash(), false)); + } + let rules = Rules { + follow: vec!["tv".to_owned()], + held_for: Duration::from_secs(60), + ..Rules::default() + }; + let long = |_: &Id| Some(Duration::from_secs(3600)); + let acts = decide(&seen(&store, &long), &rules, &record); + assert!( + !acts + .iter() + .any(|act| matches!(act, Act::Vote { yes: true, .. })), + "{acts:?}" + ); + // What it would have cast, its store refuses. + assert!(matches!( + store.offer(makefu.vote(&[proposal], on, checkpoint.hash(), true)), + Offered::Refused(_) + )); + } + #[test] fn a_node_follows_a_party_on_checkpoints() { let (tv, lass, _, first, mut store) = three(); |
