Skip to content

Commit

Permalink
Fix a bad merge in ZcashFoundation#2075
Browse files Browse the repository at this point in the history
Also tweak a constant name, an import, and a comment.
  • Loading branch information
teor2345 committed Apr 29, 2021
1 parent 9fc2388 commit a66ed69
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 140 deletions.
11 changes: 9 additions & 2 deletions zebra-chain/src/transaction/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -347,7 +347,10 @@ impl Arbitrary for Transaction {
type Strategy = BoxedStrategy<Self>;
}

/// 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.
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
139 changes: 1 addition & 138 deletions zebra-chain/src/transaction/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use super::super::*;

use crate::{
block::{Block, MAX_BLOCK_BYTES},
parameters::NetworkUpgrade::Nu5,
sapling::{PerSpendAnchor, SharedAnchor},
serialization::{ZcashDeserialize, ZcashDeserializeInto, ZcashSerialize},
};

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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<PerSpendAnchor>,
) -> Option<sapling::ShieldedData<SharedAnchor>> {
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::<SharedAnchor> {
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<PerSpendAnchor>,
) -> sapling::Spend<SharedAnchor> {
use sapling::Spend;

Spend::<SharedAnchor> {
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,
}
}

0 comments on commit a66ed69

Please sign in to comment.