Also tweak a constant name, an import, and a comment.
This commit is contained in:
parent
9fc2388fbc
commit
b52fbae30f
|
@ -143,7 +143,7 @@ impl Transaction {
|
||||||
Just(NetworkUpgrade::Heartwood),
|
Just(NetworkUpgrade::Heartwood),
|
||||||
Just(NetworkUpgrade::Canopy),
|
Just(NetworkUpgrade::Canopy),
|
||||||
Just(NetworkUpgrade::Nu5),
|
Just(NetworkUpgrade::Nu5),
|
||||||
// TODO: add future network upgrades
|
// TODO: add future network upgrades
|
||||||
]
|
]
|
||||||
.boxed()
|
.boxed()
|
||||||
}
|
}
|
||||||
|
@ -347,7 +347,10 @@ impl Arbitrary for Transaction {
|
||||||
type Strategy = BoxedStrategy<Self>;
|
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,
|
/// Convert `trans` into a fake v5 transaction,
|
||||||
/// converting sapling shielded data from v4 to v5 if possible.
|
/// converting sapling shielded data from v4 to v5 if possible.
|
||||||
|
@ -360,6 +363,7 @@ pub fn transaction_to_fake_v5(trans: &Transaction) -> Transaction {
|
||||||
outputs,
|
outputs,
|
||||||
lock_time,
|
lock_time,
|
||||||
} => V5 {
|
} => V5 {
|
||||||
|
network_upgrade: FAKE_NETWORK_UPGRADE,
|
||||||
inputs: inputs.to_vec(),
|
inputs: inputs.to_vec(),
|
||||||
outputs: outputs.to_vec(),
|
outputs: outputs.to_vec(),
|
||||||
lock_time: *lock_time,
|
lock_time: *lock_time,
|
||||||
|
@ -372,6 +376,7 @@ pub fn transaction_to_fake_v5(trans: &Transaction) -> Transaction {
|
||||||
lock_time,
|
lock_time,
|
||||||
..
|
..
|
||||||
} => V5 {
|
} => V5 {
|
||||||
|
network_upgrade: FAKE_NETWORK_UPGRADE,
|
||||||
inputs: inputs.to_vec(),
|
inputs: inputs.to_vec(),
|
||||||
outputs: outputs.to_vec(),
|
outputs: outputs.to_vec(),
|
||||||
lock_time: *lock_time,
|
lock_time: *lock_time,
|
||||||
|
@ -385,6 +390,7 @@ pub fn transaction_to_fake_v5(trans: &Transaction) -> Transaction {
|
||||||
expiry_height,
|
expiry_height,
|
||||||
..
|
..
|
||||||
} => V5 {
|
} => V5 {
|
||||||
|
network_upgrade: FAKE_NETWORK_UPGRADE,
|
||||||
inputs: inputs.to_vec(),
|
inputs: inputs.to_vec(),
|
||||||
outputs: outputs.to_vec(),
|
outputs: outputs.to_vec(),
|
||||||
lock_time: *lock_time,
|
lock_time: *lock_time,
|
||||||
|
@ -399,6 +405,7 @@ pub fn transaction_to_fake_v5(trans: &Transaction) -> Transaction {
|
||||||
sapling_shielded_data,
|
sapling_shielded_data,
|
||||||
..
|
..
|
||||||
} => V5 {
|
} => V5 {
|
||||||
|
network_upgrade: FAKE_NETWORK_UPGRADE,
|
||||||
inputs: inputs.to_vec(),
|
inputs: inputs.to_vec(),
|
||||||
outputs: outputs.to_vec(),
|
outputs: outputs.to_vec(),
|
||||||
lock_time: *lock_time,
|
lock_time: *lock_time,
|
||||||
|
|
|
@ -2,8 +2,6 @@ use super::super::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
block::{Block, MAX_BLOCK_BYTES},
|
block::{Block, MAX_BLOCK_BYTES},
|
||||||
parameters::NetworkUpgrade::Nu5,
|
|
||||||
sapling::{PerSpendAnchor, SharedAnchor},
|
|
||||||
serialization::{ZcashDeserialize, ZcashDeserializeInto, ZcashSerialize},
|
serialization::{ZcashDeserialize, ZcashDeserializeInto, ZcashSerialize},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -104,7 +102,7 @@ fn empty_v5_round_trip() {
|
||||||
zebra_test::init();
|
zebra_test::init();
|
||||||
|
|
||||||
let tx = Transaction::V5 {
|
let tx = Transaction::V5 {
|
||||||
network_upgrade: NETWORK_UPGRADE,
|
network_upgrade: NetworkUpgrade::Nu5,
|
||||||
lock_time: LockTime::min_lock_time(),
|
lock_time: LockTime::min_lock_time(),
|
||||||
expiry_height: block::Height(0),
|
expiry_height: block::Height(0),
|
||||||
inputs: Vec::new(),
|
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<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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue