Skip to content

Commit

Permalink
change(tests): Remove Matches on Network From Tests (#8295)
Browse files Browse the repository at this point in the history
* added functions for fetching block vectors

* inserted new network methods for vector fetching into zebra-chain

* changed tag back to test/feature=branch

* changed tag back to test/feature=branch

* changed tag back to test/feature=branch

* changed feature tag to proptest-impl, started implementing in zebra-consensus tests

* adding methods to zebra-consensus, lifetime error in src/transaction/tests.rs, needs refactoring

* finished adding methods to zebra-consensus

* finished adding new methods to zebrad

* added new methods to zebra-rpc and zebra-scan

* finished removing statements matching on Network from tests

* updated new error message

* 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

* removed match statements from zebra_chain::transaction::arbitrary::test_transactions() and new zebra-grpc tests

* moved zebra-chain::test_utils to zebra-chain::test

* removed get_ prefix from getter methods

* renamed zebra-chain::test to zebra-chain::tests

* renamed zebra-chain::test to zebra-chain::tests

* fixed clippy warnings

* changed block_map to return clone
  • Loading branch information
idky137 authored Mar 5, 2024
1 parent e64f3dc commit 9b91d4b
Show file tree
Hide file tree
Showing 28 changed files with 268 additions and 302 deletions.
22 changes: 2 additions & 20 deletions zebra-chain/src/block/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.block_sapling_roots_iter();

for (&height, block) in block_iter {
let block = block
Expand Down Expand Up @@ -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.block_sapling_roots_iter();

for (height, block) in block_iter {
let block = block
Expand Down
15 changes: 4 additions & 11 deletions zebra-chain/src/history_tree/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.block_sapling_roots_map();

let height = network_upgrade.activation_height(network).unwrap().0;

// Load first block (activation block of the given network upgrade)
Expand Down Expand Up @@ -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.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)
Expand Down
3 changes: 3 additions & 0 deletions zebra-chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub mod work;
#[cfg(any(test, feature = "proptest-impl"))]
pub use block::LedgerState;

#[cfg(any(test, feature = "proptest-impl"))]
pub mod tests;

/// Error type alias to make working with generic errors easier.
///
/// Note: the 'static lifetime bound means that the *type* cannot have any
Expand Down
9 changes: 2 additions & 7 deletions zebra-chain/src/primitives/zcash_history/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.block_sapling_roots_map();

let height = network_upgrade.activation_height(network).unwrap().0;

// Load Block 0 (activation block of the given network upgrade)
Expand Down
9 changes: 2 additions & 7 deletions zebra-chain/src/sapling/tests/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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.block_sapling_roots_map();

let height = NetworkUpgrade::Sapling
.activation_height(network)
.unwrap()
Expand Down
21 changes: 1 addition & 20 deletions zebra-chain/src/sprout/tests/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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.block_sprout_roots_height();

// Load the Genesis height.
let genesis_height = NetworkUpgrade::Genesis
Expand Down
4 changes: 4 additions & 0 deletions zebra-chain/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//!Chain functionality for fetching test vectors.
//!
mod vectors;
154 changes: 154 additions & 0 deletions zebra-chain/src/tests/vectors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
//! 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<u32, &'static [u8]> {
if self.is_mainnet() {
zebra_test::vectors::MAINNET_BLOCKS.clone()
} else {
zebra_test::vectors::TESTNET_BLOCKS.clone()
}
}

/// 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<Block> {
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(),
_ => None,
}
}

/// 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<u32, &'static [u8]> {
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<u32, &[u8; 32]>,
) {
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<u32, &'static [u8]>,
&std::collections::BTreeMap<u32, &'static [u8; 32]>,
) {
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])> {
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.
pub fn block_sprout_roots_height(
&self,
) -> (
&std::collections::BTreeMap<u32, &'static [u8]>,
&std::collections::BTreeMap<u32, &'static [u8; 32]>,
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,
)
}
}
}
5 changes: 1 addition & 4 deletions zebra-chain/src/transaction/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -995,10 +995,7 @@ fn sapling_spend_v4_to_fake_v5(
pub fn test_transactions(
network: Network,
) -> impl DoubleEndedIterator<Item = (block::Height, Arc<Transaction>)> {
let blocks = match network {
Network::Mainnet => zebra_test::vectors::MAINNET_BLOCKS.iter(),
Network::Testnet => zebra_test::vectors::TESTNET_BLOCKS.iter(),
};
let blocks = network.block_iter();

transactions_from_blocks(blocks)
}
Expand Down
15 changes: 3 additions & 12 deletions zebra-chain/src/transaction/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.block_iter();

let overwinter_activation_height = NetworkUpgrade::Overwinter
.activation_height(network)
Expand Down Expand Up @@ -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.block_iter();

let overwinter_activation_height = NetworkUpgrade::Overwinter
.activation_height(network)
Expand Down Expand Up @@ -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.block_iter();

for (height, bytes) in block_iter {
let upgrade = NetworkUpgrade::current(network, Height(*height));
Expand Down
5 changes: 1 addition & 4 deletions zebra-chain/src/transparent/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.block_iter();

let mut valid_addresses = 0;

Expand Down
Loading

0 comments on commit 9b91d4b

Please sign in to comment.