Skip to content

Commit

Permalink
remove nearly-unused util::Error; promote encode::Error to top-level …
Browse files Browse the repository at this point in the history
…Error type
  • Loading branch information
apoelstra committed Nov 29, 2020
1 parent 231c6e2 commit 2f4d915
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ pub use primitives::transaction::TxOut;
pub use primitives::transaction::OutPoint;
pub use primitives::transaction::SigHashType;
pub use consensus::encode::VarInt;
pub use consensus::encode::Error;
pub use network::Network;
pub use network::ServiceFlags;
pub use script::Script;
pub use util::Error;
pub use util::address::Address;
pub use util::address::AddressType;
pub use util::amount::Amount;
Expand Down
9 changes: 4 additions & 5 deletions src/primitives/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
use std::fmt;

use util;
use util::Error::{BlockBadTarget, BlockBadProofOfWork};
use util::hash::bitcoin_merkle_root;
use hashes::{Hash, HashEngine};
use hash_types::{Wtxid, BlockHash, TxMerkleNode, WitnessMerkleNode, WitnessCommitment};
use util::uint::Uint256;
use consensus::encode::Encodable;
use primitives::{max_target, WITNESS_SCALE_FACTOR};
use primitives::{max_target, PowError, WITNESS_SCALE_FACTOR};
use {script, Network, Transaction, VarInt};

/// A block header, which contains all the block's information except
Expand Down Expand Up @@ -126,16 +125,16 @@ impl BlockHeader {
}

/// Checks that the proof-of-work for the block is valid.
pub fn validate_pow(&self, required_target: &Uint256) -> Result<(), util::Error> {
pub fn validate_pow(&self, required_target: &Uint256) -> Result<(), PowError> {
let target = &self.target();
if target != required_target {
return Err(BlockBadTarget);
return Err(PowError::BadTarget);
}
let data: [u8; 32] = self.block_hash().into_inner();
let mut ret = [0u64; 4];
util::endian::bytes_to_u64_slice_le(&data, &mut ret);
let hash = &Uint256(ret);
if hash <= target { Ok(()) } else { Err(BlockBadProofOfWork) }
if hash <= target { Ok(()) } else { Err(PowError::BadProofOfWork) }
}

/// Returns the total work of the block
Expand Down
23 changes: 22 additions & 1 deletion src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,35 @@
pub mod transaction;
pub mod block;

use std::default::Default;
use std::{error, fmt};

use hashes::hex::FromHex;
use hashes::sha256d;
use script::{self, opcodes};
use util::uint::Uint256;
use {Block, BlockHeader, Network, OutPoint, Transaction, TxOut, TxIn};

/// Proof-of-work validation error
#[derive(Debug)]
pub enum PowError {
/// The header hash is not below the target
BadProofOfWork,
/// The `target` field of a block header did not match the expected difficulty
BadTarget,
}

impl fmt::Display for PowError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
PowError::BadProofOfWork => f.write_str("block target correct but not attained"),
PowError::BadTarget => f.write_str("block target incorrect"),
}
}
}

impl error::Error for PowError {
}

/// The maximum allowable sequence number
pub const MAX_SEQUENCE: u32 = 0xFFFFFFFF;
/// How many satoshis are in "one bitcoin"
Expand Down
53 changes: 0 additions & 53 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ pub mod bip158;

pub(crate) mod endian;

use std::{error, fmt};

use network;
use consensus::encode;

/// A trait which allows numbers to act as fixed-size bit arrays
pub trait BitArray {
/// Is bit set?
Expand All @@ -58,51 +53,3 @@ pub trait BitArray {
fn one() -> Self;
}

/// A general error code, other errors should implement conversions to/from this
/// if appropriate.
#[derive(Debug)]
pub enum Error {
/// Encoding error
Encode(encode::Error),
/// Network error
Network(network::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) => fmt::Display::fmt(e, f),
Error::Network(ref e) => fmt::Display::fmt(e, f),
Error::BlockBadProofOfWork => f.write_str("block target correct but not attained"),
Error::BlockBadTarget => f.write_str("block target incorrect"),
}
}
}

impl error::Error for Error {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::Encode(ref e) => Some(e),
Error::Network(ref e) => Some(e),
Error::BlockBadProofOfWork | Error::BlockBadTarget => None
}
}
}

#[doc(hidden)]
impl From<encode::Error> for Error {
fn from(e: encode::Error) -> Error {
Error::Encode(e)
}
}

#[doc(hidden)]
impl From<network::Error> for Error {
fn from(e: network::Error) -> Error {
Error::Network(e)
}
}

0 comments on commit 2f4d915

Please sign in to comment.