aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/access/envelope.rs183
-rw-r--r--src/access/rules.rs5
-rw-r--r--src/access/screen.rs28
-rw-r--r--src/access/sealed.rs11
-rw-r--r--src/api/store.rs37
-rw-r--r--src/api/users.rs2
-rw-r--r--src/document/store.rs109
-rw-r--r--src/simtest/policy.rs20
-rw-r--r--src/transport/wire.rs2
9 files changed, 321 insertions, 76 deletions
diff --git a/src/access/envelope.rs b/src/access/envelope.rs
index 5b1b368..c12fd35 100644
--- a/src/access/envelope.rs
+++ b/src/access/envelope.rs
@@ -1,17 +1,22 @@
//! Values signed by their user: under a strict policy (`access::policy`), at what
//! decides who may do what and at the paths the swarm lists as user-signed
//! (`_continuum/access/userSigned`, `listed`; `signed_paths`), the value is a signed envelope, one
-//! string, `cv1.<payload>.<signature>` (each base64url, unpadded). The payload is JSON:
-//! the `path` it is for (segments), the `value` (`null`: deleted), the `user` who
-//! signed it, when (`at`, Unix ms, by the signer's clock), and `prev`: the SHA-256 (hex)
-//! of the envelope it replaces, empty if none. The signature is the user's, with one
-//! of its keys (`registry::User::keys`), over the payload's bytes, as
+//! string, `cv2.<value>.<meta>.<signature>` (each base64url, unpadded). The value is
+//! JSON, as its user wrote it (`null`: deleted). What continuum adds is apart, in the
+//! meta, JSON too: the `swarm` and the `path` it is for (segments), the `user` who
+//! signed it, when (`at`, Unix ms, by the signer's clock), `seq`, and `prev`: the
+//! SHA-256 (hex) of the envelope it replaces, empty if none. The signature is the
+//! user's, with one of its keys (`registry::User::keys`), over the value's bytes, a
+//! newline, and the meta's bytes (neither holds a newline), as
//! `ssh-keygen -Y sign -n continuum-value` makes it (`access::sshsig`), its SSHSIG blob.
//!
//! It travels with the value, through replays and snapshots alike, and anyone can check
-//! it (`check`): a node before it writes or applies one, a client reading it. `prev`
-//! keeps a member from putting an older signed value back: an envelope only replaces
-//! the one it names. A node never writes there but as its user signed it.
+//! it (`check`): a node before it writes or applies one, a client reading it. An
+//! envelope only replaces the one it names (`prev`), and its `seq` is one more than
+//! that one's (0 where it replaces none). Of the envelopes a place holds, written at
+//! once, the one with the highest `seq` is the place's value (`document::store`): so a
+//! member that puts an older signed value back beside a newer one changes nothing. A
+//! node never writes there but as its user signed it.
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD as B64URL};
use serde::{Deserialize, Serialize};
@@ -24,7 +29,7 @@ use crate::document::{ReadDoc, meta, store};
/// What a signature over a payload is for (`sshsig`).
pub(crate) const NAMESPACE: &str = "continuum-value";
/// What every envelope starts with.
-const PREFIX: &str = "cv1";
+const PREFIX: &str = "cv2";
/// Where the swarm lists the user-signed paths: a list of paths, each a list of
/// segments.
@@ -97,19 +102,56 @@ pub(crate) fn reaches_signed(signed: &[Vec<String>], path: &[String]) -> bool {
signed.iter().any(|pattern| agree(pattern, path))
}
-/// A signed value's payload.
-#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
+/// What a signed value says: its value, and its meta.
+#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Payload {
+ pub(crate) value: JsonValue,
/// The swarm it is for: one user, with the same name and key in two swarms, signs
/// for one only.
pub(crate) swarm: String,
pub(crate) path: Vec<String>,
- pub(crate) value: JsonValue,
pub(crate) user: String,
pub(crate) at: u64,
+ /// One more than that of the envelope it replaces; 0 where it replaces none.
+ pub(crate) seq: u64,
pub(crate) prev: String,
}
+/// An envelope's meta, as it is written.
+#[derive(Serialize, Deserialize)]
+struct Meta {
+ swarm: String,
+ path: Vec<String>,
+ user: String,
+ at: u64,
+ seq: u64,
+ prev: String,
+}
+
+impl Payload {
+ /// The bytes its user signs (`seal`): the value, a newline, the meta.
+ #[cfg(test)]
+ pub(crate) fn signed(&self) -> Vec<u8> {
+ let meta = Meta {
+ swarm: self.swarm.clone(),
+ path: self.path.clone(),
+ user: self.user.clone(),
+ at: self.at,
+ seq: self.seq,
+ prev: self.prev.clone(),
+ };
+ let mut bytes = serde_json::to_vec(&self.value).expect("a value is JSON");
+ bytes.push(b'\n');
+ bytes.extend(serde_json::to_vec(&meta).expect("a meta is JSON"));
+ bytes
+ }
+}
+
+/// The `seq` of `envelope`, unchecked; None if it isn't one.
+pub(crate) fn seq(envelope: &str) -> Option<u64> {
+ read(envelope).ok().map(|payload| payload.seq)
+}
+
/// An envelope checked (`check`): what it says, and the key that signed it.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Signed {
@@ -127,35 +169,72 @@ pub(crate) fn digest(envelope: Option<&str>) -> String {
})
}
-/// The envelope of `payload` (its exact bytes) signed by `signature` (armored, as
-/// `ssh-keygen -Y sign` writes it).
-pub(crate) fn seal(payload: &[u8], armored: &str) -> Result<String, String> {
+/// The envelope of `signed` (the exact bytes signed: the value's JSON, a newline, the
+/// meta's JSON) signed by `signature` (armored, as `ssh-keygen -Y sign` writes it).
+pub(crate) fn seal(signed: &[u8], armored: &str) -> Result<String, String> {
let blob = sshsig::dearmor(armored)?;
+ let mut lines = signed.split(|byte| *byte == b'\n');
+ let (Some(value), Some(meta), None) = (lines.next(), lines.next(), lines.next()) else {
+ return Err("its value's line, then its meta's".to_owned());
+ };
Ok(format!(
- "{PREFIX}.{}.{}",
- B64URL.encode(payload),
+ "{PREFIX}.{}.{}.{}",
+ B64URL.encode(value),
+ B64URL.encode(meta),
B64URL.encode(blob)
))
}
-/// What `envelope` holds, unchecked: its payload's bytes, the payload, the signature's
+/// What `envelope` holds, unchecked: the bytes signed, what it says, the signature's
/// blob.
fn open(envelope: &str) -> Result<(Vec<u8>, Payload, Vec<u8>), String> {
let mut parts = envelope.split('.');
- let (Some(PREFIX), Some(payload), Some(signature), None) =
- (parts.next(), parts.next(), parts.next(), parts.next())
- else {
- return Err("not a signed value (cv1.<payload>.<signature>)".to_owned());
+ let (Some(PREFIX), Some(value), Some(meta), Some(signature), None) = (
+ parts.next(),
+ parts.next(),
+ parts.next(),
+ parts.next(),
+ parts.next(),
+ ) else {
+ return Err("not a signed value (cv2.<value>.<meta>.<signature>)".to_owned());
+ };
+ let decoded = |part: &str, what: &str| {
+ let bytes = B64URL
+ .decode(part)
+ .map_err(|error| format!("its {what} doesn't decode: {error}"))?;
+ // One line each: the bytes signed part at the one newline between them.
+ if bytes.contains(&b'\n') {
+ return Err(format!("its {what} holds a newline"));
+ }
+ Ok(bytes)
};
- let bytes = B64URL
- .decode(payload)
- .map_err(|error| format!("its payload doesn't decode: {error}"))?;
- let parsed: Payload = serde_json::from_slice(&bytes)
- .map_err(|error| format!("its payload isn't one: {error}"))?;
+ let mut signed = decoded(value, "value")?;
+ let value: JsonValue = serde_json::from_slice(&signed)
+ .map_err(|error| format!("its value isn't JSON: {error}"))?;
+ let meta = decoded(meta, "meta")?;
+ let Meta {
+ swarm,
+ path,
+ user,
+ at,
+ seq,
+ prev,
+ } = serde_json::from_slice(&meta).map_err(|error| format!("its meta isn't one: {error}"))?;
+ signed.push(b'\n');
+ signed.extend(meta);
let signature = B64URL
.decode(signature)
.map_err(|error| format!("its signature doesn't decode: {error}"))?;
- Ok((bytes, parsed, signature))
+ let payload = Payload {
+ value,
+ swarm,
+ path,
+ user,
+ at,
+ seq,
+ prev,
+ };
+ Ok((signed, payload, signature))
}
/// What `envelope` says, unchecked: for showing a value (`check` before taking it).
@@ -165,8 +244,8 @@ pub(crate) fn read(envelope: &str) -> Result<Payload, String> {
/// Checks `envelope` as the value at `path`, replacing `previous` (the envelope there
/// before, if any), against the user registry `doc` holds (as the writer saw it): for
-/// its swarm, that path, naming what it replaces, signed by one of its user's keys, a user who may
-/// write there.
+/// its swarm, that path, naming what it replaces and numbered after it, signed by one of
+/// its user's keys, a user who may write there.
pub(crate) fn check(
doc: &impl ReadDoc,
envelope: &str,
@@ -193,6 +272,13 @@ pub(crate) fn check(
if payload.prev != digest(previous) {
return Err("it doesn't replace the value there (prev)".to_owned());
}
+ let next = match previous.map(seq) {
+ None => Some(0),
+ Some(before) => before.and_then(|before| before.checked_add(1)),
+ };
+ if Some(payload.seq) != next {
+ return Err("it isn't numbered after the value there (seq)".to_owned());
+ }
let key = sshsig::verify_blob(&signature, NAMESPACE, &bytes)?;
let user =
registry::user(doc, &payload.user).ok_or_else(|| format!("no user {}", payload.user))?;
@@ -275,17 +361,19 @@ mod tests {
}
fn signed(key: &SigningKey, payload: &Payload) -> String {
- let bytes = serde_json::to_vec(payload).unwrap();
+ let bytes = payload.signed();
seal(&bytes, &sshsig::sign(key, NAMESPACE, &bytes)).unwrap()
}
+ /// The first at its place (`prev` empty), or the one after it.
fn payload(user: &str, at: &str, value: JsonValue, prev: &str) -> Payload {
Payload {
+ value,
swarm: "s".to_owned(),
path: path(at),
- value,
user: user.to_owned(),
at: 1,
+ seq: u64::from(!prev.is_empty()),
prev: prev.to_owned(),
}
}
@@ -320,9 +408,36 @@ mod tests {
assert!(check(&doc, &second, &path("config"), Some(&first)).is_ok());
assert!(check(&doc, &second, &path("config"), None).is_err());
assert!(check(&doc, &first, &path("config"), Some(&second)).is_err());
- // Tampered with.
- let tampered = first.replacen("cv1.", "cv1.e30", 1);
+ // Numbered after it, and nothing else: not as the first, nor two on.
+ for seq in [0, 2] {
+ let misnumbered = signed(
+ &key,
+ &Payload {
+ seq,
+ ..payload("alice", "config", json!(null), &digest(Some(&first)))
+ },
+ );
+ assert!(check(&doc, &misnumbered, &path("config"), Some(&first)).is_err());
+ }
+ let late = signed(
+ &key,
+ &Payload {
+ seq: 1,
+ ..payload("alice", "config", json!(1), "")
+ },
+ );
+ assert!(check(&doc, &late, &path("config"), None).is_err());
+ // Tampered with: its value, its meta.
+ let tampered = first.replacen("cv2.", "cv2.e30", 1);
assert!(check(&doc, &tampered, &path("config"), None).is_err());
+ let mut parts: Vec<&str> = first.split('.').collect();
+ let renumbered = B64URL.encode(
+ String::from_utf8(B64URL.decode(parts[2]).unwrap())
+ .unwrap()
+ .replace("\"seq\":0", "\"seq\":7"),
+ );
+ parts[2] = &renumbered;
+ assert!(check(&doc, &parts.join("."), &path("config"), None).is_err());
assert!(check(&doc, "plain", &path("config"), None).is_err());
// For another swarm.
let elsewhere = signed(
diff --git a/src/access/rules.rs b/src/access/rules.rs
index 5c839af..738cdf8 100644
--- a/src/access/rules.rs
+++ b/src/access/rules.rs
@@ -964,14 +964,15 @@ mod tests {
);
let sealed = |at: &str, prev: &str| {
let payload = envelope::Payload {
+ value: json!({"mode": "fast"}),
swarm: "s".to_owned(),
path: path(at),
- value: json!({"mode": "fast"}),
user: "alice".to_owned(),
at: 1,
+ seq: u64::from(!prev.is_empty()),
prev: prev.to_owned(),
};
- let bytes = serde_json::to_vec(&payload).unwrap();
+ let bytes = payload.signed();
envelope::seal(&bytes, &sshsig::sign(&key, envelope::NAMESPACE, &bytes)).unwrap()
};
let judged = |doc: &Document, ops: Vec<Op>| {
diff --git a/src/access/screen.rs b/src/access/screen.rs
index b616238..9ca8809 100644
--- a/src/access/screen.rs
+++ b/src/access/screen.rs
@@ -1064,7 +1064,8 @@ mod tests {
}
/// Under a strict policy, two signed values written one after the other at the same
- /// path, received in one batch: both taken, the second judged with the first in.
+ /// path, received in one batch: both taken, the second judged with the first in. And
+ /// the first put back beside the second changes nothing.
#[test]
fn successive_signed_values_in_one_batch() {
use crate::access::{envelope, sshsig};
@@ -1094,18 +1095,20 @@ mod tests {
write(&mut theirs, "a", None, "_continuum/members/a", json!({}));
let sealed = |prev: &str, value: serde_json::Value| {
let payload = envelope::Payload {
+ value,
swarm: "s".to_owned(),
path: path("config"),
- value,
user: "alice".to_owned(),
at: 1,
+ seq: u64::from(!prev.is_empty()),
prev: prev.to_owned(),
};
- let bytes = serde_json::to_vec(&payload).unwrap();
+ let bytes = payload.signed();
envelope::seal(&bytes, &sshsig::sign(&key, envelope::NAMESPACE, &bytes)).unwrap()
};
let first = sealed("", json!(1));
let second = sealed(&envelope::digest(Some(&first)), json!(2));
+ let before = theirs.get_heads();
write(&mut theirs, "a", Some("alice"), "config", json!(first));
write(&mut theirs, "a", Some("alice"), "config", json!(second));
@@ -1117,6 +1120,25 @@ mod tests {
store::read_path(&ours, &path("config")).unwrap(),
Some(json!(second))
);
+
+ // The first put there again, by a change that saw neither: taken, as nothing
+ // was there at its history, and held beside the second, which stays the value
+ // (its `seq` is higher), whatever the order of the operations.
+ let mut side = theirs.fork_at(&before).unwrap();
+ write(&mut side, "a", Some("alice"), "config", json!(first));
+ theirs.merge(&mut side).unwrap();
+ let mut ours = Document::new();
+ let screened = screen(&mut ours, received(&theirs, &[]), &mut Waiting::default());
+ let why: Vec<&str> = screened.refused.iter().map(|r| r.why.as_str()).collect();
+ assert!(why.is_empty(), "{why:?}");
+ assert_eq!(
+ store::read_candidates(&ours, &path("config")).unwrap(),
+ [json!(second), json!(first)]
+ );
+ assert_eq!(
+ store::read_path(&ours, &path("config")).unwrap(),
+ Some(json!(second))
+ );
}
/// A refused change offered again: refused by its hash, not judged or kept again,
diff --git a/src/access/sealed.rs b/src/access/sealed.rs
index 7e48e34..545fe76 100644
--- a/src/access/sealed.rs
+++ b/src/access/sealed.rs
@@ -148,7 +148,7 @@ pub(crate) fn check(
let JsonValue::String(text) = value else {
return Err("a sealed value is a string (cs1.<age file>)".to_owned());
};
- let sealed = if text.starts_with("cv1.") {
+ let sealed = if text.starts_with(store::ENVELOPE) {
match envelope::read(text)?.value {
JsonValue::Null => return Ok(()),
JsonValue::String(sealed) => sealed,
@@ -340,8 +340,13 @@ mod tests {
assert!(check(&doc, notes, &json!("plain")).is_err());
// Signed: holding a sealed value, or null.
let signed = |value: JsonValue| {
- let payload = json!({"swarm": "s", "path": ["notes"], "value": value, "user": "alice", "at": 1, "prev": ""});
- format!("cv1.{}.c2ln", B64URL.encode(payload.to_string()))
+ let meta = json!({"swarm": "s", "path": ["notes"], "user": "alice", "at": 1, "seq": 0, "prev": ""});
+ format!(
+ "{}{}.{}.c2ln",
+ store::ENVELOPE,
+ B64URL.encode(value.to_string()),
+ B64URL.encode(meta.to_string())
+ )
};
assert!(check(&doc, notes, &json!(signed(json!(sealed_for(&[key(9)]))))).is_ok());
assert!(check(&doc, notes, &json!(signed(JsonValue::Null))).is_ok());
diff --git a/src/api/store.rs b/src/api/store.rs
index 00ced48..a7f4686 100644
--- a/src/api/store.rs
+++ b/src/api/store.rs
@@ -515,15 +515,16 @@ pub(crate) async fn put_store_entry(
Ok(with_retyped(with_heads(read.respond()?, &heads), &retyped))
}
-/// A signed value's media type (`access::envelope`): its payload's JSON, the exact bytes
-/// signed, on the first line; then its user's signature of them, armored, as
+/// A signed value's media type (`access::envelope`): its value's JSON on the first
+/// line and its meta's on the second, the exact bytes signed (the two lines, without
+/// the second's newline); then its user's signature of them, armored, as
/// `ssh-keygen -Y sign -n continuum-value` writes it.
const SIGNED: &str = "application/continuum-signed";
/// `PUT` of a value its user signed (`SIGNED`), at a path for one
/// (`envelope::signed_paths`; `409` elsewhere): taken if the authenticated user signed it,
-/// for this path, replacing what is there (`409` if it names another: read again and
-/// sign anew), with one of its keys, and may write there (`envelope::check`; `403` if
+/// for this path, replacing what is there and numbered after it (`409` if it names
+/// another: read again and sign anew), with one of its keys, and may write there (`envelope::check`; `403` if
/// not). Written as its envelope, and answered with the value, as `PUT` is.
async fn put_signed(
state: &AppState,
@@ -535,10 +536,10 @@ async fn put_signed(
) -> Result<Response, ApiError> {
let bad = |error: String| ApiError(StatusCode::BAD_REQUEST, Some(error));
let text = std::str::from_utf8(body).map_err(|_| bad("not UTF-8".to_owned()))?;
- let (payload, armored) = text
- .split_once('\n')
- .ok_or_else(|| bad("its payload's line, then its signature".to_owned()))?;
- let sealed = envelope::seal(payload.as_bytes(), armored).map_err(bad)?;
+ let lines = || bad("its value's line, its meta's, then its signature".to_owned());
+ let (value, rest) = text.split_once('\n').ok_or_else(lines)?;
+ let (meta, armored) = rest.split_once('\n').ok_or_else(lines)?;
+ let sealed = envelope::seal(format!("{value}\n{meta}").as_bytes(), armored).map_err(bad)?;
let signer = envelope::read(&sealed).map_err(bad)?.user;
if signer != user.0 {
return Err(ApiError(
@@ -565,10 +566,16 @@ async fn put_signed(
Some(JsonValue::String(text)) if envelope::read(&text).is_ok() => Some(text),
_ => None,
};
- if envelope::read(&sealed).map_err(bad)?.prev != envelope::digest(previous.as_deref()) {
+ let signed = envelope::read(&sealed).map_err(bad)?;
+ let next = previous
+ .as_deref()
+ .map_or(Some(0), |before| Some(envelope::seq(before)? + 1));
+ if signed.prev != envelope::digest(previous.as_deref()) || Some(signed.seq) != next {
return Err(ApiError(
StatusCode::CONFLICT,
- Some("it doesn't replace the value there (prev): read it again".to_owned()),
+ Some(
+ "it doesn't replace the value there (prev, seq): read it again".to_owned(),
+ ),
));
}
envelope::check(&*doc, &sealed, &path, previous.as_deref())
@@ -1331,8 +1338,14 @@ mod tests {
fn envelope_of(value: JsonValue) -> String {
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
- let payload = json!({"swarm": "s", "path": ["x"], "value": value, "user": "alice", "at": 1, "prev": ""});
- format!("cv1.{}.c2ln", URL_SAFE_NO_PAD.encode(payload.to_string()))
+ let meta =
+ json!({"swarm": "s", "path": ["x"], "user": "alice", "at": 1, "seq": 0, "prev": ""});
+ format!(
+ "{}{}.{}.c2ln",
+ store::ENVELOPE,
+ URL_SAFE_NO_PAD.encode(value.to_string()),
+ URL_SAFE_NO_PAD.encode(meta.to_string())
+ )
}
fn path(text: &str) -> Vec<String> {
diff --git a/src/api/users.rs b/src/api/users.rs
index fc3f6dc..66b6a8b 100644
--- a/src/api/users.rs
+++ b/src/api/users.rs
@@ -153,7 +153,7 @@ pub(crate) async fn own_keys(
.flatten()
.ok_or_else(|| ApiError(StatusCode::NOT_FOUND, Some("no entry of yours".to_owned())))?;
let signed = match store::read_path(&replica.doc, &path) {
- Ok(Some(JsonValue::String(text))) if text.starts_with("cv1.") => Some(text),
+ Ok(Some(JsonValue::String(text))) if text.starts_with(store::ENVELOPE) => Some(text),
_ => None,
};
Ok(Json(json!({
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);
}
diff --git a/src/simtest/policy.rs b/src/simtest/policy.rs
index 4162a8c..4182b11 100644
--- a/src/simtest/policy.rs
+++ b/src/simtest/policy.rs
@@ -214,7 +214,9 @@ async fn write_signed(
use crate::document::typed::{TimestampFormat, Val};
let at = path(at);
let previous = match store::read_path(&node.doc.read().await.doc, &at) {
- Ok(Some(serde_json::Value::String(text))) if text.starts_with("cv1.") => Some(text),
+ Ok(Some(serde_json::Value::String(text))) if text.starts_with(store::ENVELOPE) => {
+ Some(text)
+ }
_ => None,
};
let swarm = store::read_path(&node.doc.read().await.doc, &path("_continuum/swarm"))
@@ -223,14 +225,18 @@ async fn write_signed(
.and_then(|swarm| swarm.as_str().map(str::to_owned))
.unwrap_or_default();
let payload = envelope::Payload {
+ value,
swarm,
path: at.clone(),
- value,
user: user.to_owned(),
at: 1,
+ seq: previous
+ .as_deref()
+ .and_then(envelope::seq)
+ .map_or(0, |seq| seq + 1),
prev: envelope::digest(previous.as_deref()),
};
- let bytes = serde_json::to_vec(&payload).unwrap();
+ let bytes = payload.signed();
let sealed = envelope::seal(&bytes, &sshsig::sign(key, envelope::NAMESPACE, &bytes)).unwrap();
let sign = Sign::Write {
path: at.clone(),
@@ -371,14 +377,18 @@ async fn a_strict_swarm() {
.flatten()
.and_then(|value| value.as_str().map(str::to_owned));
let payload = envelope::Payload {
+ value: json!({"mode": "elsewhere"}),
swarm: "elsewhere".to_owned(),
path: path("config"),
- value: json!({"mode": "elsewhere"}),
user: "alice".to_owned(),
at: 2,
+ seq: previous
+ .as_deref()
+ .and_then(envelope::seq)
+ .map_or(0, |seq| seq + 1),
prev: envelope::digest(previous.as_deref()),
};
- let bytes = serde_json::to_vec(&payload).unwrap();
+ let bytes = payload.signed();
envelope::seal(&bytes, &sshsig::sign(&key, envelope::NAMESPACE, &bytes)).unwrap()
};
forge(&c, "alice", "config", json!(elsewhere)).await;
diff --git a/src/transport/wire.rs b/src/transport/wire.rs
index c0db2e2..97bf6cc 100644
--- a/src/transport/wire.rs
+++ b/src/transport/wire.rs
@@ -14,7 +14,7 @@ use super::{EncodedMessage, MessageKind, ParsedMessage};
/// The wire format's version, the first byte of every payload. Bump it with every change
/// to a message type or a codec (`golden_bytes` fails then).
-pub(crate) const VERSION: u8 = 50;
+pub(crate) const VERSION: u8 = 51;
/// What the second byte of a payload says.
fn tag(kind: MessageKind) -> u8 {