From a013847bd3da00253eecee44dcc4e157bc8e8316 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 2 Sep 2022 13:19:02 +1000 Subject: [PATCH] Correctly handle bicoinconsensus::Error With the latest release of `rust-bitcoinconsensus` the `bicoinconsensus::Error` now implements `std::error::Error`. Correctly handle `bicoinconsensus::Error` by returning `Some(e)` and using `write_err` as is standard across the code base. --- src/blockdata/script.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index 3d2f371d..1e145991 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -19,6 +19,8 @@ use core::convert::TryFrom; use core::{fmt, default::Default}; use core::ops::Index; use crate::internal_macros::debug_from_display; +#[cfg(feature = "bitcoinconsensus")] +use crate::internal_macros::write_err; #[cfg(feature = "serde")] use serde; @@ -169,16 +171,15 @@ pub enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let str = match *self { - Error::NonMinimalPush => "non-minimal datapush", - Error::EarlyEndOfScript => "unexpected end of script", - Error::NumericOverflow => "numeric overflow (number on stack larger than 4 bytes)", + match *self { + Error::NonMinimalPush => f.write_str("non-minimal datapush"), + Error::EarlyEndOfScript => f.write_str("unexpected end of script"), + Error::NumericOverflow => f.write_str("numeric overflow (number on stack larger than 4 bytes)"), #[cfg(feature = "bitcoinconsensus")] - Error::BitcoinConsensus(ref _n) => "bitcoinconsensus verification failed", - Error::UnknownSpentOutput(ref _point) => "unknown spent output Transaction::verify()", - Error::Serialization => "can not serialize the spending transaction in Transaction::verify()", - }; - f.write_str(str) + Error::BitcoinConsensus(ref e) => write_err!(f, "bitcoinconsensus verification failed"; e), + Error::UnknownSpentOutput(ref point) => write!(f, "unknown spent output: {}", point), + Error::Serialization => f.write_str("can not serialize the spending transaction in Transaction::verify()"), + } } } @@ -188,14 +189,14 @@ impl std::error::Error for Error { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { use self::Error::*; - match self { + match *self { NonMinimalPush | EarlyEndOfScript | NumericOverflow | UnknownSpentOutput(_) | Serialization => None, #[cfg(feature = "bitcoinconsensus")] - BitcoinConsensus(_) => None, + BitcoinConsensus(ref e) => Some(e), } } }