diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/access/holding.rs | 225 | ||||
| -rw-r--r-- | src/access/rules.rs | 6 | ||||
| -rw-r--r-- | src/federation/held.rs | 2 | ||||
| -rw-r--r-- | src/remake/mod.rs | 2 |
4 files changed, 203 insertions, 32 deletions
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/<ID>` 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/<node ID>`), and the member list's map where -//! there is none. +//! * its node's own protocol data: its entry (`_continuum/members/<node ID>`), its acks +//! (`_continuum/votes/<node ID>`), 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 diff --git a/src/access/rules.rs b/src/access/rules.rs index d1edfd5..9597774 100644 --- a/src/access/rules.rs +++ b/src/access/rules.rs @@ -171,7 +171,7 @@ pub(crate) fn judge_reading( // its genesis change, or not at all. #[cfg(feature = "federation")] match policy { - Policy::Store(fold) => return super::holding::judge(&at, change, fold), + Policy::Store(fold) => return super::holding::judge(&at, epoch, change, fold), _ if super::holding::turns_store(change.ops) => { return Err(Refusal::Rule( "a swarm holds a store from its genesis change only".to_owned(), @@ -565,7 +565,7 @@ fn shown(path: &Path) -> String { /// Whether remake `id`, coordinated by `node`, made `epoch`: as its snapshot's metadata /// says (`remake::snapshot`), at `at`. Never genesis. -fn made_the_epoch(at: &At<'_>, epoch: Epoch, id: &str, node: &str) -> bool { +pub(super) fn made_the_epoch(at: &At<'_>, epoch: Epoch, id: &str, node: &str) -> bool { if epoch.generation == 0 { return false; } @@ -584,7 +584,7 @@ fn made_the_epoch(at: &At<'_>, epoch: Epoch, id: &str, node: &str) -> bool { } /// The coordinator remake `id`'s proposal names, at `at`. -fn coordinator(at: &At<'_>, id: &str) -> Option<String> { +pub(super) fn coordinator(at: &At<'_>, id: &str) -> Option<String> { let proposal = store::read_path(at, &meta::path(&[meta::REMAKES, id, "proposal"])) .ok() .flatten()?; diff --git a/src/federation/held.rs b/src/federation/held.rs index dd469c1..5595a08 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -1372,7 +1372,7 @@ mod tests { } /// Rows whose tests wait: remakes (stage 5), and flooding. - const LATER: &[u32] = &[11, 12, 13, 15, 16, 21, 22, 28, 29, 30, 31]; + const LATER: &[u32] = &[11, 12, 13, 15, 16, 22, 28, 29, 30, 31]; #[test] fn every_row_has_its_test() { diff --git a/src/remake/mod.rs b/src/remake/mod.rs index 33ac748..9891760 100644 --- a/src/remake/mod.rs +++ b/src/remake/mod.rs @@ -57,6 +57,8 @@ pub(crate) fn quarantine_taken<H: Host>( quarantine::add(host, epoch, epoch, document, quarantined) } pub(crate) use delta::Superseded; +#[cfg(feature = "federation")] +pub(crate) use kind::DEFAULT_KIND; pub(crate) use kind::remake_genesis; pub(crate) use local::RemakeState; pub(crate) use switch::run_worker; |
