p2p: Convert encoders to ExactSizeEncoder
What changed, and why it matters
This is a routine internal code cleanup in the rust-bitcoin peer-to-peer networking module. It swaps a general-purpose macro for a more specific one when building message encoders, because the newer macro can do everything the old one does and more. There is no user-facing behavior change and no security fix.
No security action required. Treat as a normal refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit replaces encoding::encoder_newtype! with encoding::encoder_newtype_exact! for 22 encoder definitions across the p2p crate. The commit message states that encoder_newtype_exact is a strict superset of encoder_newtype and should be preferred where the wrapped encoder has a known exact size. The diff is purely mechanical: every changed line is the macro name substitution, with no logic, bounds checking, or wire-format changes.
Changed components
p2p/src/address.rsp2p/src/bip152.rsp2p/src/lib.rsp2p/src/message.rsp2p/src/message_blockdata.rsp2p/src/message_bloom.rsp2p/src/message_compact_blocks.rsp2p/src/message_filter.rsp2p/src/message_network.rsInspect captured patch +22 / −22
diff --git a/p2p/src/address.rs b/p2p/src/address.rs
index 0e386f5a..5a8248f9 100644
--- a/p2p/src/address.rs
+++ b/p2p/src/address.rs
@@ -154,7 +154,7 @@ impl ToSocketAddrs for Address {
}
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder for the [`Address`] type.
pub struct AddressEncoder<'e>(encoding::Encoder3<
crate::ServiceFlagsEncoder<'e>,
diff --git a/p2p/src/bip152.rs b/p2p/src/bip152.rs
index 9073a532..c202b6a1 100644
--- a/p2p/src/bip152.rs
+++ b/p2p/src/bip152.rs
@@ -278,7 +278,7 @@ impl Decodable for ShortId {
}
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// Encoder type for a [`ShortId`].
pub struct ShortIdEncoder<'e>(ArrayEncoder<6>);
}
diff --git a/p2p/src/lib.rs b/p2p/src/lib.rs
index 12426883..49079462 100644
--- a/p2p/src/lib.rs
+++ b/p2p/src/lib.rs
@@ -117,7 +117,7 @@ impl Decodable for ProtocolVersion {
}
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder for the [`ProtocolVersion`] type.
pub struct ProtocolVersionEncoder<'e>(encoding::ArrayEncoder<4>);
}
@@ -342,7 +342,7 @@ impl Decodable for ServiceFlags {
}
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder for the [`ServiceFlags`] type.
pub struct ServiceFlagsEncoder<'e>(encoding::ArrayEncoder<8>);
}
diff --git a/p2p/src/message.rs b/p2p/src/message.rs
index 6ce636ac..d906d008 100644
--- a/p2p/src/message.rs
+++ b/p2p/src/message.rs
@@ -304,7 +304,7 @@ impl encoding::Encodable for V1MessageHeader {
}
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder for the [`V1MessageHeader`] type.
pub struct V1MessageHeaderEncoder<'e>(
encoding::Encoder4<
@@ -1510,7 +1510,7 @@ impl NetworkHeader {
pub const fn from_header(header: block::Header) -> Self { Self { header, length: 0 } }
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder type for a [`NetworkHeader`].
pub struct NetworkHeaderEncoder<'e>(Encoder2<HeaderEncoder<'e>, ArrayEncoder<1>>);
}
diff --git a/p2p/src/message_blockdata.rs b/p2p/src/message_blockdata.rs
index 46535213..70cdf528 100644
--- a/p2p/src/message_blockdata.rs
+++ b/p2p/src/message_blockdata.rs
@@ -108,7 +108,7 @@ impl Decodable for Inventory {
}
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder for the [`Inventory`] type.
pub struct InventoryEncoder<'e>(Encoder2<ArrayEncoder<4>, ArrayEncoder<32>>);
}
diff --git a/p2p/src/message_bloom.rs b/p2p/src/message_bloom.rs
index 1b974ed7..060ab68c 100644
--- a/p2p/src/message_bloom.rs
+++ b/p2p/src/message_bloom.rs
@@ -33,7 +33,7 @@ pub struct FilterLoad {
pub flags: BloomFlags,
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder for the [`FilterLoad`] message.
pub struct FilterLoadEncoder<'e>(
Encoder2<
@@ -140,7 +140,7 @@ pub enum BloomFlags {
PubkeyOnly,
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder for [`BloomFlags`].
pub struct BloomFlagsEncoder<'e>(ArrayEncoder<1>);
}
@@ -262,7 +262,7 @@ pub struct FilterAdd {
pub data: Vec<u8>,
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder of the [`FilterAdd`] message.
pub struct FilterAddEncoder<'e>(Encoder2<CompactSizeEncoder, BytesEncoder<'e>>);
}
diff --git a/p2p/src/message_compact_blocks.rs b/p2p/src/message_compact_blocks.rs
index 227faab3..92e52c9a 100644
--- a/p2p/src/message_compact_blocks.rs
+++ b/p2p/src/message_compact_blocks.rs
@@ -22,7 +22,7 @@ pub struct SendCmpct {
pub version: u64,
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// Encoder type for the [`SendCmpct`] message.
pub struct SendCmpctEncoder<'e>(Encoder2<ArrayEncoder<1>, ArrayEncoder<8>>);
}
diff --git a/p2p/src/message_filter.rs b/p2p/src/message_filter.rs
index ad3f5c5f..94134ff4 100644
--- a/p2p/src/message_filter.rs
+++ b/p2p/src/message_filter.rs
@@ -64,7 +64,7 @@ macro_rules! impl_hashencode {
impl_hashencode!(FilterHash);
impl_hashencode!(FilterHeader);
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// Encoder type for [`FilterHash`].
pub struct FilterHashEncoder<'e>(ArrayEncoder<32>);
}
@@ -77,7 +77,7 @@ impl encoding::Encodable for FilterHash {
}
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// Encoder type for [`FilterHeader`].
pub struct FilterHeaderEncoder<'e>(ArrayEncoder<32>);
}
@@ -211,7 +211,7 @@ pub struct GetCFilters {
pub stop_hash: BlockHash,
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// Encoder type for the [`GetCFilters`] message.
pub struct GetCFiltersEncoder<'e>(Encoder3<ArrayEncoder<1>, BlockHeightEncoder<'e>, BlockHashEncoder<'e>>);
}
@@ -296,7 +296,7 @@ pub struct CFilter {
pub filter: Vec<u8>,
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// Encoder type for a [`CFilter`] message.
pub struct CFilterEncoder<'e>(
Encoder3<
@@ -393,7 +393,7 @@ pub struct GetCFHeaders {
pub stop_hash: BlockHash,
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// Encoder type for the [`GetCFHeaders`] message.
pub struct GetCFHeadersEncoder<'e>(Encoder3<ArrayEncoder<1>, BlockHeightEncoder<'e>, BlockHashEncoder<'e>>);
}
@@ -581,7 +581,7 @@ pub struct GetCFCheckpt {
pub stop_hash: BlockHash,
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// Encoder type for the [`GetCFCheckpt`] message.
pub struct GetCFCheckptEncoder<'e>(Encoder2<ArrayEncoder<1>, BlockHashEncoder<'e>>);
}
diff --git a/p2p/src/message_network.rs b/p2p/src/message_network.rs
index 2a45b46d..02bc04d3 100644
--- a/p2p/src/message_network.rs
+++ b/p2p/src/message_network.rs
@@ -88,7 +88,7 @@ impl VersionMessage {
}
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder for the [`VersionMessage`] type.
pub struct VersionMessageEncoder<'e>(
encoding::Encoder2<
@@ -252,7 +252,7 @@ pub struct UserAgent {
user_agent: String,
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder for a [`UserAgent`] string.
pub struct UserAgentEncoder<'e>(Encoder2<CompactSizeEncoder, BytesEncoder<'e>>);
}
@@ -498,7 +498,7 @@ pub enum RejectReason {
Checkpoint = 0x43,
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder type for a [`RejectReason`].
pub struct RejectReasonEncoder<'e>(ArrayEncoder<1>);
}
@@ -618,7 +618,7 @@ pub struct Reject {
pub hash: sha256d::Hash,
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder type for a [`Reject`] message.
pub struct RejectEncoder<'e>(
Encoder4<
@@ -751,7 +751,7 @@ impl Alert {
pub fn is_final_alert(&self) -> bool { self.0.eq(&Self::FINAL_ALERT) }
}
-encoding::encoder_newtype! {
+encoding::encoder_newtype_exact! {
/// The encoder type for an [`Alert`] message.
pub struct AlertEncoder<'e>(Encoder2<CompactSizeEncoder, BytesEncoder<'e>>);
}
Why this scored 15/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.