aboutsummaryrefslogtreecommitdiffstats
path: root/src/transport
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-12-01 02:45:46 +0100
committertv <tv@krebsco.de>2026-12-01 02:45:46 +0100
commit34bfcb661ed74bf40809576bbb388362cb7ffaa1 (patch)
tree8422db7db396dd4ae83403837cd954a94f882a7a /src/transport
parent34434f7fd5a6bab1884a63c96679f48045f4bffd (diff)
parent6a06bc4a955cdf2b5a183c9ff21e25c79a4e5d9f (diff)
Merge remote-tracking branch 'events'HEADmaster
Diffstat (limited to 'src/transport')
-rw-r--r--src/transport/mod.rs14
-rw-r--r--src/transport/traffic.rs5
-rw-r--r--src/transport/wire.rs119
3 files changed, 133 insertions, 5 deletions
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..dda052b 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,24 @@ 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 {
+ // A handshake in a version that says no kind is taken for an Automerge
+ // document's: one of another kind waits for a version that says it.
+ ParsedMessage::Hello(hello) => hello.kind != crate::document::KIND,
+ ParsedMessage::HelloReply(reply) => reply.kind != crate::document::KIND,
+ 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 +280,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 +366,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 +469,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 +819,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),
@@ -1202,7 +1242,16 @@ mod tests {
other => panic!("{other:?}"),
};
assert_eq!(read(&hello("another"), KINDS), "another");
- assert_eq!(read(&hello("another"), KINDS - 1), crate::document::KIND);
+ // The version before says no kind, and one read in it is Automerge's: a
+ // handshake of another kind doesn't go in it.
+ assert_eq!(
+ read(&hello(crate::document::KIND), KINDS - 1),
+ crate::document::KIND
+ );
+ assert!(matches!(
+ encode_in(&hello("another"), KINDS - 1),
+ Err(WireError::OtherKind(_))
+ ));
assert_eq!(
read(&hello(crate::document::KIND), VERSION),
crate::document::KIND
@@ -1497,6 +1546,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 {