From 10f44bab6be521d7cebf23901b0502dd55246f12 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 16 Nov 2026 23:32:29 +0000 Subject: federation: a node writes a version as soon as it holds it passed Once a proposal passed on the current version, as the node's store reads, the node writes it by a version event (`State::version_event`): on the `true` votes of the first parties by name that pass it, each party's of the lowest ID, so that nodes holding those votes write the same bytes, one event. It offers the event to its store first and puts it only where it is taken: of two passing at once, it writes the one it takes, and refuses the other as its own (`Holding::passed`, run by the store's worker). --- src/federation/fold.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'src/federation/fold.rs') diff --git a/src/federation/fold.rs b/src/federation/fold.rs index 6ffdc5a..78f5c39 100644 --- a/src/federation/fold.rs +++ b/src/federation/fold.rs @@ -328,6 +328,34 @@ impl State { ballots } + /// The version event writing the proposal of hash `proposal` on the current version, + /// where it passed here: on the `true` votes of the first parties by name that pass it, + /// each party's of the lowest ID, so that nodes holding those votes write the same. + pub(crate) fn version_event(&self, proposal: &Id) -> Option> { + let current = self.current(); + let mut yes: BTreeMap<&str, Id> = BTreeMap::new(); + for (id, signed, _) in self.signed() { + if let Does::Vote { + on, + proposal: voted, + yes: true, + } = &signed.does + && *on == current.hash + && voted == proposal + { + let lowest = yes.entry(signed.party.as_str()).or_insert(*id); + *lowest = (*lowest).min(*id); + } + } + let needed = (2 * current.parties.len()).div_ceil(3); + if yes.len() < needed { + return None; + } + let mut deps: Vec = yes.into_values().take(needed).collect(); + deps.sort_unstable(); + Some(event::version_event(&deps, *proposal)) + } + /// Whether a proposal of hash `proposal` is proposed on `on` here. fn proposed_on(&self, proposal: &Id, on: &Id) -> bool { self.signed().any(|(_, signed, under)| { @@ -851,6 +879,7 @@ mod tests { let (tv, lass, makefu) = (tv(), lass(), makefu()); let (mut store, first, next, proposal) = proposed(); let yes = taken(store.offer(tv.vote(&[proposal], first.hash(), next.hash(), true))); + assert_eq!(store.state().version_event(&next.hash()), None); assert!(judged( store.offer(harness::version_event(&[yes], next.hash())) )); @@ -865,8 +894,13 @@ mod tests { "names/r/x", harness::str("1") )))); - let written = taken(store.offer(harness::version_event(&[yes, also], next.hash()))); + let written = store.state().version_event(&next.hash()).expect("passed"); + let mut deps = [yes, also]; + deps.sort_unstable(); + assert_eq!(written, harness::version_event(&deps, next.hash())); + let written = taken(store.offer(written)); assert_eq!(store.state().current().number, 1); + assert_eq!(store.state().version_event(&next.hash()), None); taken(store.offer(tv.write(&[written], "names/r/x", harness::str("1")))); assert_eq!(store.state().value("names/r/x"), Some(&harness::str("1"))); // A vote on the version that was current lapses with it. -- cgit v1.3.1