p2p: Remove legacy encoding from `message_blockdata`
What changed, and why it matters
This commit removes old-style encoding and decoding code for several Bitcoin peer-to-peer message types and replaces it with a newer, generated encoding system. There is no direct evidence in the commit or supplied references that this fixes a security vulnerability; it appears to be a routine refactoring or cleanup of legacy code.
No immediate security action required. Treat as normal code maintenance. If deploying a build that includes this commit, run the project's existing P2P message serialization tests to confirm the new encoders produce wire-compatible output for the removed legacy paths.
Security signals we found
No security-relevant keywords in commit title or message
No functional bug fix or input validation change visible in diff
Change is a refactoring from legacy consensus encoding traits to new generated encoders
No references to CVEs, advisories, researchers, or security issues in commit materials
Evidence from the diff
The diff deletes hand-written Encodable/Decodable implementations for Inventory and BlockLocator, plus impl_consensus_encoding! macro invocations for GetBlocksMessage and GetHeadersMessage in p2p/src/message_blockdata.rs. The file already contains newer encoding-crate-based encoders/decoders for the same types. The change is purely subtractive and migrates these types to the new encoding framework. No bug, bounds-check, or logic correction is visible in the removed or remaining code.
Changed components
p2p/src/message_blockdata.rsInventory encoding/decodingBlockLocator encoding/decodingGetBlocksMessage encoding/decodingGetHeadersMessage encoding/decodingInspect captured patch +0 / −59
diff --git a/p2p/src/message_blockdata.rs b/p2p/src/message_blockdata.rs
index 0e51b783..d9f28f8a 100644
--- a/p2p/src/message_blockdata.rs
+++ b/p2p/src/message_blockdata.rs
@@ -9,17 +9,14 @@ use alloc::vec::Vec;
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
-use bitcoin::consensus::encode::{self, Decodable, Encodable};
use encoding::{
ArrayDecoder, ArrayEncoder, CompactSizeEncoder, Decoder2, Decoder3, Encoder2, Encoder3,
SliceEncoder, VecDecoder,
};
-use io::{BufRead, Write};
use primitives::block::{BlockHashDecoder, BlockHashEncoder};
use primitives::transaction::{Txid, Wtxid};
use primitives::BlockHash;
-use crate::consensus::impl_consensus_encoding;
use crate::{ProtocolVersion, ProtocolVersionDecoder, ProtocolVersionEncoder};
#[rustfmt::skip] // Keep public re-exports separate.
@@ -74,44 +71,6 @@ impl Inventory {
}
}
-impl Encodable for Inventory {
- #[inline]
- fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
- macro_rules! encode_inv {
- ($code:expr, $item:expr) => {
- u32::consensus_encode(&$code, w)? + $item.consensus_encode(w)?
- };
- }
- Ok(match *self {
- Self::Error(ref e) => encode_inv!(0, e),
- Self::Transaction(ref t) => encode_inv!(1, t),
- Self::Block(ref b) => encode_inv!(2, b),
- Self::CompactBlock(ref b) => encode_inv!(4, b),
- Self::WTx(ref w) => encode_inv!(5, w),
- Self::WitnessTransaction(ref t) => encode_inv!(0x4000_0001, t),
- Self::WitnessBlock(ref b) => encode_inv!(0x4000_0002, b),
- Self::Unknown { inv_type: t, hash: ref d } => encode_inv!(t, d),
- })
- }
-}
-
-impl Decodable for Inventory {
- #[inline]
- fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
- let inv_type: u32 = Decodable::consensus_decode(r)?;
- Ok(match inv_type {
- 0 => Self::Error(Decodable::consensus_decode(r)?),
- 1 => Self::Transaction(Decodable::consensus_decode(r)?),
- 2 => Self::Block(Decodable::consensus_decode(r)?),
- 4 => Self::CompactBlock(Decodable::consensus_decode(r)?),
- 5 => Self::WTx(Decodable::consensus_decode(r)?),
- 0x4000_0001 => Self::WitnessTransaction(Decodable::consensus_decode(r)?),
- 0x4000_0002 => Self::WitnessBlock(Decodable::consensus_decode(r)?),
- tp => Self::Unknown { inv_type: tp, hash: Decodable::consensus_decode(r)? },
- })
- }
-}
-
encoding::encoder_newtype_exact! {
/// The encoder for the [`Inventory`] type.
#[derive(Debug, Clone)]
@@ -233,20 +192,6 @@ impl From<BlockLocator> for Vec<BlockHash> {
fn from(locator: BlockLocator) -> Self { locator.0 }
}
-impl Encodable for BlockLocator {
- #[inline]
- fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
- self.0.consensus_encode(w)
- }
-}
-
-impl Decodable for BlockLocator {
- #[inline]
- fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
- Ok(Self(Decodable::consensus_decode(r)?))
- }
-}
-
type BlockLocatorInnerEncoder<'e> = Encoder2<CompactSizeEncoder, SliceEncoder<'e, BlockHash>>;
encoding::encoder_newtype! {
@@ -446,10 +391,6 @@ impl encoding::Decode for GetHeadersMessage {
}
}
-impl_consensus_encoding!(GetBlocksMessage, version, locator_hashes, stop_hash);
-
-impl_consensus_encoding!(GetHeadersMessage, version, locator_hashes, stop_hash);
-
/// Error types for blockdata messages.
pub mod error {
use core::convert::Infallible;
Why this scored 12/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.