diff options
Diffstat (limited to 'src/federation/vote.rs')
| -rw-r--r-- | src/federation/vote.rs | 62 |
1 files changed, 58 insertions, 4 deletions
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()); |
