p2p: Implement `encoding` traits for `BlockTransactions`
What changed, and why it matters
This commit adds standard encoding and decoding machinery for a Bitcoin peer-to-peer message type called BlockTransactions. It does not change behavior, fix a bug, or close a security hole; it is a routine implementation of serialization traits used elsewhere in the crate.
No security action required. Review as normal code-quality change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch implements the project’s Encodable and Decodable traits for BlockTransactions in p2p/src/bip152.rs. It introduces BlockTransactionsEncoder, BlockTransactionsDecoder, and BlockTransactionsDecoderError, delegating to existing Encoder2/Decoder2 combinators for a block hash, a compact-size length, and a slice/vector of Transactions. The pre-existing impl_consensus_encoding! macro call remains in place. No bounds checks, resource limits, or parsing logic were altered beyond wiring the new traits.
Changed components
p2p/src/bip152.rsBlockTransactions struct and its encoding/decoding traitsInspect captured patch +82 / −0
diff --git a/p2p/src/bip152.rs b/p2p/src/bip152.rs
index 33de99b6..c2a26909 100644
--- a/p2p/src/bip152.rs
+++ b/p2p/src/bip152.rs
@@ -717,6 +717,88 @@ pub struct BlockTransactions {
/// The transactions provided.
pub transactions: Vec<Transaction>,
}
+
+encoding::encoder_newtype! {
+ /// Encoder type for [`BlockTransactions`].
+ pub struct BlockTransactionsEncoder<'e>(
+ Encoder2<
+ BlockHashEncoder<'e>,
+ Encoder2<CompactSizeEncoder, SliceEncoder<'e, Transaction>>
+ >
+ );
+}
+
+impl encoding::Encodable for BlockTransactions {
+ type Encoder<'e> = BlockTransactionsEncoder<'e>
+ where
+ Self: 'e;
+
+ fn encoder(&self) -> Self::Encoder<'_> {
+ BlockTransactionsEncoder::new(
+ Encoder2::new(
+ self.block_hash.encoder(),
+ Encoder2::new(
+ CompactSizeEncoder::new(self.transactions.len()),
+ SliceEncoder::without_length_prefix(&self.transactions),
+ )
+ )
+ )
+ }
+}
+
+type BlockTransactionsInnerDecoder = Decoder2<BlockHashDecoder, VecDecoder<Transaction>>;
+
+/// Decoder type for a [`BlockTransactions`] message.
+pub struct BlockTransactionsDecoder(BlockTransactionsInnerDecoder);
+
+impl encoding::Decoder for BlockTransactionsDecoder {
+ type Output = BlockTransactions;
+ type Error = BlockTransactionsDecoderError;
+
+ #[inline]
+ fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bool, Self::Error> {
+ self.0.push_bytes(bytes).map_err(BlockTransactionsDecoderError)
+ }
+
+ #[inline]
+ fn end(self) -> Result<Self::Output, Self::Error> {
+ let (block_hash, transactions) = self.0.end().map_err(BlockTransactionsDecoderError)?;
+ Ok(BlockTransactions { block_hash, transactions })
+ }
+
+ #[inline]
+ fn read_limit(&self) -> usize { self.0.read_limit() }
+}
+
+impl encoding::Decodable for BlockTransactions {
+ type Decoder = BlockTransactionsDecoder;
+
+ fn decoder() -> Self::Decoder {
+ BlockTransactionsDecoder(
+ Decoder2::new(BlockHashDecoder::new(), VecDecoder::<Transaction>::new())
+ )
+ }
+}
+
+/// An error occuring decoding a [`BlockTransactions`] message.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct BlockTransactionsDecoderError(<BlockTransactionsInnerDecoder as encoding::Decoder>::Error);
+
+impl From<Infallible> for BlockTransactionsDecoderError {
+ fn from(never: Infallible) -> Self { match never {} }
+}
+
+impl fmt::Display for BlockTransactionsDecoderError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write_err!(f, "blocktxn error"; self.0)
+ }
+}
+
+#[cfg(feature = "std")]
+impl std::error::Error for BlockTransactionsDecoderError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
+}
+
crate::consensus::impl_consensus_encoding!(BlockTransactions, block_hash, transactions);
impl BlockTransactions {
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.