diff options
Diffstat (limited to 'src/federation')
| -rw-r--r-- | src/federation/vote.rs | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/src/federation/vote.rs b/src/federation/vote.rs index 1cd955d..e96cb22 100644 --- a/src/federation/vote.rs +++ b/src/federation/vote.rs @@ -91,8 +91,11 @@ struct Proposed { /// What it proposes, and whether that is a version. json: Json, version: Option<Version>, - /// Its proposing events' parties, and how long the node has held the first. + /// Its proposing events' parties, how long the node has held the first, and that one's + /// party (of several held as long, the one of the lowest ID): its proposer, whoever + /// proposes it again. proposers: BTreeSet<String>, + first: String, held: Duration, /// Its votes: each party's, and the events, as bytes. yes: BTreeSet<String>, @@ -107,6 +110,7 @@ fn proposed(seen: &Seen<'_>, current: &Version) -> Vec<Proposed> { let mut proposers = BTreeSet::new(); let mut json = None; let mut held = Duration::ZERO; + let mut first: Option<(Duration, std::cmp::Reverse<Id>, String)> = None; for id in &ballot.proposals { if let Some(Event::Signed(Signed { party, @@ -114,12 +118,20 @@ fn proposed(seen: &Seen<'_>, current: &Version) -> Vec<Proposed> { .. })) = store.get(id).and_then(Event::read_taken) { - proposers.insert(party); - held = held.max((seen.held)(id).unwrap_or_default()); + proposers.insert(party.clone()); + let since = (seen.held)(id).unwrap_or_default(); + held = held.max(since); + let this = (since, std::cmp::Reverse(*id), party); + if first + .as_ref() + .is_none_or(|first| (&this.0, &this.1) > (&first.0, &first.1)) + { + first = Some(this); + } json = Some(version); } } - let Some(json) = json else { + let (Some(json), Some((_, _, first))) = (json, first) else { continue; }; let (mut yes, mut no, mut votes) = (BTreeSet::new(), BTreeSet::new(), Vec::new()); @@ -146,6 +158,7 @@ fn proposed(seen: &Seen<'_>, current: &Version) -> Vec<Proposed> { version: Version::read(&json).ok(), json, proposers, + first, held, yes, no, @@ -231,9 +244,10 @@ fn alive(current: &Version, proposed: &Proposed) -> bool { /// Whether `proposed`, of `all`, holds back a version that only moves to a checkpoint, /// which would lapse it: it can still pass, and a third of the parties back it, someone -/// but its proposer among them; or it came less than `grace` ago, and its proposer has no -/// older proposal on the version. So two parties keep no proposal alive to hold back -/// checkpoints, and one new is lapsed by none before its proposer is heard, once. +/// but its proposer among them; or it came less than `grace` ago, and its proposer, the +/// party of the first event proposing it, has no older proposal on the version. So two +/// parties keep no proposal alive to hold back checkpoints, and one new is lapsed by none +/// before its proposer is heard, once: another party proposing it again changes nothing. fn holds_back(current: &Version, proposed: &Proposed, all: &[Proposed], grace: Duration) -> bool { let backed = !proposed.yes.is_subset(&proposed.proposers) && 3 * proposed.yes.len() >= current.parties.len(); @@ -241,7 +255,7 @@ fn holds_back(current: &Version, proposed: &Proposed, all: &[Proposed], grace: D && !all.iter().any(|other| { other.hash != proposed.hash && other.held >= grace - && !other.proposers.is_disjoint(&proposed.proposers) + && other.proposers.contains(&proposed.first) }); alive(current, proposed) && (backed || fresh) } @@ -885,6 +899,18 @@ mod tests { }; assert!(decide(&seen(&store, &fresh), &rules, &record).is_empty()); assert!(proposes(decide(&seen(&store, &long), &rules, &record))); + // p4's, proposed again by p1 a moment later: p4's still, and still held back. + let echo = taken(&mut store, parties[0].propose(&[write], &junk(2).json())); + let echoed = |id: &Id| { + Some(if *id == new { + Duration::from_secs(2) + } else if *id == echo { + Duration::from_secs(1) + } else { + Duration::from_secs(3600) + }) + }; + assert!(decide(&seen(&store, &echoed), &rules, &record).is_empty()); // One by p1 again, new, while its old one is past that while: not. let again = taken(&mut store, parties[0].propose(&[write], &junk(3).json())); let fresh_again = |id: &Id| { |
