diff --git a/zebra-consensus/src/block/tests.rs b/zebra-consensus/src/block/tests.rs index 6a5afa3c5..ee0bc19ff 100644 --- a/zebra-consensus/src/block/tests.rs +++ b/zebra-consensus/src/block/tests.rs @@ -6,6 +6,7 @@ use super::*; use std::sync::Arc; +use block::Height; use chrono::Utc; use color_eyre::eyre::{eyre, Report}; use once_cell::sync::Lazy; @@ -145,6 +146,32 @@ fn coinbase_is_first_for_historical_blocks() -> Result<(), Report> { Ok(()) } +#[test] +fn difficulty_is_valid_for_historical_blocks() -> Result<(), Report> { + difficulty_is_valid_for_network(Network::Mainnet)?; + difficulty_is_valid_for_network(Network::Testnet)?; + + Ok(()) +} + +fn difficulty_is_valid_for_network(network: Network) -> Result<(), Report> { + let block_iter = match network { + Network::Mainnet => zebra_test::vectors::MAINNET_BLOCKS.iter(), + Network::Testnet => zebra_test::vectors::TESTNET_BLOCKS.iter(), + }; + + for (&height, block) in block_iter { + let block = block + .zcash_deserialize_into::() + .expect("block is structurally valid"); + + check::difficulty_is_valid(&block.header, network, &Height(height), &block.hash()) + .expect("the difficulty from a historical block should be valid"); + } + + Ok(()) +} + #[test] fn equihash_is_valid_for_historical_blocks() -> Result<(), Report> { let block_iter = zebra_test::vectors::BLOCKS.iter();