Skip to content

Commit

Permalink
Use a smaller number of legacy chain blocks in the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Sep 12, 2022
1 parent c8f8418 commit a0c4aa2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 2 additions & 0 deletions zebra-state/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use zebra_chain::{
};

use crate::{
constants::MAX_LEGACY_CHAIN_BLOCKS,
service::{
chain_tip::{ChainTipBlock, ChainTipChange, ChainTipSender, LatestChainTip},
finalized_state::{FinalizedState, ZebraDb},
Expand Down Expand Up @@ -214,6 +215,7 @@ impl StateService {
nu5_activation_height,
state.any_ancestor_blocks(tip.1),
state.network,
MAX_LEGACY_CHAIN_BLOCKS,
) {
let legacy_db_path = state.disk.path().to_path_buf();
panic!(
Expand Down
9 changes: 7 additions & 2 deletions zebra-state/src/service/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use zebra_chain::{
work::difficulty::CompactDifficulty,
};

use crate::{constants, BoxError, PreparedBlock, ValidateContextError};
use crate::{BoxError, PreparedBlock, ValidateContextError};

// use self as check
use super::check;
Expand Down Expand Up @@ -289,10 +289,15 @@ fn difficulty_threshold_is_valid(
}

/// Check if zebra is following a legacy chain and return an error if so.
///
/// `nu5_activation_height` should be `NetworkUpgrade::Nu5.activation_height(network)`, and
/// `max_legacy_chain_blocks` should be [`MAX_LEGACY_CHAIN_BLOCKS`](crate::constants::MAX_LEGACY_CHAIN_BLOCKS).
/// They are only used for testing.
pub(crate) fn legacy_chain<I>(
nu5_activation_height: block::Height,
ancestors: I,
network: Network,
max_legacy_chain_blocks: usize,
) -> Result<(), BoxError>
where
I: Iterator<Item = Arc<Block>>,
Expand All @@ -316,7 +321,7 @@ where

// If we are past our NU5 activation height, but there are no V5 transactions in recent blocks,
// the Zebra instance that verified those blocks had no NU5 activation height.
if index >= constants::MAX_LEGACY_CHAIN_BLOCKS {
if index >= max_legacy_chain_blocks {
return Err(format!(
"could not find any transactions in recent blocks: \
checked {index} blocks back from {:?}",
Expand Down
18 changes: 11 additions & 7 deletions zebra-state/src/service/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zebra_test::{prelude::*, transcript::Transcript};

use crate::{
arbitrary::Prepare,
constants::{self, MAX_LEGACY_CHAIN_BLOCKS},
constants::MAX_LEGACY_CHAIN_BLOCKS,
init_test,
service::{arbitrary::populated_state, chain_tip::TipAction, StateService},
tests::setup::{partial_nu5_chain_strategy, transaction_v4_from_coinbase},
Expand Down Expand Up @@ -277,11 +277,14 @@ fn state_behaves_when_blocks_are_committed_in_order() -> Result<()> {

const DEFAULT_PARTIAL_CHAIN_PROPTEST_CASES: u32 = 2;

/// The legacy chain limit for tests.
const TEST_LEGACY_CHAIN_LIMIT: usize = 100;

/// Check more blocks than the legacy chain limit.
const OVER_LEGACY_CHAIN_LIMIT: u32 = constants::MAX_LEGACY_CHAIN_BLOCKS as u32 + 10;
const OVER_LEGACY_CHAIN_LIMIT: u32 = TEST_LEGACY_CHAIN_LIMIT as u32 + 10;

/// Check fewer blocks than the legacy chain limit.
const UNDER_LEGACY_CHAIN_LIMIT: u32 = constants::MAX_LEGACY_CHAIN_BLOCKS as u32 - 10;
const UNDER_LEGACY_CHAIN_LIMIT: u32 = TEST_LEGACY_CHAIN_LIMIT as u32 - 10;

proptest! {
#![proptest_config(
Expand All @@ -304,7 +307,7 @@ proptest! {
fn some_block_less_than_network_upgrade(
(network, nu_activation_height, chain) in partial_nu5_chain_strategy(4, true, UNDER_LEGACY_CHAIN_LIMIT, NetworkUpgrade::Canopy)
) {
let response = crate::service::check::legacy_chain(nu_activation_height, chain.into_iter().rev(), network)
let response = crate::service::check::legacy_chain(nu_activation_height, chain.into_iter().rev(), network, TEST_LEGACY_CHAIN_LIMIT)
.map_err(|error| error.to_string());

prop_assert_eq!(response, Ok(()));
Expand All @@ -321,7 +324,7 @@ proptest! {
.coinbase_height()
.expect("chain contains valid blocks");

let response = crate::service::check::legacy_chain(nu_activation_height, chain.into_iter().rev(), network)
let response = crate::service::check::legacy_chain(nu_activation_height, chain.into_iter().rev(), network, TEST_LEGACY_CHAIN_LIMIT)
.map_err(|error| error.to_string());

prop_assert_eq!(
Expand Down Expand Up @@ -360,7 +363,8 @@ proptest! {
let response = crate::service::check::legacy_chain(
nu_activation_height,
chain.clone().into_iter().rev(),
network
network,
TEST_LEGACY_CHAIN_LIMIT,
).map_err(|error| error.to_string());

prop_assert_eq!(
Expand All @@ -377,7 +381,7 @@ proptest! {
fn at_least_one_transaction_with_valid_network_upgrade(
(network, nu_activation_height, chain) in partial_nu5_chain_strategy(5, true, UNDER_LEGACY_CHAIN_LIMIT, NetworkUpgrade::Canopy)
) {
let response = crate::service::check::legacy_chain(nu_activation_height, chain.into_iter().rev(), network)
let response = crate::service::check::legacy_chain(nu_activation_height, chain.into_iter().rev(), network, TEST_LEGACY_CHAIN_LIMIT)
.map_err(|error| error.to_string());

prop_assert_eq!(response, Ok(()));
Expand Down

0 comments on commit a0c4aa2

Please sign in to comment.