diff options
Diffstat (limited to 'src/access/rules.rs')
| -rw-r--r-- | src/access/rules.rs | 58 |
1 files changed, 39 insertions, 19 deletions
diff --git a/src/access/rules.rs b/src/access/rules.rs index 520ff0f..93cef63 100644 --- a/src/access/rules.rs +++ b/src/access/rules.rs @@ -44,7 +44,8 @@ use crate::document::at::At; use crate::document::inspect::{Action, Op, Path}; use crate::document::meta; use crate::document::replica::Epoch; -use crate::document::{ChangeHash, Document, ReadDoc, store}; +use crate::document::view::{self, Read}; +use crate::document::{ChangeHash, Document, ReadDoc}; /// A change to judge, as its bytes say. #[derive(Debug, Clone)] @@ -195,18 +196,37 @@ pub(crate) fn judge_reading( }; } let at = At::new(doc, change.seen).ok_or(Refusal::Unready)?; - judge_seen(&at, epoch, &change.unit(), reads.at(change.seen)).map_err(Refusal::Rule) + let unit = change.unit(); + // A swarm holding a store is judged by what a change writes of an Automerge document + // (`access::holding`). + let holding = |fold: &str| { + #[cfg(feature = "federation")] + { + super::holding::judge(&at, epoch, &unit, fold) + } + #[cfg(not(feature = "federation"))] + { + let _ = fold; + Ok(()) + } + }; + judge_seen(&at, epoch, &unit, reads.at(change.seen), holding).map_err(Refusal::Rule) } /// Judges `unit` by the rules, reading `at`, the document (of `epoch`) as its writer saw /// it, with what was read of it before (`read`): why they refuse it, where they do. Not /// an epoch's root, which writes what its document starts with and is judged by none. +/// In a swarm holding a store of some fold, as `holding` judges it there, which is its +/// kind of document's to say. pub(crate) fn judge_seen( - at: &impl ReadDoc, + at: &impl Read, epoch: Epoch, unit: &Unit<'_>, read: &mut Seen, + holding: impl FnOnce(&str) -> Result<(), String>, ) -> Result<(), String> { + #[cfg(not(feature = "federation"))] + let _ = &holding; // Written while the document had no policy (or one this node doesn't know): judged // as it was then, by none. History from before the policy stays as it is. let policy = read @@ -223,7 +243,7 @@ pub(crate) fn judge_seen( // its genesis change, or not at all. #[cfg(feature = "federation")] match policy { - Policy::Store(fold) => return super::holding::judge(at, epoch, unit, fold), + Policy::Store(fold) => return holding(fold), _ if super::holding::turns_store(unit.ops) => { return Err("a swarm holds a store from its genesis change only".to_owned()); } @@ -247,7 +267,7 @@ pub(crate) fn judge_seen( let member = *read .member .entry(node.to_owned()) - .or_insert_with(|| store::read_path(at, &own_entry).ok().flatten().is_some()); + .or_insert_with(|| view::read_path(at, &own_entry).ok().flatten().is_some()); let registering = unit .paths .iter() @@ -287,7 +307,7 @@ pub(crate) fn judge_seen( .paths .iter() .any(|other| other.len() > path.len() && other.starts_with(path)) - && store::read_path(at, path).ok().flatten().is_none() + && view::read_path(at, path).ok().flatten().is_none() }) .collect(); // Whether the change only makes a map at `path`, where there is none. @@ -296,7 +316,7 @@ pub(crate) fn judge_seen( .iter() .filter(|op| op.path == *path) .all(|op| op.action == Action::MakeMap) - && store::read_path(at, path).ok().flatten().is_none() + && view::read_path(at, path).ok().flatten().is_none() }; for path in unit.paths { if on_the_way.contains(path) { @@ -371,7 +391,7 @@ pub(crate) fn judge_seen( unit.paths .iter() .any(|other| other == &meta::path(&[meta::REMAKES, id, "proposal"])) - && store::read_path(at, &meta::path(&[meta::REMAKES, id])) + && view::read_path(at, &meta::path(&[meta::REMAKES, id])) .ok() .flatten() .is_none() @@ -383,7 +403,7 @@ pub(crate) fn judge_seen( // one who may write them. None => made(path) || user_may(path), Some("proposal") => { - store::read_path(at, &meta::path(&[meta::REMAKES, id, "proposal"])) + view::read_path(at, &meta::path(&[meta::REMAKES, id, "proposal"])) .ok() .flatten() .is_none() @@ -482,7 +502,7 @@ pub(crate) fn judge_held( /// string put there that is one (or deleting what is there); nothing within one; nothing /// in an object nowhere to be found, which might be one. fn sealed_values( - at: &impl ReadDoc, + at: &impl Read, declared: &[super::sealed::Declared], ops: &[Op], ) -> Result<(), String> { @@ -514,7 +534,7 @@ fn sealed_values( /// as a write replacing it does); nothing within one; above one, /// nothing replacing or deleting what holds one (a map made where there is none yet, /// what is inside a list or text, which holds none). -fn signed_values(at: &impl ReadDoc, ops: &[Op]) -> Result<(), String> { +fn signed_values(at: &impl Read, ops: &[Op]) -> Result<(), String> { let signed = super::envelope::signed_paths(at); for op in ops { let path = &op.path; @@ -533,7 +553,7 @@ fn signed_values(at: &impl ReadDoc, ops: &[Op]) -> Result<(), String> { Action::Delete if replaced() => continue, _ => return Err(format!("{} holds a value its user signs", shown(path))), }; - let previous = match store::read_path(at, path) { + let previous = match view::read_path(at, path) { Ok(Some(serde_json::Value::String(text))) if super::envelope::read(&text).is_ok() => { @@ -569,7 +589,7 @@ fn signed_values(at: &impl ReadDoc, ops: &[Op]) -> Result<(), String> { /// Whether what is at `path` in `at` holds a value at a place for one its user signs, /// of `signed` (`envelope::signed_paths`), signed or from before the policy; one that /// doesn't read, as if it did. -fn holds_signed(at: &impl ReadDoc, path: &[String], signed: &[Vec<String>]) -> bool { +fn holds_signed(at: &impl Read, path: &[String], signed: &[Vec<String>]) -> bool { fn within(value: &serde_json::Value, place: &mut Vec<String>, signed: &[Vec<String>]) -> bool { if super::envelope::is_signed(signed, place) { return true; @@ -586,7 +606,7 @@ fn holds_signed(at: &impl ReadDoc, path: &[String], signed: &[Vec<String>]) -> b _ => false, } } - match store::read_path(at, path) { + match view::read_path(at, path) { Ok(Some(value)) => within(&value, &mut path.to_vec(), signed), Ok(None) => false, Err(_) => true, @@ -608,12 +628,12 @@ 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. -pub(super) fn made_the_epoch(at: &impl ReadDoc, epoch: Epoch, id: &str, node: &str) -> bool { +pub(super) fn made_the_epoch(at: &impl Read, epoch: Epoch, id: &str, node: &str) -> bool { if epoch.generation == 0 { return false; } let generation = epoch.generation.to_string(); - let Some(metadata) = store::read_path(at, &meta::path(&[meta::EPOCHS, &generation])) + let Some(metadata) = view::read_path(at, &meta::path(&[meta::EPOCHS, &generation])) .ok() .flatten() else { @@ -627,8 +647,8 @@ pub(super) fn made_the_epoch(at: &impl ReadDoc, epoch: Epoch, id: &str, node: &s } /// The coordinator remake `id`'s proposal names, at `at`. -pub(super) fn coordinator(at: &impl ReadDoc, id: &str) -> Option<String> { - let proposal = store::read_path(at, &meta::path(&[meta::REMAKES, id, "proposal"])) +pub(super) fn coordinator(at: &impl Read, id: &str) -> Option<String> { + let proposal = view::read_path(at, &meta::path(&[meta::REMAKES, id, "proposal"])) .ok() .flatten()?; proposal @@ -659,7 +679,7 @@ mod tests { }; let at = path(at); transact(doc, UnixMillis(1), &writer, |doc| { - store::write_path(doc, &at, &value) + view::write_path(doc, &at, &value) }) .unwrap(); } |
