diff options
| author | tv <tv@krebsco.de> | 2026-11-15 12:28:33 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-15 12:28:33 +0000 |
| commit | 6be5646df342b21fe82c63f59cfec5a369ab54f8 (patch) | |
| tree | 48ede9923ab4fa762623a6e32b9b18a088276b98 /src/document/store.rs | |
| parent | d1d081cf8e0e8bb5809c1c82bbdfec6bd7ad0285 (diff) | |
signed values: value and meta apart, numbered; the highest seq is read
A member could put a user's older signed value back beside a newer
one: a change made with dependencies from before both is judged where
the place was empty, so the older envelope (`prev` empty) is taken;
and of values written at once, the one read was the one with the
highest operation, which whoever writes the change steers. A revoked
capability, a lifted ban or an earlier policy came back that way,
signed by whoever signed it then.
An envelope is now `cv2.<value>.<meta>.<signature>`: the value as its
user wrote it, and apart from it what continuum adds (swarm, path,
user, at, seq, prev), the signature over the value's line and the
meta's. `seq` is one more than that of the envelope it replaces, 0
where it replaces none; every node checks it with `prev`.
Of the values a place holds, written at once, signed ones are read
before any other, the highest `seq` first, of equal ones the lower
digest (`store::candidates`): reads, snapshots and writes all go by
it. An older envelope beside a newer one changes nothing.
`application/continuum-signed` is the value's line, the meta's, then
the signature; `scripts/continuum-signed` writes and checks it so.
The wire version goes up: nodes must agree on what a place's value is.
Diffstat (limited to 'src/document/store.rs')
| -rw-r--r-- | src/document/store.rs | 109 |
1 files changed, 94 insertions, 15 deletions
diff --git a/src/document/store.rs b/src/document/store.rs index fb28a4b..aa79e47 100644 --- a/src/document/store.rs +++ b/src/document/store.rs @@ -210,7 +210,33 @@ enum Resolved { Other(Value<'static>, ObjId), } -/// All concurrent values of `key` across a merged set of parent maps, newest first. +/// What every signed value starts with (`access::envelope`). +pub(crate) const ENVELOPE: &str = "cv2."; + +/// Where `value` stands among the values written at once at one place, if it is a +/// signed value (`access::envelope`), unchecked: its `seq`, and its digest. +fn signed_order(value: &Value<'_>) -> Option<(u64, [u8; 32])> { + use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; + use sha2::{Digest, Sha256}; + let Value::Scalar(scalar) = value else { + return None; + }; + let ScalarValue::Str(text) = scalar.as_ref() else { + return None; + }; + let meta = text.strip_prefix(ENVELOPE)?.split('.').nth(1)?; + let meta: JsonValue = serde_json::from_slice(&URL_SAFE_NO_PAD.decode(meta).ok()?).ok()?; + Some(( + meta.get("seq")?.as_u64()?, + Sha256::digest(text.as_bytes()).into(), + )) +} + +/// All concurrent values of `key` across a merged set of parent maps, the one that +/// counts first: signed values (`access::envelope`) before any other, the highest +/// `seq` first, of equal ones the lower digest; then the others, newest first. So +/// which signed value a place shows is its user's to say, not the order of operations, +/// which whoever writes a change steers. fn candidates( doc: &impl ReadDoc, parents: &[ObjId], @@ -223,6 +249,13 @@ fn candidates( } } all.sort_by(|a, b| op_order(&b.1).cmp(&op_order(&a.1))); + if all.len() > 1 { + // Stable: the others stay newest first. + all.sort_by_cached_key(|(value, _)| match signed_order(value) { + Some((seq, digest)) => (false, std::cmp::Reverse(seq), digest), + None => (true, std::cmp::Reverse(0), [0; 32]), + }); + } Ok(all) } @@ -469,20 +502,19 @@ pub(crate) fn read_plain( path: &[String], ) -> Result<Option<JsonValue>, StoreError> { Ok(match read_path(doc, path)? { - Some(JsonValue::String(text)) if text.starts_with("cv1.") => { + Some(JsonValue::String(text)) if text.starts_with(ENVELOPE) => { enveloped(&text).filter(|value| !value.is_null()) } other => other, }) } -/// The value an envelope (`cv1.<payload>.<signature>`) holds; None if it isn't one. +/// The value an envelope (`cv2.<value>.<meta>.<signature>`) holds; None if it isn't +/// one. fn enveloped(text: &str) -> Option<JsonValue> { use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; - let payload = text.split('.').nth(1)?; - let bytes = URL_SAFE_NO_PAD.decode(payload).ok()?; - let mut payload: JsonValue = serde_json::from_slice(&bytes).ok()?; - Some(payload.get_mut("value")?.take()) + let value = text.split('.').nth(1)?; + serde_json::from_slice(&URL_SAFE_NO_PAD.decode(value).ok()?).ok() } /// Reads a path; the empty path is the whole store. @@ -710,7 +742,8 @@ pub(crate) fn keys_at( }) } -/// Every concurrent value at `path`, newest first; empty if there is none. Where +/// Every concurrent value at `path`, the one read first (`candidates`); empty if there +/// is none. Where /// `read_path` picks one (or merges concurrent maps into one), this keeps them apart. pub(crate) fn read_candidates( doc: &impl ReadDoc, @@ -1704,20 +1737,66 @@ mod tests { at.split('/').map(str::to_owned).collect() } + /// An envelope holding `value`, numbered `seq`, its signature junk. + fn envelope_of(value: &JsonValue, seq: u64) -> String { + use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; + let meta = + json!({"swarm": "s", "path": ["a"], "user": "alice", "at": 1, "seq": seq, "prev": ""}); + format!( + "{ENVELOPE}{}.{}.c2ln", + URL_SAFE_NO_PAD.encode(value.to_string()), + URL_SAFE_NO_PAD.encode(meta.to_string()) + ) + } + + /// Of signed values written at once at one place, the highest `seq` is read, + /// whichever was written last, and before anything unsigned; a write replaces them + /// all. + #[test] + fn the_highest_seq_is_read() { + let doc = |actor: &[u8]| crate::document::Document::new().with_actor(ActorId::from(actor)); + let (mut ours, mut theirs, mut third) = (doc(b"a"), doc(b"b"), doc(b"c")); + // Ours: the older one, written after many operations, so that it is the newest. + try_change(&mut ours, |doc| { + for round in 0..20 { + write_path(doc, &path("padding"), &json!(round))?; + } + write_path(doc, &path("a"), &json!(envelope_of(&json!("old"), 0))) + }) + .unwrap(); + try_change(&mut theirs, |doc| { + write_path(doc, &path("a"), &json!(envelope_of(&json!("new"), 1))) + }) + .unwrap(); + try_change(&mut third, |doc| { + for round in 0..40 { + write_path(doc, &path("more"), &json!(round))?; + } + write_path(doc, &path("a"), &json!("unsigned")) + }) + .unwrap(); + ours.merge(&mut theirs).unwrap(); + assert_eq!(read_plain(&ours, &path("a")).unwrap(), Some(json!("new"))); + ours.merge(&mut third).unwrap(); + assert_eq!(read_plain(&ours, &path("a")).unwrap(), Some(json!("new"))); + try_change(&mut ours, |doc| { + write_path(doc, &path("a"), &json!("after")) + }) + .unwrap(); + assert_eq!(read_candidates(&ours, &path("a")).unwrap().len(), 1); + assert_eq!(read_plain(&ours, &path("a")).unwrap(), Some(json!("after"))); + } + /// An envelope reads as the value it holds, a signed `null` as nothing, anything /// else as it is. #[test] fn plain_reads_open_envelopes() { - use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; - let sealed = |value: JsonValue| { - let payload = json!({"swarm": "s", "path": ["a"], "value": value, "user": "alice", "at": 1, "prev": ""}); - format!("cv1.{}.c2ln", URL_SAFE_NO_PAD.encode(payload.to_string())) - }; + let sealed = |value: JsonValue| envelope_of(&value, 0); let mut doc = crate::document::Document::new(); try_change(&mut doc, |doc| { write_path(doc, &path("a"), &json!(sealed(json!({"mode": "fast"}))))?; write_path(doc, &path("gone"), &json!(sealed(JsonValue::Null)))?; - write_path(doc, &path("plain"), &json!("cv1-ish")) + write_path(doc, &path("plain"), &json!("cv2-ish")) }) .unwrap(); assert_eq!( @@ -1727,7 +1806,7 @@ mod tests { assert_eq!(read_plain(&doc, &path("gone")).unwrap(), None); assert_eq!( read_plain(&doc, &path("plain")).unwrap(), - Some(json!("cv1-ish")) + Some(json!("cv2-ish")) ); assert_eq!(read_plain(&doc, &path("none")).unwrap(), None); } |
