diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/federation/client.rs | 75 |
1 files changed, 71 insertions, 4 deletions
diff --git a/src/federation/client.rs b/src/federation/client.rs index 74be7c8..32e3b70 100644 --- a/src/federation/client.rs +++ b/src/federation/client.rs @@ -200,6 +200,35 @@ impl Vote { } } +/// Of `nays`, each checked from itself and `on`, those that make the proposal `proposal` +/// on `on` dead, so many parties voting `false` that the rest can't pass it: of each party +/// one, of the lowest ID, of the first parties by name, as many as it takes and no more. +/// None where they don't. Other parties sign as many votes as they like, and a vote builds +/// on few events (`building`): these are a third of the parties and one. +fn ending(on: &Version, proposal: &Id, nays: &[Vec<u8>]) -> Option<Vec<Vec<u8>>> { + let mut no: BTreeMap<String, (Id, &Vec<u8>)> = BTreeMap::new(); + for bytes in nays { + let Some(vote) = Vote::read(bytes, true) else { + continue; + }; + if !vote.counts_on(on) || vote.proposal != *proposal || vote.yes { + continue; + } + let id = Id::of(bytes); + let lowest = no.entry(vote.party).or_insert((id, bytes)); + if id < lowest.0 { + *lowest = (id, bytes); + } + } + let needed = on.parties.len() / 3 + 1; + (no.len() >= needed).then(|| { + no.into_values() + .take(needed) + .map(|(_, bytes)| bytes.clone()) + .collect() + }) +} + /// Whether `nays`, each checked from itself and `on`, make the proposal `proposal` on `on` /// dead: so many parties voted `false` that the rest can't pass it. fn dead_by(on: &Version, proposal: &Id, nays: &[Vec<u8>]) -> bool { @@ -314,7 +343,7 @@ impl Record { /// `nays` to keep with it, or why not. Never `true` and `false` on one proposal, nor /// the same twice; none on the version it began on; a `true` only for a version, and /// one at a time: its party's earlier `true` on `current` must be on a proposal the - /// `nays` make dead. + /// `nays` make dead, and of them it keeps those that do, no more (`ending`). pub(crate) fn may_vote( &self, current: &Version, @@ -341,15 +370,19 @@ impl Record { return Ok(Vec::new()); } backable(current, proposed)?; - let mut kept = Vec::new(); + let mut kept: Vec<Vec<u8>> = Vec::new(); for earlier in cast.iter().filter(|vote| vote.yes) { - if !dead_by(current, &earlier.proposal, nays) { + let Some(ending) = ending(current, &earlier.proposal, nays) else { return Err(format!( "one `true` at a time: the party's on {} isn't dead by the votes given", earlier.proposal )); + }; + for nay in ending { + if !kept.contains(&nay) { + kept.push(nay); + } } - kept.extend(nays.iter().cloned()); } Ok(kept) } @@ -1509,6 +1542,40 @@ mod tests { assert!(building(y, false, &many).is_err()); } + /// A second `true` keeps just the nays that end the first: of each party one, of the + /// lowest ID, and of no more parties than end it. Given every vote on the version, as + /// a node gives them, one party's `false` signed again and again on other dependencies + /// among them, it kept them all, and a vote builds on 64 events at most. + #[test] + fn a_second_true_keeps_only_the_nays_that_end_the_first() { + let (tv, lass, makefu, first, current, one, two) = three(); + let (root, on) = (Id::of(&first.root()), first.hash()); + let x = Id::of(&tv.propose(&[root], &one.json())); + let backed = tv.vote(&[x], on, one.hash(), true); + let mut record = Record::before(root, "tv"); + record.cast(¤t, &backed, Vec::new()); + let mut votes: Vec<Vec<u8>> = (0..70) + .map(|n| { + let write = lass.write(&[root], "home/lass/a", harness::str(&n.to_string())); + lass.vote(&[x, Id::of(&write)], on, one.hash(), false) + }) + .collect(); + let lowest = votes + .iter() + .min_by_key(|vote| Id::of(vote)) + .unwrap() + .clone(); + let makefus = makefu.vote(&[x], on, one.hash(), false); + votes.push(makefus.clone()); + votes.push(backed); + let kept = record + .may_vote(¤t, two.hash(), &two.json(), true, &votes) + .unwrap(); + assert_eq!(kept.len(), 2); + assert!(kept.contains(&lowest) && kept.contains(&makefus)); + assert_eq!(building(x, false, &kept).unwrap().len(), 3); + } + /// Row 72 (`docs/federation.md`): a party admitted under the name of a place's segment /// moves who writes there, rules unchanged, and so does a change of the rules' order; /// the places it moves are named, the others left out. |
