bitcoin: Move block module errors to error submodule
What changed, and why it matters
This commit is a routine code cleanup: it moves error type definitions for Bitcoin blocks into a new 'error' submodule and re-exports them. There is no change to how the code behaves, no bug fix, and no security impact.
No security action needed. Treat as normal refactoring review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors bitcoin/src/blockdata/block.rs by creating a public error submodule and moving Bip34Error, ValidationError, and the to_bip34_error helper into it. It also re-exports existing error types from primitives::block and units::block through the new submodule. Imports and visibility paths are adjusted accordingly. No logic, parsing, validation, or cryptographic behavior is altered.
Changed components
bitcoin/src/blockdata/block.rsInspect captured patch +95 / −79
diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs
index d7ea0983..88c66e53 100644
--- a/bitcoin/src/blockdata/block.rs
+++ b/bitcoin/src/blockdata/block.rs
@@ -7,9 +7,6 @@
//! module describes structures and functions needed to describe
//! these blocks and the blockchain.
-use core::convert::Infallible;
-use core::fmt;
-
use encoding::CompactSizeEncoder;
use internals::ToU64;
use io::{BufRead, Write};
@@ -18,29 +15,29 @@ use crate::consensus::encode::{self, Decodable, Encodable, WriteExt as _};
use crate::merkle_tree::{TxMerkleNode, WitnessMerkleNode};
use crate::network::Params;
use crate::prelude::Vec;
-use crate::script::{self, ScriptExt as _, ScriptIntError};
+use crate::script::ScriptExt as _;
use crate::transaction::{Coinbase, Transaction, TransactionExt as _};
use crate::{internal_macros, BlockTime, Target, Weight, Work};
#[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)]
pub use primitives::block::{
- error, Block, BlockDecoder, BlockEncoder, BlockHash, BlockHashDecoder, BlockHashEncoder,
+ Block, BlockDecoder, BlockEncoder, BlockHash, BlockHashDecoder, BlockHashEncoder,
Checked, Unchecked, Validation, Version, VersionDecoder, VersionEncoder, Header,
HeaderDecoder, HeaderEncoder, WitnessCommitment, compute_merkle_root, compute_witness_root,
};
-#[doc(no_inline)]
-pub use primitives::block::{
- BlockDecoderError, BlockHashDecoderError, HeaderDecoderError, InvalidBlockError,
- ParseBlockError, ParseHeaderError, VersionDecoderError,
-};
#[doc(inline)]
pub use units::block::{
BlockHeight, BlockHeightDecoder, BlockHeightEncoder, BlockHeightInterval, BlockMtp,
BlockMtpInterval,
};
+
#[doc(no_inline)]
-pub use units::block::{BlockHeightDecoderError, TooBigForRelativeHeightError};
+pub use self::error::{
+ Bip34Error, BlockDecoderError, BlockHashDecoderError, BlockHeightDecoderError,
+ HeaderDecoderError, InvalidBlockError, ParseBlockError, ParseHeaderError,
+ TooBigForRelativeHeightError, ValidationError, VersionDecoderError,
+};
#[deprecated(since = "TBD", note = "use `BlockHeightInterval` instead")]
#[doc(hidden)]
@@ -223,7 +220,7 @@ impl BlockCheckedExt for Block<Checked> {
.instructions_minimal()
.next()
.ok_or(Bip34Error::NotPresent)?
- .map_err(to_bip34_error)?;
+ .map_err(error::to_bip34_error)?;
match (push.script_num(), push.push_bytes().map(|b| b.read_scriptint())) {
(Some(num), Some(Ok(_)) | None) =>
Ok(num.try_into().map_err(|_| Bip34Error::NegativeHeight)?),
@@ -300,91 +297,110 @@ mod sealed {
impl<V: super::Validation> Sealed for super::Block<V> {}
}
-/// An error when looking up a BIP-0034 block height.
-#[derive(Debug, Clone, PartialEq, Eq)]
-#[non_exhaustive]
-pub enum Bip34Error {
- /// The block does not support BIP-0034 yet.
- Unsupported,
- /// No push was present where the BIP-0034 push was expected.
- NotPresent,
- /// The BIP-0034 push was not minimally encoded.
- NonMinimalPush,
- /// The BIP-0034 push was negative.
- NegativeHeight,
-}
-
-impl From<Infallible> for Bip34Error {
- fn from(never: Infallible) -> Self { match never {} }
-}
+/// Error types for Bitcoin blocks.
+pub mod error {
+ use core::convert::Infallible;
+ use core::fmt;
+
+ use crate::script::{Error, ScriptIntError};
+
+ #[rustfmt::skip]
+ #[doc(no_inline)]
+ pub use primitives::block::{
+ BlockDecoderError, BlockHashDecoderError, HeaderDecoderError, InvalidBlockError,
+ ParseBlockError, ParseHeaderError, VersionDecoderError,
+ };
+ #[doc(no_inline)]
+ pub use units::block::{BlockHeightDecoderError, TooBigForRelativeHeightError};
+
+ /// An error when looking up a BIP-0034 block height.
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ #[non_exhaustive]
+ pub enum Bip34Error {
+ /// The block does not support BIP-0034 yet.
+ Unsupported,
+ /// No push was present where the BIP-0034 push was expected.
+ NotPresent,
+ /// The BIP-0034 push was not minimally encoded.
+ NonMinimalPush,
+ /// The BIP-0034 push was negative.
+ NegativeHeight,
+ }
+
+ impl From<Infallible> for Bip34Error {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
+
+ impl fmt::Display for Bip34Error {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ Self::Unsupported => write!(f, "block doesn't support BIP-0034"),
+ Self::NotPresent => write!(f, "BIP-0034 push not present in block's coinbase"),
+ Self::NonMinimalPush => write!(f, "byte push not minimally encoded"),
+ Self::NegativeHeight => write!(f, "negative BIP-0034 height"),
+ }
+ }
+ }
-impl fmt::Display for Bip34Error {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match self {
- Self::Unsupported => write!(f, "block doesn't support BIP-0034"),
- Self::NotPresent => write!(f, "BIP-0034 push not present in block's coinbase"),
- Self::NonMinimalPush => write!(f, "byte push not minimally encoded"),
- Self::NegativeHeight => write!(f, "negative BIP-0034 height"),
+ #[cfg(feature = "std")]
+ impl std::error::Error for Bip34Error {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ match self {
+ Self::Unsupported
+ | Self::NotPresent
+ | Self::NonMinimalPush
+ | Self::NegativeHeight => None,
+ }
}
}
-}
-#[cfg(feature = "std")]
-impl std::error::Error for Bip34Error {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
- match self {
- Self::Unsupported | Self::NotPresent | Self::NonMinimalPush | Self::NegativeHeight =>
- None,
+ impl From<ScriptIntError> for Bip34Error {
+ #[inline]
+ fn from(err: ScriptIntError) -> Self {
+ match err {
+ ScriptIntError::NonMinimal => Self::NonMinimalPush,
+ _ => Self::NotPresent,
+ }
}
}
-}
-impl From<ScriptIntError> for Bip34Error {
#[inline]
- fn from(err: ScriptIntError) -> Self {
+ pub(super) fn to_bip34_error(err: Error) -> Bip34Error {
match err {
- ScriptIntError::NonMinimal => Self::NonMinimalPush,
- _ => Self::NotPresent,
+ Error::NonMinimalPush => Bip34Error::NonMinimalPush,
+ _ => Bip34Error::NotPresent,
}
}
-}
-#[inline]
-fn to_bip34_error(err: script::Error) -> Bip34Error {
- match err {
- script::Error::NonMinimalPush => Bip34Error::NonMinimalPush,
- _ => Bip34Error::NotPresent,
+ /// A block validation error.
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ #[non_exhaustive]
+ pub enum ValidationError {
+ /// The header hash is not below the target.
+ BadProofOfWork,
+ /// The `target` field of a block header did not match the expected difficulty.
+ BadTarget,
}
-}
-
-/// A block validation error.
-#[derive(Debug, Clone, PartialEq, Eq)]
-#[non_exhaustive]
-pub enum ValidationError {
- /// The header hash is not below the target.
- BadProofOfWork,
- /// The `target` field of a block header did not match the expected difficulty.
- BadTarget,
-}
-impl From<Infallible> for ValidationError {
- fn from(never: Infallible) -> Self { match never {} }
-}
+ impl From<Infallible> for ValidationError {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
-impl fmt::Display for ValidationError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match self {
- Self::BadProofOfWork => f.write_str("block target correct but not attained"),
- Self::BadTarget => f.write_str("block target incorrect"),
+ impl fmt::Display for ValidationError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ Self::BadProofOfWork => f.write_str("block target correct but not attained"),
+ Self::BadTarget => f.write_str("block target incorrect"),
+ }
}
}
-}
-#[cfg(feature = "std")]
-impl std::error::Error for ValidationError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
- match self {
- Self::BadProofOfWork | Self::BadTarget => None,
+ #[cfg(feature = "std")]
+ impl std::error::Error for ValidationError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ match self {
+ Self::BadProofOfWork | Self::BadTarget => None,
+ }
}
}
}
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.