Skip to content

Commit

Permalink
Correctly handle bicoinconsensus::Error
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tcharding committed Sep 2, 2022
1 parent 556f85d commit a013847
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/blockdata/script.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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()"),
}
}
}

Expand All @@ -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),

This comment has been minimized.

Copy link
@Kixunil

Kixunil Oct 25, 2022

Collaborator

Oops, this triggers exactly the problem I talk about in #840

}
}
}
Expand Down

0 comments on commit a013847

Please sign in to comment.