From 9e5cd23e425d83fae3a0fcd2a1fc40f058ecb0c9 Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 13 Oct 2020 08:52:03 +1000 Subject: [PATCH] Test difficulty is valid for all block test vectors --- zebra-consensus/src/block/tests.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) 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();