diff options
| -rw-r--r-- | src/document/store.rs | 123 |
1 files changed, 75 insertions, 48 deletions
diff --git a/src/document/store.rs b/src/document/store.rs index 27edf49..a9d4379 100644 --- a/src/document/store.rs +++ b/src/document/store.rs @@ -224,61 +224,77 @@ fn signed_order(value: &Value<'_>) -> Option<(u64, [u8; 32])> { Some((payload.seq, Sha256::digest(text.as_bytes()).into())) } -/// Whether `doc`'s policy asks for signed values (`access::policy`: `"values": -/// "signed"`), as a read of it shows it: below every reading that goes by it -/// (`candidates`). Its own place is read by `seq` wherever any value there asks for -/// signed values, so an older policy put back beside a strict one can't undo it; every -/// other place, as the policy that read shows says. Read here as written, not through -/// `candidates`, which asks this. +/// Whether `doc`'s policy asks for signed values (`access::policy`), as the policy reads +/// (`policy::of`): below every reading that goes by it (`candidates`). That reading +/// orders the policy's own place by `seq` wherever any value there asks for signed +/// values (`policy_by_seq`), so an older policy put back beside a strict one can't undo +/// it, and reads what is within the policy as written; so asking this from within it +/// answers false, rather than ask again. fn signs_values(doc: &impl ReadDoc) -> bool { - let mut maps = vec![automerge::ROOT]; - for key in [super::meta::ROOT, super::meta::ACCESS] { - maps = maps + thread_local! { + static READING: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; + } + if READING.get() { + return false; + } + READING.set(true); + let strict = crate::access::policy::of(doc).strict(); + READING.set(false); + strict +} + +/// Whether the values at `key` of `parents` are ordered by `seq`: at the policy's place, +/// where any of them (`all`) asks for signed values; within the policy, never; elsewhere, +/// where the policy does (`signs_values`). +fn by_seq( + doc: &impl ReadDoc, + parents: &[ObjId], + key: &str, + all: &[(Value<'static>, ObjId)], +) -> bool { + let mut access = vec![automerge::ROOT]; + for step in [super::meta::ROOT, super::meta::ACCESS] { + access = access .iter() - .flat_map(|map| doc.get_all(map, key).unwrap_or_default()) + .flat_map(|map| doc.get_all(map, step).unwrap_or_default()) .filter_map(|(value, id)| matches!(value, Value::Object(ObjType::Map)).then_some(id)) .collect(); } - let policy = |value: &Value<'_>, id: &ObjId| -> Option<JsonValue> { - match value { - Value::Object(ObjType::Map) => { - let mut fields = serde_json::Map::new(); - for key in doc.keys(id) { - if let Ok(Some((Value::Scalar(scalar), _))) = doc.get(id, key.as_str()) - && let Some(field) = typed::scalar_to_json(&scalar, JsonForm::default()) - { - fields.insert(key, field); - } - } - Some(JsonValue::Object(fields)) - } - Value::Scalar(scalar) => match scalar.as_ref() { - ScalarValue::Str(text) => super::signed::read(text).map(|payload| payload.value), - _ => None, - }, - _ => None, - } - }; - let asks = |json: Option<JsonValue>| { - json.is_some_and(|json| json.get("values") == Some(&JsonValue::from("signed"))) - }; - let mut all: Vec<(Value<'static>, ObjId)> = maps + if key == "policy" && parents.iter().all(|parent| access.contains(parent)) { + return all.iter().any(|(value, id)| policy_asks(doc, value, id)); + } + let policies: Vec<ObjId> = access .iter() .flat_map(|map| doc.get_all(map, "policy").unwrap_or_default()) - .map(|(value, id)| (value.into_owned(), id)) + .filter_map(|(value, id)| matches!(value, Value::Object(ObjType::Map)).then_some(id)) .collect(); - if !all.iter().any(|(value, id)| asks(policy(value, id))) { + if parents.iter().all(|parent| policies.contains(parent)) { return false; } - // As `candidates` orders them where the policy asks for signed values. - all.sort_by(|a, b| op_order(&b.1).cmp(&op_order(&a.1))); - all.sort_by_key(|(value, _)| match signed_order(value) { - Some((seq, digest)) => (false, std::cmp::Reverse(seq), digest), - None => (true, std::cmp::Reverse(0), [0; 32]), - }); - all.first() - .and_then(|(value, id)| policy(value, id)) - .is_some_and(|json| crate::access::policy::read(&json).strict()) + signs_values(doc) +} + +/// Whether `value` (`id`), at the policy's place, asks for signed values (`"values": +/// "signed"`), written as a map or signed. +fn policy_asks(doc: &impl ReadDoc, value: &Value<'_>, id: &ObjId) -> bool { + match value { + Value::Object(ObjType::Map) => { + doc.get_all(id, "values") + .unwrap_or_default() + .iter() + .any(|(value, _)| { + matches!(value, Value::Scalar(scalar) + if matches!(scalar.as_ref(), ScalarValue::Str(text) if text == "signed")) + }) + } + Value::Scalar(scalar) => match scalar.as_ref() { + ScalarValue::Str(text) => super::signed::read(text).is_some_and(|payload| { + payload.value.get("values") == Some(&JsonValue::from("signed")) + }), + _ => false, + }, + _ => false, + } } /// Every change a document holds, by its writer and the numbers of its operations, with @@ -390,9 +406,11 @@ fn candidates( } } all.sort_by(|a, b| op_order(&b.1).cmp(&op_order(&a.1))); - if all.len() > 1 && matches!(prop.into(), Prop::Map(_)) { + if all.len() > 1 + && let Prop::Map(key) = prop.into() + { let orders: Vec<_> = all.iter().map(|(value, _)| signed_order(value)).collect(); - if orders.iter().any(Option::is_some) && signs_values(doc) { + if orders.iter().any(Option::is_some) && by_seq(doc, parents, &key, &all) { let mut ordered: Vec<_> = orders.into_iter().zip(all).collect(); // Stable: the others stay newest first. ordered.sort_by_key(|(order, _)| match order { @@ -1906,7 +1924,16 @@ mod tests { #[test] fn values_go_by_the_policy_as_read() { let doc = |actor: &[u8]| crate::document::Document::new().with_actor(ActorId::from(actor)); - let (mut ours, mut theirs) = (doc(b"a"), doc(b"b")); + // The signed one written by either actor: newer or older by Automerge's order. + for (strict_by, signed_by) in [(b"a", b"b"), (b"b", b"a")] { + values_by_policy(doc(strict_by), doc(signed_by)); + } + } + + fn values_by_policy( + mut ours: crate::document::Document, + mut theirs: crate::document::Document, + ) { try_change(&mut ours, |doc| { write_path( doc, |
