From 9604b1aad0625c7780bcc8643316b3d221a626b0 Mon Sep 17 00:00:00 2001 From: idky137 Date: Tue, 20 Feb 2024 16:55:25 +0000 Subject: [PATCH 01/20] added functions for fetching block vectors --- zebra-chain/src/lib.rs | 1 + zebra-chain/src/serialization/error.rs | 4 + zebra-chain/src/test_utils.rs | 6 + zebra-chain/src/test_utils/vectors.rs | 164 +++++++++++++++++++++++++ 4 files changed, 175 insertions(+) create mode 100644 zebra-chain/src/test_utils.rs create mode 100644 zebra-chain/src/test_utils/vectors.rs diff --git a/zebra-chain/src/lib.rs b/zebra-chain/src/lib.rs index 2182e1ba175..a480ba474ea 100644 --- a/zebra-chain/src/lib.rs +++ b/zebra-chain/src/lib.rs @@ -35,6 +35,7 @@ pub mod serialization; pub mod shutdown; pub mod sprout; pub mod subtree; +pub mod test_utils; pub mod transaction; pub mod transparent; pub mod value_balance; diff --git a/zebra-chain/src/serialization/error.rs b/zebra-chain/src/serialization/error.rs index 17566548d1a..083ad765caf 100644 --- a/zebra-chain/src/serialization/error.rs +++ b/zebra-chain/src/serialization/error.rs @@ -50,4 +50,8 @@ pub enum SerializationError { /// rule](https://zips.z.cash/protocol/protocol.pdf#txnencodingandconsensus). #[error("transaction balance is non-zero but doesn't have Sapling shielded spends or outputs")] BadTransactionBalance, + + /// Invalid BLOCK_MAINNET_******_BYTES version given to vector fetcher + #[error("invalid block bytes version")] + UnsupportedVersion(u32), } diff --git a/zebra-chain/src/test_utils.rs b/zebra-chain/src/test_utils.rs new file mode 100644 index 00000000000..ce4528367d9 --- /dev/null +++ b/zebra-chain/src/test_utils.rs @@ -0,0 +1,6 @@ +//!Chain functionality for loading test vectors +//! + +mod vectors; + +pub use vectors::*; diff --git a/zebra-chain/src/test_utils/vectors.rs b/zebra-chain/src/test_utils/vectors.rs new file mode 100644 index 00000000000..366fbd6f921 --- /dev/null +++ b/zebra-chain/src/test_utils/vectors.rs @@ -0,0 +1,164 @@ +//! Network methods for fetching blockchain vectors. +//! + +#[cfg(test)] +use std::collections::BTreeMap; + +#[cfg(test)] +use crate::{ + block::Block, + parameters::Network, + serialization::{SerializationError, ZcashDeserializeInto}, +}; + +#[cfg(test)] +use zebra_test::vectors::{ + BLOCK_MAINNET_1046400_BYTES, BLOCK_MAINNET_653599_BYTES, BLOCK_MAINNET_982681_BYTES, + BLOCK_TESTNET_1116000_BYTES, BLOCK_TESTNET_583999_BYTES, BLOCK_TESTNET_925483_BYTES, + CONTINUOUS_MAINNET_BLOCKS, CONTINUOUS_TESTNET_BLOCKS, MAINNET_BLOCKS, + MAINNET_FINAL_SAPLING_ROOTS, MAINNET_FINAL_SPROUT_ROOTS, + SAPLING_FINAL_ROOT_MAINNET_1046400_BYTES, SAPLING_FINAL_ROOT_TESTNET_1116000_BYTES, + TESTNET_BLOCKS, TESTNET_FINAL_SAPLING_ROOTS, TESTNET_FINAL_SPROUT_ROOTS, +}; + +/// Network methods for fetching blockchain vectors. +#[cfg(test)] +impl Network { + /// Returns true if network is of type Mainnet. + pub fn is_mainnet(&self) -> bool { + matches!(self, Network::Mainnet) + } + + /// Returns iterator over blocks. + pub fn get_block_iter(&self) -> std::collections::btree_map::Iter<'_, u32, &[u8]> { + if self.is_mainnet() { + MAINNET_BLOCKS.iter() + } else { + TESTNET_BLOCKS.iter() + } + } + + /// Returns genesis block for chain. + pub fn get_gen_block(&self) -> std::option::Option<&&[u8]> { + if self.is_mainnet() { + MAINNET_BLOCKS.get(&0) + } else { + TESTNET_BLOCKS.get(&0) + } + } + + /// Returns block bytes + pub fn get_block_bytes(&self, version: u32) -> Result { + if self.is_mainnet() { + match version { + 653_599 => BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into(), + 982_681 => BLOCK_MAINNET_982681_BYTES.zcash_deserialize_into(), + _ => Err(SerializationError::UnsupportedVersion(version)), + } + } else { + match version { + 583_999 => BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into(), + 925_483 => BLOCK_TESTNET_925483_BYTES.zcash_deserialize_into(), + _ => Err(SerializationError::UnsupportedVersion(version)), + } + } + } + + /// Returns iterator over blockchain. + pub fn get_blockchain_iter(&self) -> std::collections::btree_map::Iter<'_, u32, &[u8]> { + if self.is_mainnet() { + CONTINUOUS_MAINNET_BLOCKS.iter() + } else { + CONTINUOUS_TESTNET_BLOCKS.iter() + } + } + + /// Returns BTreemap of blockchain. + pub fn get_blockchain_map(&self) -> &BTreeMap { + if self.is_mainnet() { + &CONTINUOUS_MAINNET_BLOCKS + } else { + &CONTINUOUS_TESTNET_BLOCKS + } + } + + /// Returns iterator over blocks and sapling roots. + pub fn get_block_sapling_roots_iter( + &self, + ) -> ( + std::collections::btree_map::Iter<'_, u32, &[u8]>, + std::collections::BTreeMap, + ) { + if self.is_mainnet() { + (MAINNET_BLOCKS.iter(), MAINNET_FINAL_SAPLING_ROOTS.clone()) + } else { + (TESTNET_BLOCKS.iter(), TESTNET_FINAL_SAPLING_ROOTS.clone()) + } + } + + /// Returns BTreemap of blocks and sapling roots. + pub fn get_block_sapling_roots_map( + &self, + ) -> ( + &std::collections::BTreeMap, + &std::collections::BTreeMap, + ) { + if self.is_mainnet() { + (&*MAINNET_BLOCKS, &*MAINNET_FINAL_SAPLING_ROOTS) + } else { + (&*TESTNET_BLOCKS, &*TESTNET_FINAL_SAPLING_ROOTS) + } + } + + /// Returns block and sapling root bytes + pub fn get_block_sapling_roots_bytes( + &self, + version: u32, + ) -> Result<(&[u8], [u8; 32]), SerializationError> { + if self.is_mainnet() { + match version { + 1_046_400_ => Ok(( + &BLOCK_MAINNET_1046400_BYTES[..], + *SAPLING_FINAL_ROOT_MAINNET_1046400_BYTES, + )), + _ => Err(SerializationError::UnsupportedVersion(version)), + } + } else { + match version { + 1_116_000 => Ok(( + &BLOCK_TESTNET_1116000_BYTES[..], + *SAPLING_FINAL_ROOT_TESTNET_1116000_BYTES, + )), + _ => Err(SerializationError::UnsupportedVersion(version)), + } + } + } + + /// Returns BTreemap of blocks and sprout roots, and last split height. + pub fn get_block_sprout_roots_height( + &self, + ) -> ( + &std::collections::BTreeMap, + &std::collections::BTreeMap, + u32, + ) { + // The mainnet block height at which the first JoinSplit occurred. + const MAINNET_FIRST_JOINSPLIT_HEIGHT: u32 = 396; + + // The testnet block height at which the first JoinSplit occurred. + const TESTNET_FIRST_JOINSPLIT_HEIGHT: u32 = 2259; + if self.is_mainnet() { + ( + &*MAINNET_BLOCKS, + &*MAINNET_FINAL_SPROUT_ROOTS, + MAINNET_FIRST_JOINSPLIT_HEIGHT, + ) + } else { + ( + &*TESTNET_BLOCKS, + &*TESTNET_FINAL_SPROUT_ROOTS, + TESTNET_FIRST_JOINSPLIT_HEIGHT, + ) + } + } +} From 878df924f6d2fae8634e05287a08be46cd73bbed Mon Sep 17 00:00:00 2001 From: idky137 Date: Tue, 20 Feb 2024 18:05:48 +0000 Subject: [PATCH 02/20] inserted new network methods for vector fetching into zebra-chain --- zebra-chain/src/block/tests/vectors.rs | 22 ++----------------- zebra-chain/src/history_tree/tests/vectors.rs | 15 ++++--------- zebra-chain/src/lib.rs | 4 +++- .../primitives/zcash_history/tests/vectors.rs | 9 ++------ zebra-chain/src/sapling/tests/tree.rs | 9 ++------ zebra-chain/src/sprout/tests/tree.rs | 21 +----------------- zebra-chain/src/test_utils.rs | 4 +--- zebra-chain/src/transaction/tests/vectors.rs | 15 +++---------- zebra-chain/src/transparent/tests/vectors.rs | 5 +---- .../src/work/difficulty/tests/vectors.rs | 10 ++------- 10 files changed, 21 insertions(+), 93 deletions(-) diff --git a/zebra-chain/src/block/tests/vectors.rs b/zebra-chain/src/block/tests/vectors.rs index 1cbf17f8cee..685e902181a 100644 --- a/zebra-chain/src/block/tests/vectors.rs +++ b/zebra-chain/src/block/tests/vectors.rs @@ -208,16 +208,7 @@ fn block_test_vectors_height_testnet() { /// Test that the block test vector indexes match the heights in the block data, /// and that each post-sapling block has a corresponding final sapling root. fn block_test_vectors_height(network: Network) { - let (block_iter, sapling_roots) = match network { - Mainnet => ( - zebra_test::vectors::MAINNET_BLOCKS.iter(), - zebra_test::vectors::MAINNET_FINAL_SAPLING_ROOTS.clone(), - ), - Testnet => ( - zebra_test::vectors::TESTNET_BLOCKS.iter(), - zebra_test::vectors::TESTNET_FINAL_SAPLING_ROOTS.clone(), - ), - }; + let (block_iter, sapling_roots) = network.get_block_sapling_roots_iter(); for (&height, block) in block_iter { let block = block @@ -262,16 +253,7 @@ fn block_commitment_testnet() { /// /// TODO: add chain history test vectors? fn block_commitment(network: Network) { - let (block_iter, sapling_roots) = match network { - Mainnet => ( - zebra_test::vectors::MAINNET_BLOCKS.iter(), - zebra_test::vectors::MAINNET_FINAL_SAPLING_ROOTS.clone(), - ), - Testnet => ( - zebra_test::vectors::TESTNET_BLOCKS.iter(), - zebra_test::vectors::TESTNET_FINAL_SAPLING_ROOTS.clone(), - ), - }; + let (block_iter, sapling_roots) = network.get_block_sapling_roots_iter(); for (height, block) in block_iter { let block = block diff --git a/zebra-chain/src/history_tree/tests/vectors.rs b/zebra-chain/src/history_tree/tests/vectors.rs index ef9295204cb..2eafff5ef77 100644 --- a/zebra-chain/src/history_tree/tests/vectors.rs +++ b/zebra-chain/src/history_tree/tests/vectors.rs @@ -13,9 +13,6 @@ use crate::{ use color_eyre::eyre; use eyre::Result; -use zebra_test::vectors::{ - MAINNET_BLOCKS, MAINNET_FINAL_SAPLING_ROOTS, TESTNET_BLOCKS, TESTNET_FINAL_SAPLING_ROOTS, -}; /// Test the history tree using the activation block of a network upgrade /// and its next block. @@ -36,10 +33,8 @@ fn push_and_prune_for_network_upgrade( network: Network, network_upgrade: NetworkUpgrade, ) -> Result<()> { - let (blocks, sapling_roots) = match network { - Network::Mainnet => (&*MAINNET_BLOCKS, &*MAINNET_FINAL_SAPLING_ROOTS), - Network::Testnet => (&*TESTNET_BLOCKS, &*TESTNET_FINAL_SAPLING_ROOTS), - }; + let (blocks, sapling_roots) = network.get_block_sapling_roots_map(); + let height = network_upgrade.activation_height(network).unwrap().0; // Load first block (activation block of the given network upgrade) @@ -120,10 +115,8 @@ fn upgrade() -> Result<()> { } fn upgrade_for_network_upgrade(network: Network, network_upgrade: NetworkUpgrade) -> Result<()> { - let (blocks, sapling_roots) = match network { - Network::Mainnet => (&*MAINNET_BLOCKS, &*MAINNET_FINAL_SAPLING_ROOTS), - Network::Testnet => (&*TESTNET_BLOCKS, &*TESTNET_FINAL_SAPLING_ROOTS), - }; + let (blocks, sapling_roots) = network.get_block_sapling_roots_map(); + let height = network_upgrade.activation_height(network).unwrap().0; // Load previous block (the block before the activation block of the given network upgrade) diff --git a/zebra-chain/src/lib.rs b/zebra-chain/src/lib.rs index a480ba474ea..3bd16d19f85 100644 --- a/zebra-chain/src/lib.rs +++ b/zebra-chain/src/lib.rs @@ -35,7 +35,6 @@ pub mod serialization; pub mod shutdown; pub mod sprout; pub mod subtree; -pub mod test_utils; pub mod transaction; pub mod transparent; pub mod value_balance; @@ -44,6 +43,9 @@ pub mod work; #[cfg(any(test, feature = "proptest-impl"))] pub use block::LedgerState; +#[cfg(test)] +pub mod test_utils; + /// Error type alias to make working with generic errors easier. /// /// Note: the 'static lifetime bound means that the *type* cannot have any diff --git a/zebra-chain/src/primitives/zcash_history/tests/vectors.rs b/zebra-chain/src/primitives/zcash_history/tests/vectors.rs index b2b5a0c1bbf..d596ac1a585 100644 --- a/zebra-chain/src/primitives/zcash_history/tests/vectors.rs +++ b/zebra-chain/src/primitives/zcash_history/tests/vectors.rs @@ -6,9 +6,6 @@ use crate::{ use crate::primitives::zcash_history::*; use color_eyre::eyre; use eyre::Result; -use zebra_test::vectors::{ - MAINNET_BLOCKS, MAINNET_FINAL_SAPLING_ROOTS, TESTNET_BLOCKS, TESTNET_FINAL_SAPLING_ROOTS, -}; /// Test the MMR tree using the activation block of a network upgrade /// and its next block. @@ -22,10 +19,8 @@ fn tree() -> Result<()> { } fn tree_for_network_upgrade(network: Network, network_upgrade: NetworkUpgrade) -> Result<()> { - let (blocks, sapling_roots) = match network { - Network::Mainnet => (&*MAINNET_BLOCKS, &*MAINNET_FINAL_SAPLING_ROOTS), - Network::Testnet => (&*TESTNET_BLOCKS, &*TESTNET_FINAL_SAPLING_ROOTS), - }; + let (blocks, sapling_roots) = network.get_block_sapling_roots_map(); + let height = network_upgrade.activation_height(network).unwrap().0; // Load Block 0 (activation block of the given network upgrade) diff --git a/zebra-chain/src/sapling/tests/tree.rs b/zebra-chain/src/sapling/tests/tree.rs index 666544b73d0..107c3c11dba 100644 --- a/zebra-chain/src/sapling/tests/tree.rs +++ b/zebra-chain/src/sapling/tests/tree.rs @@ -9,9 +9,6 @@ use crate::parameters::NetworkUpgrade; use crate::sapling::{self, tree::*}; use crate::serialization::ZcashDeserializeInto; use crate::{parameters::Network, sapling::tests::test_vectors}; -use zebra_test::vectors::{ - MAINNET_BLOCKS, MAINNET_FINAL_SAPLING_ROOTS, TESTNET_BLOCKS, TESTNET_FINAL_SAPLING_ROOTS, -}; #[test] fn empty_roots() { @@ -60,10 +57,8 @@ fn incremental_roots_with_blocks() -> Result<()> { } fn incremental_roots_with_blocks_for_network(network: Network) -> Result<()> { - let (blocks, sapling_roots) = match network { - Network::Mainnet => (&*MAINNET_BLOCKS, &*MAINNET_FINAL_SAPLING_ROOTS), - Network::Testnet => (&*TESTNET_BLOCKS, &*TESTNET_FINAL_SAPLING_ROOTS), - }; + let (blocks, sapling_roots) = network.get_block_sapling_roots_map(); + let height = NetworkUpgrade::Sapling .activation_height(network) .unwrap() diff --git a/zebra-chain/src/sprout/tests/tree.rs b/zebra-chain/src/sprout/tests/tree.rs index 0f2482600bf..0e61025cf0d 100644 --- a/zebra-chain/src/sprout/tests/tree.rs +++ b/zebra-chain/src/sprout/tests/tree.rs @@ -6,8 +6,6 @@ use color_eyre::eyre; use eyre::Result; use hex::FromHex; -use zebra_test::vectors; - use crate::{ block::Block, parameters::{Network, NetworkUpgrade}, @@ -88,25 +86,8 @@ fn incremental_roots_with_blocks() -> Result<()> { } fn incremental_roots_with_blocks_for_network(network: Network) -> Result<()> { - // The mainnet block height at which the first JoinSplit occurred. - const MAINNET_FIRST_JOINSPLIT_HEIGHT: u32 = 396; - - // The testnet block height at which the first JoinSplit occurred. - const TESTNET_FIRST_JOINSPLIT_HEIGHT: u32 = 2259; - // Load the test data. - let (blocks, sprout_roots, next_height) = match network { - Network::Mainnet => ( - &*vectors::MAINNET_BLOCKS, - &*vectors::MAINNET_FINAL_SPROUT_ROOTS, - MAINNET_FIRST_JOINSPLIT_HEIGHT, - ), - Network::Testnet => ( - &*vectors::TESTNET_BLOCKS, - &*vectors::TESTNET_FINAL_SPROUT_ROOTS, - TESTNET_FIRST_JOINSPLIT_HEIGHT, - ), - }; + let (blocks, sprout_roots, next_height) = network.get_block_sprout_roots_height(); // Load the Genesis height. let genesis_height = NetworkUpgrade::Genesis diff --git a/zebra-chain/src/test_utils.rs b/zebra-chain/src/test_utils.rs index ce4528367d9..7cfa4a95955 100644 --- a/zebra-chain/src/test_utils.rs +++ b/zebra-chain/src/test_utils.rs @@ -1,6 +1,4 @@ -//!Chain functionality for loading test vectors +//!Chain functionality for fetching test vectors. //! mod vectors; - -pub use vectors::*; diff --git a/zebra-chain/src/transaction/tests/vectors.rs b/zebra-chain/src/transaction/tests/vectors.rs index 297c9bc787f..d422b2bb0f9 100644 --- a/zebra-chain/src/transaction/tests/vectors.rs +++ b/zebra-chain/src/transaction/tests/vectors.rs @@ -349,10 +349,7 @@ fn fake_v5_round_trip() { } fn fake_v5_round_trip_for_network(network: Network) { - let block_iter = match network { - Network::Mainnet => zebra_test::vectors::MAINNET_BLOCKS.iter(), - Network::Testnet => zebra_test::vectors::TESTNET_BLOCKS.iter(), - }; + let block_iter = network.get_block_iter(); let overwinter_activation_height = NetworkUpgrade::Overwinter .activation_height(network) @@ -500,10 +497,7 @@ fn fake_v5_librustzcash_round_trip() { } fn fake_v5_librustzcash_round_trip_for_network(network: Network) { - let block_iter = match network { - Network::Mainnet => zebra_test::vectors::MAINNET_BLOCKS.iter(), - Network::Testnet => zebra_test::vectors::TESTNET_BLOCKS.iter(), - }; + let block_iter = network.get_block_iter(); let overwinter_activation_height = NetworkUpgrade::Overwinter .activation_height(network) @@ -943,10 +937,7 @@ fn binding_signatures() { } fn binding_signatures_for_network(network: Network) { - let block_iter = match network { - Network::Mainnet => zebra_test::vectors::MAINNET_BLOCKS.iter(), - Network::Testnet => zebra_test::vectors::TESTNET_BLOCKS.iter(), - }; + let block_iter = network.get_block_iter(); for (height, bytes) in block_iter { let upgrade = NetworkUpgrade::current(network, Height(*height)); diff --git a/zebra-chain/src/transparent/tests/vectors.rs b/zebra-chain/src/transparent/tests/vectors.rs index f1c8cd8894f..9f85a1c2f2c 100644 --- a/zebra-chain/src/transparent/tests/vectors.rs +++ b/zebra-chain/src/transparent/tests/vectors.rs @@ -92,10 +92,7 @@ fn get_transparent_output_address_with_blocks() { /// Test that the block test vector indexes match the heights in the block data, /// and that each post-sapling block has a corresponding final sapling root. fn get_transparent_output_address_with_blocks_for_network(network: Network) { - let block_iter = match network { - Network::Mainnet => zebra_test::vectors::MAINNET_BLOCKS.iter(), - Network::Testnet => zebra_test::vectors::TESTNET_BLOCKS.iter(), - }; + let block_iter = network.get_block_iter(); let mut valid_addresses = 0; diff --git a/zebra-chain/src/work/difficulty/tests/vectors.rs b/zebra-chain/src/work/difficulty/tests/vectors.rs index d198fd32c4b..1c25580a890 100644 --- a/zebra-chain/src/work/difficulty/tests/vectors.rs +++ b/zebra-chain/src/work/difficulty/tests/vectors.rs @@ -273,10 +273,7 @@ fn block_difficulty() -> Result<(), Report> { fn block_difficulty_for_network(network: Network) -> Result<(), Report> { let _init_guard = zebra_test::init(); - let block_iter = match network { - Network::Mainnet => zebra_test::vectors::MAINNET_BLOCKS.iter(), - Network::Testnet => zebra_test::vectors::TESTNET_BLOCKS.iter(), - }; + let block_iter = network.get_block_iter(); let diff_zero = ExpandedDifficulty(U256::zero()); let diff_one = ExpandedDifficulty(U256::one()); @@ -362,10 +359,7 @@ fn genesis_block_difficulty() -> Result<(), Report> { fn genesis_block_difficulty_for_network(network: Network) -> Result<(), Report> { let _init_guard = zebra_test::init(); - let block = match network { - Network::Mainnet => zebra_test::vectors::MAINNET_BLOCKS.get(&0), - Network::Testnet => zebra_test::vectors::TESTNET_BLOCKS.get(&0), - }; + let block = network.get_gen_block(); let block = block.expect("test vectors contain the genesis block"); let block = Block::zcash_deserialize(&block[..]).expect("block test vector should deserialize"); From 08863d2258b2387d33781b8b4713ec1ddc7e1811 Mon Sep 17 00:00:00 2001 From: idky137 Date: Wed, 21 Feb 2024 17:55:38 +0000 Subject: [PATCH 03/20] changed tag back to test/feature=branch --- zebra-chain/src/lib.rs | 2 +- zebra-chain/src/test_utils.rs | 1 + zebra-chain/src/test_utils/vectors.rs | 8 ++++---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/zebra-chain/src/lib.rs b/zebra-chain/src/lib.rs index 3bd16d19f85..286914e12fd 100644 --- a/zebra-chain/src/lib.rs +++ b/zebra-chain/src/lib.rs @@ -43,7 +43,7 @@ pub mod work; #[cfg(any(test, feature = "proptest-impl"))] pub use block::LedgerState; -#[cfg(test)] +#[cfg(any(test, feature = "bench"))] pub mod test_utils; /// Error type alias to make working with generic errors easier. diff --git a/zebra-chain/src/test_utils.rs b/zebra-chain/src/test_utils.rs index 7cfa4a95955..05990485aa5 100644 --- a/zebra-chain/src/test_utils.rs +++ b/zebra-chain/src/test_utils.rs @@ -1,4 +1,5 @@ //!Chain functionality for fetching test vectors. //! +#[cfg(any(test, feature = "bench"))] mod vectors; diff --git a/zebra-chain/src/test_utils/vectors.rs b/zebra-chain/src/test_utils/vectors.rs index 366fbd6f921..23c51aa1c2b 100644 --- a/zebra-chain/src/test_utils/vectors.rs +++ b/zebra-chain/src/test_utils/vectors.rs @@ -1,17 +1,17 @@ //! Network methods for fetching blockchain vectors. //! -#[cfg(test)] +#[cfg(any(test, feature = "bench"))] use std::collections::BTreeMap; -#[cfg(test)] +#[cfg(any(test, feature = "bench"))] use crate::{ block::Block, parameters::Network, serialization::{SerializationError, ZcashDeserializeInto}, }; -#[cfg(test)] +#[cfg(any(test, feature = "bench"))] use zebra_test::vectors::{ BLOCK_MAINNET_1046400_BYTES, BLOCK_MAINNET_653599_BYTES, BLOCK_MAINNET_982681_BYTES, BLOCK_TESTNET_1116000_BYTES, BLOCK_TESTNET_583999_BYTES, BLOCK_TESTNET_925483_BYTES, @@ -22,7 +22,7 @@ use zebra_test::vectors::{ }; /// Network methods for fetching blockchain vectors. -#[cfg(test)] +#[cfg(any(test, feature = "bench"))] impl Network { /// Returns true if network is of type Mainnet. pub fn is_mainnet(&self) -> bool { From be1da769b1d48a2469f03850e8ee37e4e0df775d Mon Sep 17 00:00:00 2001 From: idky137 Date: Wed, 21 Feb 2024 17:57:46 +0000 Subject: [PATCH 04/20] changed tag back to test/feature=branch --- zebra-consensus/src/checkpoint/tests.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/zebra-consensus/src/checkpoint/tests.rs b/zebra-consensus/src/checkpoint/tests.rs index 9fb29048c40..bc51ac6f1c2 100644 --- a/zebra-consensus/src/checkpoint/tests.rs +++ b/zebra-consensus/src/checkpoint/tests.rs @@ -204,6 +204,7 @@ async fn multi_item_checkpoint_list() -> Result<(), Report> { } #[tokio::test(flavor = "multi_thread")] +#[cfg(any(test, feature = "bench"))] async fn continuous_blockchain_no_restart() -> Result<(), Report> { continuous_blockchain(None, Mainnet).await?; continuous_blockchain(None, Testnet).await?; @@ -211,6 +212,7 @@ async fn continuous_blockchain_no_restart() -> Result<(), Report> { } #[tokio::test(flavor = "multi_thread")] +#[cfg(any(test, feature = "bench"))] async fn continuous_blockchain_restart() -> Result<(), Report> { for height in 0..zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS.len() { continuous_blockchain(Some(block::Height(height.try_into().unwrap())), Mainnet).await?; @@ -226,6 +228,7 @@ async fn continuous_blockchain_restart() -> Result<(), Report> { // This span is far too verbose for use during normal testing. // Turn the SPANDOC: comments into doc comments to re-enable. //#[spandoc::spandoc] +#[cfg(any(test, feature = "bench"))] async fn continuous_blockchain( restart_height: Option, network: Network, @@ -233,10 +236,12 @@ async fn continuous_blockchain( let _init_guard = zebra_test::init(); // A continuous blockchain - let blockchain = match network { - Mainnet => zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS.iter(), - Testnet => zebra_test::vectors::CONTINUOUS_TESTNET_BLOCKS.iter(), - }; + // let blockchain = match network { + // Mainnet => zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS.iter(), + // Testnet => zebra_test::vectors::CONTINUOUS_TESTNET_BLOCKS.iter(), + // }; + let blockchain = network.get_blockchain_iter(); + let blockchain: Vec<_> = blockchain .map(|(height, b)| { let block = Arc::::zcash_deserialize(*b).unwrap(); From 39886e9b8c49a460b9922eae99bf5736edc05de0 Mon Sep 17 00:00:00 2001 From: idky137 Date: Wed, 21 Feb 2024 17:58:43 +0000 Subject: [PATCH 05/20] changed tag back to test/feature=branch --- zebra-consensus/src/checkpoint/tests.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/zebra-consensus/src/checkpoint/tests.rs b/zebra-consensus/src/checkpoint/tests.rs index bc51ac6f1c2..9fb29048c40 100644 --- a/zebra-consensus/src/checkpoint/tests.rs +++ b/zebra-consensus/src/checkpoint/tests.rs @@ -204,7 +204,6 @@ async fn multi_item_checkpoint_list() -> Result<(), Report> { } #[tokio::test(flavor = "multi_thread")] -#[cfg(any(test, feature = "bench"))] async fn continuous_blockchain_no_restart() -> Result<(), Report> { continuous_blockchain(None, Mainnet).await?; continuous_blockchain(None, Testnet).await?; @@ -212,7 +211,6 @@ async fn continuous_blockchain_no_restart() -> Result<(), Report> { } #[tokio::test(flavor = "multi_thread")] -#[cfg(any(test, feature = "bench"))] async fn continuous_blockchain_restart() -> Result<(), Report> { for height in 0..zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS.len() { continuous_blockchain(Some(block::Height(height.try_into().unwrap())), Mainnet).await?; @@ -228,7 +226,6 @@ async fn continuous_blockchain_restart() -> Result<(), Report> { // This span is far too verbose for use during normal testing. // Turn the SPANDOC: comments into doc comments to re-enable. //#[spandoc::spandoc] -#[cfg(any(test, feature = "bench"))] async fn continuous_blockchain( restart_height: Option, network: Network, @@ -236,12 +233,10 @@ async fn continuous_blockchain( let _init_guard = zebra_test::init(); // A continuous blockchain - // let blockchain = match network { - // Mainnet => zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS.iter(), - // Testnet => zebra_test::vectors::CONTINUOUS_TESTNET_BLOCKS.iter(), - // }; - let blockchain = network.get_blockchain_iter(); - + 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(); From d596ece618ea9b004450cf624877b09ac6949c27 Mon Sep 17 00:00:00 2001 From: idky137 Date: Wed, 21 Feb 2024 21:12:31 +0000 Subject: [PATCH 06/20] changed feature tag to proptest-impl, started implementing in zebra-consensus tests --- zebra-chain/src/lib.rs | 2 +- zebra-chain/src/test_utils.rs | 2 +- zebra-chain/src/test_utils/vectors.rs | 8 ++++---- zebra-consensus/src/checkpoint/tests.rs | 9 +++++---- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/zebra-chain/src/lib.rs b/zebra-chain/src/lib.rs index 286914e12fd..3c276787b6d 100644 --- a/zebra-chain/src/lib.rs +++ b/zebra-chain/src/lib.rs @@ -43,7 +43,7 @@ pub mod work; #[cfg(any(test, feature = "proptest-impl"))] pub use block::LedgerState; -#[cfg(any(test, feature = "bench"))] +#[cfg(any(test, feature = "proptest-impl"))] pub mod test_utils; /// Error type alias to make working with generic errors easier. diff --git a/zebra-chain/src/test_utils.rs b/zebra-chain/src/test_utils.rs index 05990485aa5..ec546976a71 100644 --- a/zebra-chain/src/test_utils.rs +++ b/zebra-chain/src/test_utils.rs @@ -1,5 +1,5 @@ //!Chain functionality for fetching test vectors. //! -#[cfg(any(test, feature = "bench"))] +#[cfg(any(test, feature = "proptest-impl"))] mod vectors; diff --git a/zebra-chain/src/test_utils/vectors.rs b/zebra-chain/src/test_utils/vectors.rs index 23c51aa1c2b..0bf4073b2a0 100644 --- a/zebra-chain/src/test_utils/vectors.rs +++ b/zebra-chain/src/test_utils/vectors.rs @@ -1,17 +1,17 @@ //! Network methods for fetching blockchain vectors. //! -#[cfg(any(test, feature = "bench"))] +#[cfg(any(test, feature = "proptest-impl"))] use std::collections::BTreeMap; -#[cfg(any(test, feature = "bench"))] +#[cfg(any(test, feature = "proptest-impl"))] use crate::{ block::Block, parameters::Network, serialization::{SerializationError, ZcashDeserializeInto}, }; -#[cfg(any(test, feature = "bench"))] +#[cfg(any(test, feature = "proptest-impl"))] use zebra_test::vectors::{ BLOCK_MAINNET_1046400_BYTES, BLOCK_MAINNET_653599_BYTES, BLOCK_MAINNET_982681_BYTES, BLOCK_TESTNET_1116000_BYTES, BLOCK_TESTNET_583999_BYTES, BLOCK_TESTNET_925483_BYTES, @@ -22,7 +22,7 @@ use zebra_test::vectors::{ }; /// Network methods for fetching blockchain vectors. -#[cfg(any(test, feature = "bench"))] +#[cfg(any(test, feature = "proptest-impl"))] impl Network { /// Returns true if network is of type Mainnet. pub fn is_mainnet(&self) -> bool { diff --git a/zebra-consensus/src/checkpoint/tests.rs b/zebra-consensus/src/checkpoint/tests.rs index 9fb29048c40..a10353ab2c7 100644 --- a/zebra-consensus/src/checkpoint/tests.rs +++ b/zebra-consensus/src/checkpoint/tests.rs @@ -203,6 +203,7 @@ async fn multi_item_checkpoint_list() -> Result<(), Report> { Ok(()) } +#[cfg(any(test, feature = "proptest-impl"))] #[tokio::test(flavor = "multi_thread")] async fn continuous_blockchain_no_restart() -> Result<(), Report> { continuous_blockchain(None, Mainnet).await?; @@ -210,6 +211,7 @@ async fn continuous_blockchain_no_restart() -> Result<(), Report> { Ok(()) } +#[cfg(any(test, feature = "proptest-impl"))] #[tokio::test(flavor = "multi_thread")] async fn continuous_blockchain_restart() -> Result<(), Report> { for height in 0..zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS.len() { @@ -226,6 +228,7 @@ async fn continuous_blockchain_restart() -> Result<(), Report> { // This span is far too verbose for use during normal testing. // Turn the SPANDOC: comments into doc comments to re-enable. //#[spandoc::spandoc] +#[cfg(any(test, feature = "proptest-impl"))] async fn continuous_blockchain( restart_height: Option, network: Network, @@ -233,10 +236,8 @@ async fn continuous_blockchain( let _init_guard = zebra_test::init(); // A continuous blockchain - let blockchain = match network { - Mainnet => zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS.iter(), - Testnet => zebra_test::vectors::CONTINUOUS_TESTNET_BLOCKS.iter(), - }; + let blockchain = network.get_blockchain_iter(); + let blockchain: Vec<_> = blockchain .map(|(height, b)| { let block = Arc::::zcash_deserialize(*b).unwrap(); From 35b24faa721d44a1eb8b5f63d269a81df00410d8 Mon Sep 17 00:00:00 2001 From: idky137 Date: Thu, 22 Feb 2024 13:51:15 +0000 Subject: [PATCH 07/20] adding methods to zebra-consensus, lifetime error in src/transaction/tests.rs, needs refactoring --- zebra-consensus/src/block/tests.rs | 35 +++++------------------- zebra-consensus/src/checkpoint/tests.rs | 3 -- zebra-consensus/src/transaction/tests.rs | 6 ++-- 3 files changed, 9 insertions(+), 35 deletions(-) diff --git a/zebra-consensus/src/block/tests.rs b/zebra-consensus/src/block/tests.rs index 2eed87601d3..be7bfd80c3b 100644 --- a/zebra-consensus/src/block/tests.rs +++ b/zebra-consensus/src/block/tests.rs @@ -189,10 +189,7 @@ fn difficulty_is_valid_for_historical_blocks() -> Result<(), Report> { } 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(), - }; + let block_iter = network.get_block_iter(); for (&height, block) in block_iter { let block = block @@ -296,10 +293,7 @@ fn subsidy_is_valid_for_historical_blocks() -> Result<(), Report> { } fn subsidy_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(), - }; + let block_iter = network.get_block_iter(); for (&height, block) in block_iter { let block = block @@ -401,10 +395,7 @@ fn funding_stream_validation() -> Result<(), Report> { } fn funding_stream_validation_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(), - }; + let block_iter = network.get_block_iter(); let canopy_activation_height = NetworkUpgrade::Canopy .activation_height(network) @@ -479,10 +470,7 @@ fn miner_fees_validation_success() -> Result<(), Report> { } fn miner_fees_validation_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(), - }; + let block_iter = network.get_block_iter(); for (&height, block) in block_iter { if Height(height) > SLOW_START_SHIFT { @@ -568,10 +556,7 @@ fn merkle_root_is_valid() -> Result<(), Report> { } fn merkle_root_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(), - }; + let block_iter = network.get_block_iter(); for (_height, block) in block_iter { let block = block @@ -592,10 +577,7 @@ fn merkle_root_is_valid_for_network(network: Network) -> Result<(), Report> { } fn merkle_root_fake_v5_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(), - }; + let block_iter = network.get_block_iter(); for (height, block) in block_iter { let mut block = block @@ -707,10 +689,7 @@ fn transaction_expiration_height_validation() -> Result<(), Report> { } fn transaction_expiration_height_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(), - }; + let block_iter = network.get_block_iter(); for (&height, block) in block_iter { let block = Block::zcash_deserialize(&block[..]).expect("block should deserialize"); diff --git a/zebra-consensus/src/checkpoint/tests.rs b/zebra-consensus/src/checkpoint/tests.rs index a10353ab2c7..57472ee9cee 100644 --- a/zebra-consensus/src/checkpoint/tests.rs +++ b/zebra-consensus/src/checkpoint/tests.rs @@ -203,7 +203,6 @@ async fn multi_item_checkpoint_list() -> Result<(), Report> { Ok(()) } -#[cfg(any(test, feature = "proptest-impl"))] #[tokio::test(flavor = "multi_thread")] async fn continuous_blockchain_no_restart() -> Result<(), Report> { continuous_blockchain(None, Mainnet).await?; @@ -211,7 +210,6 @@ async fn continuous_blockchain_no_restart() -> Result<(), Report> { Ok(()) } -#[cfg(any(test, feature = "proptest-impl"))] #[tokio::test(flavor = "multi_thread")] async fn continuous_blockchain_restart() -> Result<(), Report> { for height in 0..zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS.len() { @@ -228,7 +226,6 @@ async fn continuous_blockchain_restart() -> Result<(), Report> { // This span is far too verbose for use during normal testing. // Turn the SPANDOC: comments into doc comments to re-enable. //#[spandoc::spandoc] -#[cfg(any(test, feature = "proptest-impl"))] async fn continuous_blockchain( restart_height: Option, network: Network, diff --git a/zebra-consensus/src/transaction/tests.rs b/zebra-consensus/src/transaction/tests.rs index 9b0e6aaaee4..5ca7cca8e67 100644 --- a/zebra-consensus/src/transaction/tests.rs +++ b/zebra-consensus/src/transaction/tests.rs @@ -914,6 +914,7 @@ fn v5_transaction_is_accepted_after_nu5_activation_for_network(network: Network) Network::Mainnet => zebra_test::vectors::MAINNET_BLOCKS.iter(), Network::Testnet => zebra_test::vectors::TESTNET_BLOCKS.iter(), }; + // let blocks = network.get_block_iter(); let state_service = service_fn(|_| async { unreachable!("Service should not be called") }); let verifier = Verifier::new(network, state_service); @@ -2772,10 +2773,7 @@ fn coinbase_outputs_are_decryptable_for_historical_blocks() -> Result<(), Report fn coinbase_outputs_are_decryptable_for_historical_blocks_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(), - }; + let block_iter = network.get_block_iter(); let mut tested_coinbase_txs = 0; let mut tested_non_coinbase_txs = 0; From fa4ebb9c496a7423d3b7e8a06a6674d722711f14 Mon Sep 17 00:00:00 2001 From: idky137 Date: Thu, 22 Feb 2024 15:34:55 +0000 Subject: [PATCH 08/20] finished adding methods to zebra-consensus --- zebra-chain/src/test_utils/vectors.rs | 2 +- zebra-consensus/src/transaction/tests.rs | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/zebra-chain/src/test_utils/vectors.rs b/zebra-chain/src/test_utils/vectors.rs index 0bf4073b2a0..9fb4931f6f5 100644 --- a/zebra-chain/src/test_utils/vectors.rs +++ b/zebra-chain/src/test_utils/vectors.rs @@ -30,7 +30,7 @@ impl Network { } /// Returns iterator over blocks. - pub fn get_block_iter(&self) -> std::collections::btree_map::Iter<'_, u32, &[u8]> { + pub fn get_block_iter(&self) -> std::collections::btree_map::Iter<'static, u32, &'static [u8]> { if self.is_mainnet() { MAINNET_BLOCKS.iter() } else { diff --git a/zebra-consensus/src/transaction/tests.rs b/zebra-consensus/src/transaction/tests.rs index 5ca7cca8e67..5f4b0b3ac5e 100644 --- a/zebra-consensus/src/transaction/tests.rs +++ b/zebra-consensus/src/transaction/tests.rs @@ -909,12 +909,7 @@ fn v5_transaction_is_accepted_after_nu5_activation_for_network(network: Network) let nu5_activation_height = nu5 .activation_height(network) .expect("NU5 activation height is specified"); - - let blocks = match network { - Network::Mainnet => zebra_test::vectors::MAINNET_BLOCKS.iter(), - Network::Testnet => zebra_test::vectors::TESTNET_BLOCKS.iter(), - }; - // let blocks = network.get_block_iter(); + let blocks = network.get_block_iter(); let state_service = service_fn(|_| async { unreachable!("Service should not be called") }); let verifier = Verifier::new(network, state_service); From ed34829009c795cf46a62ff181a73b9828983a2e Mon Sep 17 00:00:00 2001 From: idky137 Date: Thu, 22 Feb 2024 16:36:03 +0000 Subject: [PATCH 09/20] finished adding new methods to zebrad --- zebra-chain/src/test_utils/vectors.rs | 14 +++++++++----- zebrad/src/components/mempool/storage/tests.rs | 5 +---- .../components/mempool/storage/tests/vectors.rs | 10 +--------- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/zebra-chain/src/test_utils/vectors.rs b/zebra-chain/src/test_utils/vectors.rs index 9fb4931f6f5..e07741edc3f 100644 --- a/zebra-chain/src/test_utils/vectors.rs +++ b/zebra-chain/src/test_utils/vectors.rs @@ -48,18 +48,22 @@ impl Network { } /// Returns block bytes - pub fn get_block_bytes(&self, version: u32) -> Result { + pub fn get_block_bytes( + &self, + main_bytes: u32, + test_bytes: u32, + ) -> Result { if self.is_mainnet() { - match version { + match main_bytes { 653_599 => BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into(), 982_681 => BLOCK_MAINNET_982681_BYTES.zcash_deserialize_into(), - _ => Err(SerializationError::UnsupportedVersion(version)), + _ => Err(SerializationError::UnsupportedVersion(main_bytes)), } } else { - match version { + match test_bytes { 583_999 => BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into(), 925_483 => BLOCK_TESTNET_925483_BYTES.zcash_deserialize_into(), - _ => Err(SerializationError::UnsupportedVersion(version)), + _ => Err(SerializationError::UnsupportedVersion(test_bytes)), } } } diff --git a/zebrad/src/components/mempool/storage/tests.rs b/zebrad/src/components/mempool/storage/tests.rs index cad844108c4..7abd5638029 100644 --- a/zebrad/src/components/mempool/storage/tests.rs +++ b/zebrad/src/components/mempool/storage/tests.rs @@ -17,10 +17,7 @@ pub fn unmined_transactions_in_blocks( block_height_range: impl RangeBounds, network: Network, ) -> impl DoubleEndedIterator { - let blocks = match network { - Network::Mainnet => zebra_test::vectors::MAINNET_BLOCKS.iter(), - Network::Testnet => zebra_test::vectors::TESTNET_BLOCKS.iter(), - }; + let blocks = network.get_block_iter(); // Deserialize the blocks that are selected based on the specified `block_height_range`. let selected_blocks = blocks diff --git a/zebrad/src/components/mempool/storage/tests/vectors.rs b/zebrad/src/components/mempool/storage/tests/vectors.rs index 35827574946..3df298fc470 100644 --- a/zebrad/src/components/mempool/storage/tests/vectors.rs +++ b/zebrad/src/components/mempool/storage/tests/vectors.rs @@ -8,7 +8,6 @@ use zebra_chain::{ amount::Amount, block::{Block, Height}, parameters::Network, - serialization::ZcashDeserializeInto, transaction::{UnminedTxId, VerifiedUnminedTx}, }; @@ -252,14 +251,7 @@ fn mempool_expired_basic_for_network(network: Network) -> Result<()> { ..Default::default() }); - let block: Block = match network { - Network::Mainnet => { - zebra_test::vectors::BLOCK_MAINNET_982681_BYTES.zcash_deserialize_into()? - } - Network::Testnet => { - zebra_test::vectors::BLOCK_TESTNET_925483_BYTES.zcash_deserialize_into()? - } - }; + let block: Block = network.get_block_bytes(982681, 925483)?; // Get a test transaction let tx = &*(block.transactions[1]).clone(); From eb987220c9f581e018a5ae312655d657d13663e7 Mon Sep 17 00:00:00 2001 From: idky137 Date: Thu, 22 Feb 2024 17:44:37 +0000 Subject: [PATCH 10/20] added new methods to zebra-rpc and zebra-scan --- zebra-chain/src/test_utils/vectors.rs | 11 ++++++----- zebra-rpc/src/methods/tests/snapshot.rs | 5 +---- zebra-rpc/src/methods/tests/utils.rs | 15 +++------------ zebra-rpc/src/methods/tests/vectors.rs | 13 +++++-------- zebra-scan/src/storage/db/tests.rs | 7 ++----- 5 files changed, 17 insertions(+), 34 deletions(-) diff --git a/zebra-chain/src/test_utils/vectors.rs b/zebra-chain/src/test_utils/vectors.rs index e07741edc3f..d01db077d00 100644 --- a/zebra-chain/src/test_utils/vectors.rs +++ b/zebra-chain/src/test_utils/vectors.rs @@ -117,23 +117,24 @@ impl Network { /// Returns block and sapling root bytes pub fn get_block_sapling_roots_bytes( &self, - version: u32, + main_bytes: u32, + test_bytes: u32, ) -> Result<(&[u8], [u8; 32]), SerializationError> { if self.is_mainnet() { - match version { + match main_bytes { 1_046_400_ => Ok(( &BLOCK_MAINNET_1046400_BYTES[..], *SAPLING_FINAL_ROOT_MAINNET_1046400_BYTES, )), - _ => Err(SerializationError::UnsupportedVersion(version)), + _ => Err(SerializationError::UnsupportedVersion(main_bytes)), } } else { - match version { + match test_bytes { 1_116_000 => Ok(( &BLOCK_TESTNET_1116000_BYTES[..], *SAPLING_FINAL_ROOT_TESTNET_1116000_BYTES, )), - _ => Err(SerializationError::UnsupportedVersion(version)), + _ => Err(SerializationError::UnsupportedVersion(test_bytes)), } } } diff --git a/zebra-rpc/src/methods/tests/snapshot.rs b/zebra-rpc/src/methods/tests/snapshot.rs index 2a8e9149f56..5203714f937 100644 --- a/zebra-rpc/src/methods/tests/snapshot.rs +++ b/zebra-rpc/src/methods/tests/snapshot.rs @@ -44,10 +44,7 @@ async fn test_rpc_response_data() { async fn test_rpc_response_data_for_network(network: Network) { // Create a continuous chain of mainnet and testnet blocks from genesis - let block_data = match network { - Mainnet => &*zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS, - Testnet => &*zebra_test::vectors::CONTINUOUS_TESTNET_BLOCKS, - }; + let block_data = network.get_blockchain_map(); let blocks: Vec> = block_data .iter() diff --git a/zebra-rpc/src/methods/tests/utils.rs b/zebra-rpc/src/methods/tests/utils.rs index 797d712a6a8..a8ab26f7395 100644 --- a/zebra-rpc/src/methods/tests/utils.rs +++ b/zebra-rpc/src/methods/tests/utils.rs @@ -9,20 +9,11 @@ use zebra_chain::{ serialization::ZcashDeserialize, }; -use zebra_test::vectors; - /// Create a history tree with one single block for a network by using Zebra test vectors. pub fn fake_history_tree(network: Network) -> Arc { - let (block, sapling_root) = match network { - Network::Mainnet => ( - &vectors::BLOCK_MAINNET_1046400_BYTES[..], - *vectors::SAPLING_FINAL_ROOT_MAINNET_1046400_BYTES, - ), - Network::Testnet => ( - &vectors::BLOCK_TESTNET_1116000_BYTES[..], - *vectors::SAPLING_FINAL_ROOT_TESTNET_1116000_BYTES, - ), - }; + let (block, sapling_root) = network + .get_block_sapling_roots_bytes(1046400, 1116000) + .unwrap(); let block = Arc::::zcash_deserialize(block).expect("block should deserialize"); let first_sapling_root = Root::try_from(sapling_root).unwrap(); diff --git a/zebra-rpc/src/methods/tests/vectors.rs b/zebra-rpc/src/methods/tests/vectors.rs index 105dd50a386..14bad0398f3 100644 --- a/zebra-rpc/src/methods/tests/vectors.rs +++ b/zebra-rpc/src/methods/tests/vectors.rs @@ -655,14 +655,11 @@ async fn rpc_getaddresstxids_response() { let _init_guard = zebra_test::init(); for network in [Mainnet, Testnet] { - let blocks: Vec> = match network { - Mainnet => &*zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS, - Testnet => &*zebra_test::vectors::CONTINUOUS_TESTNET_BLOCKS, - } - .iter() - .map(|(_height, block_bytes)| block_bytes.zcash_deserialize_into().unwrap()) - .collect(); - + let blocks: Vec> = network + .get_blockchain_map() + .iter() + .map(|(_height, block_bytes)| block_bytes.zcash_deserialize_into().unwrap()) + .collect(); // The first few blocks after genesis send funds to the same founders reward address, // in one output per coinbase transaction. // diff --git a/zebra-scan/src/storage/db/tests.rs b/zebra-scan/src/storage/db/tests.rs index f34650ab262..9135cc404ab 100644 --- a/zebra-scan/src/storage/db/tests.rs +++ b/zebra-scan/src/storage/db/tests.rs @@ -4,7 +4,7 @@ use std::{collections::BTreeMap, sync::Arc}; use zebra_chain::{ block::{Block, Height}, - parameters::Network::{self, *}, + parameters::Network::{self}, serialization::ZcashDeserializeInto, transaction, }; @@ -45,10 +45,7 @@ pub fn add_fake_results( height: Height, add_progress_marker: bool, ) { - let blocks = match network { - Mainnet => &*zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS, - Testnet => &*zebra_test::vectors::CONTINUOUS_TESTNET_BLOCKS, - }; + let blocks = network.get_blockchain_map(); let block: Arc = blocks .get(&height.0) From 8960c45ccffdb239e7fe531eb6db9e57dc2a1e99 Mon Sep 17 00:00:00 2001 From: idky137 Date: Thu, 22 Feb 2024 19:24:10 +0000 Subject: [PATCH 11/20] finished removing statements matching on Network from tests --- zebra-chain/src/test_utils/vectors.rs | 9 ++ .../tests/snapshot/get_block_template_rpcs.rs | 14 +-- .../disk_format/tests/snapshot.rs | 5 +- .../zebra_db/block/tests/snapshot.rs | 5 +- .../non_finalized_state/tests/vectors.rs | 101 +++++------------- zebra-state/src/service/tests.rs | 5 +- zebra-state/tests/basic.rs | 11 +- 7 files changed, 54 insertions(+), 96 deletions(-) diff --git a/zebra-chain/src/test_utils/vectors.rs b/zebra-chain/src/test_utils/vectors.rs index d01db077d00..8a082c25606 100644 --- a/zebra-chain/src/test_utils/vectors.rs +++ b/zebra-chain/src/test_utils/vectors.rs @@ -38,6 +38,15 @@ impl Network { } } + /// + pub fn get_block_map(&self) -> &BTreeMap { + if self.is_mainnet() { + &*zebra_test::vectors::MAINNET_BLOCKS + } else { + &*zebra_test::vectors::TESTNET_BLOCKS + } + } + /// Returns genesis block for chain. pub fn get_gen_block(&self) -> std::option::Option<&&[u8]> { if self.is_mainnet() { diff --git a/zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs b/zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs index e4e6dceaf21..c7299565abf 100644 --- a/zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs +++ b/zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs @@ -413,9 +413,10 @@ pub async fn test_responses( snapshot_rpc_submit_block_invalid(submit_block, &settings); // `validateaddress` - let founder_address = match network { - Network::Mainnet => "t3fqvkzrrNaMcamkQMwAyHRjfDdM2xQvDTR", - Network::Testnet => "t2UNzUUx8mWBCRYPRezvA363EYXyEpHokyi", + let founder_address = if network.is_mainnet() { + "t3fqvkzrrNaMcamkQMwAyHRjfDdM2xQvDTR" + } else { + "t2UNzUUx8mWBCRYPRezvA363EYXyEpHokyi" }; let validate_address = get_block_template_rpc @@ -431,9 +432,10 @@ pub async fn test_responses( snapshot_rpc_validateaddress("invalid", validate_address, &settings); // `z_validateaddress` - let founder_address = match network { - Network::Mainnet => "t3fqvkzrrNaMcamkQMwAyHRjfDdM2xQvDTR", - Network::Testnet => "t2UNzUUx8mWBCRYPRezvA363EYXyEpHokyi", + let founder_address = if network.is_mainnet() { + "t3fqvkzrrNaMcamkQMwAyHRjfDdM2xQvDTR" + } else { + "t2UNzUUx8mWBCRYPRezvA363EYXyEpHokyi" }; let z_validate_address = get_block_template_rpc diff --git a/zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs b/zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs index 5866b08ddc2..a496413f981 100644 --- a/zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs +++ b/zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs @@ -89,10 +89,7 @@ fn test_raw_rocksdb_column_families_with_network(network: Network) { // Snapshot raw database data for: // - mainnet and testnet // - genesis, block 1, and block 2 - let blocks = match network { - Mainnet => &*zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS, - Testnet => &*zebra_test::vectors::CONTINUOUS_TESTNET_BLOCKS, - }; + let blocks = network.get_blockchain_map(); // We limit the number of blocks, because the serialized data is a few kilobytes per block. for height in 0..=2 { diff --git a/zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs b/zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs index 6fc96f8dff2..0d1b85f33bc 100644 --- a/zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs +++ b/zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs @@ -181,10 +181,7 @@ fn test_block_and_transaction_data_with_network(network: Network) { // Snapshot block and transaction database data for: // - mainnet and testnet // - genesis, block 1, and block 2 - let blocks = match network { - Mainnet => &*zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS, - Testnet => &*zebra_test::vectors::CONTINUOUS_TESTNET_BLOCKS, - }; + let blocks = network.get_blockchain_map(); // We limit the number of blocks, because the serialized data is a few kilobytes per block. // diff --git a/zebra-state/src/service/non_finalized_state/tests/vectors.rs b/zebra-state/src/service/non_finalized_state/tests/vectors.rs index eae0235ea01..e1ec6a29663 100644 --- a/zebra-state/src/service/non_finalized_state/tests/vectors.rs +++ b/zebra-state/src/service/non_finalized_state/tests/vectors.rs @@ -141,17 +141,10 @@ fn best_chain_wins() -> Result<()> { } fn best_chain_wins_for_network(network: Network) -> Result<()> { - let block1: Arc = match network { - // Since the brand new FinalizedState below will pass a None history tree - // to the NonFinalizedState, we must use pre-Heartwood blocks since - // they won't trigger the history tree update in the NonFinalizedState. - Network::Mainnet => { - zebra_test::vectors::BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into()? - } - Network::Testnet => { - zebra_test::vectors::BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into()? - } - }; + // Since the brand new FinalizedState below will pass a None history tree + // to the NonFinalizedState, we must use pre-Heartwood blocks since + // they won't trigger the history tree update in the NonFinalizedState. + let block1: Arc = Arc::new(network.get_block_bytes(653599, 583999)?); let block2 = block1.make_fake_child().set_work(10); let child = block1.make_fake_child().set_work(1); @@ -186,17 +179,10 @@ fn finalize_pops_from_best_chain() -> Result<()> { } fn finalize_pops_from_best_chain_for_network(network: Network) -> Result<()> { - let block1: Arc = match network { - // Since the brand new FinalizedState below will pass a None history tree - // to the NonFinalizedState, we must use pre-Heartwood blocks since - // they won't trigger the history tree update in the NonFinalizedState. - Network::Mainnet => { - zebra_test::vectors::BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into()? - } - Network::Testnet => { - zebra_test::vectors::BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into()? - } - }; + // Since the brand new FinalizedState below will pass a None history tree + // to the NonFinalizedState, we must use pre-Heartwood blocks since + // they won't trigger the history tree update in the NonFinalizedState. + let block1: Arc = Arc::new(network.get_block_bytes(653599, 583999)?); let block2 = block1.make_fake_child().set_work(10); let child = block1.make_fake_child().set_work(1); @@ -242,17 +228,10 @@ fn commit_block_extending_best_chain_doesnt_drop_worst_chains() -> Result<()> { fn commit_block_extending_best_chain_doesnt_drop_worst_chains_for_network( network: Network, ) -> Result<()> { - let block1: Arc = match network { - // Since the brand new FinalizedState below will pass a None history tree - // to the NonFinalizedState, we must use pre-Heartwood blocks since - // they won't trigger the history tree update in the NonFinalizedState. - Network::Mainnet => { - zebra_test::vectors::BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into()? - } - Network::Testnet => { - zebra_test::vectors::BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into()? - } - }; + // Since the brand new FinalizedState below will pass a None history tree + // to the NonFinalizedState, we must use pre-Heartwood blocks since + // they won't trigger the history tree update in the NonFinalizedState. + let block1: Arc = Arc::new(network.get_block_bytes(653599, 583999)?); let block2 = block1.make_fake_child().set_work(10); let child1 = block1.make_fake_child().set_work(1); @@ -293,17 +272,10 @@ fn shorter_chain_can_be_best_chain() -> Result<()> { } fn shorter_chain_can_be_best_chain_for_network(network: Network) -> Result<()> { - let block1: Arc = match network { - // Since the brand new FinalizedState below will pass a None history tree - // to the NonFinalizedState, we must use pre-Heartwood blocks since - // they won't trigger the history tree update in the NonFinalizedState. - Network::Mainnet => { - zebra_test::vectors::BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into()? - } - Network::Testnet => { - zebra_test::vectors::BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into()? - } - }; + // Since the brand new FinalizedState below will pass a None history tree + // to the NonFinalizedState, we must use pre-Heartwood blocks since + // they won't trigger the history tree update in the NonFinalizedState. + let block1: Arc = Arc::new(network.get_block_bytes(653599, 583999)?); let long_chain_block1 = block1.make_fake_child().set_work(1); let long_chain_block2 = long_chain_block1.make_fake_child().set_work(1); @@ -343,17 +315,10 @@ fn longer_chain_with_more_work_wins() -> Result<()> { } fn longer_chain_with_more_work_wins_for_network(network: Network) -> Result<()> { - let block1: Arc = match network { - // Since the brand new FinalizedState below will pass a None history tree - // to the NonFinalizedState, we must use pre-Heartwood blocks since - // they won't trigger the history tree update in the NonFinalizedState. - Network::Mainnet => { - zebra_test::vectors::BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into()? - } - Network::Testnet => { - zebra_test::vectors::BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into()? - } - }; + // Since the brand new FinalizedState below will pass a None history tree + // to the NonFinalizedState, we must use pre-Heartwood blocks since + // they won't trigger the history tree update in the NonFinalizedState. + let block1: Arc = Arc::new(network.get_block_bytes(653599, 583999)?); let long_chain_block1 = block1.make_fake_child().set_work(1); let long_chain_block2 = long_chain_block1.make_fake_child().set_work(1); @@ -396,17 +361,10 @@ fn equal_length_goes_to_more_work() -> Result<()> { Ok(()) } fn equal_length_goes_to_more_work_for_network(network: Network) -> Result<()> { - let block1: Arc = match network { - // Since the brand new FinalizedState below will pass a None history tree - // to the NonFinalizedState, we must use pre-Heartwood blocks since - // they won't trigger the history tree update in the NonFinalizedState. - Network::Mainnet => { - zebra_test::vectors::BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into()? - } - Network::Testnet => { - zebra_test::vectors::BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into()? - } - }; + // Since the brand new FinalizedState below will pass a None history tree + // to the NonFinalizedState, we must use pre-Heartwood blocks since + // they won't trigger the history tree update in the NonFinalizedState. + let block1: Arc = Arc::new(network.get_block_bytes(653599, 583999)?); let less_work_child = block1.make_fake_child().set_work(1); let more_work_child = block1.make_fake_child().set_work(3); @@ -447,10 +405,8 @@ fn history_tree_is_updated_for_network_upgrade( network: Network, network_upgrade: NetworkUpgrade, ) -> Result<()> { - let blocks = match network { - Network::Mainnet => &*zebra_test::vectors::MAINNET_BLOCKS, - Network::Testnet => &*zebra_test::vectors::TESTNET_BLOCKS, - }; + let blocks = network.get_block_map(); + let height = network_upgrade.activation_height(network).unwrap().0; let prev_block = Arc::new( @@ -548,10 +504,7 @@ fn commitment_is_validated() { } fn commitment_is_validated_for_network_upgrade(network: Network, network_upgrade: NetworkUpgrade) { - let blocks = match network { - Network::Mainnet => &*zebra_test::vectors::MAINNET_BLOCKS, - Network::Testnet => &*zebra_test::vectors::TESTNET_BLOCKS, - }; + let blocks = network.get_block_map(); let height = network_upgrade.activation_height(network).unwrap().0; let prev_block = Arc::new( diff --git a/zebra-state/src/service/tests.rs b/zebra-state/src/service/tests.rs index 0321e1bb965..6de5d0b616e 100644 --- a/zebra-state/src/service/tests.rs +++ b/zebra-state/src/service/tests.rs @@ -562,10 +562,7 @@ fn continuous_empty_blocks_from_test_vectors() -> impl Strategy< any::() .prop_flat_map(|network| { // Select the test vector based on the network - let raw_blocks = match network { - Network::Mainnet => &*zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS, - Network::Testnet => &*zebra_test::vectors::CONTINUOUS_TESTNET_BLOCKS, - }; + let raw_blocks = network.get_blockchain_map(); // Transform the test vector's block bytes into a vector of `SemanticallyVerifiedBlock`s. let blocks: Vec<_> = raw_blocks diff --git a/zebra-state/tests/basic.rs b/zebra-state/tests/basic.rs index 638ab0f1a41..a96b176dc1a 100644 --- a/zebra-state/tests/basic.rs +++ b/zebra-state/tests/basic.rs @@ -73,10 +73,13 @@ async fn check_transcripts(network: Network) -> Result<(), Report> { let mainnet_transcript = &[&COMMIT_FINALIZED_BLOCK_MAINNET]; let testnet_transcript = &[&COMMIT_FINALIZED_BLOCK_TESTNET]; - for transcript_data in match network { - Network::Mainnet => mainnet_transcript, - Network::Testnet => testnet_transcript, - } { + let net_data = if network.is_mainnet() { + mainnet_transcript + } else { + testnet_transcript + }; + + for transcript_data in net_data { // We're not verifying UTXOs here. let (service, _, _, _) = zebra_state::init(Config::ephemeral(), network, Height::MAX, 0); let transcript = Transcript::from(transcript_data.iter().cloned()); From fc17002d453eb4595d73890c5bdb2fe62f7ae463 Mon Sep 17 00:00:00 2001 From: idky137 Date: Fri, 23 Feb 2024 16:19:50 +0000 Subject: [PATCH 12/20] updated new error message --- zebra-chain/src/serialization/error.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zebra-chain/src/serialization/error.rs b/zebra-chain/src/serialization/error.rs index 083ad765caf..25a38901778 100644 --- a/zebra-chain/src/serialization/error.rs +++ b/zebra-chain/src/serialization/error.rs @@ -51,7 +51,7 @@ pub enum SerializationError { #[error("transaction balance is non-zero but doesn't have Sapling shielded spends or outputs")] BadTransactionBalance, - /// Invalid BLOCK_MAINNET_******_BYTES version given to vector fetcher - #[error("invalid block bytes version")] + /// Invalid BLOCK_MAINNET_******_BYTES vector ID given to vector fetcher + #[error("invalid mainnet / testnet bytes id given to vector fetcher")] UnsupportedVersion(u32), } From 2e14602e8fecf54aceec9886e348ce720966ec96 Mon Sep 17 00:00:00 2001 From: idky137 Date: Fri, 1 Mar 2024 12:46:55 +0000 Subject: [PATCH 13/20] changed get_block_bytes() and get_block_sapling_roots_bytes to return option and removed serialization error types as per requested changes in PR review --- zebra-chain/src/serialization/error.rs | 4 -- zebra-chain/src/test_utils.rs | 1 - zebra-chain/src/test_utils/vectors.rs | 63 +++++++------------ zebra-rpc/src/methods/tests/utils.rs | 2 +- .../non_finalized_state/tests/vectors.rs | 12 ++-- .../mempool/storage/tests/vectors.rs | 2 +- 6 files changed, 29 insertions(+), 55 deletions(-) diff --git a/zebra-chain/src/serialization/error.rs b/zebra-chain/src/serialization/error.rs index 25a38901778..17566548d1a 100644 --- a/zebra-chain/src/serialization/error.rs +++ b/zebra-chain/src/serialization/error.rs @@ -50,8 +50,4 @@ pub enum SerializationError { /// rule](https://zips.z.cash/protocol/protocol.pdf#txnencodingandconsensus). #[error("transaction balance is non-zero but doesn't have Sapling shielded spends or outputs")] BadTransactionBalance, - - /// Invalid BLOCK_MAINNET_******_BYTES vector ID given to vector fetcher - #[error("invalid mainnet / testnet bytes id given to vector fetcher")] - UnsupportedVersion(u32), } diff --git a/zebra-chain/src/test_utils.rs b/zebra-chain/src/test_utils.rs index ec546976a71..7cfa4a95955 100644 --- a/zebra-chain/src/test_utils.rs +++ b/zebra-chain/src/test_utils.rs @@ -1,5 +1,4 @@ //!Chain functionality for fetching test vectors. //! -#[cfg(any(test, feature = "proptest-impl"))] mod vectors; diff --git a/zebra-chain/src/test_utils/vectors.rs b/zebra-chain/src/test_utils/vectors.rs index 8a082c25606..36170b15db9 100644 --- a/zebra-chain/src/test_utils/vectors.rs +++ b/zebra-chain/src/test_utils/vectors.rs @@ -1,17 +1,10 @@ //! Network methods for fetching blockchain vectors. //! -#[cfg(any(test, feature = "proptest-impl"))] use std::collections::BTreeMap; -#[cfg(any(test, feature = "proptest-impl"))] -use crate::{ - block::Block, - parameters::Network, - serialization::{SerializationError, ZcashDeserializeInto}, -}; +use crate::{block::Block, parameters::Network, serialization::ZcashDeserializeInto}; -#[cfg(any(test, feature = "proptest-impl"))] use zebra_test::vectors::{ BLOCK_MAINNET_1046400_BYTES, BLOCK_MAINNET_653599_BYTES, BLOCK_MAINNET_982681_BYTES, BLOCK_TESTNET_1116000_BYTES, BLOCK_TESTNET_583999_BYTES, BLOCK_TESTNET_925483_BYTES, @@ -22,7 +15,6 @@ use zebra_test::vectors::{ }; /// Network methods for fetching blockchain vectors. -#[cfg(any(test, feature = "proptest-impl"))] impl Network { /// Returns true if network is of type Mainnet. pub fn is_mainnet(&self) -> bool { @@ -57,24 +49,15 @@ impl Network { } /// Returns block bytes - pub fn get_block_bytes( - &self, - main_bytes: u32, - test_bytes: u32, - ) -> Result { - if self.is_mainnet() { - match main_bytes { - 653_599 => BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into(), - 982_681 => BLOCK_MAINNET_982681_BYTES.zcash_deserialize_into(), - _ => Err(SerializationError::UnsupportedVersion(main_bytes)), - } - } else { - match test_bytes { - 583_999 => BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into(), - 925_483 => BLOCK_TESTNET_925483_BYTES.zcash_deserialize_into(), - _ => Err(SerializationError::UnsupportedVersion(test_bytes)), - } - } + pub fn get_test_block(&self, main_height: u32, test_height: u32) -> Option { + let block_bytes = match (self.is_mainnet(), main_height, test_height) { + (true, 653_599, _) => BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into().ok(), + (true, 982_681, _) => BLOCK_MAINNET_982681_BYTES.zcash_deserialize_into().ok(), + (false, _, 583_999) => BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into().ok(), + (false, _, 925_483) => BLOCK_TESTNET_925483_BYTES.zcash_deserialize_into().ok(), + _ => return None, + }; + block_bytes } /// Returns iterator over blockchain. @@ -124,28 +107,24 @@ impl Network { } /// Returns block and sapling root bytes - pub fn get_block_sapling_roots_bytes( + pub fn get_test_block_sapling_roots( &self, - main_bytes: u32, - test_bytes: u32, - ) -> Result<(&[u8], [u8; 32]), SerializationError> { - if self.is_mainnet() { - match main_bytes { - 1_046_400_ => Ok(( + main_height: u32, + test_height: u32, + ) -> Option<(&[u8], [u8; 32])> { + let block_bytes: Option<(&[u8], [u8; 32])> = + match (self.is_mainnet(), main_height, test_height) { + (true, 1_046_400, _) => Some(( &BLOCK_MAINNET_1046400_BYTES[..], *SAPLING_FINAL_ROOT_MAINNET_1046400_BYTES, )), - _ => Err(SerializationError::UnsupportedVersion(main_bytes)), - } - } else { - match test_bytes { - 1_116_000 => Ok(( + (false, _, 1_116_000) => Some(( &BLOCK_TESTNET_1116000_BYTES[..], *SAPLING_FINAL_ROOT_TESTNET_1116000_BYTES, )), - _ => Err(SerializationError::UnsupportedVersion(test_bytes)), - } - } + _ => return None, + }; + block_bytes } /// Returns BTreemap of blocks and sprout roots, and last split height. diff --git a/zebra-rpc/src/methods/tests/utils.rs b/zebra-rpc/src/methods/tests/utils.rs index a8ab26f7395..e8c32fb3e09 100644 --- a/zebra-rpc/src/methods/tests/utils.rs +++ b/zebra-rpc/src/methods/tests/utils.rs @@ -12,7 +12,7 @@ use zebra_chain::{ /// Create a history tree with one single block for a network by using Zebra test vectors. pub fn fake_history_tree(network: Network) -> Arc { let (block, sapling_root) = network - .get_block_sapling_roots_bytes(1046400, 1116000) + .get_test_block_sapling_roots(1046400, 1116000) .unwrap(); let block = Arc::::zcash_deserialize(block).expect("block should deserialize"); diff --git a/zebra-state/src/service/non_finalized_state/tests/vectors.rs b/zebra-state/src/service/non_finalized_state/tests/vectors.rs index e1ec6a29663..da9c3e1bec6 100644 --- a/zebra-state/src/service/non_finalized_state/tests/vectors.rs +++ b/zebra-state/src/service/non_finalized_state/tests/vectors.rs @@ -144,7 +144,7 @@ fn best_chain_wins_for_network(network: Network) -> Result<()> { // Since the brand new FinalizedState below will pass a None history tree // to the NonFinalizedState, we must use pre-Heartwood blocks since // they won't trigger the history tree update in the NonFinalizedState. - let block1: Arc = Arc::new(network.get_block_bytes(653599, 583999)?); + let block1: Arc = Arc::new(network.get_test_block(653599, 583999).unwrap()); let block2 = block1.make_fake_child().set_work(10); let child = block1.make_fake_child().set_work(1); @@ -182,7 +182,7 @@ fn finalize_pops_from_best_chain_for_network(network: Network) -> Result<()> { // Since the brand new FinalizedState below will pass a None history tree // to the NonFinalizedState, we must use pre-Heartwood blocks since // they won't trigger the history tree update in the NonFinalizedState. - let block1: Arc = Arc::new(network.get_block_bytes(653599, 583999)?); + let block1: Arc = Arc::new(network.get_test_block(653599, 583999).unwrap()); let block2 = block1.make_fake_child().set_work(10); let child = block1.make_fake_child().set_work(1); @@ -231,7 +231,7 @@ fn commit_block_extending_best_chain_doesnt_drop_worst_chains_for_network( // Since the brand new FinalizedState below will pass a None history tree // to the NonFinalizedState, we must use pre-Heartwood blocks since // they won't trigger the history tree update in the NonFinalizedState. - let block1: Arc = Arc::new(network.get_block_bytes(653599, 583999)?); + let block1: Arc = Arc::new(network.get_test_block(653599, 583999).unwrap()); let block2 = block1.make_fake_child().set_work(10); let child1 = block1.make_fake_child().set_work(1); @@ -275,7 +275,7 @@ fn shorter_chain_can_be_best_chain_for_network(network: Network) -> Result<()> { // Since the brand new FinalizedState below will pass a None history tree // to the NonFinalizedState, we must use pre-Heartwood blocks since // they won't trigger the history tree update in the NonFinalizedState. - let block1: Arc = Arc::new(network.get_block_bytes(653599, 583999)?); + let block1: Arc = Arc::new(network.get_test_block(653599, 583999).unwrap()); let long_chain_block1 = block1.make_fake_child().set_work(1); let long_chain_block2 = long_chain_block1.make_fake_child().set_work(1); @@ -318,7 +318,7 @@ fn longer_chain_with_more_work_wins_for_network(network: Network) -> Result<()> // Since the brand new FinalizedState below will pass a None history tree // to the NonFinalizedState, we must use pre-Heartwood blocks since // they won't trigger the history tree update in the NonFinalizedState. - let block1: Arc = Arc::new(network.get_block_bytes(653599, 583999)?); + let block1: Arc = Arc::new(network.get_test_block(653599, 583999).unwrap()); let long_chain_block1 = block1.make_fake_child().set_work(1); let long_chain_block2 = long_chain_block1.make_fake_child().set_work(1); @@ -364,7 +364,7 @@ fn equal_length_goes_to_more_work_for_network(network: Network) -> Result<()> { // Since the brand new FinalizedState below will pass a None history tree // to the NonFinalizedState, we must use pre-Heartwood blocks since // they won't trigger the history tree update in the NonFinalizedState. - let block1: Arc = Arc::new(network.get_block_bytes(653599, 583999)?); + let block1: Arc = Arc::new(network.get_test_block(653599, 583999).unwrap()); let less_work_child = block1.make_fake_child().set_work(1); let more_work_child = block1.make_fake_child().set_work(3); diff --git a/zebrad/src/components/mempool/storage/tests/vectors.rs b/zebrad/src/components/mempool/storage/tests/vectors.rs index 3df298fc470..6658d76c4d5 100644 --- a/zebrad/src/components/mempool/storage/tests/vectors.rs +++ b/zebrad/src/components/mempool/storage/tests/vectors.rs @@ -251,7 +251,7 @@ fn mempool_expired_basic_for_network(network: Network) -> Result<()> { ..Default::default() }); - let block: Block = network.get_block_bytes(982681, 925483)?; + let block: Block = network.get_test_block(982681, 925483).unwrap(); // Get a test transaction let tx = &*(block.transactions[1]).clone(); From daa0a4e668526849b5606158ba9beb75ceb07407 Mon Sep 17 00:00:00 2001 From: idky137 Date: Fri, 1 Mar 2024 13:13:04 +0000 Subject: [PATCH 14/20] removed match statements from zebra_chain::transaction::arbitrary::test_transactions() and new zebra-grpc tests --- zebra-chain/src/transaction/arbitrary.rs | 5 +-- zebra-grpc/src/tests/vectors.rs | 52 ++++++++++-------------- 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/zebra-chain/src/transaction/arbitrary.rs b/zebra-chain/src/transaction/arbitrary.rs index 704a0d23cd1..8a7ad29cb04 100644 --- a/zebra-chain/src/transaction/arbitrary.rs +++ b/zebra-chain/src/transaction/arbitrary.rs @@ -995,10 +995,7 @@ fn sapling_spend_v4_to_fake_v5( pub fn test_transactions( network: Network, ) -> impl DoubleEndedIterator)> { - let blocks = match network { - Network::Mainnet => zebra_test::vectors::MAINNET_BLOCKS.iter(), - Network::Testnet => zebra_test::vectors::TESTNET_BLOCKS.iter(), - }; + let blocks = network.get_block_iter(); transactions_from_blocks(blocks) } diff --git a/zebra-grpc/src/tests/vectors.rs b/zebra-grpc/src/tests/vectors.rs index 41959a3bdd1..f84bcec299c 100644 --- a/zebra-grpc/src/tests/vectors.rs +++ b/zebra-grpc/src/tests/vectors.rs @@ -81,19 +81,16 @@ async fn test_mocked_getresults_for_network( .1 .by_height .len(); - match network { - Network::Mainnet => { - assert_eq!( - transaction_heights, 3, - "there should be 3 transaction heights" - ); - } - Network::Testnet => { - assert_eq!( - transaction_heights, 1, - "there should be 1 transaction height" - ); - } + if network.is_mainnet() { + assert_eq!( + transaction_heights, 3, + "there should be 3 transaction heights" + ); + } else { + assert_eq!( + transaction_heights, 1, + "there should be 1 transaction height" + ); } // create request, fake empty results and get response @@ -150,13 +147,10 @@ async fn test_mocked_clear_results_for_network( .1 .by_height .len(); - match network { - Network::Mainnet => { - assert_eq!(transaction_heights, 3); - } - Network::Testnet => { - assert_eq!(transaction_heights, 1); - } + if network.is_mainnet() { + assert_eq!(transaction_heights, 3); + } else { + assert_eq!(transaction_heights, 1) } // create request, fake results and get response @@ -210,13 +204,10 @@ async fn test_mocked_delete_keys_for_network( .1 .by_height .len(); - match network { - Network::Mainnet => { - assert_eq!(transaction_heights, 3); - } - Network::Testnet => { - assert_eq!(transaction_heights, 1); - } + if network.is_mainnet() { + assert_eq!(transaction_heights, 3); + } else { + assert_eq!(transaction_heights, 1); } let delete_keys_response = @@ -282,9 +273,10 @@ async fn add_fake_populated_results( tokio::spawn(async move { let zec_pages_sapling_efvk = ZECPAGES_SAPLING_VIEWING_KEY.to_string(); let mut fake_results = BTreeMap::new(); - let heights = match network { - Network::Mainnet => vec![Height::MIN, Height(1), Height::MAX], - Network::Testnet => vec![Height::MIN], + let heights = if network.is_mainnet() { + vec![Height::MIN, Height(1), Height::MAX] + } else { + vec![Height::MIN] }; for fake_result_height in heights { fake_results.insert( From 15d6e4cb740432754063afa350667bf701dfb1a6 Mon Sep 17 00:00:00 2001 From: idky137 Date: Fri, 1 Mar 2024 16:17:38 +0000 Subject: [PATCH 15/20] moved zebra-chain::test_utils to zebra-chain::test --- zebra-chain/src/lib.rs | 2 +- zebra-chain/src/{test_utils.rs => test.rs} | 0 zebra-chain/src/{test_utils => test}/vectors.rs | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename zebra-chain/src/{test_utils.rs => test.rs} (100%) rename zebra-chain/src/{test_utils => test}/vectors.rs (100%) diff --git a/zebra-chain/src/lib.rs b/zebra-chain/src/lib.rs index 3c276787b6d..b7bfff8d0ed 100644 --- a/zebra-chain/src/lib.rs +++ b/zebra-chain/src/lib.rs @@ -44,7 +44,7 @@ pub mod work; pub use block::LedgerState; #[cfg(any(test, feature = "proptest-impl"))] -pub mod test_utils; +pub mod test; /// Error type alias to make working with generic errors easier. /// diff --git a/zebra-chain/src/test_utils.rs b/zebra-chain/src/test.rs similarity index 100% rename from zebra-chain/src/test_utils.rs rename to zebra-chain/src/test.rs diff --git a/zebra-chain/src/test_utils/vectors.rs b/zebra-chain/src/test/vectors.rs similarity index 100% rename from zebra-chain/src/test_utils/vectors.rs rename to zebra-chain/src/test/vectors.rs From e72749812e21196b4b4b08be5fa6a8a07dde34bc Mon Sep 17 00:00:00 2001 From: idky137 Date: Mon, 4 Mar 2024 12:15:58 +0000 Subject: [PATCH 16/20] removed get_ prefix from getter methods --- zebra-chain/src/block/tests/vectors.rs | 4 ++-- zebra-chain/src/history_tree/tests/vectors.rs | 4 ++-- .../primitives/zcash_history/tests/vectors.rs | 2 +- zebra-chain/src/sapling/tests/tree.rs | 2 +- zebra-chain/src/sprout/tests/tree.rs | 2 +- zebra-chain/src/test/vectors.rs | 20 +++++++++---------- zebra-chain/src/transaction/arbitrary.rs | 2 +- zebra-chain/src/transaction/tests/vectors.rs | 6 +++--- zebra-chain/src/transparent/tests/vectors.rs | 2 +- .../src/work/difficulty/tests/vectors.rs | 4 ++-- zebra-consensus/src/block/tests.rs | 14 ++++++------- zebra-consensus/src/checkpoint/tests.rs | 2 +- zebra-consensus/src/transaction/tests.rs | 4 ++-- zebra-rpc/src/methods/tests/snapshot.rs | 2 +- zebra-rpc/src/methods/tests/utils.rs | 4 +--- zebra-rpc/src/methods/tests/vectors.rs | 2 +- zebra-scan/src/storage/db/tests.rs | 2 +- .../disk_format/tests/snapshot.rs | 2 +- .../zebra_db/block/tests/snapshot.rs | 2 +- .../non_finalized_state/tests/vectors.rs | 16 +++++++-------- zebra-state/src/service/tests.rs | 2 +- .../src/components/mempool/storage/tests.rs | 2 +- .../mempool/storage/tests/vectors.rs | 2 +- 23 files changed, 51 insertions(+), 53 deletions(-) diff --git a/zebra-chain/src/block/tests/vectors.rs b/zebra-chain/src/block/tests/vectors.rs index 685e902181a..afd86d21a79 100644 --- a/zebra-chain/src/block/tests/vectors.rs +++ b/zebra-chain/src/block/tests/vectors.rs @@ -208,7 +208,7 @@ fn block_test_vectors_height_testnet() { /// Test that the block test vector indexes match the heights in the block data, /// and that each post-sapling block has a corresponding final sapling root. fn block_test_vectors_height(network: Network) { - let (block_iter, sapling_roots) = network.get_block_sapling_roots_iter(); + let (block_iter, sapling_roots) = network.block_sapling_roots_iter(); for (&height, block) in block_iter { let block = block @@ -253,7 +253,7 @@ fn block_commitment_testnet() { /// /// TODO: add chain history test vectors? fn block_commitment(network: Network) { - let (block_iter, sapling_roots) = network.get_block_sapling_roots_iter(); + let (block_iter, sapling_roots) = network.block_sapling_roots_iter(); for (height, block) in block_iter { let block = block diff --git a/zebra-chain/src/history_tree/tests/vectors.rs b/zebra-chain/src/history_tree/tests/vectors.rs index 2eafff5ef77..6d63d439163 100644 --- a/zebra-chain/src/history_tree/tests/vectors.rs +++ b/zebra-chain/src/history_tree/tests/vectors.rs @@ -33,7 +33,7 @@ fn push_and_prune_for_network_upgrade( network: Network, network_upgrade: NetworkUpgrade, ) -> Result<()> { - let (blocks, sapling_roots) = network.get_block_sapling_roots_map(); + let (blocks, sapling_roots) = network.block_sapling_roots_map(); let height = network_upgrade.activation_height(network).unwrap().0; @@ -115,7 +115,7 @@ fn upgrade() -> Result<()> { } fn upgrade_for_network_upgrade(network: Network, network_upgrade: NetworkUpgrade) -> Result<()> { - let (blocks, sapling_roots) = network.get_block_sapling_roots_map(); + let (blocks, sapling_roots) = network.block_sapling_roots_map(); let height = network_upgrade.activation_height(network).unwrap().0; diff --git a/zebra-chain/src/primitives/zcash_history/tests/vectors.rs b/zebra-chain/src/primitives/zcash_history/tests/vectors.rs index d596ac1a585..4d986ed78ba 100644 --- a/zebra-chain/src/primitives/zcash_history/tests/vectors.rs +++ b/zebra-chain/src/primitives/zcash_history/tests/vectors.rs @@ -19,7 +19,7 @@ fn tree() -> Result<()> { } fn tree_for_network_upgrade(network: Network, network_upgrade: NetworkUpgrade) -> Result<()> { - let (blocks, sapling_roots) = network.get_block_sapling_roots_map(); + let (blocks, sapling_roots) = network.block_sapling_roots_map(); let height = network_upgrade.activation_height(network).unwrap().0; diff --git a/zebra-chain/src/sapling/tests/tree.rs b/zebra-chain/src/sapling/tests/tree.rs index 107c3c11dba..7ea22c4ce74 100644 --- a/zebra-chain/src/sapling/tests/tree.rs +++ b/zebra-chain/src/sapling/tests/tree.rs @@ -57,7 +57,7 @@ fn incremental_roots_with_blocks() -> Result<()> { } fn incremental_roots_with_blocks_for_network(network: Network) -> Result<()> { - let (blocks, sapling_roots) = network.get_block_sapling_roots_map(); + let (blocks, sapling_roots) = network.block_sapling_roots_map(); let height = NetworkUpgrade::Sapling .activation_height(network) diff --git a/zebra-chain/src/sprout/tests/tree.rs b/zebra-chain/src/sprout/tests/tree.rs index 0e61025cf0d..61185a5dcfb 100644 --- a/zebra-chain/src/sprout/tests/tree.rs +++ b/zebra-chain/src/sprout/tests/tree.rs @@ -87,7 +87,7 @@ fn incremental_roots_with_blocks() -> Result<()> { fn incremental_roots_with_blocks_for_network(network: Network) -> Result<()> { // Load the test data. - let (blocks, sprout_roots, next_height) = network.get_block_sprout_roots_height(); + let (blocks, sprout_roots, next_height) = network.block_sprout_roots_height(); // Load the Genesis height. let genesis_height = NetworkUpgrade::Genesis diff --git a/zebra-chain/src/test/vectors.rs b/zebra-chain/src/test/vectors.rs index 36170b15db9..143d9674542 100644 --- a/zebra-chain/src/test/vectors.rs +++ b/zebra-chain/src/test/vectors.rs @@ -22,7 +22,7 @@ impl Network { } /// Returns iterator over blocks. - pub fn get_block_iter(&self) -> std::collections::btree_map::Iter<'static, u32, &'static [u8]> { + pub fn block_iter(&self) -> std::collections::btree_map::Iter<'static, u32, &'static [u8]> { if self.is_mainnet() { MAINNET_BLOCKS.iter() } else { @@ -31,7 +31,7 @@ impl Network { } /// - pub fn get_block_map(&self) -> &BTreeMap { + pub fn block_map(&self) -> &BTreeMap { if self.is_mainnet() { &*zebra_test::vectors::MAINNET_BLOCKS } else { @@ -40,7 +40,7 @@ impl Network { } /// Returns genesis block for chain. - pub fn get_gen_block(&self) -> std::option::Option<&&[u8]> { + pub fn gen_block(&self) -> std::option::Option<&&[u8]> { if self.is_mainnet() { MAINNET_BLOCKS.get(&0) } else { @@ -49,7 +49,7 @@ impl Network { } /// Returns block bytes - pub fn get_test_block(&self, main_height: u32, test_height: u32) -> Option { + pub fn test_block(&self, main_height: u32, test_height: u32) -> Option { let block_bytes = match (self.is_mainnet(), main_height, test_height) { (true, 653_599, _) => BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into().ok(), (true, 982_681, _) => BLOCK_MAINNET_982681_BYTES.zcash_deserialize_into().ok(), @@ -61,7 +61,7 @@ impl Network { } /// Returns iterator over blockchain. - pub fn get_blockchain_iter(&self) -> std::collections::btree_map::Iter<'_, u32, &[u8]> { + pub fn blockchain_iter(&self) -> std::collections::btree_map::Iter<'_, u32, &[u8]> { if self.is_mainnet() { CONTINUOUS_MAINNET_BLOCKS.iter() } else { @@ -70,7 +70,7 @@ impl Network { } /// Returns BTreemap of blockchain. - pub fn get_blockchain_map(&self) -> &BTreeMap { + pub fn blockchain_map(&self) -> &BTreeMap { if self.is_mainnet() { &CONTINUOUS_MAINNET_BLOCKS } else { @@ -79,7 +79,7 @@ impl Network { } /// Returns iterator over blocks and sapling roots. - pub fn get_block_sapling_roots_iter( + pub fn block_sapling_roots_iter( &self, ) -> ( std::collections::btree_map::Iter<'_, u32, &[u8]>, @@ -93,7 +93,7 @@ impl Network { } /// Returns BTreemap of blocks and sapling roots. - pub fn get_block_sapling_roots_map( + pub fn block_sapling_roots_map( &self, ) -> ( &std::collections::BTreeMap, @@ -107,7 +107,7 @@ impl Network { } /// Returns block and sapling root bytes - pub fn get_test_block_sapling_roots( + pub fn test_block_sapling_roots( &self, main_height: u32, test_height: u32, @@ -128,7 +128,7 @@ impl Network { } /// Returns BTreemap of blocks and sprout roots, and last split height. - pub fn get_block_sprout_roots_height( + pub fn block_sprout_roots_height( &self, ) -> ( &std::collections::BTreeMap, diff --git a/zebra-chain/src/transaction/arbitrary.rs b/zebra-chain/src/transaction/arbitrary.rs index 8a7ad29cb04..df914c208e9 100644 --- a/zebra-chain/src/transaction/arbitrary.rs +++ b/zebra-chain/src/transaction/arbitrary.rs @@ -995,7 +995,7 @@ fn sapling_spend_v4_to_fake_v5( pub fn test_transactions( network: Network, ) -> impl DoubleEndedIterator)> { - let blocks = network.get_block_iter(); + let blocks = network.block_iter(); transactions_from_blocks(blocks) } diff --git a/zebra-chain/src/transaction/tests/vectors.rs b/zebra-chain/src/transaction/tests/vectors.rs index d422b2bb0f9..cfac4898b25 100644 --- a/zebra-chain/src/transaction/tests/vectors.rs +++ b/zebra-chain/src/transaction/tests/vectors.rs @@ -349,7 +349,7 @@ fn fake_v5_round_trip() { } fn fake_v5_round_trip_for_network(network: Network) { - let block_iter = network.get_block_iter(); + let block_iter = network.block_iter(); let overwinter_activation_height = NetworkUpgrade::Overwinter .activation_height(network) @@ -497,7 +497,7 @@ fn fake_v5_librustzcash_round_trip() { } fn fake_v5_librustzcash_round_trip_for_network(network: Network) { - let block_iter = network.get_block_iter(); + let block_iter = network.block_iter(); let overwinter_activation_height = NetworkUpgrade::Overwinter .activation_height(network) @@ -937,7 +937,7 @@ fn binding_signatures() { } fn binding_signatures_for_network(network: Network) { - let block_iter = network.get_block_iter(); + let block_iter = network.block_iter(); for (height, bytes) in block_iter { let upgrade = NetworkUpgrade::current(network, Height(*height)); diff --git a/zebra-chain/src/transparent/tests/vectors.rs b/zebra-chain/src/transparent/tests/vectors.rs index 9f85a1c2f2c..b0a43e665eb 100644 --- a/zebra-chain/src/transparent/tests/vectors.rs +++ b/zebra-chain/src/transparent/tests/vectors.rs @@ -92,7 +92,7 @@ fn get_transparent_output_address_with_blocks() { /// Test that the block test vector indexes match the heights in the block data, /// and that each post-sapling block has a corresponding final sapling root. fn get_transparent_output_address_with_blocks_for_network(network: Network) { - let block_iter = network.get_block_iter(); + let block_iter = network.block_iter(); let mut valid_addresses = 0; diff --git a/zebra-chain/src/work/difficulty/tests/vectors.rs b/zebra-chain/src/work/difficulty/tests/vectors.rs index 1c25580a890..e8b19022199 100644 --- a/zebra-chain/src/work/difficulty/tests/vectors.rs +++ b/zebra-chain/src/work/difficulty/tests/vectors.rs @@ -273,7 +273,7 @@ fn block_difficulty() -> Result<(), Report> { fn block_difficulty_for_network(network: Network) -> Result<(), Report> { let _init_guard = zebra_test::init(); - let block_iter = network.get_block_iter(); + let block_iter = network.block_iter(); let diff_zero = ExpandedDifficulty(U256::zero()); let diff_one = ExpandedDifficulty(U256::one()); @@ -359,7 +359,7 @@ fn genesis_block_difficulty() -> Result<(), Report> { fn genesis_block_difficulty_for_network(network: Network) -> Result<(), Report> { let _init_guard = zebra_test::init(); - let block = network.get_gen_block(); + let block = network.gen_block(); let block = block.expect("test vectors contain the genesis block"); let block = Block::zcash_deserialize(&block[..]).expect("block test vector should deserialize"); diff --git a/zebra-consensus/src/block/tests.rs b/zebra-consensus/src/block/tests.rs index be7bfd80c3b..f074cf73649 100644 --- a/zebra-consensus/src/block/tests.rs +++ b/zebra-consensus/src/block/tests.rs @@ -189,7 +189,7 @@ fn difficulty_is_valid_for_historical_blocks() -> Result<(), Report> { } fn difficulty_is_valid_for_network(network: Network) -> Result<(), Report> { - let block_iter = network.get_block_iter(); + let block_iter = network.block_iter(); for (&height, block) in block_iter { let block = block @@ -293,7 +293,7 @@ fn subsidy_is_valid_for_historical_blocks() -> Result<(), Report> { } fn subsidy_is_valid_for_network(network: Network) -> Result<(), Report> { - let block_iter = network.get_block_iter(); + let block_iter = network.block_iter(); for (&height, block) in block_iter { let block = block @@ -395,7 +395,7 @@ fn funding_stream_validation() -> Result<(), Report> { } fn funding_stream_validation_for_network(network: Network) -> Result<(), Report> { - let block_iter = network.get_block_iter(); + let block_iter = network.block_iter(); let canopy_activation_height = NetworkUpgrade::Canopy .activation_height(network) @@ -470,7 +470,7 @@ fn miner_fees_validation_success() -> Result<(), Report> { } fn miner_fees_validation_for_network(network: Network) -> Result<(), Report> { - let block_iter = network.get_block_iter(); + let block_iter = network.block_iter(); for (&height, block) in block_iter { if Height(height) > SLOW_START_SHIFT { @@ -556,7 +556,7 @@ fn merkle_root_is_valid() -> Result<(), Report> { } fn merkle_root_is_valid_for_network(network: Network) -> Result<(), Report> { - let block_iter = network.get_block_iter(); + let block_iter = network.block_iter(); for (_height, block) in block_iter { let block = block @@ -577,7 +577,7 @@ fn merkle_root_is_valid_for_network(network: Network) -> Result<(), Report> { } fn merkle_root_fake_v5_for_network(network: Network) -> Result<(), Report> { - let block_iter = network.get_block_iter(); + let block_iter = network.block_iter(); for (height, block) in block_iter { let mut block = block @@ -689,7 +689,7 @@ fn transaction_expiration_height_validation() -> Result<(), Report> { } fn transaction_expiration_height_for_network(network: Network) -> Result<(), Report> { - let block_iter = network.get_block_iter(); + let block_iter = network.block_iter(); for (&height, block) in block_iter { let block = Block::zcash_deserialize(&block[..]).expect("block should deserialize"); diff --git a/zebra-consensus/src/checkpoint/tests.rs b/zebra-consensus/src/checkpoint/tests.rs index 57472ee9cee..e4be1c2b6b7 100644 --- a/zebra-consensus/src/checkpoint/tests.rs +++ b/zebra-consensus/src/checkpoint/tests.rs @@ -233,7 +233,7 @@ async fn continuous_blockchain( let _init_guard = zebra_test::init(); // A continuous blockchain - let blockchain = network.get_blockchain_iter(); + let blockchain = network.blockchain_iter(); let blockchain: Vec<_> = blockchain .map(|(height, b)| { diff --git a/zebra-consensus/src/transaction/tests.rs b/zebra-consensus/src/transaction/tests.rs index 5f4b0b3ac5e..c5e25be64fa 100644 --- a/zebra-consensus/src/transaction/tests.rs +++ b/zebra-consensus/src/transaction/tests.rs @@ -909,7 +909,7 @@ fn v5_transaction_is_accepted_after_nu5_activation_for_network(network: Network) let nu5_activation_height = nu5 .activation_height(network) .expect("NU5 activation height is specified"); - let blocks = network.get_block_iter(); + let blocks = network.block_iter(); let state_service = service_fn(|_| async { unreachable!("Service should not be called") }); let verifier = Verifier::new(network, state_service); @@ -2768,7 +2768,7 @@ fn coinbase_outputs_are_decryptable_for_historical_blocks() -> Result<(), Report fn coinbase_outputs_are_decryptable_for_historical_blocks_for_network( network: Network, ) -> Result<(), Report> { - let block_iter = network.get_block_iter(); + let block_iter = network.block_iter(); let mut tested_coinbase_txs = 0; let mut tested_non_coinbase_txs = 0; diff --git a/zebra-rpc/src/methods/tests/snapshot.rs b/zebra-rpc/src/methods/tests/snapshot.rs index 5203714f937..6d775f13255 100644 --- a/zebra-rpc/src/methods/tests/snapshot.rs +++ b/zebra-rpc/src/methods/tests/snapshot.rs @@ -44,7 +44,7 @@ async fn test_rpc_response_data() { async fn test_rpc_response_data_for_network(network: Network) { // Create a continuous chain of mainnet and testnet blocks from genesis - let block_data = network.get_blockchain_map(); + let block_data = network.blockchain_map(); let blocks: Vec> = block_data .iter() diff --git a/zebra-rpc/src/methods/tests/utils.rs b/zebra-rpc/src/methods/tests/utils.rs index e8c32fb3e09..bb5c728cf0b 100644 --- a/zebra-rpc/src/methods/tests/utils.rs +++ b/zebra-rpc/src/methods/tests/utils.rs @@ -11,9 +11,7 @@ use zebra_chain::{ /// Create a history tree with one single block for a network by using Zebra test vectors. pub fn fake_history_tree(network: Network) -> Arc { - let (block, sapling_root) = network - .get_test_block_sapling_roots(1046400, 1116000) - .unwrap(); + let (block, sapling_root) = network.test_block_sapling_roots(1046400, 1116000).unwrap(); let block = Arc::::zcash_deserialize(block).expect("block should deserialize"); let first_sapling_root = Root::try_from(sapling_root).unwrap(); diff --git a/zebra-rpc/src/methods/tests/vectors.rs b/zebra-rpc/src/methods/tests/vectors.rs index 14bad0398f3..0a9dcb9e1bd 100644 --- a/zebra-rpc/src/methods/tests/vectors.rs +++ b/zebra-rpc/src/methods/tests/vectors.rs @@ -656,7 +656,7 @@ async fn rpc_getaddresstxids_response() { for network in [Mainnet, Testnet] { let blocks: Vec> = network - .get_blockchain_map() + .blockchain_map() .iter() .map(|(_height, block_bytes)| block_bytes.zcash_deserialize_into().unwrap()) .collect(); diff --git a/zebra-scan/src/storage/db/tests.rs b/zebra-scan/src/storage/db/tests.rs index 9135cc404ab..b6b20740341 100644 --- a/zebra-scan/src/storage/db/tests.rs +++ b/zebra-scan/src/storage/db/tests.rs @@ -45,7 +45,7 @@ pub fn add_fake_results( height: Height, add_progress_marker: bool, ) { - let blocks = network.get_blockchain_map(); + let blocks = network.blockchain_map(); let block: Arc = blocks .get(&height.0) diff --git a/zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs b/zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs index a496413f981..80224954c4d 100644 --- a/zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs +++ b/zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs @@ -89,7 +89,7 @@ fn test_raw_rocksdb_column_families_with_network(network: Network) { // Snapshot raw database data for: // - mainnet and testnet // - genesis, block 1, and block 2 - let blocks = network.get_blockchain_map(); + let blocks = network.blockchain_map(); // We limit the number of blocks, because the serialized data is a few kilobytes per block. for height in 0..=2 { diff --git a/zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs b/zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs index 0d1b85f33bc..54b17c8465f 100644 --- a/zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs +++ b/zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs @@ -181,7 +181,7 @@ fn test_block_and_transaction_data_with_network(network: Network) { // Snapshot block and transaction database data for: // - mainnet and testnet // - genesis, block 1, and block 2 - let blocks = network.get_blockchain_map(); + let blocks = network.blockchain_map(); // We limit the number of blocks, because the serialized data is a few kilobytes per block. // diff --git a/zebra-state/src/service/non_finalized_state/tests/vectors.rs b/zebra-state/src/service/non_finalized_state/tests/vectors.rs index da9c3e1bec6..a869a01ea96 100644 --- a/zebra-state/src/service/non_finalized_state/tests/vectors.rs +++ b/zebra-state/src/service/non_finalized_state/tests/vectors.rs @@ -144,7 +144,7 @@ fn best_chain_wins_for_network(network: Network) -> Result<()> { // Since the brand new FinalizedState below will pass a None history tree // to the NonFinalizedState, we must use pre-Heartwood blocks since // they won't trigger the history tree update in the NonFinalizedState. - let block1: Arc = Arc::new(network.get_test_block(653599, 583999).unwrap()); + let block1: Arc = Arc::new(network.test_block(653599, 583999).unwrap()); let block2 = block1.make_fake_child().set_work(10); let child = block1.make_fake_child().set_work(1); @@ -182,7 +182,7 @@ fn finalize_pops_from_best_chain_for_network(network: Network) -> Result<()> { // Since the brand new FinalizedState below will pass a None history tree // to the NonFinalizedState, we must use pre-Heartwood blocks since // they won't trigger the history tree update in the NonFinalizedState. - let block1: Arc = Arc::new(network.get_test_block(653599, 583999).unwrap()); + let block1: Arc = Arc::new(network.test_block(653599, 583999).unwrap()); let block2 = block1.make_fake_child().set_work(10); let child = block1.make_fake_child().set_work(1); @@ -231,7 +231,7 @@ fn commit_block_extending_best_chain_doesnt_drop_worst_chains_for_network( // Since the brand new FinalizedState below will pass a None history tree // to the NonFinalizedState, we must use pre-Heartwood blocks since // they won't trigger the history tree update in the NonFinalizedState. - let block1: Arc = Arc::new(network.get_test_block(653599, 583999).unwrap()); + let block1: Arc = Arc::new(network.test_block(653599, 583999).unwrap()); let block2 = block1.make_fake_child().set_work(10); let child1 = block1.make_fake_child().set_work(1); @@ -275,7 +275,7 @@ fn shorter_chain_can_be_best_chain_for_network(network: Network) -> Result<()> { // Since the brand new FinalizedState below will pass a None history tree // to the NonFinalizedState, we must use pre-Heartwood blocks since // they won't trigger the history tree update in the NonFinalizedState. - let block1: Arc = Arc::new(network.get_test_block(653599, 583999).unwrap()); + let block1: Arc = Arc::new(network.test_block(653599, 583999).unwrap()); let long_chain_block1 = block1.make_fake_child().set_work(1); let long_chain_block2 = long_chain_block1.make_fake_child().set_work(1); @@ -318,7 +318,7 @@ fn longer_chain_with_more_work_wins_for_network(network: Network) -> Result<()> // Since the brand new FinalizedState below will pass a None history tree // to the NonFinalizedState, we must use pre-Heartwood blocks since // they won't trigger the history tree update in the NonFinalizedState. - let block1: Arc = Arc::new(network.get_test_block(653599, 583999).unwrap()); + let block1: Arc = Arc::new(network.test_block(653599, 583999).unwrap()); let long_chain_block1 = block1.make_fake_child().set_work(1); let long_chain_block2 = long_chain_block1.make_fake_child().set_work(1); @@ -364,7 +364,7 @@ fn equal_length_goes_to_more_work_for_network(network: Network) -> Result<()> { // Since the brand new FinalizedState below will pass a None history tree // to the NonFinalizedState, we must use pre-Heartwood blocks since // they won't trigger the history tree update in the NonFinalizedState. - let block1: Arc = Arc::new(network.get_test_block(653599, 583999).unwrap()); + let block1: Arc = Arc::new(network.test_block(653599, 583999).unwrap()); let less_work_child = block1.make_fake_child().set_work(1); let more_work_child = block1.make_fake_child().set_work(3); @@ -405,7 +405,7 @@ fn history_tree_is_updated_for_network_upgrade( network: Network, network_upgrade: NetworkUpgrade, ) -> Result<()> { - let blocks = network.get_block_map(); + let blocks = network.block_map(); let height = network_upgrade.activation_height(network).unwrap().0; @@ -504,7 +504,7 @@ fn commitment_is_validated() { } fn commitment_is_validated_for_network_upgrade(network: Network, network_upgrade: NetworkUpgrade) { - let blocks = network.get_block_map(); + let blocks = network.block_map(); let height = network_upgrade.activation_height(network).unwrap().0; let prev_block = Arc::new( diff --git a/zebra-state/src/service/tests.rs b/zebra-state/src/service/tests.rs index 6de5d0b616e..7acd02406fa 100644 --- a/zebra-state/src/service/tests.rs +++ b/zebra-state/src/service/tests.rs @@ -562,7 +562,7 @@ fn continuous_empty_blocks_from_test_vectors() -> impl Strategy< any::() .prop_flat_map(|network| { // Select the test vector based on the network - let raw_blocks = network.get_blockchain_map(); + let raw_blocks = network.blockchain_map(); // Transform the test vector's block bytes into a vector of `SemanticallyVerifiedBlock`s. let blocks: Vec<_> = raw_blocks diff --git a/zebrad/src/components/mempool/storage/tests.rs b/zebrad/src/components/mempool/storage/tests.rs index 7abd5638029..3a6c84b856d 100644 --- a/zebrad/src/components/mempool/storage/tests.rs +++ b/zebrad/src/components/mempool/storage/tests.rs @@ -17,7 +17,7 @@ pub fn unmined_transactions_in_blocks( block_height_range: impl RangeBounds, network: Network, ) -> impl DoubleEndedIterator { - let blocks = network.get_block_iter(); + let blocks = network.block_iter(); // Deserialize the blocks that are selected based on the specified `block_height_range`. let selected_blocks = blocks diff --git a/zebrad/src/components/mempool/storage/tests/vectors.rs b/zebrad/src/components/mempool/storage/tests/vectors.rs index 6658d76c4d5..89d626cef0a 100644 --- a/zebrad/src/components/mempool/storage/tests/vectors.rs +++ b/zebrad/src/components/mempool/storage/tests/vectors.rs @@ -251,7 +251,7 @@ fn mempool_expired_basic_for_network(network: Network) -> Result<()> { ..Default::default() }); - let block: Block = network.get_test_block(982681, 925483).unwrap(); + let block: Block = network.test_block(982681, 925483).unwrap(); // Get a test transaction let tx = &*(block.transactions[1]).clone(); From 88be32dceba22847c855b9727a3e18da67116c25 Mon Sep 17 00:00:00 2001 From: idky137 Date: Mon, 4 Mar 2024 12:21:28 +0000 Subject: [PATCH 17/20] renamed zebra-chain::test to zebra-chain::tests --- zebra-chain/src/lib.rs | 2 +- zebra-chain/src/test.rs | 4 - zebra-chain/src/test/vectors.rs | 157 -------------------------------- 3 files changed, 1 insertion(+), 162 deletions(-) delete mode 100644 zebra-chain/src/test.rs delete mode 100644 zebra-chain/src/test/vectors.rs diff --git a/zebra-chain/src/lib.rs b/zebra-chain/src/lib.rs index b7bfff8d0ed..4faaeab70cc 100644 --- a/zebra-chain/src/lib.rs +++ b/zebra-chain/src/lib.rs @@ -44,7 +44,7 @@ pub mod work; pub use block::LedgerState; #[cfg(any(test, feature = "proptest-impl"))] -pub mod test; +pub mod tests; /// Error type alias to make working with generic errors easier. /// diff --git a/zebra-chain/src/test.rs b/zebra-chain/src/test.rs deleted file mode 100644 index 7cfa4a95955..00000000000 --- a/zebra-chain/src/test.rs +++ /dev/null @@ -1,4 +0,0 @@ -//!Chain functionality for fetching test vectors. -//! - -mod vectors; diff --git a/zebra-chain/src/test/vectors.rs b/zebra-chain/src/test/vectors.rs deleted file mode 100644 index 143d9674542..00000000000 --- a/zebra-chain/src/test/vectors.rs +++ /dev/null @@ -1,157 +0,0 @@ -//! Network methods for fetching blockchain vectors. -//! - -use std::collections::BTreeMap; - -use crate::{block::Block, parameters::Network, serialization::ZcashDeserializeInto}; - -use zebra_test::vectors::{ - BLOCK_MAINNET_1046400_BYTES, BLOCK_MAINNET_653599_BYTES, BLOCK_MAINNET_982681_BYTES, - BLOCK_TESTNET_1116000_BYTES, BLOCK_TESTNET_583999_BYTES, BLOCK_TESTNET_925483_BYTES, - CONTINUOUS_MAINNET_BLOCKS, CONTINUOUS_TESTNET_BLOCKS, MAINNET_BLOCKS, - MAINNET_FINAL_SAPLING_ROOTS, MAINNET_FINAL_SPROUT_ROOTS, - SAPLING_FINAL_ROOT_MAINNET_1046400_BYTES, SAPLING_FINAL_ROOT_TESTNET_1116000_BYTES, - TESTNET_BLOCKS, TESTNET_FINAL_SAPLING_ROOTS, TESTNET_FINAL_SPROUT_ROOTS, -}; - -/// Network methods for fetching blockchain vectors. -impl Network { - /// Returns true if network is of type Mainnet. - pub fn is_mainnet(&self) -> bool { - matches!(self, Network::Mainnet) - } - - /// Returns iterator over blocks. - pub fn block_iter(&self) -> std::collections::btree_map::Iter<'static, u32, &'static [u8]> { - if self.is_mainnet() { - MAINNET_BLOCKS.iter() - } else { - TESTNET_BLOCKS.iter() - } - } - - /// - pub fn block_map(&self) -> &BTreeMap { - if self.is_mainnet() { - &*zebra_test::vectors::MAINNET_BLOCKS - } else { - &*zebra_test::vectors::TESTNET_BLOCKS - } - } - - /// Returns genesis block for chain. - pub fn gen_block(&self) -> std::option::Option<&&[u8]> { - if self.is_mainnet() { - MAINNET_BLOCKS.get(&0) - } else { - TESTNET_BLOCKS.get(&0) - } - } - - /// Returns block bytes - pub fn test_block(&self, main_height: u32, test_height: u32) -> Option { - let block_bytes = match (self.is_mainnet(), main_height, test_height) { - (true, 653_599, _) => BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into().ok(), - (true, 982_681, _) => BLOCK_MAINNET_982681_BYTES.zcash_deserialize_into().ok(), - (false, _, 583_999) => BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into().ok(), - (false, _, 925_483) => BLOCK_TESTNET_925483_BYTES.zcash_deserialize_into().ok(), - _ => return None, - }; - block_bytes - } - - /// Returns iterator over blockchain. - pub fn blockchain_iter(&self) -> std::collections::btree_map::Iter<'_, u32, &[u8]> { - if self.is_mainnet() { - CONTINUOUS_MAINNET_BLOCKS.iter() - } else { - CONTINUOUS_TESTNET_BLOCKS.iter() - } - } - - /// Returns BTreemap of blockchain. - pub fn blockchain_map(&self) -> &BTreeMap { - if self.is_mainnet() { - &CONTINUOUS_MAINNET_BLOCKS - } else { - &CONTINUOUS_TESTNET_BLOCKS - } - } - - /// Returns iterator over blocks and sapling roots. - pub fn block_sapling_roots_iter( - &self, - ) -> ( - std::collections::btree_map::Iter<'_, u32, &[u8]>, - std::collections::BTreeMap, - ) { - if self.is_mainnet() { - (MAINNET_BLOCKS.iter(), MAINNET_FINAL_SAPLING_ROOTS.clone()) - } else { - (TESTNET_BLOCKS.iter(), TESTNET_FINAL_SAPLING_ROOTS.clone()) - } - } - - /// Returns BTreemap of blocks and sapling roots. - pub fn block_sapling_roots_map( - &self, - ) -> ( - &std::collections::BTreeMap, - &std::collections::BTreeMap, - ) { - if self.is_mainnet() { - (&*MAINNET_BLOCKS, &*MAINNET_FINAL_SAPLING_ROOTS) - } else { - (&*TESTNET_BLOCKS, &*TESTNET_FINAL_SAPLING_ROOTS) - } - } - - /// Returns block and sapling root bytes - pub fn test_block_sapling_roots( - &self, - main_height: u32, - test_height: u32, - ) -> Option<(&[u8], [u8; 32])> { - let block_bytes: Option<(&[u8], [u8; 32])> = - match (self.is_mainnet(), main_height, test_height) { - (true, 1_046_400, _) => Some(( - &BLOCK_MAINNET_1046400_BYTES[..], - *SAPLING_FINAL_ROOT_MAINNET_1046400_BYTES, - )), - (false, _, 1_116_000) => Some(( - &BLOCK_TESTNET_1116000_BYTES[..], - *SAPLING_FINAL_ROOT_TESTNET_1116000_BYTES, - )), - _ => return None, - }; - block_bytes - } - - /// Returns BTreemap of blocks and sprout roots, and last split height. - pub fn block_sprout_roots_height( - &self, - ) -> ( - &std::collections::BTreeMap, - &std::collections::BTreeMap, - u32, - ) { - // The mainnet block height at which the first JoinSplit occurred. - const MAINNET_FIRST_JOINSPLIT_HEIGHT: u32 = 396; - - // The testnet block height at which the first JoinSplit occurred. - const TESTNET_FIRST_JOINSPLIT_HEIGHT: u32 = 2259; - if self.is_mainnet() { - ( - &*MAINNET_BLOCKS, - &*MAINNET_FINAL_SPROUT_ROOTS, - MAINNET_FIRST_JOINSPLIT_HEIGHT, - ) - } else { - ( - &*TESTNET_BLOCKS, - &*TESTNET_FINAL_SPROUT_ROOTS, - TESTNET_FIRST_JOINSPLIT_HEIGHT, - ) - } - } -} From 813e1fffd84b4ff4972277774c240e008b67f188 Mon Sep 17 00:00:00 2001 From: idky137 Date: Mon, 4 Mar 2024 12:22:22 +0000 Subject: [PATCH 18/20] renamed zebra-chain::test to zebra-chain::tests --- zebra-chain/src/tests.rs | 4 + zebra-chain/src/tests/vectors.rs | 157 +++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 zebra-chain/src/tests.rs create mode 100644 zebra-chain/src/tests/vectors.rs diff --git a/zebra-chain/src/tests.rs b/zebra-chain/src/tests.rs new file mode 100644 index 00000000000..7cfa4a95955 --- /dev/null +++ b/zebra-chain/src/tests.rs @@ -0,0 +1,4 @@ +//!Chain functionality for fetching test vectors. +//! + +mod vectors; diff --git a/zebra-chain/src/tests/vectors.rs b/zebra-chain/src/tests/vectors.rs new file mode 100644 index 00000000000..143d9674542 --- /dev/null +++ b/zebra-chain/src/tests/vectors.rs @@ -0,0 +1,157 @@ +//! Network methods for fetching blockchain vectors. +//! + +use std::collections::BTreeMap; + +use crate::{block::Block, parameters::Network, serialization::ZcashDeserializeInto}; + +use zebra_test::vectors::{ + BLOCK_MAINNET_1046400_BYTES, BLOCK_MAINNET_653599_BYTES, BLOCK_MAINNET_982681_BYTES, + BLOCK_TESTNET_1116000_BYTES, BLOCK_TESTNET_583999_BYTES, BLOCK_TESTNET_925483_BYTES, + CONTINUOUS_MAINNET_BLOCKS, CONTINUOUS_TESTNET_BLOCKS, MAINNET_BLOCKS, + MAINNET_FINAL_SAPLING_ROOTS, MAINNET_FINAL_SPROUT_ROOTS, + SAPLING_FINAL_ROOT_MAINNET_1046400_BYTES, SAPLING_FINAL_ROOT_TESTNET_1116000_BYTES, + TESTNET_BLOCKS, TESTNET_FINAL_SAPLING_ROOTS, TESTNET_FINAL_SPROUT_ROOTS, +}; + +/// Network methods for fetching blockchain vectors. +impl Network { + /// Returns true if network is of type Mainnet. + pub fn is_mainnet(&self) -> bool { + matches!(self, Network::Mainnet) + } + + /// Returns iterator over blocks. + pub fn block_iter(&self) -> std::collections::btree_map::Iter<'static, u32, &'static [u8]> { + if self.is_mainnet() { + MAINNET_BLOCKS.iter() + } else { + TESTNET_BLOCKS.iter() + } + } + + /// + pub fn block_map(&self) -> &BTreeMap { + if self.is_mainnet() { + &*zebra_test::vectors::MAINNET_BLOCKS + } else { + &*zebra_test::vectors::TESTNET_BLOCKS + } + } + + /// Returns genesis block for chain. + pub fn gen_block(&self) -> std::option::Option<&&[u8]> { + if self.is_mainnet() { + MAINNET_BLOCKS.get(&0) + } else { + TESTNET_BLOCKS.get(&0) + } + } + + /// Returns block bytes + pub fn test_block(&self, main_height: u32, test_height: u32) -> Option { + let block_bytes = match (self.is_mainnet(), main_height, test_height) { + (true, 653_599, _) => BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into().ok(), + (true, 982_681, _) => BLOCK_MAINNET_982681_BYTES.zcash_deserialize_into().ok(), + (false, _, 583_999) => BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into().ok(), + (false, _, 925_483) => BLOCK_TESTNET_925483_BYTES.zcash_deserialize_into().ok(), + _ => return None, + }; + block_bytes + } + + /// Returns iterator over blockchain. + pub fn blockchain_iter(&self) -> std::collections::btree_map::Iter<'_, u32, &[u8]> { + if self.is_mainnet() { + CONTINUOUS_MAINNET_BLOCKS.iter() + } else { + CONTINUOUS_TESTNET_BLOCKS.iter() + } + } + + /// Returns BTreemap of blockchain. + pub fn blockchain_map(&self) -> &BTreeMap { + if self.is_mainnet() { + &CONTINUOUS_MAINNET_BLOCKS + } else { + &CONTINUOUS_TESTNET_BLOCKS + } + } + + /// Returns iterator over blocks and sapling roots. + pub fn block_sapling_roots_iter( + &self, + ) -> ( + std::collections::btree_map::Iter<'_, u32, &[u8]>, + std::collections::BTreeMap, + ) { + if self.is_mainnet() { + (MAINNET_BLOCKS.iter(), MAINNET_FINAL_SAPLING_ROOTS.clone()) + } else { + (TESTNET_BLOCKS.iter(), TESTNET_FINAL_SAPLING_ROOTS.clone()) + } + } + + /// Returns BTreemap of blocks and sapling roots. + pub fn block_sapling_roots_map( + &self, + ) -> ( + &std::collections::BTreeMap, + &std::collections::BTreeMap, + ) { + if self.is_mainnet() { + (&*MAINNET_BLOCKS, &*MAINNET_FINAL_SAPLING_ROOTS) + } else { + (&*TESTNET_BLOCKS, &*TESTNET_FINAL_SAPLING_ROOTS) + } + } + + /// Returns block and sapling root bytes + pub fn test_block_sapling_roots( + &self, + main_height: u32, + test_height: u32, + ) -> Option<(&[u8], [u8; 32])> { + let block_bytes: Option<(&[u8], [u8; 32])> = + match (self.is_mainnet(), main_height, test_height) { + (true, 1_046_400, _) => Some(( + &BLOCK_MAINNET_1046400_BYTES[..], + *SAPLING_FINAL_ROOT_MAINNET_1046400_BYTES, + )), + (false, _, 1_116_000) => Some(( + &BLOCK_TESTNET_1116000_BYTES[..], + *SAPLING_FINAL_ROOT_TESTNET_1116000_BYTES, + )), + _ => return None, + }; + block_bytes + } + + /// Returns BTreemap of blocks and sprout roots, and last split height. + pub fn block_sprout_roots_height( + &self, + ) -> ( + &std::collections::BTreeMap, + &std::collections::BTreeMap, + u32, + ) { + // The mainnet block height at which the first JoinSplit occurred. + const MAINNET_FIRST_JOINSPLIT_HEIGHT: u32 = 396; + + // The testnet block height at which the first JoinSplit occurred. + const TESTNET_FIRST_JOINSPLIT_HEIGHT: u32 = 2259; + if self.is_mainnet() { + ( + &*MAINNET_BLOCKS, + &*MAINNET_FINAL_SPROUT_ROOTS, + MAINNET_FIRST_JOINSPLIT_HEIGHT, + ) + } else { + ( + &*TESTNET_BLOCKS, + &*TESTNET_FINAL_SPROUT_ROOTS, + TESTNET_FIRST_JOINSPLIT_HEIGHT, + ) + } + } +} From ca74f276b4e12a3bc39bef5fbc5cc3f9ab5f8bb0 Mon Sep 17 00:00:00 2001 From: idky137 Date: Mon, 4 Mar 2024 12:34:38 +0000 Subject: [PATCH 19/20] fixed clippy warnings --- zebra-chain/src/tests/vectors.rs | 35 +++++++++++++++----------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/zebra-chain/src/tests/vectors.rs b/zebra-chain/src/tests/vectors.rs index 143d9674542..d61656ae994 100644 --- a/zebra-chain/src/tests/vectors.rs +++ b/zebra-chain/src/tests/vectors.rs @@ -33,9 +33,9 @@ impl Network { /// pub fn block_map(&self) -> &BTreeMap { if self.is_mainnet() { - &*zebra_test::vectors::MAINNET_BLOCKS + &zebra_test::vectors::MAINNET_BLOCKS } else { - &*zebra_test::vectors::TESTNET_BLOCKS + &zebra_test::vectors::TESTNET_BLOCKS } } @@ -50,14 +50,13 @@ impl Network { /// Returns block bytes pub fn test_block(&self, main_height: u32, test_height: u32) -> Option { - let block_bytes = match (self.is_mainnet(), main_height, test_height) { + match (self.is_mainnet(), main_height, test_height) { (true, 653_599, _) => BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into().ok(), (true, 982_681, _) => BLOCK_MAINNET_982681_BYTES.zcash_deserialize_into().ok(), (false, _, 583_999) => BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into().ok(), (false, _, 925_483) => BLOCK_TESTNET_925483_BYTES.zcash_deserialize_into().ok(), - _ => return None, - }; - block_bytes + _ => None, + } } /// Returns iterator over blockchain. @@ -112,19 +111,17 @@ impl Network { main_height: u32, test_height: u32, ) -> Option<(&[u8], [u8; 32])> { - let block_bytes: Option<(&[u8], [u8; 32])> = - match (self.is_mainnet(), main_height, test_height) { - (true, 1_046_400, _) => Some(( - &BLOCK_MAINNET_1046400_BYTES[..], - *SAPLING_FINAL_ROOT_MAINNET_1046400_BYTES, - )), - (false, _, 1_116_000) => Some(( - &BLOCK_TESTNET_1116000_BYTES[..], - *SAPLING_FINAL_ROOT_TESTNET_1116000_BYTES, - )), - _ => return None, - }; - block_bytes + match (self.is_mainnet(), main_height, test_height) { + (true, 1_046_400, _) => Some(( + &BLOCK_MAINNET_1046400_BYTES[..], + *SAPLING_FINAL_ROOT_MAINNET_1046400_BYTES, + )), + (false, _, 1_116_000) => Some(( + &BLOCK_TESTNET_1116000_BYTES[..], + *SAPLING_FINAL_ROOT_TESTNET_1116000_BYTES, + )), + _ => None, + } } /// Returns BTreemap of blocks and sprout roots, and last split height. From ed0b060ae75cf905dad5266a285394e819f53eec Mon Sep 17 00:00:00 2001 From: idky137 Date: Mon, 4 Mar 2024 12:49:38 +0000 Subject: [PATCH 20/20] changed block_map to return clone --- zebra-chain/src/tests/vectors.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zebra-chain/src/tests/vectors.rs b/zebra-chain/src/tests/vectors.rs index d61656ae994..0e60f6e99db 100644 --- a/zebra-chain/src/tests/vectors.rs +++ b/zebra-chain/src/tests/vectors.rs @@ -31,11 +31,11 @@ impl Network { } /// - pub fn block_map(&self) -> &BTreeMap { + pub fn block_map(&self) -> BTreeMap { if self.is_mainnet() { - &zebra_test::vectors::MAINNET_BLOCKS + zebra_test::vectors::MAINNET_BLOCKS.clone() } else { - &zebra_test::vectors::TESTNET_BLOCKS + zebra_test::vectors::TESTNET_BLOCKS.clone() } }