From eadf0952260ba4b21e791226cf5ad39e8f079c86 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 1 Dec 2026 01:58:17 +0000 Subject: federation: a node on a set begins anew from another checkpoint its operator pins A pin counted only while the node had no file of its set, so a node behind for longer than the others keep what a checkpoint forgot, or one undoing a move, had to start without its file, and lost what it refused of its own and what its operator had it drop. The file now says which checkpoint was pinned when the set began: with another pinned, and not the one the store is at, the node gathers its set anew from that one, its store refusing what the one in the file refused. A node of both kinds makes its set of its document then, where it finds the pin. --- src/federation/store/passing.rs | 44 ++++++++++++++++++++++++++++++--- src/federation/store/set.rs | 55 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 5 deletions(-) (limited to 'src/federation/store') diff --git a/src/federation/store/passing.rs b/src/federation/store/passing.rs index 88e66a4..2e69a96 100644 --- a/src/federation/store/passing.rs +++ b/src/federation/store/passing.rs @@ -81,6 +81,11 @@ pub(crate) struct Start { /// The checkpoint named `at`, once it came, and the events it keeps, by name. checkpoint: Option<(Vec, Vec)>, kept: BTreeMap>, + /// What its node refused of its own, what its operator had it drop, and what the + /// dropped write (`Store::refusing`): of a set the node held before. + own: Vec, + dropped: Vec, + writes: Vec<(Id, Id)>, fold: std::marker::PhantomData, } @@ -93,10 +98,22 @@ impl Start { keeps_forgotten, checkpoint: None, kept: BTreeMap::new(), + own: Vec::new(), + dropped: Vec::new(), + writes: Vec::new(), fold: std::marker::PhantomData, } } + /// Itself, its store to refuse what `held`, a set the node held before, refuses of + /// its node's own and as its operator had it drop: a node that begins anew from + /// another checkpoint keeps what it refused. + pub(crate) fn refusing_as(mut self, held: &Set) -> Self { + (self.own, self.dropped) = held.store().own(); + self.writes = held.store().dropped_writes(); + self + } + /// The store it is to hold. pub(crate) fn store(&self) -> Id { self.store @@ -152,7 +169,13 @@ impl Start { Store::pinned(self.store, checkpoint.clone(), kept) }; match store { - Ok(store) => Some(Set::new(store, self.keeps_forgotten)), + Ok(store) => Some( + Set::new( + store.refusing(&self.own, &self.dropped, &self.writes), + self.keeps_forgotten, + ) + .pinned_at((self.at != self.store).then_some(self.at)), + ), Err(_) => { self.checkpoint = None; self.kept.clear(); @@ -571,9 +594,17 @@ mod tests { .take(vec![Unit::Checkpoint(bytes)]) .expect("a set, the root keeping nothing"); assert_eq!(begun.store().id(), root); - - // From a checkpoint its operator pins: that, then what it keeps. - let mut start = Start::::new(root, pinned, KEEPS); + assert_eq!(begun.pinned(), None); + + // From a checkpoint its operator pins: that, then what it keeps; and refusing + // what a set the node held before refused as its operator had it drop. + let mut held = set(); + let dropped = match held.offer(testfold::put(&[root], "c", "1"), now) { + Offered::Taken(ids) => ids[0], + other => panic!("not taken: {other:?}"), + }; + held.drop_taken(&[dropped], now).expect("dropped"); + let mut start = Start::::new(root, pinned, KEEPS).refusing_as(&held); assert_eq!(start.wants(), vec![pinned.0]); // What isn't what its name says isn't taken. assert!( @@ -594,6 +625,11 @@ mod tests { let begun = start.take(kept).expect("a set"); assert_eq!(begun.store().id(), root); assert_eq!(begun.store().checkpoint(), ahead.store().checkpoint()); + assert_eq!(begun.store().own(), (Vec::new(), vec![dropped])); + // Its file says which checkpoint was pinned. + assert_eq!(begun.pinned(), Some(pinned)); + let read = Set::::load(&begun.save(now), KEEPS, now).expect("it reads"); + assert_eq!(read.pinned(), Some(pinned)); // And it goes on as any set: what the other holds after the checkpoint comes. let mut sets = [ahead, begun]; let mut rng = Rng::new(9); diff --git a/src/federation/store/set.rs b/src/federation/store/set.rs index d0dbe18..8c2be6d 100644 --- a/src/federation/store/set.rs +++ b/src/federation/store/set.rs @@ -92,6 +92,10 @@ struct Saved { writes: Vec<([u8; 32], [u8; 32])>, beside: Vec<(serde_bytes::ByteBuf, SavedBeside)>, entries: Vec, + /// The checkpoint the node's operator pinned, which the document began from, if + /// any: none in a file written before it was kept. + #[serde(default)] + pinned: Option<[u8; 32]>, } /// Why a document holds an event beside its store, as its file says it: what a @@ -144,6 +148,9 @@ pub(crate) struct Set { /// Where the store stands: of its checkpoint and its heads, read anew where it /// changed. stands: [u8; 32], + /// The checkpoint the node's operator pinned, which the document began from, if + /// any. + pinned: Option, } impl Set { @@ -170,9 +177,22 @@ impl Set { names, entry_names: BTreeMap::new(), stands, + pinned: None, } } + /// Itself, begun from the checkpoint `pin` that its node's operator pinned, if any. + pub(crate) fn pinned_at(mut self, pin: Option) -> Self { + self.pinned = pin; + self + } + + /// The checkpoint the node's operator pinned, which the document began from, if + /// any: with another pinned, the node begins anew from that one. + pub(crate) fn pinned(&self) -> Option { + self.pinned + } + /// Takes a member's entry, where it is its node's newest: of a higher count than the /// one held, or of the same and a lower name. pub(crate) fn put_entry(&mut self, entry: Entry) -> Put { @@ -532,6 +552,7 @@ impl Set { .values() .map(|entry| entry.encode().into()) .collect(), + pinned: self.pinned.map(|id| id.0), }; let mut out = FILE.to_vec(); rmp_serde::encode::write(&mut out, &saved).expect("a document encodes"); @@ -570,7 +591,7 @@ impl Set { .map(|(id, writes)| (Id(*id), Id(*writes))) .collect(); let store = store.refusing(&ids(&saved.own), &ids(&saved.dropped), &writes); - let mut set = Self::new(store, keeps_forgotten); + let mut set = Self::new(store, keeps_forgotten).pinned_at(saved.pinned.map(Id)); for event in saved.taken { set.store.offer(event.into_vec()); } @@ -1096,4 +1117,36 @@ mod tests { assert_eq!(read.beside(&at), Some(Beside::Own)); assert!(read.names().contains(&at.0)); } + + /// A file written before it said which checkpoint was pinned: read, with none. + #[test] + fn a_file_that_says_no_pinned_checkpoint_reads() { + use serde_bytes::ByteBuf; + type Before = ( + [u8; 32], + ByteBuf, + Vec, + Vec, + Vec<[u8; 32]>, + Vec<[u8; 32]>, + Vec<([u8; 32], [u8; 32])>, + Vec<(ByteBuf, SavedBeside)>, + Vec, + ); + let mut set = set(); + let now = Instant::now(); + let root = set.store().id(); + let id = taken(set.offer(testfold::put(&[root], "a", "1"), now)); + let saved = set.pinned_at(Some(root)).save(now); + let body = saved.strip_prefix(FILE).expect("a file"); + let (a, b, c, d, e, f, g, h, i, pinned): (_, _, _, _, _, _, _, _, _, Option<[u8; 32]>) = + rmp_serde::from_slice(body).expect("as written"); + assert_eq!(pinned, Some(root.0)); + let before: Before = (a, b, c, d, e, f, g, h, i); + let mut file = FILE.to_vec(); + rmp_serde::encode::write(&mut file, &before).expect("it encodes"); + let read = Set::::load(&file, KEEPS, now).expect("it reads"); + assert_eq!(read.pinned(), None); + assert_eq!(read.store().taken(), &[id]); + } } -- cgit v1.3.1