From d232133216489434d0af5924fa8a5fa7cd62591a Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Thu, 17 Sep 2020 11:55:39 -0600 Subject: [PATCH] Add constants to support static resolution of network parameters. --- zcash_primitives/src/consensus.rs | 138 +++++++++----- zcash_primitives/src/note_encryption.rs | 197 +++++++++----------- zcash_primitives/src/transaction/builder.rs | 24 +-- 3 files changed, 195 insertions(+), 164 deletions(-) diff --git a/zcash_primitives/src/consensus.rs b/zcash_primitives/src/consensus.rs index 9c3e6ecd6..e0d6f6973 100644 --- a/zcash_primitives/src/consensus.rs +++ b/zcash_primitives/src/consensus.rs @@ -132,14 +132,76 @@ pub trait Parameters { fn b58_script_address_prefix(&self) -> [u8; 2]; fn is_nu_active(&self, nu: NetworkUpgrade, height: BlockHeight) -> bool { - match self.activation_height(nu) { - Some(h) if h <= height => true, - _ => false, - } + self.activation_height(nu).map_or(false, |h| h <= height) } } /// Marker struct for the production network. +pub struct MainNetwork {} + +pub const MAIN_NETWORK: MainNetwork = MainNetwork {}; + +impl Parameters for MainNetwork { + fn activation_height(&self, nu: NetworkUpgrade) -> Option { + match nu { + NetworkUpgrade::Overwinter => Some(BlockHeight(347_500)), + NetworkUpgrade::Sapling => Some(BlockHeight(419_200)), + NetworkUpgrade::Blossom => Some(BlockHeight(653_600)), + NetworkUpgrade::Heartwood => Some(BlockHeight(903_000)), + NetworkUpgrade::Canopy => Some(BlockHeight(1_046_400)), + } + } + + fn hrp_sapling_extended_full_viewing_key(&self) -> &str { + constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY + } + + fn hrp_sapling_payment_address(&self) -> &str { + constants::mainnet::HRP_SAPLING_PAYMENT_ADDRESS + } + + fn b58_pubkey_address_prefix(&self) -> [u8; 2] { + constants::mainnet::B58_PUBKEY_ADDRESS_PREFIX + } + + fn b58_script_address_prefix(&self) -> [u8; 2] { + constants::mainnet::B58_SCRIPT_ADDRESS_PREFIX + } +} + +/// Marker struct for the test network. +pub struct TestNetwork {} + +pub const TEST_NETWORK: TestNetwork = TestNetwork {}; + +impl Parameters for TestNetwork { + fn activation_height(&self, nu: NetworkUpgrade) -> Option { + match nu { + NetworkUpgrade::Overwinter => Some(BlockHeight(207_500)), + NetworkUpgrade::Sapling => Some(BlockHeight(280_000)), + NetworkUpgrade::Blossom => Some(BlockHeight(584_000)), + NetworkUpgrade::Heartwood => Some(BlockHeight(903_800)), + NetworkUpgrade::Canopy => Some(BlockHeight(1_028_500)), + } + } + + fn hrp_sapling_extended_full_viewing_key(&self) -> &str { + constants::testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY + } + + fn hrp_sapling_payment_address(&self) -> &str { + constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS + } + + fn b58_pubkey_address_prefix(&self) -> [u8; 2] { + constants::testnet::B58_PUBKEY_ADDRESS_PREFIX + } + + fn b58_script_address_prefix(&self) -> [u8; 2] { + constants::testnet::B58_SCRIPT_ADDRESS_PREFIX + } +} + #[derive(Clone, Copy, Debug)] pub enum Network { MainNetwork, @@ -149,52 +211,36 @@ pub enum Network { impl Parameters for Network { fn activation_height(&self, nu: NetworkUpgrade) -> Option { match self { - Network::MainNetwork => match nu { - NetworkUpgrade::Overwinter => Some(BlockHeight(347_500)), - NetworkUpgrade::Sapling => Some(BlockHeight(419_200)), - NetworkUpgrade::Blossom => Some(BlockHeight(653_600)), - NetworkUpgrade::Heartwood => Some(BlockHeight(903_000)), - NetworkUpgrade::Canopy => Some(BlockHeight(1_046_400)), - }, - Network::TestNetwork => match nu { - NetworkUpgrade::Overwinter => Some(BlockHeight(207_500)), - NetworkUpgrade::Sapling => Some(BlockHeight(280_000)), - NetworkUpgrade::Blossom => Some(BlockHeight(584_000)), - NetworkUpgrade::Heartwood => Some(BlockHeight(903_800)), - NetworkUpgrade::Canopy => Some(BlockHeight(1_028_500)), - }, + Network::MainNetwork => MAIN_NETWORK.activation_height(nu), + Network::TestNetwork => TEST_NETWORK.activation_height(nu), } } fn hrp_sapling_extended_full_viewing_key(&self) -> &str { match self { - Network::MainNetwork => constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, - - Network::TestNetwork => constants::testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, + Network::MainNetwork => MAIN_NETWORK.hrp_sapling_extended_full_viewing_key(), + Network::TestNetwork => TEST_NETWORK.hrp_sapling_extended_full_viewing_key(), } } fn hrp_sapling_payment_address(&self) -> &str { match self { - Network::MainNetwork => constants::mainnet::HRP_SAPLING_PAYMENT_ADDRESS, - - Network::TestNetwork => constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS, + Network::MainNetwork => MAIN_NETWORK.hrp_sapling_payment_address(), + Network::TestNetwork => TEST_NETWORK.hrp_sapling_payment_address(), } } fn b58_pubkey_address_prefix(&self) -> [u8; 2] { match self { - Network::MainNetwork => constants::mainnet::B58_PUBKEY_ADDRESS_PREFIX, - - Network::TestNetwork => constants::testnet::B58_PUBKEY_ADDRESS_PREFIX, + Network::MainNetwork => MAIN_NETWORK.b58_pubkey_address_prefix(), + Network::TestNetwork => TEST_NETWORK.b58_pubkey_address_prefix(), } } fn b58_script_address_prefix(&self) -> [u8; 2] { match self { - Network::MainNetwork => constants::mainnet::B58_SCRIPT_ADDRESS_PREFIX, - - Network::TestNetwork => constants::testnet::B58_SCRIPT_ADDRESS_PREFIX, + Network::MainNetwork => MAIN_NETWORK.b58_script_address_prefix(), + Network::TestNetwork => TEST_NETWORK.b58_script_address_prefix(), } } } @@ -344,7 +390,9 @@ impl BranchId { mod tests { use std::convert::TryFrom; - use super::{BlockHeight, BranchId, Network, NetworkUpgrade, Parameters, UPGRADES_IN_ORDER}; + use super::{ + BlockHeight, BranchId, NetworkUpgrade, Parameters, MAIN_NETWORK, UPGRADES_IN_ORDER, + }; #[test] fn nu_ordering() { @@ -352,12 +400,10 @@ mod tests { let nu_a = UPGRADES_IN_ORDER[i - 1]; let nu_b = UPGRADES_IN_ORDER[i]; match ( - Network::MainNetwork.activation_height(nu_a), - Network::MainNetwork.activation_height(nu_b), + MAIN_NETWORK.activation_height(nu_a), + MAIN_NETWORK.activation_height(nu_b), ) { - (Some(a), Some(b)) if a < b => (), - (Some(_), None) => (), - (None, None) => (), + (a, b) if a < b => (), _ => panic!( "{} should not be before {} in UPGRADES_IN_ORDER", nu_a, nu_b @@ -368,11 +414,9 @@ mod tests { #[test] fn nu_is_active() { - assert!(!Network::MainNetwork.is_nu_active(NetworkUpgrade::Overwinter, BlockHeight(0))); - assert!( - !Network::MainNetwork.is_nu_active(NetworkUpgrade::Overwinter, BlockHeight(347_499)) - ); - assert!(Network::MainNetwork.is_nu_active(NetworkUpgrade::Overwinter, BlockHeight(347_500))); + assert!(!MAIN_NETWORK.is_nu_active(NetworkUpgrade::Overwinter, BlockHeight(0))); + assert!(!MAIN_NETWORK.is_nu_active(NetworkUpgrade::Overwinter, BlockHeight(347_499))); + assert!(MAIN_NETWORK.is_nu_active(NetworkUpgrade::Overwinter, BlockHeight(347_500))); } #[test] @@ -384,27 +428,27 @@ mod tests { #[test] fn branch_id_for_height() { assert_eq!( - BranchId::for_height(&Network::MainNetwork, BlockHeight(0)), + BranchId::for_height(&MAIN_NETWORK, BlockHeight(0)), BranchId::Sprout, ); assert_eq!( - BranchId::for_height(&Network::MainNetwork, BlockHeight(419_199)), + BranchId::for_height(&MAIN_NETWORK, BlockHeight(419_199)), BranchId::Overwinter, ); assert_eq!( - BranchId::for_height(&Network::MainNetwork, BlockHeight(419_200)), + BranchId::for_height(&MAIN_NETWORK, BlockHeight(419_200)), BranchId::Sapling, ); assert_eq!( - BranchId::for_height(&Network::MainNetwork, BlockHeight(903_000)), + BranchId::for_height(&MAIN_NETWORK, BlockHeight(903_000)), BranchId::Heartwood, ); assert_eq!( - BranchId::for_height(&Network::MainNetwork, BlockHeight(1_046_400)), + BranchId::for_height(&MAIN_NETWORK, BlockHeight(1_046_400)), BranchId::Canopy, ); assert_eq!( - BranchId::for_height(&Network::MainNetwork, BlockHeight(5_000_000)), + BranchId::for_height(&MAIN_NETWORK, BlockHeight(5_000_000)), BranchId::Canopy, ); } diff --git a/zcash_primitives/src/note_encryption.rs b/zcash_primitives/src/note_encryption.rs index e7a2c67e9..d67880215 100644 --- a/zcash_primitives/src/note_encryption.rs +++ b/zcash_primitives/src/note_encryption.rs @@ -1,7 +1,7 @@ //! Implementation of in-band secret distribution for Zcash transactions. use crate::{ - consensus::{self, BlockHeight, NetworkUpgrade, ZIP212_GRACE_PERIOD}, + consensus::{self, BlockHeight, NetworkUpgrade::Canopy, ZIP212_GRACE_PERIOD}, primitives::{Diversifier, Note, PaymentAddress, Rseed}, }; use blake2b_simd::{Hash as Blake2bHash, Params as Blake2bParams}; @@ -412,11 +412,9 @@ pub fn plaintext_version_is_valid( height: BlockHeight, leadbyte: u8, ) -> bool { - if params.is_nu_active(NetworkUpgrade::Canopy, height) { - let grace_period_end_height = params - .activation_height(NetworkUpgrade::Canopy) - .expect("Should have Canopy activation height") - + ZIP212_GRACE_PERIOD; + if params.is_nu_active(Canopy, height) { + let grace_period_end_height = + params.activation_height(Canopy).unwrap() + ZIP212_GRACE_PERIOD; if height < grace_period_end_height && leadbyte != 0x01 && leadbyte != 0x02 { // non-{0x01,0x02} received after Canopy activation and before grace period has elapsed @@ -662,11 +660,9 @@ mod tests { use crate::{ consensus::{ - BlockHeight, Network, - Network::TestNetwork, - NetworkUpgrade, + BlockHeight, NetworkUpgrade::{Canopy, Sapling}, - Parameters, ZIP212_GRACE_PERIOD, + Parameters, TEST_NETWORK, ZIP212_GRACE_PERIOD, }, // jubjub::{ // edwards, @@ -813,7 +809,7 @@ mod tests { random_enc_ciphertext_with(height, ivk, rng); assert!(try_sapling_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &ivk, &epk, @@ -822,7 +818,7 @@ mod tests { ) .is_some()); assert!(try_sapling_compact_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &ivk, &epk, @@ -832,7 +828,7 @@ mod tests { .is_some()); let ovk_output_recovery = try_sapling_output_recovery( - &Network::TestNetwork, + &TEST_NETWORK, height, &ovk, &cv, @@ -843,7 +839,7 @@ mod tests { ); let ock_output_recovery = try_sapling_output_recovery_with_ock( - &Network::TestNetwork, + &TEST_NETWORK, height, &ock, &cmu, @@ -884,7 +880,7 @@ mod tests { }; let cv = value_commitment.commitment().into(); - let rseed = generate_random_rseed(&Network::TestNetwork, height, &mut rng); + let rseed = generate_random_rseed(&TEST_NETWORK, height, &mut rng); let note = pa.create_note(value, rseed).unwrap(); let cmu = note.cmu(); @@ -986,8 +982,8 @@ mod tests { fn decryption_with_invalid_ivk() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -995,7 +991,7 @@ mod tests { assert_eq!( try_sapling_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &jubjub::Fr::random(&mut rng), &epk, @@ -1011,8 +1007,8 @@ mod tests { fn decryption_with_invalid_epk() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1020,7 +1016,7 @@ mod tests { assert_eq!( try_sapling_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &ivk, &jubjub::SubgroupPoint::random(&mut rng), @@ -1036,8 +1032,8 @@ mod tests { fn decryption_with_invalid_cmu() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1045,7 +1041,7 @@ mod tests { assert_eq!( try_sapling_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &ivk, &epk, @@ -1061,8 +1057,8 @@ mod tests { fn decryption_with_invalid_tag() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1072,7 +1068,7 @@ mod tests { enc_ciphertext[ENC_CIPHERTEXT_SIZE - 1] ^= 0xff; assert_eq!( try_sapling_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &ivk, &epk, @@ -1087,7 +1083,7 @@ mod tests { #[test] fn decryption_with_invalid_version_byte() { let mut rng = OsRng; - let canopy_activation_height = TestNetwork.activation_height(Canopy).unwrap(); + let canopy_activation_height = TEST_NETWORK.activation_height(Canopy).unwrap(); let heights = [ canopy_activation_height - 1, canopy_activation_height, @@ -1110,7 +1106,7 @@ mod tests { ); assert_eq!( try_sapling_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &ivk, &epk, @@ -1126,8 +1122,8 @@ mod tests { fn decryption_with_invalid_diversifier() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1145,7 +1141,7 @@ mod tests { ); assert_eq!( try_sapling_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &ivk, &epk, @@ -1161,8 +1157,8 @@ mod tests { fn decryption_with_incorrect_diversifier() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1181,7 +1177,7 @@ mod tests { assert_eq!( try_sapling_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &ivk, &epk, @@ -1197,8 +1193,8 @@ mod tests { fn compact_decryption_with_invalid_ivk() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1206,7 +1202,7 @@ mod tests { assert_eq!( try_sapling_compact_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &jubjub::Fr::random(&mut rng), &epk, @@ -1222,8 +1218,8 @@ mod tests { fn compact_decryption_with_invalid_epk() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1231,7 +1227,7 @@ mod tests { assert_eq!( try_sapling_compact_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &ivk, &jubjub::SubgroupPoint::random(&mut rng), @@ -1247,8 +1243,8 @@ mod tests { fn compact_decryption_with_invalid_cmu() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1256,7 +1252,7 @@ mod tests { assert_eq!( try_sapling_compact_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &ivk, &epk, @@ -1271,7 +1267,7 @@ mod tests { #[test] fn compact_decryption_with_invalid_version_byte() { let mut rng = OsRng; - let canopy_activation_height = TestNetwork.activation_height(Canopy).unwrap(); + let canopy_activation_height = TEST_NETWORK.activation_height(Canopy).unwrap(); let heights = [ canopy_activation_height - 1, canopy_activation_height, @@ -1294,7 +1290,7 @@ mod tests { ); assert_eq!( try_sapling_compact_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &ivk, &epk, @@ -1310,8 +1306,8 @@ mod tests { fn compact_decryption_with_invalid_diversifier() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1329,7 +1325,7 @@ mod tests { ); assert_eq!( try_sapling_compact_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &ivk, &epk, @@ -1345,8 +1341,8 @@ mod tests { fn compact_decryption_with_incorrect_diversifier() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1364,7 +1360,7 @@ mod tests { ); assert_eq!( try_sapling_compact_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &ivk, &epk, @@ -1380,8 +1376,8 @@ mod tests { fn recovery_with_invalid_ovk() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1391,7 +1387,7 @@ mod tests { ovk.0[0] ^= 0xff; assert_eq!( try_sapling_output_recovery( - &Network::TestNetwork, + &TEST_NETWORK, height, &ovk, &cv, @@ -1409,8 +1405,8 @@ mod tests { fn recovery_with_invalid_ock() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1419,7 +1415,7 @@ mod tests { assert_eq!( try_sapling_output_recovery_with_ock( - &Network::TestNetwork, + &TEST_NETWORK, height, &OutgoingCipherKey([0u8; 32]), &cmu, @@ -1436,8 +1432,8 @@ mod tests { fn recovery_with_invalid_cv() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1446,7 +1442,7 @@ mod tests { assert_eq!( try_sapling_output_recovery( - &Network::TestNetwork, + &TEST_NETWORK, height, &ovk, &jubjub::ExtendedPoint::random(&mut rng), @@ -1464,8 +1460,8 @@ mod tests { fn recovery_with_invalid_cmu() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1474,7 +1470,7 @@ mod tests { assert_eq!( try_sapling_output_recovery( - &Network::TestNetwork, + &TEST_NETWORK, height, &ovk, &cv, @@ -1488,7 +1484,7 @@ mod tests { assert_eq!( try_sapling_output_recovery_with_ock( - &Network::TestNetwork, + &TEST_NETWORK, height, &ock, &bls12_381::Scalar::random(&mut rng), @@ -1505,8 +1501,8 @@ mod tests { fn recovery_with_invalid_epk() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1515,7 +1511,7 @@ mod tests { assert_eq!( try_sapling_output_recovery( - &Network::TestNetwork, + &TEST_NETWORK, height, &ovk, &cv, @@ -1529,7 +1525,7 @@ mod tests { assert_eq!( try_sapling_output_recovery_with_ock( - &Network::TestNetwork, + &TEST_NETWORK, height, &ock, &cmu, @@ -1546,8 +1542,8 @@ mod tests { fn recovery_with_invalid_enc_tag() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1557,7 +1553,7 @@ mod tests { enc_ciphertext[ENC_CIPHERTEXT_SIZE - 1] ^= 0xff; assert_eq!( try_sapling_output_recovery( - &Network::TestNetwork, + &TEST_NETWORK, height, &ovk, &cv, @@ -1570,7 +1566,7 @@ mod tests { ); assert_eq!( try_sapling_output_recovery_with_ock( - &Network::TestNetwork, + &TEST_NETWORK, height, &ock, &cmu, @@ -1587,8 +1583,8 @@ mod tests { fn recovery_with_invalid_out_tag() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1598,7 +1594,7 @@ mod tests { out_ciphertext[OUT_CIPHERTEXT_SIZE - 1] ^= 0xff; assert_eq!( try_sapling_output_recovery( - &Network::TestNetwork, + &TEST_NETWORK, height, &ovk, &cv, @@ -1611,7 +1607,7 @@ mod tests { ); assert_eq!( try_sapling_output_recovery_with_ock( - &Network::TestNetwork, + &TEST_NETWORK, height, &ock, &cmu, @@ -1627,7 +1623,7 @@ mod tests { #[test] fn recovery_with_invalid_version_byte() { let mut rng = OsRng; - let canopy_activation_height = TestNetwork.activation_height(Canopy).unwrap(); + let canopy_activation_height = TEST_NETWORK.activation_height(Canopy).unwrap(); let heights = [ canopy_activation_height - 1, canopy_activation_height, @@ -1650,7 +1646,7 @@ mod tests { ); assert_eq!( try_sapling_output_recovery( - &Network::TestNetwork, + &TEST_NETWORK, height, &ovk, &cv, @@ -1663,7 +1659,7 @@ mod tests { ); assert_eq!( try_sapling_output_recovery_with_ock( - &Network::TestNetwork, + &TEST_NETWORK, height, &ock, &cmu, @@ -1680,8 +1676,8 @@ mod tests { fn recovery_with_invalid_diversifier() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1699,7 +1695,7 @@ mod tests { ); assert_eq!( try_sapling_output_recovery( - &Network::TestNetwork, + &TEST_NETWORK, height, &ovk, &cv, @@ -1712,7 +1708,7 @@ mod tests { ); assert_eq!( try_sapling_output_recovery_with_ock( - &Network::TestNetwork, + &TEST_NETWORK, height, &ock, &cmu, @@ -1729,8 +1725,8 @@ mod tests { fn recovery_with_incorrect_diversifier() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1748,7 +1744,7 @@ mod tests { ); assert_eq!( try_sapling_output_recovery( - &Network::TestNetwork, + &TEST_NETWORK, height, &ovk, &cv, @@ -1761,7 +1757,7 @@ mod tests { ); assert_eq!( try_sapling_output_recovery_with_ock( - &Network::TestNetwork, + &TEST_NETWORK, height, &ock, &cmu, @@ -1778,8 +1774,8 @@ mod tests { fn recovery_with_invalid_pk_d() { let mut rng = OsRng; let heights = [ - TestNetwork.activation_height(Sapling).unwrap(), - TestNetwork.activation_height(Canopy).unwrap(), + TEST_NETWORK.activation_height(Sapling).unwrap(), + TEST_NETWORK.activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1789,7 +1785,7 @@ mod tests { assert_eq!( try_sapling_output_recovery( - &Network::TestNetwork, + &TEST_NETWORK, height, &ovk, &cv, @@ -1802,7 +1798,7 @@ mod tests { ); assert_eq!( try_sapling_output_recovery_with_ock( - &Network::TestNetwork, + &TEST_NETWORK, height, &ock, &cmu, @@ -1837,9 +1833,7 @@ mod tests { }; } - let height = TestNetwork - .activation_height(NetworkUpgrade::Sapling) - .expect("Should have Sapling activation height"); + let height = TEST_NETWORK.activation_height(Sapling).unwrap(); for tv in test_vectors { // @@ -1877,14 +1871,7 @@ mod tests { // (Tested first because it only requires immutable references.) // - match try_sapling_note_decryption( - &Network::TestNetwork, - height, - &ivk, - &epk, - &cmu, - &tv.c_enc, - ) { + match try_sapling_note_decryption(&TEST_NETWORK, height, &ivk, &epk, &cmu, &tv.c_enc) { Some((decrypted_note, decrypted_to, decrypted_memo)) => { assert_eq!(decrypted_note, note); assert_eq!(decrypted_to, to); @@ -1894,7 +1881,7 @@ mod tests { } match try_sapling_compact_note_decryption( - &Network::TestNetwork, + &TEST_NETWORK, height, &ivk, &epk, @@ -1909,7 +1896,7 @@ mod tests { } match try_sapling_output_recovery( - &Network::TestNetwork, + &TEST_NETWORK, height, &ovk, &cv, diff --git a/zcash_primitives/src/transaction/builder.rs b/zcash_primitives/src/transaction/builder.rs index 64fc115f0..374c2af15 100644 --- a/zcash_primitives/src/transaction/builder.rs +++ b/zcash_primitives/src/transaction/builder.rs @@ -720,7 +720,7 @@ mod tests { use super::{Builder, Error}; use crate::{ - consensus::{self, Network::TestNetwork, H0}, + consensus::{self, Parameters, H0, TEST_NETWORK}, legacy::TransparentAddress, merkle_tree::{CommitmentTree, IncrementalWitness}, primitives::Rseed, @@ -737,7 +737,7 @@ mod tests { let ovk = extfvk.fvk.ovk; let to = extfvk.default_address().unwrap().1; - let mut builder = Builder::new(TestNetwork, H0); + let mut builder = Builder::new(TEST_NETWORK, H0); assert_eq!( builder.add_sapling_output(Some(ovk), to, Amount::from_i64(-1).unwrap(), None), Err(Error::InvalidAmount) @@ -746,19 +746,19 @@ mod tests { #[test] fn binding_sig_absent_if_no_shielded_spend_or_output() { - use crate::consensus::{NetworkUpgrade, Parameters}; + use crate::consensus::NetworkUpgrade; use crate::transaction::{ builder::{self, TransparentInputs}, TransactionData, }; - let sapling_activation_height = TestNetwork + let sapling_activation_height = TEST_NETWORK .activation_height(NetworkUpgrade::Sapling) .unwrap(); // Create a builder with 0 fee, so we can construct t outputs let mut builder = builder::Builder { - params: TestNetwork, + params: TEST_NETWORK, rng: OsRng, height: sapling_activation_height, mtx: TransactionData::new(), @@ -799,7 +799,7 @@ mod tests { tree.append(cmu1).unwrap(); let witness1 = IncrementalWitness::from_tree(&tree); - let mut builder = Builder::new(TestNetwork, H0); + let mut builder = Builder::new(TEST_NETWORK, H0); // Create a tx with a sapling spend. binding_sig should be present builder @@ -825,7 +825,7 @@ mod tests { #[test] fn fails_on_negative_transparent_output() { - let mut builder = Builder::new(TestNetwork, H0); + let mut builder = Builder::new(TEST_NETWORK, H0); assert_eq!( builder.add_transparent_output( &TransparentAddress::PublicKey([0; 20]), @@ -845,7 +845,7 @@ mod tests { // Fails with no inputs or outputs // 0.0001 t-ZEC fee { - let builder = Builder::new(TestNetwork, H0); + let builder = Builder::new(TEST_NETWORK, H0); assert_eq!( builder.build(consensus::BranchId::Sapling, &MockTxProver), Err(Error::ChangeIsNegative(Amount::from_i64(-10000).unwrap())) @@ -859,7 +859,7 @@ mod tests { // Fail if there is only a Sapling output // 0.0005 z-ZEC out, 0.0001 t-ZEC fee { - let mut builder = Builder::new(TestNetwork, H0); + let mut builder = Builder::new(TEST_NETWORK, H0); builder .add_sapling_output( ovk.clone(), @@ -877,7 +877,7 @@ mod tests { // Fail if there is only a transparent output // 0.0005 t-ZEC out, 0.0001 t-ZEC fee { - let mut builder = Builder::new(TestNetwork, H0); + let mut builder = Builder::new(TEST_NETWORK, H0); builder .add_transparent_output( &TransparentAddress::PublicKey([0; 20]), @@ -901,7 +901,7 @@ mod tests { // Fail if there is insufficient input // 0.0003 z-ZEC out, 0.0002 t-ZEC out, 0.0001 t-ZEC fee, 0.00059999 z-ZEC in { - let mut builder = Builder::new(TestNetwork, H0); + let mut builder = Builder::new(TEST_NETWORK, H0); builder .add_sapling_spend( extsk.clone(), @@ -944,7 +944,7 @@ mod tests { // (Still fails because we are using a MockTxProver which doesn't correctly // compute bindingSig.) { - let mut builder = Builder::new(TestNetwork, H0); + let mut builder = Builder::new(TEST_NETWORK, H0); builder .add_sapling_spend( extsk.clone(),