diff options
| author | tv <tv@krebsco.de> | 2026-11-16 20:37:13 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-16 22:03:06 +0100 |
| commit | 7e6c79b2ca23edcbd0400c0d522420db09861c9d (patch) | |
| tree | a6f314d7e6e891f3f850da3dfd0311b25f53e06a /src | |
| parent | 5ded0fd2cf6740d5582cded2e7b33015b4579fbb (diff) | |
node: the party a node of a swarm holding a store is of
A node's group is its operator's, in its configuration (`docs/store.md`, Members):
`--store-party NAME`, which `--store-root` and `--store-id` need, a party's name as the
federation spells one. The node keeps it with its store (`Holding::party`), to name it
in its entry and its handshakes. The options of a node of a swarm holding a store
(`--store-file`, `--store-checkpoint`, `--store-party`) set without either are refused
together.
Diffstat (limited to 'src')
| -rw-r--r-- | src/config/mod.rs | 74 | ||||
| -rw-r--r-- | src/federation/held.rs | 32 | ||||
| -rw-r--r-- | src/main.rs | 1 |
3 files changed, 90 insertions, 17 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs index e2557cd..5dae0c2 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -232,6 +232,14 @@ pub(crate) const OPTIONS: &[OptionSpec] = &[ 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.", ), + #[cfg(feature = "federation")] + option( + "store-party", + Kind::Text, + "NAME", + None, + "With store-root or store-id, which need it: the party this node is of (docs/store.md, Members), named in its entry and its handshakes. Nodes of a swarm holding a store sync with the nodes its parties list, each as its party's, and with no others.", + ), option( "strict-keys", Kind::List, @@ -671,6 +679,8 @@ pub(crate) struct Settings { pub(crate) store_file: Option<PathBuf>, #[cfg(feature = "federation")] pub(crate) store_checkpoint: Option<String>, + #[cfg(feature = "federation")] + pub(crate) store_party: Option<String>, #[serde(default)] pub(crate) strict_keys: Vec<String>, @@ -1141,16 +1151,36 @@ impl Settings { } (Some(_), None) => "store-root", (None, Some(_)) => "store-id", - (None, None) if self.store_file.is_some() => { - bail!( - "store-file keeps what is the node's own of a store: set store-root or store-id" - ) - } - (None, None) if self.store_checkpoint.is_some() => { - bail!("store-checkpoint pins a checkpoint of a store: set store-root or store-id") + (None, None) => { + let of_a_store = [ + ("store-file", self.store_file.is_some()), + ("store-checkpoint", self.store_checkpoint.is_some()), + ("store-party", self.store_party.is_some()), + ]; + let set: Vec<&str> = of_a_store + .iter() + .filter(|(_, set)| *set) + .map(|(name, _)| *name) + .collect(); + if !set.is_empty() { + bail!( + "{} {} of a node of a swarm holding a store: set store-root or store-id", + set.join(", "), + if set.len() == 1 { "is" } else { "are" } + ); + } + return Ok(()); } - (None, None) => return Ok(()), }; + match &self.store_party { + None => bail!( + "{store}: set store-party, the party this node is of: in a swarm holding a store, nodes sync with the nodes its parties list, each as its party's" + ), + Some(party) if !crate::federation::version::is_party(party) => bail!( + "store-party: {party:?} isn't a party's name (1 to 64 of A-Z a-z 0-9 . _ @ -)" + ), + Some(_) => {} + } let first_change = [ ("genesis", self.genesis), ("bootstrap-users", !self.bootstrap_users.is_empty()), @@ -1206,6 +1236,8 @@ impl Settings { ("store-file", self.store_file.is_some()), #[cfg(feature = "federation")] ("store-checkpoint", self.store_checkpoint.is_some()), + #[cfg(feature = "federation")] + ("store-party", self.store_party.is_some()), ("alias", self.alias.is_some()), ("dashboard-url", self.dashboard_url.is_some()), ("debug-api", self.debug_api), @@ -2282,6 +2314,32 @@ mod tests { assert_eq!(names, supervised); } + /// A node of a swarm holding a store: started from the root or joined by the ID, of a + /// party, and nothing of a store without either. + #[cfg(feature = "federation")] + #[test] + fn a_store_node_is_of_a_party() { + let checked = |values: JsonValue| { + let mut values = values; + values["htpasswd"] = json!("u"); + values["node-key-file"] = json!("node.key"); + resolve(layer(values), Vec::new()).map(|_| ()) + }; + let id = "ab".repeat(32); + assert!(checked(json!({ "store-id": id, "store-party": "tv" })).is_ok()); + assert!(checked(json!({ "store-root": "root.json", "store-party": "tv" })).is_ok()); + assert!(checked(json!({ "store-id": id })).is_err()); + assert!(checked(json!({ "store-id": id, "store-party": "t v" })).is_err()); + assert!(checked(json!({ "store-id": "ab", "store-party": "tv" })).is_err()); + assert!(checked(json!({ "store-party": "tv" })).is_err()); + assert!(checked(json!({ "store-file": "store.json" })).is_err()); + assert!( + checked(json!({ "store-id": id, "store-root": "root.json", "store-party": "tv" })) + .is_err() + ); + assert!(checked(json!({ "store-id": id, "store-party": "tv", "genesis": true })).is_err()); + } + #[test] fn checks() { assert!(resolve(layer(json!({})), Vec::new()).is_err()); // htpasswd is required diff --git a/src/federation/held.rs b/src/federation/held.rs index 8f48c65..f8825e5 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -77,12 +77,13 @@ impl Kept { } } -/// The store a node holds, if its swarm holds one, the file it keeps its own in, and the -/// checkpoint its operator pins. +/// The store a node holds, if its swarm holds one, the file it keeps its own in, the +/// checkpoint its operator pins, and the party its operator says it is of. #[derive(Default)] pub(crate) struct Holding { file: Option<(PathBuf, Arc<Files>)>, pin: Option<Id>, + party: Option<String>, // A std mutex: never held while waiting for the document. inner: Mutex<Inner>, } @@ -115,11 +116,18 @@ pub(crate) struct Unread { impl Holding { /// Keeping the node's own in the file at `path`, if any, which holds what it kept /// before: a missing file nothing. One that doesn't read is an error: the node would - /// take what it refused. Its store starts from the checkpoint `pin`, if any. - pub(crate) fn open(path: Option<PathBuf>, files: Arc<Files>, pin: Option<Id>) -> Result<Self> { + /// take what it refused. Its store starts from the checkpoint `pin`, if any; the node + /// is of `party`. + pub(crate) fn open( + path: Option<PathBuf>, + files: Arc<Files>, + pin: Option<Id>, + party: Option<String>, + ) -> Result<Self> { let Some(path) = path else { return Ok(Holding { pin, + party, ..Holding::default() }); }; @@ -139,6 +147,7 @@ impl Holding { Ok(Holding { file: Some((path, files)), pin, + party, inner: Mutex::new(Inner { kept, ..Inner::default() @@ -307,6 +316,11 @@ impl Holding { .unwrap_or_default() } + /// The party its operator says the node is of (`--store-party`). + pub(crate) fn party(&self) -> Option<&str> { + self.party.as_deref() + } + /// `f` of the store, where the node holds one. pub(crate) fn with<T>(&self, f: impl FnOnce(&Store<Federation>) -> T) -> Option<T> { self.inner.lock_or_recover().store.as_ref().map(f) @@ -521,7 +535,7 @@ mod tests { assert_ne!(pin, Id::of(&root)); let files = Arc::new(Files::default()); - let pinned = Holding::open(None, Arc::clone(&files), Some(pin)).unwrap(); + let pinned = Holding::open(None, Arc::clone(&files), Some(pin), None).unwrap(); read(&pinned, &doc, 0); assert_eq!(pinned.with(|store| Id::of(store.checkpoint())), Some(pin)); let taken = |holding: &Holding| { @@ -534,7 +548,7 @@ mod tests { pinned.with(|store| store.holds(&Id::of(&beside))), Some(false) ); - let elsewhere = Holding::open(None, files, Some(Id::of(b"elsewhere"))).unwrap(); + let elsewhere = Holding::open(None, files, Some(Id::of(b"elsewhere")), None).unwrap(); read(&elsewhere, &doc, 0); assert!(elsewhere.with(|_| ()).is_none()); } @@ -638,7 +652,7 @@ mod tests { let dir = crate::files::scratch_dir("held"); let path = dir.join("store.json"); let files = Arc::new(Files::default()); - let holding = Holding::open(Some(path.clone()), Arc::clone(&files), None).unwrap(); + let holding = Holding::open(Some(path.clone()), Arc::clone(&files), None, None).unwrap(); let mut doc = events::started(Federation::NAME, &root).unwrap(); for event in before.iter().copied().chain([&first_taken]) { put(&mut doc, event); @@ -650,12 +664,12 @@ mod tests { assert!(!taken(&holding).contains(&Id::of(&later))); // Restarted: the store read anew, all at once, refuses the later as before. - let anew = Holding::open(Some(path.clone()), Arc::clone(&files), None).unwrap(); + let anew = Holding::open(Some(path.clone()), Arc::clone(&files), None, None).unwrap(); read(&anew, &all, 0); assert!(taken(&anew).contains(&Id::of(&first_taken))); assert!(!taken(&anew).contains(&Id::of(&later))); std::fs::write(&path, b"{\"refused\": [\"not an ID\"]}").unwrap(); - assert!(Holding::open(Some(path), files, None).is_err()); + assert!(Holding::open(Some(path), files, None, None).is_err()); } } diff --git a/src/main.rs b/src/main.rs index 16afb7b..91b1bc8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -318,6 +318,7 @@ async fn main() -> Result<()> { args.store_checkpoint .as_deref() .and_then(federation::store::Id::from_hex), + args.store_party.clone(), )?, strict_keys: args .strict_keys |
