From d6194cdc78b69914810cfc02bbd31aef41067be4 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 17 Nov 2026 03:15:02 +0000 Subject: holding: a node writes its acks and the compactions it coordinates A change of a swarm holding a store may write its node's own protocol data a remake needs (`docs/store.md`, What a change writes): its acks, a remake's record and proposal, where there is none, of a compaction it coordinates, and, as the coordinator, its decision, or the record of the remake that made the epoch, carried into it whole; and the maps holding them, where there are none. An abort asked for by another than the coordinator stays closed, as does any other kind of remake. Rows 14 and 21 are judged here. --- src/access/holding.rs | 225 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 197 insertions(+), 28 deletions(-) (limited to 'src/access/holding.rs') diff --git a/src/access/holding.rs b/src/access/holding.rs index 4fc41f5..4bef52a 100644 --- a/src/access/holding.rs +++ b/src/access/holding.rs @@ -4,11 +4,16 @@ //! //! * events: at `_continuum/events/` the bytes whose ID (SHA-256) it is, no more than //! the fold allows, at a key held nowhere at the change's history; -//! * its node's entry (`_continuum/members/`), and the member list's map where -//! there is none. +//! * its node's own protocol data: its entry (`_continuum/members/`), its acks +//! (`_continuum/votes/`), a remake's record and proposal, where there is none, +//! of a compaction it coordinates, and, as a remake's coordinator, its decision or its +//! abort, or the record of the remake that made the epoch, carried into it whole; and +//! the maps holding them, where there are none. //! -//! Everything else is closed: whatever isn't under `_continuum`, the policy, the swarm's -//! ID, users, bans, the swarm's topology, remakes and their acks. Nothing deletes or +//! An epoch's root writes what it starts with (`rules::judge_reading`). Everything else +//! is closed: whatever isn't under `_continuum`, the policy, the swarm's ID, users, bans, +//! the swarm's topology, an abort asked for by another than the coordinator. Nothing +//! deletes or //! replaces an event. A change names its node, as signed (signatures are required), but //! for the changes founding the swarm, which every node builds and nobody signs: the //! genesis change, which the epoch's root is, and those of its writer putting the events @@ -20,9 +25,10 @@ use sha2::{Digest, Sha256}; -use super::rules::{Change, Refusal}; +use super::rules::{Change, Refusal, coordinator, made_the_epoch}; use crate::document::at::At; use crate::document::inspect::{Action, Op, Path}; +use crate::document::replica::Epoch; use crate::document::{events, meta, store}; use crate::federation::store::Id; @@ -66,8 +72,14 @@ fn founding(at: &At<'_>, change: &Change<'_>, fold: &str) -> bool { events::is_founding(at, fold, (&root, &keeps), &change.hash, &written) } -/// Judges `change`, of a swarm holding a store of the fold `fold`, at its history `at`. -pub(crate) fn judge(at: &At<'_>, change: &Change<'_>, fold: &str) -> Result<(), Refusal> { +/// Judges `change`, of a swarm holding a store of the fold `fold`, at its history `at`, in +/// the document of `epoch`. +pub(crate) fn judge( + at: &At<'_>, + epoch: Epoch, + change: &Change<'_>, + fold: &str, +) -> Result<(), Refusal> { let refuse = |why: String| Err(Refusal::Rule(why)); let Some(node) = change.committer else { if founding(at, change, fold) { @@ -78,18 +90,18 @@ pub(crate) fn judge(at: &At<'_>, change: &Change<'_>, fold: &str) -> Result<(), let events = meta::path(&[meta::EVENTS]); let members = meta::path(&[meta::MEMBERS]); let entry = meta::path(&[meta::MEMBERS, node]); + let votes = meta::path(&[meta::VOTES]); + let acks = meta::path(&[meta::VOTES, node]); + let remakes = meta::path(&[meta::REMAKES]); for op in change.ops { let allowed = if op.path.starts_with(&events) { event(at, op, fold) - } else if op.path.starts_with(&entry) { + } else if op.path.starts_with(&entry) || op.path.starts_with(&acks) { Ok(()) - } else if op.path == members { - // The member list's map, made where there is none. - if op.action == Action::MakeMap && !store::held_anywhere(at, &members) { - Ok(()) - } else { - Err("it writes the member list".to_owned()) - } + } else if op.path == members || op.path == votes || op.path == remakes { + made(at, op) + } else if op.path.starts_with(&remakes) { + remake(at, epoch, change, node, op) } else { Err(format!("it writes {}, closed", shown(&op.path))) }; @@ -98,6 +110,90 @@ pub(crate) fn judge(at: &At<'_>, change: &Change<'_>, fold: &str) -> Result<(), Ok(()) } +/// Whether `op` makes the map at its path, where there is none at `at`. +fn made(at: &At<'_>, op: &Op) -> Result<(), String> { + if op.action == Action::MakeMap && !store::held_anywhere(at, &op.path) { + Ok(()) + } else { + Err(format!("it writes {}, made already", shown(&op.path))) + } +} + +/// Whether `op`, under `_continuum/remakes`, is one of `node`'s, at `at`, in the document of +/// `epoch`: a record made where there is none, its proposal written where there is none, +/// of a compaction `node` coordinates, and its decision, written by its coordinator; or +/// the record of the remake that made the epoch, carried into it whole by its +/// coordinator, where there is none. +fn remake( + at: &At<'_>, + epoch: Epoch, + change: &Change<'_>, + node: &str, + op: &Op, +) -> Result<(), String> { + let [_, _, id, rest @ ..] = op.path.as_slice() else { + return Err(format!("it writes {}", shown(&op.path))); + }; + let proposal = meta::path(&[meta::REMAKES, id, "proposal"]); + match rest.first().map(String::as_str) { + None => made(at, op), + Some("proposal") => { + if store::held_anywhere(at, &proposal) { + Err(format!( + "it writes the proposal of remake {id}, written already" + )) + } else if !proposes(change, &proposal, node) { + Err(format!( + "it proposes remake {id}, no compaction that {node} coordinates" + )) + } else { + Ok(()) + } + } + Some("decision") => match coordinator(at, id) { + Some(coordinator) if coordinator == node => Ok(()), + Some(coordinator) => Err(format!( + "it decides remake {id}, which is {coordinator}'s to decide" + )), + None if change.paths.contains(&proposal) + && !store::held_anywhere(at, &meta::path(&[meta::REMAKES, id])) + && made_the_epoch(at, epoch, id, node) => + { + Ok(()) + } + None => Err(format!("it decides remake {id}, proposed nowhere")), + }, + Some(_) => Err(format!( + "it writes {}: a remake's abort is its coordinator's alone", + shown(&op.path) + )), + } +} + +/// Whether `change` writes at `proposal` a compaction's proposal that names `node` its +/// coordinator. +fn proposes(change: &Change<'_>, proposal: &Path, node: &str) -> bool { + let at = |key: &str| { + let mut path = proposal.clone(); + path.push(key.to_owned()); + path + }; + let (coordinator, kind) = (at("coordinator"), at("kind")); + let put = |path: &Path| { + change.ops.iter().find_map(|op| match &op.action { + Action::PutString(text) if op.path == *path => Some(text.as_str()), + _ => None, + }) + }; + put(&coordinator) == Some(node) + && put(&kind).is_none_or(|kind| kind == crate::remake::DEFAULT_KIND) + && change + .ops + .iter() + .filter(|op| op.path == kind) + .all(|op| matches!(&op.action, Action::PutString(_))) +} + /// Whether `op`, under `_continuum/events`, puts an event at its ID, held nowhere at `at`, /// no larger than the fold `fold` allows. fn event(at: &At<'_>, op: &Op, fold: &str) -> Result<(), String> { @@ -219,7 +315,7 @@ mod tests { action: Action::PutBytes(event.clone()), }], }; - assert!(judge(&at, &again, "federation").is_err()); + assert!(judge(&at, Epoch::GENESIS, &again, "federation").is_err()); // A string, not bytes: refused. let mut doc = swarm(); assert!( @@ -268,7 +364,7 @@ mod tests { meta::path(&[meta::USERS, "alice"]), meta::path(&[meta::SWARM]), meta::path(&[meta::NEIGHBOURS]), - meta::path(&[meta::VOTES, "node-a"]), + meta::path(&[meta::VOTES, "node-b"]), crate::access::policy::path(), ] { let mut doc = swarm(); @@ -355,19 +451,92 @@ mod tests { ); } - /// A decision or abort written by a node not its coordinator: refused, as a swarm - /// holding a store takes no remake records. + /// `node`'s proposal of remake `id`, of `kind`, naming `coordinator`. + fn propose( + doc: &mut Document, + node: &str, + id: &str, + kind: &str, + coordinator: &str, + ) -> Result<(), String> { + judged(doc, node, |doc| { + store::write_path( + doc, + &meta::path(&[meta::REMAKES, id, "proposal"]), + &json!({"coordinator": coordinator, "kind": kind, "quorum": 1}), + ) + }) + } + + fn decide(doc: &mut Document, node: &str, id: &str) -> Result<(), String> { + judged(doc, node, |doc| { + store::write_path( + doc, + &meta::path(&[meta::REMAKES, id, "decision"]), + &json!({"outcome": "committed"}), + ) + }) + } + + /// A node's acks and a compaction it coordinates, proposed and decided by it: taken. + /// Another's acks, a second proposal of one remake, a proposal of another kind or + /// naming another coordinator: refused. + #[test] + fn a_node_proposes_acks_and_decides_compactions() { + let mut doc = swarm(); + let acks = |node: &str| meta::path(&[meta::VOTES, node]); + assert_eq!( + judged(&mut doc, "node-a", |doc| { + store::write_path(doc, &acks("node-a"), &json!({"remake": "r"})) + }), + Ok(()) + ); + assert!( + judged(&mut doc, "node-a", |doc| { + store::write_path(doc, &acks("node-b"), &json!({"remake": "r"})) + }) + .is_err() + ); + assert_eq!( + propose(&mut doc, "node-a", "r", "compact", "node-a"), + Ok(()) + ); + assert!(propose(&mut doc, "node-a", "r", "compact", "node-a").is_err()); + assert!(propose(&mut doc, "node-a", "s", "replace", "node-a").is_err()); + assert!(propose(&mut doc, "node-a", "s", "compact", "node-b").is_err()); + assert_eq!(decide(&mut doc, "node-a", "r"), Ok(())); + } + + /// A decision or abort written by a node not its coordinator: refused. #[test] fn store_attack_14() { - for part in ["decision", "abortRequested"] { - let mut doc = swarm(); - assert!( - judged(&mut doc, "node-a", |doc| { - store::write_path(doc, &meta::path(&[meta::REMAKES, "r", part]), &json!({})) - }) - .is_err() - ); - } + let mut doc = swarm(); + assert_eq!( + propose(&mut doc, "node-a", "r", "compact", "node-a"), + Ok(()) + ); + assert!(decide(&mut doc, "node-b", "r").is_err()); + assert!(decide(&mut doc, "node-b", "unproposed").is_err()); + assert!( + judged(&mut doc, "node-b", |doc| { + store::write_path( + doc, + &meta::path(&[meta::REMAKES, "r", "abortRequested"]), + &json!({}), + ) + }) + .is_err() + ); + } + + /// A node no longer a member decides a remake, its decision brought by a member: taken, + /// as any decision, as nothing here asks who is a member. + #[test] + fn store_attack_21() { + let mut doc = swarm(); + let gone = "a-node-no-party-lists"; + assert_eq!(propose(&mut doc, gone, "r", "compact", gone), Ok(())); + assert_eq!(decide(&mut doc, gone, "r"), Ok(())); } /// An event larger than the fold allows, put by a change: refused, by the change's own -- cgit v1.3.1