diff options
Diffstat (limited to 'src/access/holding.rs')
| -rw-r--r-- | src/access/holding.rs | 58 |
1 files changed, 31 insertions, 27 deletions
diff --git a/src/access/holding.rs b/src/access/holding.rs index 798ba46..c68ca92 100644 --- a/src/access/holding.rs +++ b/src/access/holding.rs @@ -25,8 +25,8 @@ use sha2::{Digest, Sha256}; -use super::rules::{Change, Refusal, coordinator, made_the_epoch}; -use crate::document::at::At; +use super::rules::{Unit, coordinator, made_the_epoch}; +use crate::document::ReadDoc; use crate::document::inspect::{Action, Op, Path}; use crate::document::replica::Epoch; use crate::document::{events, meta, store}; @@ -54,14 +54,14 @@ fn keeps(fold: &str, root: &[u8]) -> Option<Vec<Id>> { /// Whether `change`, naming no node, is one of those founding the swarm, which every node /// builds from the store's root and the events it keeps (`events::is_founding`). -fn founding(at: &At<'_>, change: &Change<'_>, fold: &str) -> bool { +fn founding(at: &impl ReadDoc, unit: &Unit<'_>, fold: &str) -> bool { let Some(root) = events::swarm(at).and_then(|swarm| events::bytes(at, &swarm)) else { return false; }; let Some(keeps) = keeps(fold, &root) else { return false; }; - let written: Vec<Vec<u8>> = change + let written: Vec<Vec<u8>> = unit .ops .iter() .filter_map(|op| match &op.action { @@ -69,20 +69,26 @@ fn founding(at: &At<'_>, change: &Change<'_>, fold: &str) -> bool { _ => None, }) .collect(); - events::is_founding(at, fold, (&root, &keeps), &change.hash, &written) + events::is_founding( + at, + fold, + (&root, &keeps), + &crate::document::ChangeHash(unit.id), + &written, + ) } /// 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<'_>, + at: &impl ReadDoc, epoch: Epoch, - change: &Change<'_>, + unit: &Unit<'_>, fold: &str, -) -> Result<(), Refusal> { - let refuse = |why: String| Err(Refusal::Rule(why)); - let Some(node) = change.committer else { - if founding(at, change, fold) { +) -> Result<(), String> { + let refuse = |why: String| Err(why); + let Some(node) = unit.committer else { + if founding(at, unit, fold) { return Ok(()); } return refuse("it names no node".to_owned()); @@ -93,7 +99,7 @@ pub(crate) fn judge( let votes = meta::path(&[meta::VOTES]); let acks = meta::path(&[meta::VOTES, node]); let remakes = meta::path(&[meta::REMAKES]); - for op in change.ops { + for op in unit.ops { let allowed = if op.path.starts_with(&events) { event(at, op, fold) } else if op.path.starts_with(&entry) || op.path.starts_with(&acks) { @@ -101,7 +107,7 @@ pub(crate) fn judge( } 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) + remake(at, epoch, unit, node, op) } else { Err(format!("it writes {}, closed", shown(&op.path))) }; @@ -111,7 +117,7 @@ pub(crate) fn judge( } /// Whether `op` makes the map at its path, where there is none at `at`. -fn made(at: &At<'_>, op: &Op) -> Result<(), String> { +fn made(at: &impl ReadDoc, op: &Op) -> Result<(), String> { if op.action == Action::MakeMap && !store::held_anywhere(at, &op.path) { Ok(()) } else { @@ -125,9 +131,9 @@ fn made(at: &At<'_>, op: &Op) -> Result<(), String> { /// the record of the remake that made the epoch, carried into it whole by its /// coordinator, where there is none. fn remake( - at: &At<'_>, + at: &impl ReadDoc, epoch: Epoch, - change: &Change<'_>, + unit: &Unit<'_>, node: &str, op: &Op, ) -> Result<(), String> { @@ -142,7 +148,7 @@ fn remake( Err(format!( "it writes the proposal of remake {id}, written already" )) - } else if !proposes(change, &proposal, node) { + } else if !proposes(unit, &proposal, node) { Err(format!( "it proposes remake {id}, no compaction that {node} coordinates" )) @@ -155,7 +161,7 @@ fn remake( Some(coordinator) => Err(format!( "it decides remake {id}, which is {coordinator}'s to decide" )), - None if change.paths.contains(&proposal) + None if unit.paths.contains(&proposal) && !store::held_anywhere(at, &meta::path(&[meta::REMAKES, id])) && made_the_epoch(at, epoch, id, node) => { @@ -172,7 +178,7 @@ fn remake( /// Whether `change` writes at `proposal` a compaction's proposal that names `node` its /// coordinator. -fn proposes(change: &Change<'_>, proposal: &Path, node: &str) -> bool { +fn proposes(unit: &Unit<'_>, proposal: &Path, node: &str) -> bool { let at = |key: &str| { let mut path = proposal.clone(); path.push(key.to_owned()); @@ -180,14 +186,14 @@ fn proposes(change: &Change<'_>, proposal: &Path, node: &str) -> bool { }; let (coordinator, kind) = (at("coordinator"), at("kind")); let put = |path: &Path| { - change.ops.iter().find_map(|op| match &op.action { + unit.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 + && unit .ops .iter() .filter(|op| op.path == kind) @@ -196,7 +202,7 @@ fn proposes(change: &Change<'_>, proposal: &Path, node: &str) -> bool { /// 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> { +fn event(at: &impl ReadDoc, op: &Op, fold: &str) -> Result<(), String> { let [_, _, key] = op.path.as_slice() else { return Err(format!("it writes {}, no event's place", shown(&op.path))); }; @@ -309,12 +315,10 @@ mod tests { // Again, at a key held already: refused. Automerge writes no put of the value a // key holds, so the change is one made by hand. let heads = doc.get_heads(); - let at = At::new(&doc, &heads).unwrap(); + let at = crate::document::at::At::new(&doc, &heads).unwrap(); let path = meta::path(&[meta::EVENTS, &key]); - let again = Change { - hash: crate::document::ChangeHash([0; 32]), - deps: &heads, - seen: &heads, + let again = Unit { + id: [0; 32], committer: Some("node-b"), user: None, paths: &std::collections::BTreeSet::from([path.clone()]), |
