diff options
| -rw-r--r-- | src/access/screen.rs | 69 | ||||
| -rw-r--r-- | src/document/store.rs | 23 |
2 files changed, 87 insertions, 5 deletions
diff --git a/src/access/screen.rs b/src/access/screen.rs index 909133b..a57c2fe 100644 --- a/src/access/screen.rs +++ b/src/access/screen.rs @@ -145,7 +145,8 @@ impl Screening { /// Itself for the document `epoch_seq`: emptied if it was another's. /// Whether `doc` holds anything at the policy's place, a policy known or not or what - /// reads as none (an envelope holding `null`), or did since this node took it up: + /// reads as none (an envelope holding `null`), in any of the maps on the way to it, a + /// read showing them or not (`store::held_anywhere`), or did since this node took it up: /// then what it receives is judged (`apply`), each change by the policy at its /// dependencies, though the policy is gone or unknown now; changes written under it /// may still come, and one writing into what reads as none may turn it into a policy. @@ -153,10 +154,7 @@ impl Screening { /// a restart: a node that starts with nothing at the policy's place judges as one /// that never had a policy. pub(crate) fn policed(&mut self, doc: &Document) -> bool { - self.policed |= !matches!( - crate::document::store::read_held(doc, &super::policy::path()), - Ok(None) - ); + self.policed |= crate::document::store::held_anywhere(doc, &super::policy::path()); self.policed } @@ -1550,4 +1548,65 @@ mod tests { ); assert!(Screening::default().policed(&doc)); } + + /// A value at `_continuum/access` written beside the map holding the policy, and read + /// as newer, hides it from a read: what is received is judged all the same. + #[test] + fn policed_where_a_value_beside_hides_the_policy() { + let mut doc = Document::new(); + transact( + &mut doc, + UnixMillis(1), + &Writer { + node: "a", + user: None, + }, + |doc| store::write_path(doc, &path("_continuum/swarm"), &json!("s")), + ) + .unwrap(); + let mut beside = doc.clone().with_actor(automerge::ActorId::random()); + transact( + &mut doc, + UnixMillis(2), + &Writer { + node: "a", + user: None, + }, + |doc| store::write_path(doc, &path("_continuum/access/policy"), &json!({})), + ) + .unwrap(); + // Writes enough to be the newer when they meet. + for at in 0..8 { + transact( + &mut beside, + UnixMillis(3 + at), + &Writer { + node: "b", + user: None, + }, + |doc| store::write_path(doc, &path("notes/x"), &json!(at)), + ) + .unwrap(); + } + transact( + &mut beside, + UnixMillis(20), + &Writer { + node: "b", + user: None, + }, + |doc| store::write_path(doc, &path("_continuum/access"), &json!("x")), + ) + .unwrap(); + doc.merge(&mut beside).unwrap(); + assert_eq!( + store::read_path(&doc, &path("_continuum/access")).unwrap(), + Some(json!("x")) + ); + assert_eq!( + crate::access::policy::of(&doc), + crate::access::policy::Policy::Open + ); + assert!(Screening::default().policed(&doc)); + } } diff --git a/src/document/store.rs b/src/document/store.rs index cd455b6..7de7373 100644 --- a/src/document/store.rs +++ b/src/document/store.rs @@ -654,6 +654,29 @@ pub(crate) fn read_path_with_conflicts_as( Ok(value.map(|value| (value, log.reports))) } +/// Whether anything is at `path` (map keys only) in any of the values written at once on +/// the way to it, not only those a read shows: of every key, every map among its values, +/// then the key itself (as `by_seq` walks them). A read shows the newest value of a key, +/// merging maps only where that one is a map, so a value beside a map hides what is in it. +pub(crate) fn held_anywhere(doc: &impl ReadDoc, path: &[String]) -> bool { + let Some((last, way)) = path.split_last() else { + return true; + }; + let mut maps = vec![automerge::ROOT]; + for step in way { + maps = maps + .iter() + .flat_map(|map| doc.get_all(map, step.as_str()).unwrap_or_default()) + .filter_map(|(value, id)| matches!(value, Value::Object(ObjType::Map)).then_some(id)) + .collect(); + } + maps.iter().any(|map| { + !doc.get_all(map, last.as_str()) + .unwrap_or_default() + .is_empty() + }) +} + /// What is stored at `path` (the empty path: the root map), and, where it is a scalar /// or a text, the value itself, to show natively (`typed::native`); None if nothing is /// there. |
