From 2b6ef0afcf97e3783022ea9b995031cc87468977 Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Sat, 3 Dec 2022 00:24:58 -0500 Subject: [PATCH] fix(eth-wire): encode p2p message id as valid rlp (#324) --- crates/net/eth-wire/src/p2pstream.rs | 5 +++-- crates/primitives/src/hex_bytes.rs | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/net/eth-wire/src/p2pstream.rs b/crates/net/eth-wire/src/p2pstream.rs index 8c08b8975b..312863af12 100644 --- a/crates/net/eth-wire/src/p2pstream.rs +++ b/crates/net/eth-wire/src/p2pstream.rs @@ -549,7 +549,7 @@ impl P2PMessage { /// compressed in the `p2p` subprotocol. impl Encodable for P2PMessage { fn encode(&self, out: &mut dyn bytes::BufMut) { - out.put_u8(self.message_id() as u8); + (self.message_id() as u8).encode(out); match self { P2PMessage::Hello(msg) => msg.encode(out), P2PMessage::Disconnect(msg) => msg.encode(out), @@ -926,6 +926,7 @@ mod tests { let mut hello_encoded = Vec::new(); hello.encode(&mut hello_encoded); - assert_eq!(hello_encoded[0], P2PMessageID::Hello as u8); + // zero is encoded as 0x80, the empty string code in RLP + assert_eq!(hello_encoded[0], EMPTY_STRING_CODE); } } diff --git a/crates/primitives/src/hex_bytes.rs b/crates/primitives/src/hex_bytes.rs index 633fa5019e..efba227f10 100644 --- a/crates/primitives/src/hex_bytes.rs +++ b/crates/primitives/src/hex_bytes.rs @@ -169,7 +169,7 @@ impl FromStr for Bytes { hex::decode(value) } .map(Into::into) - .map_err(|e| ParseBytesError(format!("Invalid hex: {}", e))) + .map_err(|e| ParseBytesError(format!("Invalid hex: {e}"))) } }