p2p: Move message_blockdata module errors to error submodule
What changed, and why it matters
This commit is a pure code reorganization: it moves several error type definitions from the main message_blockdata module into a new error submodule and re-exports them. There are no functional changes to how errors are created, handled, or displayed, and no security behavior changes.
No security action needed. Treat as routine refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors rust-bitcoin’s p2p message_blockdata module by relocating InventoryDecoderError, BlockLocatorDecoderError, GetBlocksMessageDecoderError, and GetHeadersMessageDecoderError into a new pub mod error block. It adds pub(super) visibility to the inner tuple fields (previously private by default in the same module), and uses #[doc(no_inline)] re-exports. No logic, trait implementations, error messages, or public API semantics are altered.
Changed components
p2p/src/message_blockdata.rsInspect captured patch +99 / −75
diff --git a/p2p/src/message_blockdata.rs b/p2p/src/message_blockdata.rs
index e0935dba..2e4fc7eb 100644
--- a/p2p/src/message_blockdata.rs
+++ b/p2p/src/message_blockdata.rs
@@ -6,8 +6,6 @@
//! Bitcoin data (blocks and transactions) around.
use alloc::vec::Vec;
-use core::convert::Infallible;
-use core::fmt;
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
@@ -16,7 +14,6 @@ use encoding::{
ArrayDecoder, ArrayEncoder, CompactSizeEncoder, Decoder2, Decoder3, Encoder2, Encoder3,
SliceEncoder, VecDecoder,
};
-use internals::write_err;
use io::{BufRead, Write};
use primitives::block::{BlockHashDecoder, BlockHashEncoder};
use primitives::transaction::{Txid, Wtxid};
@@ -25,6 +22,13 @@ use primitives::BlockHash;
use crate::consensus::impl_consensus_encoding;
use crate::{ProtocolVersion, ProtocolVersionDecoder, ProtocolVersionEncoder};
+#[rustfmt::skip] // Keep public re-exports separate.
+#[doc(no_inline)]
+pub use self::error::{
+ BlockLocatorDecoderError, GetBlocksMessageDecoderError, GetHeadersMessageDecoderError,
+ InventoryDecoderError,
+};
+
/// An inventory item.
#[derive(PartialEq, Eq, Clone, Debug, Copy, Hash, PartialOrd, Ord)]
pub enum Inventory {
@@ -177,25 +181,6 @@ impl encoding::Decodable for Inventory {
}
}
-/// An error consensus decoding an [`Inventory`].
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct InventoryDecoderError(<InventoryInnerDecoder as encoding::Decoder>::Error);
-
-impl From<Infallible> for InventoryDecoderError {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
-impl fmt::Display for InventoryDecoderError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write_err!(f, "inventory error"; self.0)
- }
-}
-
-#[cfg(feature = "std")]
-impl std::error::Error for InventoryDecoderError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
-}
-
/// A block locator.
///
/// Maximum number of hashes in a block locator, matching Bitcoin Core's `MAX_LOCATOR_SZ`.
@@ -319,25 +304,6 @@ impl encoding::Decodable for BlockLocator {
fn decoder() -> Self::Decoder { BlockLocatorDecoder::new() }
}
-/// An error consensus decoding a [`BlockLocator`].
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct BlockLocatorDecoderError(<BlockLocatorInnerDecoder as encoding::Decoder>::Error);
-
-impl From<Infallible> for BlockLocatorDecoderError {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
-impl fmt::Display for BlockLocatorDecoderError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write_err!(f, "block locator error"; self.0)
- }
-}
-
-#[cfg(feature = "std")]
-impl std::error::Error for BlockLocatorDecoderError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
-}
-
// Some simple messages
/// The `getblocks` message
@@ -480,51 +446,109 @@ impl encoding::Decodable for GetHeadersMessage {
}
}
-/// An error consensus decoding a [`GetBlocksMessage`].
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct GetBlocksMessageDecoderError(
- <GetBlocksOrHeadersInnerDecoder as encoding::Decoder>::Error,
-);
+impl_consensus_encoding!(GetBlocksMessage, version, locator_hashes, stop_hash);
-impl From<Infallible> for GetBlocksMessageDecoderError {
- fn from(never: Infallible) -> Self { match never {} }
-}
+impl_consensus_encoding!(GetHeadersMessage, version, locator_hashes, stop_hash);
-impl fmt::Display for GetBlocksMessageDecoderError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write_err!(f, "getblocks decoder error"; self.0)
+/// Error types for blockdata messages.
+pub mod error {
+ use core::convert::Infallible;
+ use core::fmt;
+
+ use internals::write_err;
+
+ /// An error consensus decoding an [`Inventory`].
+ ///
+ /// [`Inventory`]: super::Inventory
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ pub struct InventoryDecoderError(
+ pub(super) <super::InventoryInnerDecoder as encoding::Decoder>::Error,
+ );
+
+ impl From<Infallible> for InventoryDecoderError {
+ fn from(never: Infallible) -> Self { match never {} }
}
-}
-#[cfg(feature = "std")]
-impl std::error::Error for GetBlocksMessageDecoderError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
-}
+ impl fmt::Display for InventoryDecoderError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write_err!(f, "inventory error"; self.0)
+ }
+ }
-/// An error consensus decoding a [`GetHeadersMessage`].
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct GetHeadersMessageDecoderError(
- <GetBlocksOrHeadersInnerDecoder as encoding::Decoder>::Error,
-);
+ #[cfg(feature = "std")]
+ impl std::error::Error for InventoryDecoderError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
+ }
-impl From<Infallible> for GetHeadersMessageDecoderError {
- fn from(never: Infallible) -> Self { match never {} }
-}
+ /// An error consensus decoding a [`BlockLocator`].
+ ///
+ /// [`BlockLocator`]: super::BlockLocator
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ pub struct BlockLocatorDecoderError(
+ pub(super) <super::BlockLocatorInnerDecoder as encoding::Decoder>::Error,
+ );
+
+ impl From<Infallible> for BlockLocatorDecoderError {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
-impl fmt::Display for GetHeadersMessageDecoderError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write_err!(f, "getheaders decoder error"; self.0)
+ impl fmt::Display for BlockLocatorDecoderError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write_err!(f, "block locator error"; self.0)
+ }
}
-}
-#[cfg(feature = "std")]
-impl std::error::Error for GetHeadersMessageDecoderError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
-}
+ #[cfg(feature = "std")]
+ impl std::error::Error for BlockLocatorDecoderError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
+ }
-impl_consensus_encoding!(GetBlocksMessage, version, locator_hashes, stop_hash);
+ /// An error consensus decoding a [`GetBlocksMessage`].
+ ///
+ /// [`GetBlocksMessage`]: super::GetBlocksMessage
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ pub struct GetBlocksMessageDecoderError(
+ pub(super) <super::GetBlocksOrHeadersInnerDecoder as encoding::Decoder>::Error,
+ );
+
+ impl From<Infallible> for GetBlocksMessageDecoderError {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
-impl_consensus_encoding!(GetHeadersMessage, version, locator_hashes, stop_hash);
+ impl fmt::Display for GetBlocksMessageDecoderError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write_err!(f, "getblocks decoder error"; self.0)
+ }
+ }
+
+ #[cfg(feature = "std")]
+ impl std::error::Error for GetBlocksMessageDecoderError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
+ }
+
+ /// An error consensus decoding a [`GetHeadersMessage`].
+ ///
+ /// [`GetHeadersMessage`]: super::GetHeadersMessage
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ pub struct GetHeadersMessageDecoderError(
+ pub(super) <super::GetBlocksOrHeadersInnerDecoder as encoding::Decoder>::Error,
+ );
+
+ impl From<Infallible> for GetHeadersMessageDecoderError {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
+
+ impl fmt::Display for GetHeadersMessageDecoderError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write_err!(f, "getheaders decoder error"; self.0)
+ }
+ }
+
+ #[cfg(feature = "std")]
+ impl std::error::Error for GetHeadersMessageDecoderError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
+ }
+}
#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for BlockLocator {
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.