From e49f96caf787c04ebee15ead34baf78f79a51668 Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 13 Jul 2021 10:32:51 +1000 Subject: [PATCH] Move zebra_state::service::check tests to their own module (#2483) --- zebra-state/src/service/check.rs | 54 +------------------ zebra-state/src/service/check/tests.rs | 3 ++ .../src/service/check/tests/vectors.rs | 50 +++++++++++++++++ 3 files changed, 55 insertions(+), 52 deletions(-) create mode 100644 zebra-state/src/service/check/tests.rs create mode 100644 zebra-state/src/service/check/tests/vectors.rs diff --git a/zebra-state/src/service/check.rs b/zebra-state/src/service/check.rs index bdb8bc28..31a536db 100644 --- a/zebra-state/src/service/check.rs +++ b/zebra-state/src/service/check.rs @@ -17,6 +17,8 @@ use super::check; use difficulty::{AdjustedDifficulty, POW_MEDIAN_BLOCK_SPAN}; pub(crate) mod difficulty; +#[cfg(test)] +mod tests; /// Check that `block` is contextually valid for `network`, based on the /// `finalized_tip_height` and `relevant_chain`. @@ -167,55 +169,3 @@ fn difficulty_threshold_is_valid( Ok(()) } - -#[cfg(test)] -mod tests { - use std::sync::Arc; - - use zebra_chain::serialization::ZcashDeserializeInto; - - use super::*; - - #[test] - fn test_orphan_consensus_check() { - zebra_test::init(); - - let height = zebra_test::vectors::BLOCK_MAINNET_347499_BYTES - .zcash_deserialize_into::>() - .unwrap() - .coinbase_height() - .unwrap(); - - block_is_not_orphaned(block::Height(0), height).expect("tip is lower so it should be fine"); - block_is_not_orphaned(block::Height(347498), height) - .expect("tip is lower so it should be fine"); - block_is_not_orphaned(block::Height(347499), height) - .expect_err("tip is equal so it should error"); - block_is_not_orphaned(block::Height(500000), height) - .expect_err("tip is higher so it should error"); - } - - #[test] - fn test_sequential_height_check() { - zebra_test::init(); - - let height = zebra_test::vectors::BLOCK_MAINNET_347499_BYTES - .zcash_deserialize_into::>() - .unwrap() - .coinbase_height() - .unwrap(); - - height_one_more_than_parent_height(block::Height(0), height) - .expect_err("block is much lower, should panic"); - height_one_more_than_parent_height(block::Height(347497), height) - .expect_err("parent height is 2 less, should panic"); - height_one_more_than_parent_height(block::Height(347498), height) - .expect("parent height is 1 less, should be good"); - height_one_more_than_parent_height(block::Height(347499), height) - .expect_err("parent height is equal, should panic"); - height_one_more_than_parent_height(block::Height(347500), height) - .expect_err("parent height is way more, should panic"); - height_one_more_than_parent_height(block::Height(500000), height) - .expect_err("parent height is way more, should panic"); - } -} diff --git a/zebra-state/src/service/check/tests.rs b/zebra-state/src/service/check/tests.rs new file mode 100644 index 00000000..ef4349e3 --- /dev/null +++ b/zebra-state/src/service/check/tests.rs @@ -0,0 +1,3 @@ +//! Tests for state contextual validation checks. + +mod vectors; diff --git a/zebra-state/src/service/check/tests/vectors.rs b/zebra-state/src/service/check/tests/vectors.rs new file mode 100644 index 00000000..83cdc39a --- /dev/null +++ b/zebra-state/src/service/check/tests/vectors.rs @@ -0,0 +1,50 @@ +//! Fixed test vectors for state contextual validation checks. + +use std::sync::Arc; + +use zebra_chain::serialization::ZcashDeserializeInto; + +use super::super::*; + +#[test] +fn test_orphan_consensus_check() { + zebra_test::init(); + + let height = zebra_test::vectors::BLOCK_MAINNET_347499_BYTES + .zcash_deserialize_into::>() + .unwrap() + .coinbase_height() + .unwrap(); + + block_is_not_orphaned(block::Height(0), height).expect("tip is lower so it should be fine"); + block_is_not_orphaned(block::Height(347498), height) + .expect("tip is lower so it should be fine"); + block_is_not_orphaned(block::Height(347499), height) + .expect_err("tip is equal so it should error"); + block_is_not_orphaned(block::Height(500000), height) + .expect_err("tip is higher so it should error"); +} + +#[test] +fn test_sequential_height_check() { + zebra_test::init(); + + let height = zebra_test::vectors::BLOCK_MAINNET_347499_BYTES + .zcash_deserialize_into::>() + .unwrap() + .coinbase_height() + .unwrap(); + + height_one_more_than_parent_height(block::Height(0), height) + .expect_err("block is much lower, should panic"); + height_one_more_than_parent_height(block::Height(347497), height) + .expect_err("parent height is 2 less, should panic"); + height_one_more_than_parent_height(block::Height(347498), height) + .expect("parent height is 1 less, should be good"); + height_one_more_than_parent_height(block::Height(347499), height) + .expect_err("parent height is equal, should panic"); + height_one_more_than_parent_height(block::Height(347500), height) + .expect_err("parent height is way more, should panic"); + height_one_more_than_parent_height(block::Height(500000), height) + .expect_err("parent height is way more, should panic"); +}