Skip to content

Commit

Permalink
Test committing blocks update best tip height
Browse files Browse the repository at this point in the history
Create a mock blockchain state, with a chain of finalized blocks and a
chain of non-finalized blocks. Commit all the blocks appropriately, and
verify that the best tip height is updated.
  • Loading branch information
jvff committed Aug 6, 2021
1 parent 94a65b2 commit 73a96e0
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion zebra-state/src/service/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{env, sync::Arc};

use futures::stream::FuturesUnordered;
use tower::{buffer::Buffer, util::BoxService, Service, ServiceExt};

use zebra_chain::{
block::Block,
parameters::{Network, NetworkUpgrade},
Expand All @@ -13,8 +14,9 @@ use zebra_test::{prelude::*, transcript::Transcript};
use crate::{
arbitrary::Prepare,
constants, init_test,
service::StateService,
tests::setup::{partial_nu5_chain_strategy, transaction_v4_from_coinbase},
BoxError, FinalizedBlock, PreparedBlock, Request, Response,
BoxError, Config, FinalizedBlock, PreparedBlock, Request, Response,
};

const LAST_BLOCK_HEIGHT: u32 = 10;
Expand Down Expand Up @@ -277,6 +279,41 @@ proptest! {

prop_assert_eq!(response, Ok(()));
}

/// Test that the best tip height is updated accordingly.
///
/// 1. Generate a finalized chain and some non-finalized blocks.
/// 2. Check that initially the best tip height is empty.
/// 3. Commit the finalized blocks and check that the best tip height is updated accordingly.
/// 4. Commit the non-finalized blocks and check that the best tip height is also updated
/// accordingly.
#[test]
fn best_tip_height_is_updated(
(network, finalized_blocks, non_finalized_blocks)
in continuous_empty_blocks_from_test_vectors(),
) {
zebra_test::init();

let (mut state_service, best_tip_height) = StateService::new(Config::ephemeral(), network);

prop_assert_eq!(*best_tip_height.borrow(), None);

for block in finalized_blocks {
let expected_height = block.height;

state_service.queue_and_commit_finalized(block);

prop_assert_eq!(*best_tip_height.borrow(), Some(expected_height));
}

for block in non_finalized_blocks {
let expected_height = block.height;

state_service.queue_and_commit_non_finalized(block);

prop_assert_eq!(*best_tip_height.borrow(), Some(expected_height));
}
}
}

/// Test strategy to generate a chain split in two from the test vectors.
Expand Down

0 comments on commit 73a96e0

Please sign in to comment.