bitcoin: Move consensus_validation module errors to error submodule
What changed, and why it matters
This commit is a routine code cleanup. It moves two error types (BitcoinconsensusError and TxVerifyError) from the main consensus_validation file into a new error submodule and re-exports them so existing code keeps working. There is no functional change to how transactions are validated or how errors behave.
No security action required. Treat as normal maintenance/refactoring review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
Pure refactor: error types BitcoinconsensusError and TxVerifyError are relocated into a new pub mod error within bitcoin/src/consensus_validation.rs and re-exported with #[doc(no_inline)]. The types, derives, trait implementations, and visibility semantics remain effectively identical; the inner field of BitcoinconsensusError changes from private tuple field to pub(super), which is still not publicly constructible. No logic, parsing, validation, or API surface changes in a security-relevant way.
Changed components
bitcoin/src/consensus_validation.rsconsensus_validation::error submoduleInspect captured patch +60 / −51
diff --git a/bitcoin/src/consensus_validation.rs b/bitcoin/src/consensus_validation.rs
index 9ac9e804..7dad714c 100644
--- a/bitcoin/src/consensus_validation.rs
+++ b/bitcoin/src/consensus_validation.rs
@@ -4,11 +4,6 @@
//!
//! Relies on the `bitcoinconsensus` crate that uses Bitcoin Core libconsensus to perform validation.
-use core::convert::Infallible;
-use core::fmt;
-
-use internals::write_err;
-
use crate::amount::Amount;
use crate::consensus::encode;
#[cfg(doc)]
@@ -17,6 +12,10 @@ use crate::internal_macros::define_extension_trait;
use crate::script::ScriptPubKey;
use crate::transaction::{OutPoint, Transaction, TxOut};
+#[rustfmt::skip] // Keep public re-exports separate.
+#[doc(no_inline)]
+pub use self::error::{BitcoinconsensusError, TxVerifyError};
+
/// Verifies spend of an input script.
///
/// Shorthand for [`consensus_validation::verify_script_with_flags`] with flag
@@ -207,62 +206,72 @@ mod sealed {
impl Sealed for super::Transaction {}
}
-/// Wrapped error from `bitcoinconsensus`.
-// We do this for two reasons:
-// 1. We don't want the error to be part of the public API because we do not want to expose the
-// unusual versioning used in `bitcoinconsensus` to users of `rust-bitcoin`.
-// 2. We want to implement `std::error::Error` if the "std" feature is enabled in `rust-bitcoin` but
-// not in `bitcoinconsensus`.
-#[derive(Debug, Clone, PartialEq, Eq)]
-#[non_exhaustive]
-pub struct BitcoinconsensusError(bitcoinconsensus::Error);
+/// Error types for consensus validation
+pub mod error {
+ use core::convert::Infallible;
+ use core::fmt;
+
+ use internals::write_err;
+
+ use crate::transaction::OutPoint;
-impl fmt::Display for BitcoinconsensusError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write_err!(f, "bitcoinconsensus error"; &self.0)
+ /// Wrapped error from `bitcoinconsensus`.
+ // We do this for two reasons:
+ // 1. We don't want the error to be part of the public API because we do not want to expose the
+ // unusual versioning used in `bitcoinconsensus` to users of `rust-bitcoin`.
+ // 2. We want to implement `std::error::Error` if the "std" feature is enabled in `rust-bitcoin` but
+ // not in `bitcoinconsensus`.
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ #[non_exhaustive]
+ pub struct BitcoinconsensusError(pub(super) bitcoinconsensus::Error);
+
+ impl fmt::Display for BitcoinconsensusError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write_err!(f, "bitcoinconsensus error"; &self.0)
+ }
}
-}
-#[cfg(feature = "bitcoinconsensus")]
-#[cfg(feature = "std")]
-impl std::error::Error for BitcoinconsensusError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
-}
+ #[cfg(feature = "bitcoinconsensus")]
+ #[cfg(feature = "std")]
+ impl std::error::Error for BitcoinconsensusError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
+ }
-/// An error during transaction validation.
-#[derive(Debug, Clone, PartialEq, Eq)]
-#[non_exhaustive]
-pub enum TxVerifyError {
- /// Error validating the script with bitcoinconsensus library.
- ScriptVerification(BitcoinconsensusError),
- /// Cannot find the spent output.
- UnknownSpentOutput(OutPoint),
-}
+ /// An error during transaction validation.
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ #[non_exhaustive]
+ pub enum TxVerifyError {
+ /// Error validating the script with bitcoinconsensus library.
+ ScriptVerification(BitcoinconsensusError),
+ /// Cannot find the spent output.
+ UnknownSpentOutput(OutPoint),
+ }
-impl From<Infallible> for TxVerifyError {
- fn from(never: Infallible) -> Self { match never {} }
-}
+ impl From<Infallible> for TxVerifyError {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
-impl fmt::Display for TxVerifyError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match self {
- Self::ScriptVerification(ref e) =>
- write_err!(f, "bitcoinconsensus verification failed"; e),
- Self::UnknownSpentOutput(ref p) => write!(f, "unknown spent output: {}", p),
+ impl fmt::Display for TxVerifyError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ Self::ScriptVerification(ref e) =>
+ write_err!(f, "bitcoinconsensus verification failed"; e),
+ Self::UnknownSpentOutput(ref p) => write!(f, "unknown spent output: {}", p),
+ }
}
}
-}
-#[cfg(feature = "std")]
-impl std::error::Error for TxVerifyError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
- match self {
- Self::ScriptVerification(ref e) => Some(e),
- Self::UnknownSpentOutput(_) => None,
+ #[cfg(feature = "std")]
+ impl std::error::Error for TxVerifyError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ match self {
+ Self::ScriptVerification(ref e) => Some(e),
+ Self::UnknownSpentOutput(_) => None,
+ }
}
}
-}
-impl From<BitcoinconsensusError> for TxVerifyError {
- fn from(e: BitcoinconsensusError) -> Self { Self::ScriptVerification(e) }
+ impl From<BitcoinconsensusError> for TxVerifyError {
+ fn from(e: BitcoinconsensusError) -> Self { Self::ScriptVerification(e) }
+ }
}
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.