Add block::ValidationError
Add a `ValidationError` to the `block` module and remove the two variants out of `crate::Error`. This error is only used by the `validate_pow` function, a specific error better serves our purposes.
This commit is contained in:
parent
3a9b5526b3
commit
74154c2294
|
@ -16,7 +16,6 @@ use super::Weight;
|
||||||
use crate::blockdata::script;
|
use crate::blockdata::script;
|
||||||
use crate::blockdata::transaction::Transaction;
|
use crate::blockdata::transaction::Transaction;
|
||||||
use crate::consensus::{encode, Decodable, Encodable};
|
use crate::consensus::{encode, Decodable, Encodable};
|
||||||
use crate::error::Error::{self, BlockBadProofOfWork, BlockBadTarget};
|
|
||||||
pub use crate::hash_types::BlockHash;
|
pub use crate::hash_types::BlockHash;
|
||||||
use crate::hash_types::{TxMerkleNode, WitnessCommitment, WitnessMerkleNode, Wtxid};
|
use crate::hash_types::{TxMerkleNode, WitnessCommitment, WitnessMerkleNode, Wtxid};
|
||||||
use crate::internal_macros::impl_consensus_encoding;
|
use crate::internal_macros::impl_consensus_encoding;
|
||||||
|
@ -72,16 +71,16 @@ impl Header {
|
||||||
pub fn difficulty_float(&self) -> f64 { self.target().difficulty_float() }
|
pub fn difficulty_float(&self) -> f64 { self.target().difficulty_float() }
|
||||||
|
|
||||||
/// Checks that the proof-of-work for the block is valid, returning the block hash.
|
/// 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, Error> {
|
pub fn validate_pow(&self, required_target: Target) -> Result<BlockHash, ValidationError> {
|
||||||
let target = self.target();
|
let target = self.target();
|
||||||
if target != required_target {
|
if target != required_target {
|
||||||
return Err(BlockBadTarget);
|
return Err(ValidationError::BadTarget);
|
||||||
}
|
}
|
||||||
let block_hash = self.block_hash();
|
let block_hash = self.block_hash();
|
||||||
if target.is_met_by(block_hash) {
|
if target.is_met_by(block_hash) {
|
||||||
Ok(block_hash)
|
Ok(block_hash)
|
||||||
} else {
|
} else {
|
||||||
Err(BlockBadProofOfWork)
|
Err(ValidationError::BadProofOfWork)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -391,6 +390,37 @@ impl std::error::Error for Bip34Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A block validation error.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum ValidationError {
|
||||||
|
/// 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 ValidationError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
use ValidationError::*;
|
||||||
|
|
||||||
|
match *self {
|
||||||
|
BadProofOfWork => f.write_str("block target correct but not attained"),
|
||||||
|
BadTarget => f.write_str("block target incorrect"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
impl std::error::Error for ValidationError {
|
||||||
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||||
|
use self::ValidationError::*;
|
||||||
|
|
||||||
|
match *self {
|
||||||
|
BadProofOfWork | BadTarget => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use hashes::hex::FromHex;
|
use hashes::hex::FromHex;
|
||||||
|
@ -528,7 +558,7 @@ mod tests {
|
||||||
|
|
||||||
// test with zero target
|
// test with zero target
|
||||||
match some_header.validate_pow(Target::ZERO) {
|
match some_header.validate_pow(Target::ZERO) {
|
||||||
Err(BlockBadTarget) => (),
|
Err(ValidationError::BadTarget) => (),
|
||||||
_ => panic!("unexpected result from validate_pow"),
|
_ => panic!("unexpected result from validate_pow"),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -536,7 +566,7 @@ mod tests {
|
||||||
let mut invalid_header: Header = some_header;
|
let mut invalid_header: Header = some_header;
|
||||||
invalid_header.version.0 += 1;
|
invalid_header.version.0 += 1;
|
||||||
match invalid_header.validate_pow(invalid_header.target()) {
|
match invalid_header.validate_pow(invalid_header.target()) {
|
||||||
Err(BlockBadProofOfWork) => (),
|
Err(ValidationError::BadProofOfWork) => (),
|
||||||
_ => panic!("unexpected result from validate_pow"),
|
_ => panic!("unexpected result from validate_pow"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,18 +16,12 @@ pub use crate::parse::ParseIntError;
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
/// Encoding error
|
/// Encoding error
|
||||||
Encode(encode::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 {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
Error::Encode(ref e) => write_err!(f, "encoding error"; e),
|
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"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,7 +33,6 @@ impl std::error::Error for Error {
|
||||||
|
|
||||||
match self {
|
match self {
|
||||||
Encode(e) => Some(e),
|
Encode(e) => Some(e),
|
||||||
BlockBadProofOfWork | BlockBadTarget => None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue