Fix tests

This commit is contained in:
therealyingtong 2020-07-31 00:44:23 +08:00
parent 6904c8f933
commit 895e251793
No known key found for this signature in database
GPG Key ID: 179F32A1503D607E
7 changed files with 724 additions and 318 deletions

View File

@ -195,10 +195,11 @@ mod tests {
use rand_core::{OsRng, RngCore};
use zcash_primitives::{
consensus,
consensus::{NetworkUpgrade, Parameters},
jubjub::{fs::Fs, FixedGenerators, JubjubParams, ToUniform},
merkle_tree::CommitmentTree,
note_encryption::{Memo, SaplingNoteEncryption},
primitives::Note,
primitives::{Note, Rseed},
transaction::components::Amount,
zip32::{ExtendedFullViewingKey, ExtendedSpendingKey},
JUBJUB,
@ -257,18 +258,26 @@ mod tests {
// Create a fake Note for the account
let mut rng = OsRng;
let rseed = if consensus::MainNetwork.is_nu_active(NetworkUpgrade::Canopy, height as u32) {
let mut buffer = [0u8; 32];
&rng.fill_bytes(&mut buffer);
Rseed::AfterZip212(buffer)
} else {
Rseed::BeforeZip212(Fs::random(&mut rng))
};
let note = Note {
g_d: to.diversifier().g_d::<Bls12>(&JUBJUB).unwrap(),
pk_d: to.pk_d().clone(),
value: value.into(),
r: Fs::random(&mut rng),
rseed,
};
let esk = note.generate_or_derive_esk(&mut rng);
let encryptor = SaplingNoteEncryption::new(
extfvk.fvk.ovk,
note.clone(),
to.clone(),
Memo::default(),
&mut rng,
esk,
);
let cmu = note.cm(&JUBJUB).to_repr().as_ref().to_owned();
let mut epk = vec![];

View File

@ -100,9 +100,11 @@ mod tests {
};
use zcash_primitives::{
block::BlockHash,
consensus,
consensus::{NetworkUpgrade, Parameters},
jubjub::fs::Fs,
note_encryption::{Memo, SaplingNoteEncryption},
primitives::{Note, PaymentAddress},
primitives::{Note, PaymentAddress, Rseed},
transaction::components::Amount,
zip32::ExtendedFullViewingKey,
JUBJUB,
@ -120,18 +122,26 @@ mod tests {
// Create a fake Note for the account
let mut rng = OsRng;
let rseed = if consensus::MainNetwork.is_nu_active(NetworkUpgrade::Canopy, height as u32) {
let mut buffer = [0u8; 32];
&rng.fill_bytes(&mut buffer);
Rseed::AfterZip212(buffer)
} else {
Rseed::BeforeZip212(Fs::random(&mut rng))
};
let note = Note {
g_d: to.diversifier().g_d::<Bls12>(&JUBJUB).unwrap(),
pk_d: to.pk_d().clone(),
value: value.into(),
r: Fs::random(&mut rng),
rseed,
};
let esk = note.generate_or_derive_esk(&mut rng);
let encryptor = SaplingNoteEncryption::new(
extfvk.fvk.ovk,
note.clone(),
to.clone(),
Memo::default(),
&mut rng,
esk,
);
let cmu = note.cm(&JUBJUB).to_repr().as_ref().to_vec();
let mut epk = vec![];
@ -168,6 +178,13 @@ mod tests {
value: Amount,
) -> CompactBlock {
let mut rng = OsRng;
let rseed = if consensus::MainNetwork.is_nu_active(NetworkUpgrade::Canopy, height as u32) {
let mut buffer = [0u8; 32];
&rng.fill_bytes(&mut buffer);
Rseed::AfterZip212(buffer)
} else {
Rseed::BeforeZip212(Fs::random(&mut rng))
};
// Create a fake CompactBlock containing the note
let mut cspend = CompactSpend::new();
@ -184,15 +201,11 @@ mod tests {
g_d: to.diversifier().g_d::<Bls12>(&JUBJUB).unwrap(),
pk_d: to.pk_d().clone(),
value: value.into(),
r: Fs::random(&mut rng),
rseed,
};
let encryptor = SaplingNoteEncryption::new(
extfvk.fvk.ovk,
note.clone(),
to,
Memo::default(),
&mut rng,
);
let esk = note.generate_or_derive_esk(&mut rng);
let encryptor =
SaplingNoteEncryption::new(extfvk.fvk.ovk, note.clone(), to, Memo::default(), esk);
let cmu = note.cm(&JUBJUB).to_repr().as_ref().to_vec();
let mut epk = vec![];
encryptor.epk().write(&mut epk).unwrap();
@ -208,18 +221,27 @@ mod tests {
// Create a fake Note for the change
ctx.outputs.push({
let change_addr = extfvk.default_address().unwrap().1;
let rseed =
if consensus::MainNetwork.is_nu_active(NetworkUpgrade::Canopy, height as u32) {
let mut buffer = [0u8; 32];
&rng.fill_bytes(&mut buffer);
Rseed::AfterZip212(buffer)
} else {
Rseed::BeforeZip212(Fs::random(&mut rng))
};
let note = Note {
g_d: change_addr.diversifier().g_d::<Bls12>(&JUBJUB).unwrap(),
pk_d: change_addr.pk_d().clone(),
value: (in_value - value).into(),
r: Fs::random(&mut rng),
rseed,
};
let esk = note.generate_or_derive_esk(&mut rng);
let encryptor = SaplingNoteEncryption::new(
extfvk.fvk.ovk,
note.clone(),
change_addr,
Memo::default(),
&mut rng,
esk,
);
let cmu = note.cm(&JUBJUB).to_repr().as_ref().to_vec();
let mut epk = vec![];

View File

@ -816,6 +816,8 @@ mod tests {
let output = &tx.shielded_outputs[output_index as usize];
try_sapling_output_recovery(
&consensus::MainNetwork,
SAPLING_ACTIVATION_HEIGHT as u32,
&extfvk.fvk.ovk,
&output.cv,
&output.cmu,

View File

@ -3,6 +3,12 @@
use std::convert::TryFrom;
use std::fmt;
#[cfg(feature = "mainnet")]
pub const SAPLING_ACTIVATION_HEIGHT: u32 = 419_200;
#[cfg(not(feature = "mainnet"))]
pub const SAPLING_ACTIVATION_HEIGHT: u32 = 280_000;
/// Zcash consensus parameters.
pub trait Parameters {
fn activation_height(&self, nu: NetworkUpgrade) -> Option<u32>;
@ -202,8 +208,8 @@ mod tests {
let nu_a = UPGRADES_IN_ORDER[i - 1];
let nu_b = UPGRADES_IN_ORDER[i];
match (
MainNetwork::activation_height(nu_a),
MainNetwork::activation_height(nu_b),
MainNetwork.activation_height(nu_a),
MainNetwork.activation_height(nu_b),
) {
(Some(a), Some(b)) if a < b => (),
(Some(_), None) => (),
@ -218,15 +224,9 @@ mod tests {
#[test]
fn nu_is_active() {
assert!(!MainNetwork::is_nu_active(NetworkUpgrade::Overwinter, 0));
assert!(!MainNetwork::is_nu_active(
NetworkUpgrade::Overwinter,
347_499
));
assert!(MainNetwork::is_nu_active(
NetworkUpgrade::Overwinter,
347_500
));
assert!(!MainNetwork.is_nu_active(NetworkUpgrade::Overwinter, 0));
assert!(!MainNetwork.is_nu_active(NetworkUpgrade::Overwinter, 347_499));
assert!(MainNetwork.is_nu_active(NetworkUpgrade::Overwinter, 347_500));
}
#[test]
@ -237,25 +237,28 @@ mod tests {
#[test]
fn branch_id_for_height() {
assert_eq!(BranchId::for_height::<MainNetwork>(0), BranchId::Sprout,);
assert_eq!(
BranchId::for_height::<MainNetwork>(419_199),
BranchId::for_height::<MainNetwork>(MainNetwork, 0),
BranchId::Sprout,
);
assert_eq!(
BranchId::for_height::<MainNetwork>(MainNetwork, 419_199),
BranchId::Overwinter,
);
assert_eq!(
BranchId::for_height::<MainNetwork>(419_200),
BranchId::for_height::<MainNetwork>(MainNetwork, 419_200),
BranchId::Sapling,
);
assert_eq!(
BranchId::for_height::<MainNetwork>(903_000),
BranchId::for_height::<MainNetwork>(MainNetwork, 903_000),
BranchId::Heartwood,
);
assert_eq!(
BranchId::for_height::<MainNetwork>(1_046_400),
BranchId::for_height::<MainNetwork>(MainNetwork, 1_046_400),
BranchId::Canopy,
);
assert_eq!(
BranchId::for_height::<MainNetwork>(5_000_000),
BranchId::for_height::<MainNetwork>(MainNetwork, 5_000_000),
BranchId::Canopy,
);
}

File diff suppressed because it is too large Load Diff

View File

@ -726,6 +726,7 @@ mod tests {
use super::{Builder, Error};
use crate::{
consensus,
consensus::SAPLING_ACTIVATION_HEIGHT,
legacy::TransparentAddress,
merkle_tree::{CommitmentTree, IncrementalWitness},
primitives::Rseed,
@ -760,6 +761,7 @@ mod tests {
// Create a builder with 0 fee, so we can construct t outputs
let mut builder = builder::Builder {
rng: OsRng,
height: SAPLING_ACTIVATION_HEIGHT,
mtx: TransactionData::new(),
fee: Amount::zero(),
anchor: None,

View File

@ -544,7 +544,7 @@ fn test_input_circuit_with_bls12_381() {
use zcash_primitives::{
jubjub::{edwards, fs, JubjubBls12},
pedersen_hash,
primitives::{Diversifier, Note, ProofGenerationKey},
primitives::{Diversifier, Note, ProofGenerationKey, Rseed},
};
let params = &JubjubBls12::new();
@ -598,7 +598,7 @@ fn test_input_circuit_with_bls12_381() {
value: value_commitment.value,
g_d: g_d.clone(),
pk_d: payment_address.pk_d().clone(),
r: commitment_randomness.clone(),
rseed: Rseed::BeforeZip212(commitment_randomness.clone()),
};
let mut position = 0u64;
@ -694,7 +694,7 @@ fn test_input_circuit_with_bls12_381_external_test_vectors() {
use zcash_primitives::{
jubjub::{edwards, fs, JubjubBls12},
pedersen_hash,
primitives::{Diversifier, Note, ProofGenerationKey},
primitives::{Diversifier, Note, ProofGenerationKey, Rseed},
};
let params = &JubjubBls12::new();
@ -782,7 +782,7 @@ fn test_input_circuit_with_bls12_381_external_test_vectors() {
value: value_commitment.value,
g_d: g_d.clone(),
pk_d: payment_address.pk_d().clone(),
r: commitment_randomness.clone(),
rseed: Rseed::BeforeZip212(commitment_randomness.clone()),
};
let mut position = 0u64;
@ -877,7 +877,7 @@ fn test_output_circuit_with_bls12_381() {
use rand_xorshift::XorShiftRng;
use zcash_primitives::{
jubjub::{edwards, fs, JubjubBls12},
primitives::{Diversifier, ProofGenerationKey},
primitives::{Diversifier, ProofGenerationKey, Rseed},
};
let params = &JubjubBls12::new();
@ -941,7 +941,11 @@ fn test_output_circuit_with_bls12_381() {
);
let expected_cm = payment_address
.create_note(value_commitment.value, commitment_randomness, params)
.create_note(
value_commitment.value,
Rseed::BeforeZip212(commitment_randomness),
params,
)
.expect("should be valid")
.cm(params);