From e1002ce2cef445f0a59c766d8abb56f1ad8212eb Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 2 Mar 2021 17:11:08 +1000 Subject: [PATCH] Test checkpoints on mainnet and testnet --- zebra-consensus/src/checkpoint/tests.rs | 67 +++++++++++-------------- zebra-test/src/vectors/block.rs | 22 ++++++++ 2 files changed, 51 insertions(+), 38 deletions(-) diff --git a/zebra-consensus/src/checkpoint/tests.rs b/zebra-consensus/src/checkpoint/tests.rs index 2f2a9be3..d7df5fb2 100644 --- a/zebra-consensus/src/checkpoint/tests.rs +++ b/zebra-consensus/src/checkpoint/tests.rs @@ -215,63 +215,54 @@ async fn multi_item_checkpoint_list() -> Result<(), Report> { #[tokio::test] async fn continuous_blockchain_no_restart() -> Result<(), Report> { - continuous_blockchain(None).await?; + continuous_blockchain(None, Mainnet).await?; + continuous_blockchain(None, Testnet).await?; Ok(()) } #[tokio::test] async fn continuous_blockchain_restart() -> Result<(), Report> { for height in 0..=10 { - continuous_blockchain(Some(block::Height(height))).await?; + continuous_blockchain(Some(block::Height(height)), Mainnet).await?; + } + for height in 0..=10 { + continuous_blockchain(Some(block::Height(height)), Testnet).await?; } Ok(()) } -/// Test a continuous blockchain, restarting verification at `restart_height`. +/// Test a continuous blockchain on `network`, restarting verification at `restart_height`. #[spandoc::spandoc] -async fn continuous_blockchain(restart_height: Option) -> Result<(), Report> { +async fn continuous_blockchain( + restart_height: Option, + network: Network, +) -> Result<(), Report> { zebra_test::init(); // A continuous blockchain - let mut blockchain = Vec::new(); - for b in &[ - &zebra_test::vectors::BLOCK_MAINNET_GENESIS_BYTES[..], - &zebra_test::vectors::BLOCK_MAINNET_1_BYTES[..], - &zebra_test::vectors::BLOCK_MAINNET_2_BYTES[..], - &zebra_test::vectors::BLOCK_MAINNET_3_BYTES[..], - &zebra_test::vectors::BLOCK_MAINNET_4_BYTES[..], - &zebra_test::vectors::BLOCK_MAINNET_5_BYTES[..], - &zebra_test::vectors::BLOCK_MAINNET_6_BYTES[..], - &zebra_test::vectors::BLOCK_MAINNET_7_BYTES[..], - &zebra_test::vectors::BLOCK_MAINNET_8_BYTES[..], - &zebra_test::vectors::BLOCK_MAINNET_9_BYTES[..], - &zebra_test::vectors::BLOCK_MAINNET_10_BYTES[..], - ] { - let block = Arc::::zcash_deserialize(*b)?; - let hash = block.hash(); - blockchain.push((block.clone(), block.coinbase_height().unwrap(), hash)); - } + let blockchain = match network { + Mainnet => zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS.iter(), + Testnet => zebra_test::vectors::CONTINUOUS_TESTNET_BLOCKS.iter(), + }; + let blockchain: Vec<_> = blockchain + .map(|(height, b)| { + let block = Arc::::zcash_deserialize(*b).unwrap(); + let hash = block.hash(); + let coinbase_height = block.coinbase_height().unwrap(); + assert_eq!(*height, coinbase_height.0); + (block, coinbase_height, hash) + }) + .collect(); let blockchain_len = blockchain.len(); - // Parse only some blocks as checkpoints - let mut checkpoints = Vec::new(); - for b in &[ - &zebra_test::vectors::BLOCK_MAINNET_GENESIS_BYTES[..], - &zebra_test::vectors::BLOCK_MAINNET_5_BYTES[..], - &zebra_test::vectors::BLOCK_MAINNET_9_BYTES[..], - ] { - let block = Arc::::zcash_deserialize(*b)?; - let hash = block.hash(); - checkpoints.push((block.clone(), block.coinbase_height().unwrap(), hash)); - } - - // The checkpoint list will contain only block 0, 5 and 9 - let checkpoint_list: BTreeMap = checkpoints + // Use some of the blocks as checkpoints + let checkpoint_list = vec![&blockchain[0], &blockchain[5], &blockchain[9]]; + let checkpoint_list: BTreeMap = checkpoint_list .iter() .map(|(_block, height, hash)| (*height, *hash)) .collect(); - /// SPANDOC: Verify blocks, restarting at {?restart_height} + /// SPANDOC: Verify blocks, restarting at {?restart_height} {?network} { let initial_tip = restart_height.map(|block::Height(height)| { (blockchain[height as usize].1, blockchain[height as usize].2) @@ -401,7 +392,7 @@ async fn continuous_blockchain(restart_height: Option) -> Result< ); } - /// SPANDOC: wait on spawned verification tasks for restart height {?restart_height} + /// SPANDOC: wait on spawned verification tasks for restart height {?restart_height} {?network} while let Some(result) = handles.next().await { result??.map_err(|e| eyre!(e))?; } diff --git a/zebra-test/src/vectors/block.rs b/zebra-test/src/vectors/block.rs index b0ae841c..29353171 100644 --- a/zebra-test/src/vectors/block.rs +++ b/zebra-test/src/vectors/block.rs @@ -14,6 +14,28 @@ lazy_static! { .map(|(_height, block)| *block) .collect(); + /// Continuous mainnet blocks, indexed by height + /// + /// Contains the continuous blockchain from genesis onwards. + /// Stops at the first gap in the chain. + pub static ref CONTINUOUS_MAINNET_BLOCKS: BTreeMap = MAINNET_BLOCKS + .iter() + .enumerate() + .take_while(|(i, (height, _block))| *i == **height as usize) + .map(|(_i, (height, block))| (*height, *block)) + .collect(); + + /// Continuous testnet blocks, indexed by height + /// + /// Contains the continuous blockchain from genesis onwards. + /// Stops at the first gap in the chain. + pub static ref CONTINUOUS_TESTNET_BLOCKS: BTreeMap = TESTNET_BLOCKS + .iter() + .enumerate() + .take_while(|(i, (height, _block))| *i == **height as usize) + .map(|(_i, (height, block))| (*height, *block)) + .collect(); + // Update these lists of blocks when you add new block test vectors to // this file //