diff options
Diffstat (limited to 'src/transport/wire.rs')
| -rw-r--r-- | src/transport/wire.rs | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/src/transport/wire.rs b/src/transport/wire.rs index 62aa3ca..fcbee85 100644 --- a/src/transport/wire.rs +++ b/src/transport/wire.rs @@ -14,7 +14,7 @@ use super::{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 = 5; +pub(crate) const VERSION: u8 = 6; /// What the second byte of a payload says. fn tag(kind: MessageKind) -> u8 { @@ -27,6 +27,8 @@ fn tag(kind: MessageKind) -> u8 { MessageKind::Blob => 5, MessageKind::Who => 6, MessageKind::Me => 7, + MessageKind::Ping => 8, + MessageKind::Pong => 9, } } @@ -62,6 +64,9 @@ pub(crate) fn encode(message: &ParsedMessage) -> Result<Vec<u8>, WireError> { ParsedMessage::Who(proof) | ParsedMessage::Me(proof) => { rmp_serde::encode::write(&mut payload, proof)? } + ParsedMessage::Ping(nonce) | ParsedMessage::Pong(nonce) => { + rmp_serde::encode::write(&mut payload, nonce)? + } } Ok(payload) } @@ -90,6 +95,10 @@ pub(crate) fn decode(payload: &[u8]) -> Result<ParsedMessage, WireError> { ParsedMessage::Who(rmp_serde::from_slice(body)?) } else if is(MessageKind::Me) { ParsedMessage::Me(rmp_serde::from_slice(body)?) + } else if is(MessageKind::Ping) { + ParsedMessage::Ping(rmp_serde::from_slice(body)?) + } else if is(MessageKind::Pong) { + ParsedMessage::Pong(rmp_serde::from_slice(body)?) } else { return Err(WireError::UnknownKind(kind)); }) @@ -470,7 +479,7 @@ mod tests { }); assert_eq!( hex(&encode(&hello).unwrap()), - format!("050097a5737761726d{epoch_hex}01c403010203c3{}07", run("04")) + format!("060097a5737761726d{epoch_hex}01c403010203c3{}07", run("04")) ); let reply = ParsedMessage::HelloReply(HelloReply { @@ -490,7 +499,7 @@ mod tests { assert_eq!( hex(&encode(&reply).unwrap()), format!( - "050199a5737761726d9192a17091a161{epoch_hex}01c400c2{}05{}", + "060199a5737761726d9192a17091a161{epoch_hex}01c400c2{}05{}", run("04"), run("06") ) @@ -505,7 +514,7 @@ mod tests { }); assert_eq!( hex(&encode(&status).unwrap()), - format!("0503959290c400{epoch_hex}01c3c2") + format!("0603959290c400{epoch_hex}01c3c2") ); let who = ParsedMessage::Who(IdentityProof { @@ -514,7 +523,7 @@ mod tests { }); assert_eq!( hex(&encode(&who).unwrap()), - format!("050692c420{}c440{}", "08".repeat(32), "09".repeat(64)) + format!("060692c420{}c440{}", "08".repeat(32), "09".repeat(64)) ); let ParsedMessage::Me(proof) = decode( &encode(&ParsedMessage::Me(IdentityProof { @@ -527,5 +536,15 @@ mod tests { panic!("not a ME"); }; assert_eq!((proof.key, proof.signature), ([8; 32], [9; 64])); + + assert_eq!(hex(&encode(&ParsedMessage::Ping(7)).unwrap()), "060807"); + assert_eq!( + hex(&encode(&ParsedMessage::Pong(u64::MAX)).unwrap()), + "0609cfffffffffffffffff" + ); + let ParsedMessage::Pong(nonce) = decode(&[VERSION, 9, 0x2a]).unwrap() else { + panic!("not a PONG"); + }; + assert_eq!(nonce, 42); } } |
