units: Move block module errors to error submodule
What changed, and why it matters
This commit is a routine code reorganization. It moves two error type definitions from the main block module into a new 'error' submodule and re-exports them so existing code continues to work. There is no functional change to how the library behaves, and no security issue is present.
No security action required. Treat as normal maintenance/refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors units/src/block.rs by creating a pub mod error and relocating BlockHeightDecoderError and TooBigForRelativeHeightError into it. It adds pub(super) visibility to the inner fields (previously implicit private fields in the same module, now in a submodule) and updates bitcoin/src/blockdata/block.rs and primitives/src/block.rs to re-export the new error submodule. The public API remains unchanged; this is a pure structural cleanup.
Changed components
units/src/block.rsprimitives/src/block.rsbitcoin/src/blockdata/block.rsInspect captured patch +64 / −49
diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs
index 1425c9a7..57ae6348 100644
--- a/bitcoin/src/blockdata/block.rs
+++ b/bitcoin/src/blockdata/block.rs
@@ -36,7 +36,7 @@ pub use primitives::block::{
};
#[doc(inline)]
pub use units::block::{
- BlockHeight, BlockHeightDecoder, BlockHeightEncoder, BlockHeightInterval, BlockMtp,
+ error, BlockHeight, BlockHeightDecoder, BlockHeightEncoder, BlockHeightInterval, BlockMtp,
BlockMtpInterval,
};
#[doc(no_inline)]
diff --git a/primitives/src/block.rs b/primitives/src/block.rs
index b507068d..5050dfa4 100644
--- a/primitives/src/block.rs
+++ b/primitives/src/block.rs
@@ -33,7 +33,7 @@ use crate::{Transaction, WitnessMerkleNode};
#[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)]
-pub use units::block::{BlockHeight, BlockHeightDecoder, BlockHeightEncoder, BlockHeightInterval, BlockMtp, BlockMtpInterval};
+pub use units::block::{error, BlockHeight, BlockHeightDecoder, BlockHeightEncoder, BlockHeightInterval, BlockMtp, BlockMtpInterval};
// Re-export errors that appear directly in the API - but no doc inline.
#[doc(no_inline)]
pub use units::block::{BlockHeightDecoderError, TooBigForRelativeHeightError};
diff --git a/units/src/block.rs b/units/src/block.rs
index 80999895..7aee6320 100644
--- a/units/src/block.rs
+++ b/units/src/block.rs
@@ -11,13 +11,10 @@
//! The difference between these types and the locktime types is that these types are thin wrappers
//! whereas the locktime types contain more complex locktime specific abstractions.
-use core::convert::Infallible;
use core::{fmt, ops};
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
-#[cfg(feature = "encoding")]
-use internals::write_err;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -26,6 +23,13 @@ use crate::locktime;
use crate::locktime::{absolute, relative};
use crate::parse_int::{self, PrefixedHexError, UnprefixedHexError};
+#[rustfmt::skip] // Keep public re-exports separate.
+#[cfg(feature = "encoding")]
+#[doc(no_inline)]
+pub use self::error::BlockHeightDecoderError;
+#[doc(no_inline)]
+pub use self::error::TooBigForRelativeHeightError;
+
macro_rules! impl_u32_wrapper {
{
$(#[$($type_attrs:tt)*])*
@@ -248,28 +252,6 @@ impl encoding::Decodable for BlockHeight {
fn decoder() -> Self::Decoder { BlockHeightDecoder(encoding::ArrayDecoder::<4>::new()) }
}
-/// An error consensus decoding an `BlockHeight`.
-#[cfg(feature = "encoding")]
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct BlockHeightDecoderError(encoding::UnexpectedEofError);
-
-#[cfg(feature = "encoding")]
-impl From<Infallible> for BlockHeightDecoderError {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
-#[cfg(feature = "encoding")]
-impl fmt::Display for BlockHeightDecoderError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write_err!(f, "block height decoder error"; self.0)
- }
-}
-
-#[cfg(all(feature = "std", feature = "encoding"))]
-impl std::error::Error for BlockHeightDecoderError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
-}
-
impl_u32_wrapper! {
/// An unsigned block interval.
///
@@ -490,28 +472,6 @@ impl From<relative::NumberOf512Seconds> for BlockMtpInterval {
fn from(h: relative::NumberOf512Seconds) -> Self { Self::from_u32(h.to_seconds()) }
}
-/// Error returned when the block interval is too big to be used as a relative lock time.
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct TooBigForRelativeHeightError(u32);
-
-impl From<Infallible> for TooBigForRelativeHeightError {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
-impl fmt::Display for TooBigForRelativeHeightError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(
- f,
- "block interval is too big to be used as a relative lock time: {} (max: {})",
- self.0,
- relative::NumberOfBlocks::MAX
- )
- }
-}
-
-#[cfg(feature = "std")]
-impl std::error::Error for TooBigForRelativeHeightError {}
-
crate::internal_macros::impl_op_for_references! {
// height - height = interval
impl ops::Sub<BlockHeight> for BlockHeight {
@@ -653,6 +613,61 @@ impl<'a> core::iter::Sum<&'a Self> for BlockMtpInterval {
}
}
+/// Error types for block height and interval types.
+pub mod error {
+ use core::convert::Infallible;
+ use core::fmt;
+
+ #[cfg(feature = "encoding")]
+ use internals::write_err;
+
+ use crate::locktime::relative;
+
+ /// Error returned when the block interval is too big to be used as a relative lock time.
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ pub struct TooBigForRelativeHeightError(pub(super) u32);
+
+ impl From<Infallible> for TooBigForRelativeHeightError {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
+
+ impl fmt::Display for TooBigForRelativeHeightError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(
+ f,
+ "block interval is too big to be used as a relative lock time: {} (max: {})",
+ self.0,
+ relative::NumberOfBlocks::MAX
+ )
+ }
+ }
+
+ #[cfg(feature = "std")]
+ impl std::error::Error for TooBigForRelativeHeightError {}
+
+ /// An error consensus decoding an `BlockHeight`.
+ #[cfg(feature = "encoding")]
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ pub struct BlockHeightDecoderError(pub(super) encoding::UnexpectedEofError);
+
+ #[cfg(feature = "encoding")]
+ impl From<Infallible> for BlockHeightDecoderError {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
+
+ #[cfg(feature = "encoding")]
+ impl fmt::Display for BlockHeightDecoderError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write_err!(f, "block height decoder error"; self.0)
+ }
+ }
+
+ #[cfg(all(feature = "std", feature = "encoding"))]
+ impl std::error::Error for BlockHeightDecoderError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
+ }
+}
+
#[cfg(test)]
mod tests {
#[cfg(feature = "alloc")]
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.