diff options
| -rw-r--r-- | docs/document.md | 5 | ||||
| -rw-r--r-- | docs/guides/federation/refusals-and-drops.md | 4 | ||||
| -rw-r--r-- | docs/guides/federation/running-a-node.md | 15 | ||||
| -rw-r--r-- | docs/guides/federation/when-things-go-wrong.md | 4 | ||||
| -rw-r--r-- | src/config/mod.rs | 2 | ||||
| -rw-r--r-- | src/federation/held.rs | 8 | ||||
| -rw-r--r-- | src/federation/store/passing.rs | 42 | ||||
| -rw-r--r-- | src/federation/store/set.rs | 55 | ||||
| -rw-r--r-- | src/simtest/holding.rs | 12 | ||||
| -rw-r--r-- | src/swarm/bootstrap.rs | 38 |
10 files changed, 160 insertions, 25 deletions
diff --git a/docs/document.md b/docs/document.md index 07d3420..641fa92 100644 --- a/docs/document.md +++ b/docs/document.md @@ -754,7 +754,10 @@ in its handshakes, and syncs with nodes that say so and no others. * **Starting**: from the file; without one, from the root, where given and no checkpoint is pinned; else with nothing, asking the members it reaches for the checkpoint named by the store's ID or by `--store-checkpoint`, and for the events - that one keeps, each taken as what its name says. + that one keeps, each taken as what its name says. The file says which checkpoint was + pinned when the set began: with another pinned, and not the one the store is at, the + node begins anew from that one, its store refusing what the one in the file refused + of the node's own and as its operator had it drop. * **The node's own entry** is what `swarm::members` writes of a member, put as the node's next entry; its other writes of protocol data are refused. What reads members, the policy and the swarm's ID of a document reads them of the set diff --git a/docs/guides/federation/refusals-and-drops.md b/docs/guides/federation/refusals-and-drops.md index 40f37de..8111210 100644 --- a/docs/guides/federation/refusals-and-drops.md +++ b/docs/guides/federation/refusals-and-drops.md @@ -61,7 +61,9 @@ events from whoever brings them, and any version event naming a version among th whoever wrote it anew; it writes none itself, though it holds the votes passing it, and keeps so across restarts. Not what its checkpoint keeps or holds by ID: every event after it builds on that. A move to a checkpoint is undone -only by starting from a checkpoint its operator pins (`--store-checkpoint`), anew +only by starting from a checkpoint its operator pins (`--store-checkpoint`): given +another than its set began from, the node begins its set anew from that one, and +refuses on what it refused and dropped ([`running-a-node.md`](running-a-node.md), [Joining](running-a-node.md#joining)). ## Taking back diff --git a/docs/guides/federation/running-a-node.md b/docs/guides/federation/running-a-node.md index 2392578..066b219 100644 --- a/docs/guides/federation/running-a-node.md +++ b/docs/guides/federation/running-a-node.md @@ -28,8 +28,9 @@ nodes of the other don't sync with each other. what its operator had it drop, and the members' entries. Started again, the node goes on from it. One that doesn't read, or is of another store, stops the node from starting. -* `--store-checkpoint ID`: the checkpoint a node joining with nothing begins its set - from, instead of the root, by its ID ([Joining](#joining), below). +* `--store-checkpoint ID`: the checkpoint the node begins its set from, instead of the + root, by its ID: joining with nothing, or anew, where its set began from another + ([Joining](#joining), below). * `--store-keeps-forgotten DURATION` (30 days unless given): for how long the node keeps what a checkpoint its store moved to forgot, for nodes still before that checkpoint. * `--store-neighbour-parties NAME,…`: parties among whose members, with its own party's, @@ -121,14 +122,16 @@ its operator pins by its ID (`--store-checkpoint`): ask a node you trust for it (`checkpoint` in `GET /events`). The node asks the members it reaches for the checkpoint by that ID, and for the events it keeps, and holds no store until it has them all. From the root, it would take what was grown anew from it since, by keys of versions long -replaced. The pin counts only while the node has no file of its set: with one, it goes on -from there. +replaced. A node that has a file of its set goes on from there, as long as the pin is the +one its set began from, or none: its store moves on to later checkpoints by itself. A node that was away across a move to a checkpoint builds that checkpoint itself, from what the others keep of what the checkpoint forgot (`--store-keeps-forgotten`). Away for longer than they keep it, it stays before the checkpoint, and the others refuse what it -writes: start it anew, without its set's file and with the checkpoint pinned. It then -holds none of what it refused of its own or dropped +writes: pin the checkpoint, and start it again. Given another pin than the one its set +began from, and not the checkpoint its store is at, the node begins its set anew from +that one (`Another checkpoint is pinned than the store's document began from`), and +refuses on what it refused of its own and what its operator had it drop ([`refusals-and-drops.md`](refusals-and-drops.md)). ## What it writes by itself diff --git a/docs/guides/federation/when-things-go-wrong.md b/docs/guides/federation/when-things-go-wrong.md index 5bd7f62..1ef6fd3 100644 --- a/docs/guides/federation/when-things-go-wrong.md +++ b/docs/guides/federation/when-things-go-wrong.md @@ -61,8 +61,8 @@ curl -u tv:secret -X POST -H 'Content-Type: application/json' \ The node refuses what it dropped from then on, from whoever brings it, and writes no version naming what it dropped, until it is allowed again. A node drops only what it took after its checkpoint: a version that moved it to a checkpoint it undoes only by -starting from a checkpoint its operator pins (`--store-checkpoint`), anew, without its -set's file ([`running-a-node.md`](running-a-node.md), [Joining](running-a-node.md#joining)). +starting from a checkpoint its operator pins (`--store-checkpoint`), another than its set +began from ([`running-a-node.md`](running-a-node.md), [Joining](running-a-node.md#joining)). The parties answer keys that voted twice by forking, without the party that voted twice (`continuum-party fork remove lass`) or with its keys named anew (`fork keys lass diff --git a/src/config/mod.rs b/src/config/mod.rs index e68b60c..1b9a439 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -244,7 +244,7 @@ pub(crate) const OPTIONS: &[OptionSpec] = &[ Kind::Text, "ID", None, - "With store-root or store-id: a checkpoint of the store, by its ID (hex), from which the node reads its store whenever it starts, instead of from the root: once a store moved to a checkpoint, a node reading from the root could take what was grown anew from it since, by keys of versions long replaced (docs/federation.md, Checkpoints). The node finds it in its document, at heads a proposal names, built from their history as any node builds it; until then, it holds no store.", + "With store-root or store-id: a checkpoint of the store, by its ID (hex), from which the node reads its store whenever it starts, instead of from the root: once a store moved to a checkpoint, a node reading from the root could take what was grown anew from it since, by keys of versions long replaced (docs/federation.md, Checkpoints). The node finds it in its document, at heads a proposal names, built from their history as any node builds it; until then, it holds no store. With store-document: the checkpoint the node begins its set from, asked of the members it reaches by this ID: with nothing of the store, or, with a file whose set began from another, anew, refusing what it refused before.", ), #[cfg(feature = "federation")] option( diff --git a/src/federation/held.rs b/src/federation/held.rs index 39d6102..4846266 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -140,7 +140,7 @@ pub(crate) struct Voter { /// root; or still to be gathered of its peers, where the node was given neither. pub(crate) enum Begun { Set(Box<Set<Federation>>), - Start(super::store::passing::Start<Federation>), + Start(Box<super::store::passing::Start<Federation>>), } /// What carries the store: a document it is read from, or a set of events, which is @@ -397,7 +397,9 @@ impl Holding { let store = store.refusing(&own, &dropped, &writes); // A node of both kinds: the store in a set too, from here on. inner.store = Some(if self.of_a_set { - Carried::Set(Box::new(Set::new(store, self.keeps_forgotten))) + Carried::Set(Box::new( + Set::new(store, self.keeps_forgotten).pinned_at(self.pin), + )) } else { Carried::Store(Box::new(store)) }); @@ -1129,7 +1131,7 @@ impl Holding { ) -> Self { let holding = match begun { Some(Begun::Set(set)) => self.carrying(*set), - Some(Begun::Start(start)) => self.awaiting(start), + Some(Begun::Start(start)) => self.awaiting(*start), None => self, }; match path { 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<F: Fold> { /// The checkpoint named `at`, once it came, and the events it keeps, by name. checkpoint: Option<(Vec<u8>, Vec<Id>)>, kept: BTreeMap<Id, Vec<u8>>, + /// 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<Id>, + dropped: Vec<Id>, + writes: Vec<(Id, Id)>, fold: std::marker::PhantomData<F>, } @@ -93,10 +98,22 @@ impl<F: Fold> Start<F> { 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<F>) -> 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<F: Fold> Start<F> { 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); + assert_eq!(begun.pinned(), None); - // From a checkpoint its operator pins: that, then what it keeps. - let mut start = Start::<TestFold>::new(root, pinned, KEEPS); + // 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::<TestFold>::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::<TestFold>::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<serde_bytes::ByteBuf>, + /// 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<F: Fold> { /// 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<Id>, } impl<F: Fold> Set<F> { @@ -170,9 +177,22 @@ impl<F: Fold> Set<F> { 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<Id>) -> 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<Id> { + 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<F: Fold> Set<F> { .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<F: Fold> Set<F> { .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<ByteBuf>, + Vec<ByteBuf>, + Vec<[u8; 32]>, + Vec<[u8; 32]>, + Vec<([u8; 32], [u8; 32])>, + Vec<(ByteBuf, SavedBeside)>, + Vec<ByteBuf>, + ); + 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::<TestFold>::load(&file, KEEPS, now).expect("it reads"); + assert_eq!(read.pinned(), None); + assert_eq!(read.store().taken(), &[id]); + } } diff --git a/src/simtest/holding.rs b/src/simtest/holding.rs index 8f601dd..5d5cecf 100644 --- a/src/simtest/holding.rs +++ b/src/simtest/holding.rs @@ -2029,11 +2029,11 @@ async fn a_node_of_a_set_joins_with_nothing_but_the_stores_id() { move |config| { config.store = Some(id); config.holding = holding("lass").carrying_if( - Some(held::Begun::Start(passing::Start::new( + Some(held::Begun::Start(Box::new(passing::Start::new( id, id, Duration::from_secs(600), - ))), + )))), None, Arc::new(Files::default()), ); @@ -2086,11 +2086,11 @@ async fn a_node_no_party_lists_gets_nothing_of_a_set() { move |config| { config.store = Some(id); config.holding = holding("lass").carrying_if( - Some(held::Begun::Start(passing::Start::new( + Some(held::Begun::Start(Box::new(passing::Start::new( id, id, Duration::from_secs(600), - ))), + )))), None, Arc::new(Files::default()), ); @@ -2175,11 +2175,11 @@ async fn a_node_of_a_set_away_across_a_move_builds_the_checkpoint_itself() { move |config| { config.store = Some(id); config.holding = holding("makefu").carrying_if( - Some(held::Begun::Start(passing::Start::new( + Some(held::Begun::Start(Box::new(passing::Start::new( id, moved, Duration::from_secs(600), - ))), + )))), None, Arc::new(Files::default()), ); diff --git a/src/swarm/bootstrap.rs b/src/swarm/bootstrap.rs index 45c86e6..30eaf73 100644 --- a/src/swarm/bootstrap.rs +++ b/src/swarm/bootstrap.rs @@ -377,6 +377,27 @@ impl StoreStart { self.id ); } + // Another checkpoint pinned than the one the set began from, and not the + // one its store is at: the node begins anew from that one, keeping what + // it refused of its own and what its operator had it drop. + if let Some(pin) = pin + && set.pinned() != Some(pin) + { + if crate::federation::store::Id::of(set.store().checkpoint()) != pin { + // A node of both kinds finds it in its document, and makes its set + // of the store it reads there. + if both { + return Ok(None); + } + info!(swarm_id = %self.id, from = %pin, path = %path.display(), "Another checkpoint is pinned than the store's document began from: its set begins anew from what members give by that name"); + return Ok(Some(Begun::Start(Box::new( + Start::new(self.id, pin, keeps).refusing_as(&set), + )))); + } + let set = set.pinned_at(Some(pin)); + info!(swarm_id = %self.id, path = %path.display(), "Going on with the store's document"); + return Ok(Some(Begun::Set(Box::new(set)))); + } info!(swarm_id = %self.id, path = %path.display(), "Going on with the store's document"); return Ok(Some(Begun::Set(Box::new(set)))); } @@ -397,7 +418,7 @@ impl StoreStart { (_, pin) => { let at = pin.unwrap_or(self.id); info!(swarm_id = %self.id, from = %at, "Joining the swarm holding the store with nothing of it: its set begins from what members give by that name"); - Ok(Some(Begun::Start(Start::new(self.id, at, keeps)))) + Ok(Some(Begun::Start(Box::new(Start::new(self.id, at, keeps))))) } } } @@ -781,6 +802,21 @@ mod tests { let read = set_of(start, None).expect("from the file"); assert_eq!(read.store().taken(), &[Id::of(&write)]); } + // Another checkpoint pinned than the one the set began from: anew, from that + // one; a node of both kinds makes its set of its document then. The one its + // store is at: gone on with, and kept as pinned. + let another = Some(Id::of(b"a checkpoint")); + assert!(matches!( + store.set(&path, &files, another, keeps, false), + Ok(Some(Begun::Start(_))) + )); + assert!(matches!( + store.set(&path, &files, another, keeps, true), + Ok(None) + )); + let read = set_of(&store, Some(id)).expect("from the file"); + assert_eq!(read.store().taken(), &[Id::of(&write)]); + assert_eq!(read.pinned(), Some(id)); // Of another store, or no document: refused. let other_root = Spec::first(&[&Party::new("lass", 2)], &[]).root(); let other = StoreStart { |
