diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index ea2cd2b7..a9911f15 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -250,9 +250,9 @@ impl Default for TxIn { #[cfg_attr(feature = "serde", serde(crate = "actual_serde"))] pub struct Sequence(pub u32); +/// An error in creating relative lock-times. #[derive(Clone, PartialEq, Eq, Debug)] #[non_exhaustive] -/// An error in creating relative lock-times. pub enum RelativeLockTimeError { /// The input was too large IntegerOverflow(u32) diff --git a/src/util/bip152.rs b/src/util/bip152.rs index fdeda7d9..1638a1e6 100644 --- a/src/util/bip152.rs +++ b/src/util/bip152.rs @@ -20,6 +20,7 @@ use crate::{Block, BlockHash, BlockHeader, Transaction}; /// A BIP-152 error #[derive(Clone, PartialEq, Eq, Debug, Copy, PartialOrd, Ord, Hash)] +#[non_exhaustive] pub enum Error { /// An unknown version number was used. UnknownVersion, @@ -37,7 +38,16 @@ impl fmt::Display for Error { } #[cfg(feature = "std")] -impl error::Error for Error {} +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + use self::Error::*; + + match *self { + UnknownVersion | InvalidPrefill => None, + } + } +} /// A [PrefilledTransaction] structure is used in [HeaderAndShortIds] to /// provide a list of a few transactions explicitly. diff --git a/src/util/bip158.rs b/src/util/bip158.rs index 44e9bddd..de3492b0 100644 --- a/src/util/bip158.rs +++ b/src/util/bip158.rs @@ -62,6 +62,7 @@ const M: u64 = 784931; /// Errors for blockfilter. #[derive(Debug)] +#[non_exhaustive] pub enum Error { /// Missing UTXO, cannot calculate script filter. UtxoMissing(OutPoint), diff --git a/src/util/merkleblock.rs b/src/util/merkleblock.rs index 75b18ad7..ea65b716 100644 --- a/src/util/merkleblock.rs +++ b/src/util/merkleblock.rs @@ -40,6 +40,8 @@ //! assert_eq!(1, index[0]); //! ``` +use core::fmt; + use crate::prelude::*; use crate::io; @@ -53,19 +55,45 @@ use crate::consensus::encode::{self, Decodable, Encodable}; use crate::util::merkleblock::MerkleBlockError::*; use crate::{Block, BlockHeader}; -/// An error when verifying the merkle block +/// An error when verifying the merkle block. #[derive(Clone, PartialEq, Eq, Debug)] +#[non_exhaustive] pub enum MerkleBlockError { - /// When header merkle root don't match to the root calculated from the partial merkle tree + /// Merkle root in the header doesn't match to the root calculated from partial merkle tree. MerkleRootMismatch, - /// When partial merkle tree contains no transactions + /// Partial merkle tree contains no transactions. NoTransactions, - /// When there are too many transactions + /// There are too many transactions. TooManyTransactions, - /// General format error + /// General format error. BadFormat(String), } +impl fmt::Display for MerkleBlockError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::MerkleBlockError::*; + + match *self { + MerkleRootMismatch => write!(f, "merkle header root doesn't match to the root calculated from the partial merkle tree"), + NoTransactions => write!(f, "partial merkle tree contains no transactions"), + TooManyTransactions => write!(f, "too many transactions"), + BadFormat(ref s) => write!(f, "general format error: {}", s), + } + } +} + +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +impl std::error::Error for MerkleBlockError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + use self::MerkleBlockError::*; + + match *self { + MerkleRootMismatch | NoTransactions | TooManyTransactions | BadFormat(_) => None, + } + } +} + /// Data structure that represents a partial merkle tree. /// /// It represents a subset of the txid's of a known block, in a way that