diff options
Diffstat (limited to 'src/transport/wire.rs')
| -rw-r--r-- | src/transport/wire.rs | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/src/transport/wire.rs b/src/transport/wire.rs index 0270e6c..abaa408 100644 --- a/src/transport/wire.rs +++ b/src/transport/wire.rs @@ -7,6 +7,11 @@ //! with older nodes to keep. But every change bumps `VERSION`, so nodes on different //! builds tell each other apart instead of misreading each other's messages. The //! transport's topic says which application a message is for, not which version. +//! +//! One message stands outside the versions: SPEAKS (`SPEAKS`), its first byte the +//! version none is, laid out as no build changes, saying which versions a node speaks +//! and the newest revision of the federation it knows. A build from before it reads it +//! as a message of another version, and ignores it. use thiserror::Error; @@ -16,6 +21,19 @@ use super::{EncodedMessage, MessageKind, ParsedMessage}; /// to a message type or a codec (`golden_bytes` fails then). pub(crate) const VERSION: u8 = 52; +/// The oldest version this build speaks: the messages of every version from it to +/// `VERSION` it reads. Its SPEAKS says so. +pub(crate) const OLDEST: u8 = VERSION; + +/// The first byte of a SPEAKS, which no version is. Its layout, never to change: that +/// byte, the oldest version the sender speaks, the newest, then the 32 bytes of the ID +/// of the newest revision of the federation it knows, where it knows one. A reader takes +/// those and leaves whatever follows, which a later build may add. +pub(crate) const SPEAKS: u8 = 0; + +// Whatever this build's versions, a SPEAKS reads as one. +const _: () = assert!(SPEAKS < OLDEST && OLDEST <= VERSION); + /// What the second byte of a payload says. fn tag(kind: MessageKind) -> u8 { match kind { @@ -37,6 +55,8 @@ fn tag(kind: MessageKind) -> u8 { MessageKind::NeighbourReply => 17, MessageKind::Disconnect => 18, MessageKind::Refusals => 20, + // None: a SPEAKS has no second byte (`SPEAKS`). + MessageKind::Speaks => u8::MAX, } } @@ -75,6 +95,11 @@ pub(crate) fn encoded(peer: &str, message: &ParsedMessage) -> anyhow::Result<Enc /// The payload of `message`. pub(crate) fn encode(message: &ParsedMessage) -> Result<Vec<u8>, WireError> { + if let ParsedMessage::Speaks(speaks) = message { + let mut payload = vec![SPEAKS, speaks.oldest, speaks.newest]; + payload.extend(speaks.revision.iter().flatten()); + return Ok(payload); + } let mut payload = vec![VERSION, tag(message.kind())]; match message { ParsedMessage::Hello(hello) => rmp_serde::encode::write(&mut payload, hello)?, @@ -99,6 +124,7 @@ pub(crate) fn encode(message: &ParsedMessage) -> Result<Vec<u8>, WireError> { rmp_serde::encode::write(&mut payload, answer)? } ParsedMessage::Refusals(message) => rmp_serde::encode::write(&mut payload, message)?, + ParsedMessage::Speaks(_) => unreachable!("encoded above"), } Ok(payload) } @@ -138,7 +164,17 @@ impl OtherVersions { pub(crate) fn decode(payload: &[u8]) -> Result<ParsedMessage, WireError> { let (&version, payload) = payload.split_first().ok_or(WireError::Empty)?; - if version != VERSION { + if version == SPEAKS { + let [oldest, newest, rest @ ..] = payload else { + return Err(WireError::Empty); + }; + return Ok(ParsedMessage::Speaks(super::Speaks { + oldest: *oldest, + newest: *newest, + revision: rest.get(..32).map(|id| id.try_into().expect("32 bytes")), + })); + } + if !(OLDEST..=VERSION).contains(&version) { return Err(WireError::Version(version)); } let (&kind, body) = payload.split_first().ok_or(WireError::Empty)?; @@ -729,6 +765,39 @@ mod tests { crate::transport::Hex(bytes).to_string() } + /// A SPEAKS is laid out as no build changes: its version byte, the oldest and newest + /// version, the revision's ID where there is one; what follows is left. + #[test] + fn speaks_keeps_its_layout() { + let speaks = super::super::Speaks { + oldest: 50, + newest: 52, + revision: Some([7; 32]), + }; + let bytes = encode(&ParsedMessage::Speaks(speaks)).unwrap(); + assert_eq!(hex(&bytes), format!("003234{}", "07".repeat(32))); + let without = super::super::Speaks { + revision: None, + ..speaks + }; + assert_eq!( + hex(&encode(&ParsedMessage::Speaks(without)).unwrap()), + "003234" + ); + for (payload, read) in [ + (bytes.clone(), speaks), + ([bytes.as_slice(), b"later"].concat(), speaks), + (vec![0, 50, 52], without), + (vec![0, 50, 52, 1, 2], without), + ] { + match decode(&payload) { + Ok(ParsedMessage::Speaks(decoded)) => assert_eq!(decoded, read), + other => panic!("{other:?}"), + } + } + assert!(decode(&[0, 50]).is_err()); + } + /// The exact bytes of a few messages. 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. #[test] |
