aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-29 15:06:04 +0000
committertv <tv@krebsco.de>2026-11-29 15:06:04 +0000
commit8cdde74fcdcf31414da0d22222d2cc338df20fa1 (patch)
tree37911374599ed825fa3264c8e99ae830cec843fc /src
parenta3af68fafee80b3eef493e8b041e21352e547da2 (diff)
wire: what another kind of document says, in a SYNC's body and a STATUS's stamp
From version 53 on, beside Automerge's (SyncBody::Kind, HeadsSummary::Kind): a SYNC's body is one more of its shapes, another kind's bytes; a STATUS's stamp is another kind's bytes after a byte no Automerge stamp begins with. Nothing version 53 said changes, and version 52 says neither (WireError::OtherKind). Two stamps of another kind are the same where their bytes are. A node holding an Automerge document passes over a body of another kind: a peer holding one isn't let near it.
Diffstat (limited to 'src')
-rw-r--r--src/document/passing.rs1
-rw-r--r--src/swarm/sync.rs3
-rw-r--r--src/transport/mod.rs14
-rw-r--r--src/transport/traffic.rs5
-rw-r--r--src/transport/wire.rs104
5 files changed, 123 insertions, 4 deletions
diff --git a/src/document/passing.rs b/src/document/passing.rs
index 0b9a3dd..dc116e3 100644
--- a/src/document/passing.rs
+++ b/src/document/passing.rs
@@ -194,6 +194,7 @@ pub(crate) fn carried_bytes(body: &SyncBody) -> Option<usize> {
SyncBody::Message(payload) | SyncBody::Deflated(payload) => Some(payload.0.len()),
SyncBody::Changes { changes, .. } => Some(changes.iter().map(|change| change.len()).sum()),
SyncBody::Delta(_) => None,
+ SyncBody::Kind(bytes) => Some(bytes.len()),
}
}
diff --git a/src/swarm/sync.rs b/src/swarm/sync.rs
index 65a02ea..c953752 100644
--- a/src/swarm/sync.rs
+++ b/src/swarm/sync.rs
@@ -405,6 +405,9 @@ pub(crate) async fn handle_sync(
// Changes pushed along a tree (`SyncBody::Changes`): their heads and bytes, made
// into the Automerge message that would have brought them (`Received::pushed`).
let (payload, deflated, pushed) = match envelope.body {
+ // Of a document of another kind: none this node holds, which a peer holding
+ // one isn't let near (`peer::inbound`).
+ transport::SyncBody::Kind(_) => return Ok(()),
transport::SyncBody::Message(payload) => (payload, false, None),
transport::SyncBody::Deflated(payload) => (payload, true, None),
transport::SyncBody::Changes { heads, changes } => {
diff --git a/src/transport/mod.rs b/src/transport/mod.rs
index 61abe9c..4189890 100644
--- a/src/transport/mod.rs
+++ b/src/transport/mod.rs
@@ -114,6 +114,9 @@ pub(crate) enum HeadsSummary {
/// of starting the sync over to find out what it lacks.
sample: Vec<ChangeHash>,
},
+ /// Where a document of another kind than Automerge's stands, as that kind's bytes
+ /// (`docs/document.md`, Passing on): neither heads nor a digest of any.
+ Kind(Vec<u8>),
}
/// Where the next sample starts (`HeadsSummary::of`).
@@ -144,6 +147,7 @@ impl HeadsSummary {
match self {
Self::Heads(heads) => digest_of(heads),
Self::Digest { digest, .. } => *digest,
+ Self::Kind(bytes) => Sha256::digest(bytes).into(),
}
}
@@ -151,7 +155,7 @@ impl HeadsSummary {
pub(crate) fn heads(&self) -> Option<&[ChangeHash]> {
match self {
Self::Heads(heads) => Some(heads),
- Self::Digest { .. } => None,
+ Self::Digest { .. } | Self::Kind(_) => None,
}
}
@@ -160,6 +164,7 @@ impl HeadsSummary {
match self {
Self::Heads(heads) => heads,
Self::Digest { sample, .. } => sample,
+ Self::Kind(_) => &[],
}
}
}
@@ -845,6 +850,9 @@ pub(crate) enum SyncBody {
/// Instead of a message too large to send in one piece: fetch these changes as a
/// blob (`BlobId::Delta`), then sync on.
Delta(DeltaSpec),
+ /// What a document of another kind than Automerge's says, as that kind's bytes
+ /// (`docs/document.md`, Passing on).
+ Kind(#[serde(with = "serde_bytes")] Vec<u8>),
}
/// The changes in the history of `to` but not of `from`: what a peer at `from` lacks of
@@ -934,6 +942,10 @@ impl std::fmt::Debug for SyncBody {
)
.finish(),
Self::Delta(spec) => f.debug_tuple("Delta").field(spec).finish(),
+ Self::Kind(bytes) => f
+ .debug_tuple("Kind")
+ .field(&format!("{} bytes", bytes.len()))
+ .finish(),
}
}
}
diff --git a/src/transport/traffic.rs b/src/transport/traffic.rs
index 33903ca..01bba65 100644
--- a/src/transport/traffic.rs
+++ b/src/transport/traffic.rs
@@ -225,7 +225,10 @@ fn related(peer: &str, message: &ParsedMessage) -> Option<String> {
SyncBody::Delta(spec) => {
format!("blob {}", crate::transport::BlobId::Delta(spec.id()))
}
- SyncBody::Message(_) | SyncBody::Deflated(_) | SyncBody::Changes { .. } => {
+ SyncBody::Message(_)
+ | SyncBody::Deflated(_)
+ | SyncBody::Changes { .. }
+ | SyncBody::Kind(_) => {
return None;
}
},
diff --git a/src/transport/wire.rs b/src/transport/wire.rs
index 79532fb..54efd0f 100644
--- a/src/transport/wire.rs
+++ b/src/transport/wire.rs
@@ -89,6 +89,9 @@ pub(crate) enum WireError {
#[error("wire format version {0} (we speak version {VERSION})")]
Version(u8),
+ #[error("a document of another kind than Automerge's, which wire version {0} doesn't say")]
+ OtherKind(u8),
+
#[error("unknown message kind {0}")]
UnknownKind(u8),
@@ -139,6 +142,9 @@ pub(crate) fn encode_in(message: &ParsedMessage, version: u8) -> Result<Vec<u8>,
if !(OLDEST..=VERSION).contains(&version) && !proof {
return Err(WireError::Version(version));
}
+ if version < KINDS && of_another_kind(message) {
+ return Err(WireError::OtherKind(version));
+ }
let mut payload = vec![version, tag(message.kind())];
// From `KINDS` on, what is a document's kind's goes as bytes (`kinds`); before, as
// the messages' own types have it, which are Automerge's.
@@ -173,6 +179,20 @@ pub(crate) fn encode_in(message: &ParsedMessage, version: u8) -> Result<Vec<u8>,
Ok(payload)
}
+/// Whether `message` carries what a document of another kind than Automerge's says,
+/// which only the versions from `KINDS` on carry.
+fn of_another_kind(message: &ParsedMessage) -> bool {
+ match message {
+ ParsedMessage::Sync(envelope) => {
+ matches!(envelope.body, crate::transport::SyncBody::Kind(_))
+ }
+ ParsedMessage::Status(status) | ParsedMessage::StatusReply(status) => {
+ matches!(status.heads, crate::transport::HeadsSummary::Kind(_))
+ }
+ _ => false,
+ }
+}
+
/// The payload of `message`, in the newest wire version.
#[cfg(test)]
pub(crate) fn encode(message: &ParsedMessage) -> Result<Vec<u8>, WireError> {
@@ -256,6 +276,10 @@ mod kinds {
round_trips: Option<Vec<u8>>,
}
+ /// What the stamp of a document of another kind than Automerge's begins with: an
+ /// Automerge stamp is a MessagePack array, which begins otherwise.
+ const OTHER_STAMP: u8 = 0;
+
/// Where an Automerge document stands, as its bytes have it.
struct StampOut<'a>(&'a HeadsSummary);
@@ -338,7 +362,11 @@ mod kinds {
)?
}
ParsedMessage::Status(status) | ParsedMessage::StatusReply(status) => {
- let stamp = rmp_serde::to_vec(&StampOut(&status.heads))?;
+ let stamp = match &status.heads {
+ // Another kind's, after a byte that begins no Automerge stamp.
+ HeadsSummary::Kind(bytes) => [&[OTHER_STAMP], bytes.as_slice()].concat(),
+ heads => rmp_serde::to_vec(&StampOut(heads))?,
+ };
rmp_serde::encode::write(
payload,
&StatusOut {
@@ -437,7 +465,10 @@ mod kinds {
/// The STATUS, or the STATUS_REPLY, in `body`.
pub(super) fn status(body: &[u8]) -> Result<Status, WireError> {
let status: StatusIn = rmp_serde::from_slice(body)?;
- let StampIn(heads) = rmp_serde::from_slice(&status.stamp)?;
+ let heads = match status.stamp.split_first() {
+ Some((&OTHER_STAMP, bytes)) => HeadsSummary::Kind(bytes.to_vec()),
+ _ => rmp_serde::from_slice::<StampIn>(&status.stamp)?.0,
+ };
Ok(Status {
heads,
epoch: status.epoch,
@@ -784,6 +815,11 @@ pub(crate) mod codec {
heads.iter().map(|head| Bytes::new(&head.0)).collect(),
Bytes::new(&[]),
),
+ HeadsSummary::Kind(_) => {
+ return Err(serde::ser::Error::custom(
+ "where a document of another kind stands, which heads don't say",
+ ));
+ }
HeadsSummary::Digest { digest, sample } => (
sample.iter().map(|head| Bytes::new(&head.0)).collect(),
Bytes::new(digest),
@@ -1497,6 +1533,70 @@ mod tests {
/// The exact bytes of the messages that carry what is a document's kind's, from
/// `KINDS` on (`kinds`). If this fails after a change to a message type or a codec,
/// the change is a new wire format: bump `VERSION` and take the new bytes.
+ /// What a document of another kind than Automerge's says goes where Automerge's
+ /// does, from the version that carries a kind's bytes on; the version before
+ /// doesn't say it.
+ #[test]
+ fn another_kinds_bytes_go_in_sync_and_status() {
+ let epoch = Epoch {
+ generation: 0,
+ id: [0; 32],
+ };
+ let status = ParsedMessage::Status(Status {
+ heads: HeadsSummary::Kind(vec![0xaa; 4]),
+ epoch,
+ epoch_seq: 0,
+ ready: true,
+ syncs_with_you: true,
+ leaving: false,
+ announcing: false,
+ reaches: None,
+ round_trips: None,
+ });
+ let sync = ParsedMessage::Sync(crate::transport::SyncEnvelope {
+ epoch: crate::transport::EpochTag::of(epoch),
+ epoch_seq: 0,
+ body: crate::transport::SyncBody::Kind(vec![5, 6]),
+ ready: true,
+ writer: None,
+ delivered: false,
+ sent: crate::clock::UnixMillis(8),
+ signatures: serde_bytes::ByteBuf::new(),
+ });
+ let status_bytes = encode_in(&status, KINDS).unwrap();
+ let sync_bytes = encode_in(&sync, KINDS).unwrap();
+ assert_eq!(
+ hex(&status_bytes),
+ "350399c40500aaaaaaaa9200c420000000000000000000000000000000000000000000000000000000000000000000c3c3c2c2c0c0"
+ );
+ assert_eq!(
+ hex(&sync_bytes),
+ "3502979200c408000000000000000000c40c9281a44b696e64c4020506c2c3c008c400"
+ );
+ let ParsedMessage::Status(read) = decode(&status_bytes).unwrap() else {
+ panic!("no status");
+ };
+ assert!(matches!(read.heads, HeadsSummary::Kind(bytes) if bytes == vec![0xaa; 4]));
+ let ParsedMessage::Sync(read) = decode(&sync_bytes).unwrap() else {
+ panic!("no sync");
+ };
+ assert!(
+ matches!(read.body, crate::transport::SyncBody::Kind(bytes) if bytes == vec![5, 6])
+ );
+ assert!(!read.delivered);
+ // Two stamps of another kind are the same where their bytes are.
+ assert_eq!(HeadsSummary::Kind(vec![1]), HeadsSummary::Kind(vec![1]));
+ assert_ne!(HeadsSummary::Kind(vec![1]), HeadsSummary::Kind(vec![2]));
+ assert!(matches!(
+ encode_in(&status, 52),
+ Err(WireError::OtherKind(52))
+ ));
+ assert!(matches!(
+ encode_in(&sync, 52),
+ Err(WireError::OtherKind(52))
+ ));
+ }
+
#[test]
fn golden_bytes_of_kinds() {
let epoch = Epoch {