diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index 1d652efc..1fde7047 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -13,8 +13,8 @@ use crate::prelude::*; use core::fmt; -use crate::{merkle_tree, util}; -use crate::util::Error::{BlockBadTarget, BlockBadProofOfWork}; +use crate::merkle_tree; +use crate::error::Error::{self, BlockBadTarget, BlockBadProofOfWork}; use crate::hashes::{Hash, HashEngine}; use crate::hash_types::{Wtxid, TxMerkleNode, WitnessMerkleNode, WitnessCommitment}; use crate::consensus::{encode, Encodable, Decodable}; @@ -77,7 +77,7 @@ impl Header { } /// Checks that the proof-of-work for the block is valid, returning the block hash. - pub fn validate_pow(&self, required_target: Target) -> Result { + pub fn validate_pow(&self, required_target: Target) -> Result { let target = self.target(); if target != required_target { return Err(BlockBadTarget); diff --git a/bitcoin/src/error.rs b/bitcoin/src/error.rs index 58bbb2d1..d31c09f8 100644 --- a/bitcoin/src/error.rs +++ b/bitcoin/src/error.rs @@ -1,7 +1,53 @@ //! Contains error types and other error handling tools. +use core::fmt; + +use bitcoin_internals::write_err; + +use crate::consensus::encode; pub use crate::parse::ParseIntError; +/// A general error code, other errors should implement conversions to/from this +/// if appropriate. +#[derive(Debug)] +#[non_exhaustive] +pub enum Error { + /// Encoding error + Encode(encode::Error), + /// The header hash is not below the target + BlockBadProofOfWork, + /// The `target` field of a block header did not match the expected difficulty + BlockBadTarget, +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Error::Encode(ref e) => write_err!(f, "encoding error"; e), + Error::BlockBadProofOfWork => f.write_str("block target correct but not attained"), + Error::BlockBadTarget => f.write_str("block target incorrect"), + } + } +} + +#[cfg(feature = "std")] +#[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 { + Encode(e) => Some(e), + BlockBadProofOfWork | BlockBadTarget => None, + } + } +} + +#[doc(hidden)] +impl From for Error { + fn from(e: encode::Error) -> Error { Error::Encode(e) } +} + /// Impls std::error::Error for the specified type with appropriate attributes, possibly returning /// source. macro_rules! impl_std_error { diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index 06c78407..007b9c13 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -122,6 +122,7 @@ pub use crate::blockdata::transaction::{self, OutPoint, Sequence, Transaction, T pub use crate::blockdata::witness::{self, Witness}; pub use crate::blockdata::{constants, opcodes}; pub use crate::consensus::encode::VarInt; +pub use crate::error::Error; pub use crate::hash_types::*; pub use crate::network::constants::Network; pub use crate::pow::{CompactTarget, Target, Work}; @@ -130,7 +131,7 @@ pub use crate::util::ecdsa::{self, EcdsaSig, EcdsaSigError}; pub use crate::util::key::{KeyPair, PrivateKey, PublicKey, XOnlyPublicKey}; pub use crate::merkle_tree::MerkleBlock; pub use crate::util::schnorr::{self, SchnorrSig, SchnorrSigError}; -pub use crate::util::{psbt, Error}; +pub use crate::util::psbt; #[cfg(not(feature = "std"))] mod io_extras { diff --git a/bitcoin/src/util/mod.rs b/bitcoin/src/util/mod.rs index 52c5f3e8..acd7acae 100644 --- a/bitcoin/src/util/mod.rs +++ b/bitcoin/src/util/mod.rs @@ -15,54 +15,6 @@ pub mod taproot; use crate::prelude::*; use crate::io; -use core::fmt; - -use bitcoin_internals::write_err; - -use crate::consensus::encode; - -/// A general error code, other errors should implement conversions to/from this -/// if appropriate. -#[derive(Debug)] -#[non_exhaustive] -pub enum Error { - /// Encoding error - Encode(encode::Error), - /// The header hash is not below the target - BlockBadProofOfWork, - /// The `target` field of a block header did not match the expected difficulty - BlockBadTarget, -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Error::Encode(ref e) => write_err!(f, "encoding error"; e), - Error::BlockBadProofOfWork => f.write_str("block target correct but not attained"), - Error::BlockBadTarget => f.write_str("block target incorrect"), - } - } -} - -#[cfg(feature = "std")] -#[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 { - Encode(e) => Some(e), - BlockBadProofOfWork | BlockBadTarget => None - } - } -} - -#[doc(hidden)] -impl From for Error { - fn from(e: encode::Error) -> Error { - Error::Encode(e) - } -} // core2 doesn't have read_to_end pub(crate) fn read_to_end(mut d: D) -> Result, io::Error> {