Move difficulty threshold checks into their own function
This commit is contained in:
parent
77e227dfe4
commit
4953282005
|
@ -135,18 +135,7 @@ where
|
||||||
|
|
||||||
// Do the difficulty checks first, to raise the threshold for
|
// Do the difficulty checks first, to raise the threshold for
|
||||||
// attacks that use any other fields.
|
// attacks that use any other fields.
|
||||||
let difficulty_threshold = block
|
check::difficulty_is_valid(&block.header, &height, &hash)?;
|
||||||
.header
|
|
||||||
.difficulty_threshold
|
|
||||||
.to_expanded()
|
|
||||||
.ok_or(BlockError::InvalidDifficulty(height, hash))?;
|
|
||||||
if hash > difficulty_threshold {
|
|
||||||
Err(BlockError::DifficultyFilter(
|
|
||||||
height,
|
|
||||||
hash,
|
|
||||||
difficulty_threshold,
|
|
||||||
))?;
|
|
||||||
}
|
|
||||||
check::equihash_solution_is_valid(&block.header)?;
|
check::equihash_solution_is_valid(&block.header)?;
|
||||||
|
|
||||||
// Since errors cause an early exit, try to do the
|
// Since errors cause an early exit, try to do the
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
|
||||||
use zebra_chain::{
|
use zebra_chain::{
|
||||||
|
block::Hash,
|
||||||
|
block::Height,
|
||||||
block::{Block, Header},
|
block::{Block, Header},
|
||||||
parameters::NetworkUpgrade,
|
parameters::NetworkUpgrade,
|
||||||
work::equihash,
|
work::equihash,
|
||||||
|
@ -39,6 +41,35 @@ pub fn coinbase_is_first(block: &Block) -> Result<(), BlockError> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `Ok(())` if `hash` passes the difficulty filter and PoW limit,
|
||||||
|
/// based on the fields in `header`.
|
||||||
|
///
|
||||||
|
/// If the block is invalid, returns an error containing `height` and `hash`.
|
||||||
|
pub fn difficulty_is_valid(
|
||||||
|
header: &Header,
|
||||||
|
height: &Height,
|
||||||
|
hash: &Hash,
|
||||||
|
) -> Result<(), BlockError> {
|
||||||
|
// TODO:
|
||||||
|
// - PoW limit
|
||||||
|
|
||||||
|
let difficulty_threshold = header
|
||||||
|
.difficulty_threshold
|
||||||
|
.to_expanded()
|
||||||
|
.ok_or(BlockError::InvalidDifficulty(*height, *hash))?;
|
||||||
|
|
||||||
|
// Difficulty filter
|
||||||
|
if hash > &difficulty_threshold {
|
||||||
|
Err(BlockError::DifficultyFilter(
|
||||||
|
*height,
|
||||||
|
*hash,
|
||||||
|
difficulty_threshold,
|
||||||
|
))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns `Ok(())` if the `EquihashSolution` is valid for `header`
|
/// Returns `Ok(())` if the `EquihashSolution` is valid for `header`
|
||||||
pub fn equihash_solution_is_valid(header: &Header) -> Result<(), equihash::Error> {
|
pub fn equihash_solution_is_valid(header: &Header) -> Result<(), equihash::Error> {
|
||||||
header.solution.check(&header)
|
header.solution.check(&header)
|
||||||
|
|
Loading…
Reference in New Issue