aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/held.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/held.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/held.rs')
-rw-r--r--src/federation/held.rs88
1 files changed, 84 insertions, 4 deletions
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];