p2p: Convert encoders to ExactSizeEncoder
What changed, and why it matters
This commit is a small internal refactoring in the peer-to-peer networking code. It swaps a generic encoder helper macro for an exact-size version so callers can ask 'how many bytes will this produce?' without actually encoding anything. There is no indication this fixes a bug, let alone a security vulnerability.
No security action required. Treat as a normal API enhancement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change replaces encoding::encoder_newtype! with encoding::encoder_newtype_exact! for four encoders: AddrV1MessageEncoder, AddrV2MessageEncoder, PingEncoder, and PongEncoder. The new macro additionally implements the ExactSizeEncoder trait, exposing a way to compute the encoded byte length up front. The underlying field types and encoding logic are unchanged; this is an API/ergonomics improvement.
Changed components
p2p/src/address.rsp2p/src/message.rsInspect captured patch +4 / −4
diff --git a/p2p/src/address.rs b/p2p/src/address.rs
index eb031a9d..ef145436 100644
--- a/p2p/src/address.rs
+++ b/p2p/src/address.rs
@@ -258,7 +258,7 @@ pub struct AddrV1Message {
pub address: Address,
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder for an [`AddrV1Message`].
#[derive(Debug, Clone)]
pub struct AddrV1MessageEncoder<'e>(Encoder2<ArrayEncoder<4>, AddressEncoder<'e>>);
@@ -881,7 +881,7 @@ impl ToSocketAddrs for AddrV2Message {
}
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder type for an [`AddrV2Message`].
#[derive(Debug, Clone)]
pub struct AddrV2MessageEncoder<'e>(Encoder4<ArrayEncoder<4>, CompactSizeEncoder, AddrV2Encoder<'e>, ArrayEncoder<2>>);
diff --git a/p2p/src/message.rs b/p2p/src/message.rs
index 96b61d23..7ceefe5a 100644
--- a/p2p/src/message.rs
+++ b/p2p/src/message.rs
@@ -749,7 +749,7 @@ impl Ping {
pub fn new(nonce: u64) -> Self { Self(nonce) }
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder for the [`Ping`] type.
#[derive(Debug, Clone)]
pub struct PingEncoder<'e>(encoding::ArrayEncoder<8>);
@@ -825,7 +825,7 @@ impl Pong {
}
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder for the [`Pong`] type.
#[derive(Debug, Clone)]
pub struct PongEncoder<'e>(encoding::ArrayEncoder<8>);
Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.