diff options
| author | tv <tv@krebsco.de> | 2026-11-24 19:07:35 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-24 19:07:35 +0000 |
| commit | 9c491018933833916d5d72e9185671638cb7e99b (patch) | |
| tree | f094784f23973453281512b42fa449ac1c7326a9 /src | |
| parent | fb54044c688e0252b4af54a057151b9c4d4ccf98 (diff) | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/federation/held.rs | 49 | ||||
| -rw-r--r-- | src/federation/vote.rs | 62 |
2 files changed, 103 insertions, 8 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs index 9c87995..02b94d2 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -143,6 +143,11 @@ struct Inner { /// When, by its own clock, the node took each event its store took after its /// checkpoint, and when the store moved to its checkpoint, or started there. taken_at: HashMap<Id, Instant>, + /// Of those, the ones its store held when the node first read it, at its start: taken + /// some time before, which no clock of this run says. + from_start: HashSet<Id>, + /// Whether the store was read once since the node started (`from_start`). + started: bool, at_checkpoint: Option<(Id, Instant)>, /// Its party's record, voting by itself, once read; and why it isn't, said once. record: Option<Record>, @@ -369,6 +374,12 @@ impl Holding { } let taken: HashSet<&Id> = store.taken().iter().collect(); inner.taken_at.retain(|id, _| taken.contains(id)); + // What the store holds as it is first read was taken before this run's clock. + if !inner.started { + inner.started = true; + inner.from_start = store.taken().iter().copied().collect(); + } + inner.from_start.retain(|id| taken.contains(id)); for id in store.taken() { inner.taken_at.entry(*id).or_insert(now); } @@ -712,9 +723,11 @@ impl Holding { } let now = Instant::now(); let held = |id: &Id| inner.taken_at.get(id).map(|at| now.duration_since(*at)); + let from_start = |id: &Id| inner.from_start.contains(id); let seen = Seen { store, held: &held, + from_start: &from_start, since_move: inner .at_checkpoint .map_or(Duration::ZERO, |(_, at)| now.duration_since(at)), @@ -2431,9 +2444,8 @@ mod tests { let proposal = tv.propose(&[Id::of(&write)], &next.json()); let yes = tv.vote(&[Id::of(&proposal)], on, next.hash(), true); let mut doc = events::started(Federation::NAME, &root, &[]).unwrap(); - for event in [&write, &beside, &proposal, &yes] { - put(&mut doc, event); - } + put(&mut doc, &write); + let events = [&beside, &proposal, &yes]; let dir = crate::files::scratch_dir("held-votes-above"); let record = dir.join("makefu.json"); let version = Version::read(&first.json()).unwrap(); @@ -2450,7 +2462,8 @@ mod tests { .voting(Some(Voter { key: makefu.key.clone(), record, - // Lass's write, just taken, not held for a while: it may be left out. + // Lass's write, taken while it runs and not held for a while: it may be left + // out. (Held when the node started, it may not: `Inner::from_start`.) rules: Rules { follow: vec!["tv".to_owned()], held_for: Duration::from_secs(3600), @@ -2458,6 +2471,10 @@ mod tests { }, })); read(&holding, &doc, 0); + for event in events { + put(&mut doc, event); + } + read(&holding, &doc, 0); let cast = holding.act(); assert_eq!(cast.len(), 1); assert!(matches!( @@ -2470,6 +2487,30 @@ mod tests { holding.with(|store| store.state().current().hash), Some(next.hash()) ); + // A node starting with all of it in its document, as after a restart, held lass's + // write when it started, for however long: it backs no checkpoint leaving it out. + let record = dir.join("restarted.json"); + let mut begun = Record::begin(Id::of(&root), "makefu", &version); + begun.began = Id::of(b"before the federation").to_string(); + begun.save(&record).unwrap(); + let restarted = Holding::open( + None, + Arc::new(Files::default()), + None, + Some("makefu".into()), + ) + .unwrap() + .voting(Some(Voter { + key: makefu.key.clone(), + record, + rules: Rules { + follow: vec!["tv".to_owned()], + held_for: Duration::from_secs(3600), + ..Rules::default() + }, + })); + read(&restarted, &doc, 0); + assert_eq!(restarted.act(), Vec::<Vec<u8>>::new()); } /// Rows whose tests wait: remakes (stage 5), and flooding. 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<Federation>, pub(crate) held: &'a dyn Fn(&Id) -> Option<Duration>, + 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<Proposed> { /// 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()); |
