Move difficulty threshold checks into their own function

This commit is contained in:
teor 2020-10-13 07:27:15 +10:00
parent 77e227dfe4
commit 4953282005
2 changed files with 32 additions and 12 deletions

View File

@ -135,18 +135,7 @@ where
// Do the difficulty checks first, to raise the threshold for
// attacks that use any other fields.
let difficulty_threshold = block
.header
.difficulty_threshold
.to_expanded()
.ok_or(BlockError::InvalidDifficulty(height, hash))?;
if hash > difficulty_threshold {
Err(BlockError::DifficultyFilter(
height,
hash,
difficulty_threshold,
))?;
}
check::difficulty_is_valid(&block.header, &height, &hash)?;
check::equihash_solution_is_valid(&block.header)?;
// Since errors cause an early exit, try to do the

View File

@ -3,6 +3,8 @@
use chrono::{DateTime, Utc};
use zebra_chain::{
block::Hash,
block::Height,
block::{Block, Header},
parameters::NetworkUpgrade,
work::equihash,
@ -39,6 +41,35 @@ pub fn coinbase_is_first(block: &Block) -> Result<(), BlockError> {
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`
pub fn equihash_solution_is_valid(header: &Header) -> Result<(), equihash::Error> {
header.solution.check(&header)