From 4f22077cf6adf8df42ec9e78fbb6f1335c7cc3af Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Thu, 6 Aug 2020 12:30:48 +0800 Subject: [PATCH] Remove const activation heights from consensus.rs --- zcash_client_backend/src/welding_rig.rs | 2 +- zcash_primitives/src/consensus.rs | 22 +---- zcash_primitives/src/note_encryption.rs | 107 +++++++++++++----------- 3 files changed, 60 insertions(+), 71 deletions(-) diff --git a/zcash_client_backend/src/welding_rig.rs b/zcash_client_backend/src/welding_rig.rs index fcb9c7a09..b37ee13f0 100644 --- a/zcash_client_backend/src/welding_rig.rs +++ b/zcash_client_backend/src/welding_rig.rs @@ -191,7 +191,7 @@ mod tests { use pairing::bls12_381::{Bls12, Fr}; use rand_core::{OsRng, RngCore}; use zcash_primitives::{ - consensus::{TestNetwork}, + consensus::TestNetwork, jubjub::{fs::Fs, FixedGenerators, JubjubParams, ToUniform}, merkle_tree::CommitmentTree, note_encryption::{Memo, SaplingNoteEncryption}, diff --git a/zcash_primitives/src/consensus.rs b/zcash_primitives/src/consensus.rs index 7020bae6e..4cc67f49b 100644 --- a/zcash_primitives/src/consensus.rs +++ b/zcash_primitives/src/consensus.rs @@ -13,12 +13,6 @@ pub trait Parameters { _ => false, } } - - const OVERWINTER_ACTIVATION_HEIGHT: u32; - const SAPLING_ACTIVATION_HEIGHT: u32; - const BLOSSOM_ACTIVATION_HEIGHT: u32; - const HEARTWOOD_ACTIVATION_HEIGHT: u32; - const CANOPY_ACTIVATION_HEIGHT: u32; } /// Marker struct for the production network. @@ -35,12 +29,6 @@ impl Parameters for MainNetwork { NetworkUpgrade::Canopy => Some(1_046_400), } } - - const OVERWINTER_ACTIVATION_HEIGHT: u32 = 347_500; - const SAPLING_ACTIVATION_HEIGHT: u32 = 419_200; - const BLOSSOM_ACTIVATION_HEIGHT: u32 = 653_600; - const HEARTWOOD_ACTIVATION_HEIGHT: u32 = 903_000; - const CANOPY_ACTIVATION_HEIGHT: u32 = 1_046_400; } /// Marker struct for the test network. @@ -57,12 +45,6 @@ impl Parameters for TestNetwork { NetworkUpgrade::Canopy => Some(1_028_500), } } - - const OVERWINTER_ACTIVATION_HEIGHT: u32 = 207_500; - const SAPLING_ACTIVATION_HEIGHT: u32 = 280_200; - const BLOSSOM_ACTIVATION_HEIGHT: u32 = 584_000; - const HEARTWOOD_ACTIVATION_HEIGHT: u32 = 903_800; - const CANOPY_ACTIVATION_HEIGHT: u32 = 1_028_500; } /// An event that occurs at a specified height on the Zcash chain, at which point the @@ -208,10 +190,10 @@ impl BranchId { #[cfg(test)] mod tests { - use super::{BranchId, MainNetwork, NetworkUpgrade, UPGRADES_IN_ORDER}; - use crate::consensus::Parameters; use std::convert::TryFrom; + use super::{BranchId, MainNetwork, NetworkUpgrade, Parameters, UPGRADES_IN_ORDER}; + #[test] fn nu_ordering() { for i in 1..UPGRADES_IN_ORDER.len() { diff --git a/zcash_primitives/src/note_encryption.rs b/zcash_primitives/src/note_encryption.rs index 5d91321ad..7a94fc66a 100644 --- a/zcash_primitives/src/note_encryption.rs +++ b/zcash_primitives/src/note_encryption.rs @@ -586,7 +586,11 @@ pub fn try_sapling_output_recovery( #[cfg(test)] mod tests { use crate::{ - consensus::{NetworkUpgrade, Parameters, TestNetwork, ZIP212_GRACE_PERIOD}, + consensus::{ + NetworkUpgrade, + NetworkUpgrade::{Canopy, Sapling}, + Parameters, TestNetwork, ZIP212_GRACE_PERIOD, + }, jubjub::{ edwards, fs::{Fs, FsRepr}, @@ -912,8 +916,8 @@ mod tests { fn decryption_with_invalid_ivk() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -936,8 +940,8 @@ mod tests { fn decryption_with_invalid_epk() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -960,8 +964,8 @@ mod tests { fn decryption_with_invalid_cmu() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -984,8 +988,8 @@ mod tests { fn decryption_with_invalid_tag() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1009,10 +1013,11 @@ mod tests { #[test] fn decryption_with_invalid_version_byte() { let mut rng = OsRng; + let canopy_activation_height = TestNetwork::activation_height(Canopy).unwrap(); let heights = [ - TestNetwork::CANOPY_ACTIVATION_HEIGHT - 1, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT + ZIP212_GRACE_PERIOD, + canopy_activation_height - 1, + canopy_activation_height, + canopy_activation_height + ZIP212_GRACE_PERIOD, ]; let leadbyte_array = [0x02, 0x03, 0x01]; @@ -1046,8 +1051,8 @@ mod tests { fn decryption_with_invalid_diversifier() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1080,8 +1085,8 @@ mod tests { fn decryption_with_incorrect_diversifier() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1114,8 +1119,8 @@ mod tests { fn compact_decryption_with_invalid_ivk() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1138,8 +1143,8 @@ mod tests { fn compact_decryption_with_invalid_epk() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1162,8 +1167,8 @@ mod tests { fn compact_decryption_with_invalid_cmu() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1185,10 +1190,11 @@ mod tests { #[test] fn compact_decryption_with_invalid_version_byte() { let mut rng = OsRng; + let canopy_activation_height = TestNetwork::activation_height(Canopy).unwrap(); let heights = [ - TestNetwork::CANOPY_ACTIVATION_HEIGHT - 1, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT + ZIP212_GRACE_PERIOD, + canopy_activation_height - 1, + canopy_activation_height, + canopy_activation_height + ZIP212_GRACE_PERIOD, ]; let leadbyte_array = [0x02, 0x03, 0x01]; @@ -1222,8 +1228,8 @@ mod tests { fn compact_decryption_with_invalid_diversifier() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1256,8 +1262,8 @@ mod tests { fn compact_decryption_with_incorrect_diversifier() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1290,8 +1296,8 @@ mod tests { fn recovery_with_invalid_ovk() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1318,8 +1324,8 @@ mod tests { fn recovery_with_invalid_cv() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1345,8 +1351,8 @@ mod tests { fn recovery_with_invalid_cmu() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1372,8 +1378,8 @@ mod tests { fn recovery_with_invalid_epk() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1399,8 +1405,8 @@ mod tests { fn recovery_with_invalid_enc_tag() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1427,8 +1433,8 @@ mod tests { fn recovery_with_invalid_out_tag() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1454,10 +1460,11 @@ mod tests { #[test] fn recovery_with_invalid_version_byte() { let mut rng = OsRng; + let canopy_activation_height = TestNetwork::activation_height(Canopy).unwrap(); let heights = [ - TestNetwork::CANOPY_ACTIVATION_HEIGHT - 1, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT + ZIP212_GRACE_PERIOD, + canopy_activation_height - 1, + canopy_activation_height, + canopy_activation_height + ZIP212_GRACE_PERIOD, ]; let leadbyte_array = [0x02, 0x03, 0x01]; @@ -1493,8 +1500,8 @@ mod tests { fn recovery_with_invalid_diversifier() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1529,8 +1536,8 @@ mod tests { fn recovery_with_incorrect_diversifier() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() { @@ -1565,8 +1572,8 @@ mod tests { fn recovery_with_invalid_pk_d() { let mut rng = OsRng; let heights = [ - TestNetwork::SAPLING_ACTIVATION_HEIGHT, - TestNetwork::CANOPY_ACTIVATION_HEIGHT, + TestNetwork::activation_height(Sapling).unwrap(), + TestNetwork::activation_height(Canopy).unwrap(), ]; for &height in heights.iter() {