aboutsummaryrefslogtreecommitdiffstats
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
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).
-rw-r--r--src/federation/fold.rs36
-rw-r--r--src/federation/held.rs88
2 files changed, 119 insertions, 5 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.
diff --git a/src/federation/held.rs b/src/federation/held.rs
index ae2264b..6e075aa 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -546,6 +546,34 @@ impl Holding {
Ok(signed)
}
+ /// The version events writing what passed on the current version, as its store reads
+ /// now, offered to it: those it takes, to put into the document. A node writes a version
+ /// as soon as it holds it passed (`docs/federation.md`, Changing the policy).
+ pub(crate) fn passed(&self) -> Vec<Vec<u8>> {
+ let mut inner = self.inner.lock_or_recover();
+ let inner = &mut *inner;
+ let Some(store) = inner.store.as_mut() else {
+ return Vec::new();
+ };
+ let state = store.state();
+ let writing: Vec<Vec<u8>> = state
+ .ballots(&state.current().hash)
+ .keys()
+ .filter_map(|proposal| state.version_event(proposal))
+ .collect();
+ let mut written = Vec::new();
+ for bytes in writing {
+ if let Offered::Taken(_) = store.offer(bytes.clone()) {
+ info!(event = %Id::of(&bytes), "Writing a version that passed");
+ written.push(bytes);
+ }
+ }
+ if !written.is_empty() {
+ self.settle(inner);
+ }
+ written
+ }
+
/// What it signs now, voting by itself by its operator's rules: to put into the
/// document.
pub(crate) fn act(&self) -> Vec<Vec<u8>> {
@@ -629,8 +657,8 @@ fn found(pin: &Id, root: &[u8], events: &BTreeMap<Id, Vec<u8>>) -> Option<Built>
}
/// Reads the store from the document whenever it changes, and from the changes the node
-/// refused now and then; puts again what the document lacks, a random wait after it
-/// first lacked it.
+/// refused now and then; writes what passed and what it signs voting by itself; puts again
+/// what the document lacks, a random wait after it first lacked it.
pub(crate) async fn run_worker(state: crate::state::AppState) {
let mut changed = state.change_tx.subscribe();
let mut due: Option<tokio::time::Instant> = None;
@@ -663,20 +691,27 @@ pub(crate) async fn run_worker(state: crate::state::AppState) {
holding.read(unread);
}
let signed = holding.act();
+ let passed = holding.passed();
(
!holding.lacking(&doc.blocking_read().doc).is_empty(),
signed,
+ passed,
)
});
- let (lacking, signed) = read.await.unwrap_or_else(|error| {
+ let (lacking, signed, passed) = read.await.unwrap_or_else(|error| {
error!(error = %error, "Reading the store from the document failed");
- (false, Vec::new())
+ (false, Vec::new(), Vec::new())
});
for bytes in signed {
if let Err(error) = write(&state, None, bytes).await {
warn!(error = %error, "Couldn't put what it signed for its party: put again later");
}
}
+ for bytes in passed {
+ if let Err(error) = write(&state, None, bytes).await {
+ warn!(error = %error, "Couldn't write a version that passed: put again later");
+ }
+ }
let member = state
.holding
.member(state.local_id(), state.holding.party());
@@ -1166,6 +1201,51 @@ mod tests {
assert!(events::ids(&doc).contains(&Id::of(&beside)));
}
+ /// Two proposals on the first version passing at once, tv backing both: a node writes
+ /// the one it takes, and refuses the other, of its own.
+ #[test]
+ fn a_node_writes_what_passed() {
+ let (tv, lass, makefu) = (
+ Party::new("tv", 1),
+ Party::new("lass", 2),
+ Party::new("makefu", 3),
+ );
+ let first = Spec::first(&[&tv, &lass, &makefu], &[]);
+ let root = first.root();
+ let on = first.hash();
+ let one = first.after(on);
+ let mut two = first.after(on);
+ two.rules.push(("names/x".to_owned(), harness::str("tv")));
+ let p1 = tv.propose(&[Id::of(&root)], &one.json());
+ let p2 = lass.propose(&[Id::of(&root)], &two.json());
+ let mut doc = events::started(Federation::NAME, &root).unwrap();
+ for event in [
+ &p1,
+ &p2,
+ &tv.vote(&[Id::of(&p1)], on, one.hash(), true),
+ &lass.vote(&[Id::of(&p1)], on, one.hash(), true),
+ &tv.vote(&[Id::of(&p2)], on, two.hash(), true),
+ &makefu.vote(&[Id::of(&p2)], on, two.hash(), true),
+ ] {
+ put(&mut doc, event);
+ }
+ let holding = Holding::default();
+ assert_eq!(holding.passed(), Vec::<Vec<u8>>::new());
+ read(&holding, &doc, 0);
+ let written = holding.passed();
+ assert_eq!(written.len(), 1);
+ let current = holding.with(|store| store.state().current().hash).unwrap();
+ assert!(current == one.hash() || current == two.hash());
+ let other = if current == one.hash() { &two } else { &one };
+ let refused = holding.with(|store| store.own().0).unwrap();
+ assert_eq!(refused.len(), 1);
+ assert_eq!(
+ holding.with(|store| store.state().version_event(&other.hash())),
+ Some(None)
+ );
+ assert_eq!(holding.passed(), Vec::<Vec<u8>>::new());
+ }
+
/// Rows the next stages test.
const LATER: &[u32] = &[21, 22];