diff --git a/zebra-chain/src/transaction/arbitrary.rs b/zebra-chain/src/transaction/arbitrary.rs index 1af3bf059ec..47da8040e99 100644 --- a/zebra-chain/src/transaction/arbitrary.rs +++ b/zebra-chain/src/transaction/arbitrary.rs @@ -143,7 +143,7 @@ impl Transaction { Just(NetworkUpgrade::Heartwood), Just(NetworkUpgrade::Canopy), Just(NetworkUpgrade::Nu5), - // TODO: add future network upgrades + // TODO: add future network upgrades ] .boxed() } @@ -347,7 +347,10 @@ impl Arbitrary for Transaction { type Strategy = BoxedStrategy; } -/// Transaction utility tests functions +// Utility functions + +/// The network upgrade for any fake transactions we will create. +const FAKE_NETWORK_UPGRADE: NetworkUpgrade = NetworkUpgrade::Nu5; /// Convert `trans` into a fake v5 transaction, /// converting sapling shielded data from v4 to v5 if possible. @@ -360,6 +363,7 @@ pub fn transaction_to_fake_v5(trans: &Transaction) -> Transaction { outputs, lock_time, } => V5 { + network_upgrade: FAKE_NETWORK_UPGRADE, inputs: inputs.to_vec(), outputs: outputs.to_vec(), lock_time: *lock_time, @@ -372,6 +376,7 @@ pub fn transaction_to_fake_v5(trans: &Transaction) -> Transaction { lock_time, .. } => V5 { + network_upgrade: FAKE_NETWORK_UPGRADE, inputs: inputs.to_vec(), outputs: outputs.to_vec(), lock_time: *lock_time, @@ -385,6 +390,7 @@ pub fn transaction_to_fake_v5(trans: &Transaction) -> Transaction { expiry_height, .. } => V5 { + network_upgrade: FAKE_NETWORK_UPGRADE, inputs: inputs.to_vec(), outputs: outputs.to_vec(), lock_time: *lock_time, @@ -399,6 +405,7 @@ pub fn transaction_to_fake_v5(trans: &Transaction) -> Transaction { sapling_shielded_data, .. } => V5 { + network_upgrade: FAKE_NETWORK_UPGRADE, inputs: inputs.to_vec(), outputs: outputs.to_vec(), lock_time: *lock_time, diff --git a/zebra-chain/src/transaction/tests/vectors.rs b/zebra-chain/src/transaction/tests/vectors.rs index c7444fd3026..14f414b88c2 100644 --- a/zebra-chain/src/transaction/tests/vectors.rs +++ b/zebra-chain/src/transaction/tests/vectors.rs @@ -2,8 +2,6 @@ use super::super::*; use crate::{ block::{Block, MAX_BLOCK_BYTES}, - parameters::NetworkUpgrade::Nu5, - sapling::{PerSpendAnchor, SharedAnchor}, serialization::{ZcashDeserialize, ZcashDeserializeInto, ZcashSerialize}, }; @@ -104,7 +102,7 @@ fn empty_v5_round_trip() { zebra_test::init(); let tx = Transaction::V5 { - network_upgrade: NETWORK_UPGRADE, + network_upgrade: NetworkUpgrade::Nu5, lock_time: LockTime::min_lock_time(), expiry_height: block::Height(0), inputs: Vec::new(), @@ -264,138 +262,3 @@ fn fake_v5_round_trip() { ); } } - -// Utility functions - -/// The network upgrade for any fake transactions we will create. -const NETWORK_UPGRADE: NetworkUpgrade = Nu5; - -/// Convert `trans` into a fake v5 transaction, -/// converting sapling shielded data from v4 to v5 if possible. -fn transaction_to_fake_v5(trans: &Transaction) -> Transaction { - use Transaction::*; - - match trans { - V1 { - inputs, - outputs, - lock_time, - } => V5 { - network_upgrade: NETWORK_UPGRADE, - inputs: inputs.to_vec(), - outputs: outputs.to_vec(), - lock_time: *lock_time, - expiry_height: block::Height(0), - sapling_shielded_data: None, - }, - V2 { - inputs, - outputs, - lock_time, - .. - } => V5 { - network_upgrade: NETWORK_UPGRADE, - inputs: inputs.to_vec(), - outputs: outputs.to_vec(), - lock_time: *lock_time, - expiry_height: block::Height(0), - sapling_shielded_data: None, - }, - V3 { - inputs, - outputs, - lock_time, - expiry_height, - .. - } => V5 { - network_upgrade: NETWORK_UPGRADE, - inputs: inputs.to_vec(), - outputs: outputs.to_vec(), - lock_time: *lock_time, - expiry_height: *expiry_height, - sapling_shielded_data: None, - }, - V4 { - inputs, - outputs, - lock_time, - expiry_height, - sapling_shielded_data, - .. - } => V5 { - network_upgrade: NETWORK_UPGRADE, - inputs: inputs.to_vec(), - outputs: outputs.to_vec(), - lock_time: *lock_time, - expiry_height: *expiry_height, - sapling_shielded_data: sapling_shielded_data - .clone() - .map(sapling_shielded_v4_to_fake_v5) - .flatten(), - }, - v5 @ V5 { .. } => v5.clone(), - } -} - -/// Convert a v4 sapling shielded data into a fake v5 sapling shielded data, -/// if possible. -fn sapling_shielded_v4_to_fake_v5( - v4_shielded: sapling::ShieldedData, -) -> Option> { - use sapling::ShieldedData; - use sapling::TransferData::*; - - let unique_anchors: Vec<_> = v4_shielded - .spends() - .map(|spend| spend.per_spend_anchor) - .unique() - .collect(); - - let fake_spends: Vec<_> = v4_shielded - .spends() - .cloned() - .map(sapling_spend_v4_to_fake_v5) - .collect(); - - let transfers = match v4_shielded.transfers { - SpendsAndMaybeOutputs { maybe_outputs, .. } => { - let shared_anchor = match unique_anchors.as_slice() { - [unique_anchor] => *unique_anchor, - // Multiple different anchors, can't convert to v5 - _ => return None, - }; - - SpendsAndMaybeOutputs { - shared_anchor, - spends: fake_spends.try_into().unwrap(), - maybe_outputs, - } - } - JustOutputs { outputs } => JustOutputs { outputs }, - }; - - let fake_shielded_v5 = ShieldedData:: { - value_balance: v4_shielded.value_balance, - transfers, - binding_sig: v4_shielded.binding_sig, - }; - - Some(fake_shielded_v5) -} - -/// Convert a v4 sapling spend into a fake v5 sapling spend. -fn sapling_spend_v4_to_fake_v5( - v4_spend: sapling::Spend, -) -> sapling::Spend { - use sapling::Spend; - - Spend:: { - cv: v4_spend.cv, - per_spend_anchor: FieldNotPresent, - nullifier: v4_spend.nullifier, - rk: v4_spend.rk, - zkproof: v4_spend.zkproof, - spend_auth_sig: v4_spend.spend_auth_sig, - } -} -