From 9c491018933833916d5d72e9185671638cb7e99b Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 24 Nov 2026 19:07:35 +0000 Subject: federation: what a node held when it started, it held before any proposal A node keeps when it took each event in memory, by its own clock: after a start, everything its document held was stamped at once, a checkpoint's proposal with the writes it leaves out. The voters' rule asks what was held for a while before the proposal, which was nothing then, and once --store-back-after had passed the node backed it, whatever its heads left out. A followed key left such a proposal pending and gathered its followers' votes as they restarted, as all do to roll out a revision. Now what the store holds as the node first reads it counts as held before any proposal (Inner::from_start, Seen::from_start): the node backs no checkpoint leaving any of it out. One proposed just before a restart at heads that left out the writes then under way isn't backed by that node either; the next one is. a_node_votes_above_the_heads_of_the_checkpoint_it_votes_on read write, proposal and vote into a fresh node at once, which is this case: its node runs before they come now, and a second one, started on all of it, casts nothing. --- src/federation/vote.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 4 deletions(-) (limited to 'src/federation/vote.rs') diff --git a/src/federation/vote.rs b/src/federation/vote.rs index 530b339..29ceb10 100644 --- a/src/federation/vote.rs +++ b/src/federation/vote.rs @@ -62,11 +62,13 @@ pub(crate) struct Rules { pub(crate) back_after: Duration, } -/// What the node sees: its store, how long it has held each event it took, and how long -/// since its store moved to its checkpoint. +/// What the node sees: its store, how long it has held each event it took, which of them +/// it held when it started (for how long before, it can't tell: its clock began then), and +/// how long since its store moved to its checkpoint. pub(crate) struct Seen<'a> { pub(crate) store: &'a Store, pub(crate) held: &'a dyn Fn(&Id) -> Option, + pub(crate) from_start: &'a dyn Fn(&Id) -> bool, pub(crate) since_move: Duration, } @@ -156,7 +158,8 @@ fn proposed(seen: &Seen<'_>, current: &Version) -> Vec { /// Whether the node backs `version`, proposed `proposed_held` ago, by the voters' rule: one /// naming a checkpoint only where it changes nothing of `current` but its number, what it /// changes and its checkpoint, and its heads leave out nothing the node held for a while -/// before it was proposed, proposals and votes aside, which lapse with the move, and are +/// before it was proposed, or when it started, proposals and votes aside, which lapse +/// with the move, and are /// heads a version may name (`State::may_name`); and only once its store held its /// checkpoint, or ran, for a while (`Rules::back_after`). pub(crate) fn backs_checkpoint( @@ -203,7 +206,11 @@ pub(crate) fn backs_checkpoint( .store .taken() .iter() - .filter(|id| (seen.held)(id).is_some_and(|held| held >= before) && !lapsing(id)) + .filter(|id| { + // Held when it started: before the proposal, whenever that came. + ((seen.from_start)(id) || (seen.held)(id).is_some_and(|held| held >= before)) + && !lapsing(id) + }) .copied() .collect(); let left_out = seen.store.left_out(heads, &held); @@ -524,6 +531,7 @@ mod tests { Seen { store, held, + from_start: &|_| false, since_move: Duration::ZERO, } } @@ -558,6 +566,7 @@ mod tests { let seen = Seen { store: &store, held: &long, + from_start: &|_| false, since_move: Duration::from_secs(86400), }; let current = store.state().current().clone(); @@ -593,6 +602,7 @@ mod tests { let seen = Seen { store: &store, held: &long, + from_start: &|_| false, since_move: Duration::from_secs(86400), }; let current = store.state().current().clone(); @@ -617,6 +627,49 @@ mod tests { )); } + /// What a node held when it started, it held before any proposal: its clock began at + /// its start, so a write and a checkpoint's proposal both read then look as old as each + /// other, and without this nothing was held for a while before the proposal. + #[test] + fn a_node_backs_no_checkpoint_leaving_out_what_it_held_when_it_started() { + let (tv, lass, _, first, mut store) = three(); + let root = store.id(); + let head = taken( + &mut store, + tv.write(&[root], "home/tv/a", harness::str("1")), + ); + let write = taken( + &mut store, + lass.write(&[root], "home/lass/b", harness::str("2")), + ); + let mut checkpoint = first.after(first.hash()); + checkpoint.checkpoint = Some(vec![head]); + let version = Version::read(&checkpoint.json()).unwrap(); + let rules = Rules { + held_for: Duration::from_secs(60), + ..Rules::default() + }; + // Everything read at the start, an hour ago: the proposal as long as the write. + let hour = Duration::from_secs(3600); + let since_start = |_: &Id| Some(hour); + let current = store.state().current().clone(); + let started = Seen { + store: &store, + held: &since_start, + from_start: &|_| true, + since_move: hour, + }; + let why = backs_checkpoint(&started, &rules, ¤t, &version, hour) + .expect_err("it leaves out a write held at the start"); + assert!(why.contains(&write.to_string()), "{why}"); + // Taken since the start, as late as the proposal: not held for a while before it. + let running = Seen { + from_start: &|_| false, + ..started + }; + assert!(backs_checkpoint(&running, &rules, ¤t, &version, hour).is_ok()); + } + #[test] fn a_node_follows_a_party_on_checkpoints() { let (tv, lass, _, first, mut store) = three(); @@ -878,6 +931,7 @@ mod tests { let soon = Seen { store: &store, held: &long, + from_start: &|_| false, since_move: Duration::from_secs(60), }; assert!(decide(&soon, &rules, &record).is_empty()); -- cgit v1.3.1