aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/fold.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-16 23:32:29 +0000
committertv <tv@krebsco.de>2026-11-17 01:02:32 +0100
commit10f44bab6be521d7cebf23901b0502dd55246f12 (patch)
tree11d2136b6fadc2458eb90b20dc0d2c1588b2175a /src/federation/fold.rs
parent9c4ae741ae4303661c05e11b42bf5687bd277c05 (diff)
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).
Diffstat (limited to 'src/federation/fold.rs')
-rw-r--r--src/federation/fold.rs36
1 files changed, 35 insertions, 1 deletions
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<Vec<u8>> {
+ 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<Id> = 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.