Move util::Error to error module
We now have an `error` module but the `util::Error`, which is a general error, is not in it. Make `Error` more ergonomic to use by doing: - Move the `util::Error` to `crate::error::Error` - Re-export it from the crate root since it is our most general error - Re-export and deprecated it from `util`
This commit is contained in:
parent
70eb92cc87
commit
445b07c94c
|
@ -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<BlockHash, util::Error> {
|
||||
pub fn validate_pow(&self, required_target: Target) -> Result<BlockHash, Error> {
|
||||
let target = self.target();
|
||||
if target != required_target {
|
||||
return Err(BlockBadTarget);
|
||||
|
|
|
@ -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<encode::Error> 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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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<encode::Error> for Error {
|
||||
fn from(e: encode::Error) -> Error {
|
||||
Error::Encode(e)
|
||||
}
|
||||
}
|
||||
|
||||
// core2 doesn't have read_to_end
|
||||
pub(crate) fn read_to_end<D: io::Read>(mut d: D) -> Result<Vec<u8>, io::Error> {
|
||||
|
|
Loading…
Reference in New Issue