Skip to content

Commit dab5fd9

Browse files
committed
Refactor insert_fake_orchard_shielded_data into separate V5 and V6 helpers
1 parent 40ba2bd commit dab5fd9

File tree

3 files changed

+63
-32
lines changed

3 files changed

+63
-32
lines changed

zebra-chain/src/orchard/shielded_data_flavor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub trait ShieldedDataFlavor: OrchardFlavor {
5252
/// A type representing a burn field for this protocol version.
5353
#[cfg(feature = "tx_v6")]
5454
type BurnType: Clone
55+
+ Default
5556
+ Debug
5657
+ ZcashDeserialize
5758
+ ZcashSerialize

zebra-chain/src/transaction/arbitrary.rs

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,22 +1140,13 @@ pub fn transactions_from_blocks<'a>(
11401140
})
11411141
}
11421142

1143-
// FIXME: make it a generic to support V6?
1144-
/// Modify a V5 transaction to insert fake Orchard shielded data.
1145-
///
11461143
/// Creates a fake instance of [`orchard::ShieldedData`] with one fake action. Note that both the
11471144
/// action and the shielded data are invalid and shouldn't be used in tests that require them to be
11481145
/// valid.
1149-
///
1150-
/// A mutable reference to the inserted shielded data is returned, so that the caller can further
1151-
/// customize it if required.
1152-
///
1153-
/// # Panics
1154-
///
1155-
/// Panics if the transaction to be modified is not V5.
1156-
pub fn insert_fake_orchard_shielded_data(
1157-
transaction: &mut Transaction,
1158-
) -> &mut orchard::ShieldedData<orchard::OrchardVanilla> {
1146+
pub fn create_fake_orchard_shielded_data<Flavor: orchard::ShieldedDataFlavor + 'static>(
1147+
) -> orchard::ShieldedData<Flavor>
1148+
//where <<Flavor as orchard::ShieldedDataFlavor>::EncryptedNote as Arbitrary>::Strategy: 'static
1149+
{
11591150
// Create a dummy action
11601151
let mut runner = TestRunner::default();
11611152
let dummy_action = orchard::Action::arbitrary()
@@ -1170,7 +1161,7 @@ pub fn insert_fake_orchard_shielded_data(
11701161
};
11711162

11721163
// Place the dummy action inside the Orchard shielded data
1173-
let dummy_shielded_data = orchard::ShieldedData::<orchard::OrchardVanilla> {
1164+
orchard::ShieldedData::<Flavor> {
11741165
flags: orchard::Flags::empty(),
11751166
value_balance: Amount::try_from(0).expect("invalid transaction amount"),
11761167
shared_anchor: orchard::tree::Root::default(),
@@ -1179,15 +1170,27 @@ pub fn insert_fake_orchard_shielded_data(
11791170
binding_sig: Signature::from([0u8; 64]),
11801171
#[cfg(feature = "tx_v6")]
11811172
burn: Default::default(),
1182-
};
1173+
}
1174+
}
11831175

1176+
/// Modify a V5 transaction to insert fake Orchard shielded data.
1177+
///
1178+
/// A mutable reference to the inserted shielded data is returned, so that the caller can further
1179+
/// customize it if required.
1180+
///
1181+
/// # Panics
1182+
///
1183+
/// Panics if the transaction to be modified is not V5.
1184+
pub fn insert_fake_v5_orchard_shielded_data(
1185+
transaction: &mut Transaction,
1186+
) -> &mut orchard::ShieldedData<orchard::OrchardVanilla> {
11841187
// Replace the shielded data in the transaction
11851188
match transaction {
11861189
Transaction::V5 {
11871190
orchard_shielded_data,
11881191
..
11891192
} => {
1190-
*orchard_shielded_data = Some(dummy_shielded_data);
1193+
*orchard_shielded_data = Some(create_fake_orchard_shielded_data());
11911194

11921195
orchard_shielded_data
11931196
.as_mut()
@@ -1196,3 +1199,30 @@ pub fn insert_fake_orchard_shielded_data(
11961199
_ => panic!("Fake V5 transaction is not V5"),
11971200
}
11981201
}
1202+
1203+
/// Modify a V6 transaction to insert fake Orchard shielded data.
1204+
///
1205+
/// A mutable reference to the inserted shielded data is returned, so that the caller can further
1206+
/// customize it if required.
1207+
///
1208+
/// # Panics
1209+
///
1210+
/// Panics if the transaction to be modified is not V6.
1211+
pub fn insert_fake_v6_orchard_shielded_data(
1212+
transaction: &mut Transaction,
1213+
) -> &mut orchard::ShieldedData<orchard::OrchardZSA> {
1214+
// Replace the shielded data in the transaction
1215+
match transaction {
1216+
Transaction::V6 {
1217+
orchard_shielded_data,
1218+
..
1219+
} => {
1220+
*orchard_shielded_data = Some(create_fake_orchard_shielded_data());
1221+
1222+
orchard_shielded_data
1223+
.as_mut()
1224+
.expect("shielded data was just inserted")
1225+
}
1226+
_ => panic!("Fake V6 transaction is not V6"),
1227+
}
1228+
}

zebra-consensus/src/transaction/tests.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use zebra_chain::{
2020
sprout,
2121
transaction::{
2222
arbitrary::{
23-
fake_v5_transactions_for_network, insert_fake_orchard_shielded_data, test_transactions,
24-
transactions_from_blocks,
23+
fake_v5_transactions_for_network, insert_fake_v5_orchard_shielded_data,
24+
test_transactions, transactions_from_blocks,
2525
},
2626
zip317, Hash, HashType, JoinSplitData, LockTime, Transaction,
2727
},
@@ -77,7 +77,7 @@ fn fake_v5_transaction_with_orchard_actions_has_inputs_and_outputs() {
7777

7878
// Insert fake Orchard shielded data to the transaction, which has at least one action (this is
7979
// guaranteed structurally by `orchard::ShieldedData`)
80-
insert_fake_orchard_shielded_data(&mut transaction);
80+
insert_fake_v5_orchard_shielded_data(&mut transaction);
8181

8282
// The check will fail if the transaction has no flags
8383
assert_eq!(
@@ -86,8 +86,8 @@ fn fake_v5_transaction_with_orchard_actions_has_inputs_and_outputs() {
8686
);
8787

8888
// If we add ENABLE_SPENDS flag it will pass the inputs check but fails with the outputs
89-
// TODO: Avoid new calls to `insert_fake_orchard_shielded_data` for each check #2409.
90-
let shielded_data = insert_fake_orchard_shielded_data(&mut transaction);
89+
// TODO: Avoid new calls to `insert_fake_v5_orchard_shielded_data` for each check #2409.
90+
let shielded_data = insert_fake_v5_orchard_shielded_data(&mut transaction);
9191
shielded_data.flags = zebra_chain::orchard::Flags::ENABLE_SPENDS;
9292

9393
assert_eq!(
@@ -96,7 +96,7 @@ fn fake_v5_transaction_with_orchard_actions_has_inputs_and_outputs() {
9696
);
9797

9898
// If we add ENABLE_OUTPUTS flag it will pass the outputs check but fails with the inputs
99-
let shielded_data = insert_fake_orchard_shielded_data(&mut transaction);
99+
let shielded_data = insert_fake_v5_orchard_shielded_data(&mut transaction);
100100
shielded_data.flags = zebra_chain::orchard::Flags::ENABLE_OUTPUTS;
101101

102102
assert_eq!(
@@ -105,7 +105,7 @@ fn fake_v5_transaction_with_orchard_actions_has_inputs_and_outputs() {
105105
);
106106

107107
// Finally make it valid by adding both required flags
108-
let shielded_data = insert_fake_orchard_shielded_data(&mut transaction);
108+
let shielded_data = insert_fake_v5_orchard_shielded_data(&mut transaction);
109109
shielded_data.flags =
110110
zebra_chain::orchard::Flags::ENABLE_SPENDS | zebra_chain::orchard::Flags::ENABLE_OUTPUTS;
111111

@@ -131,7 +131,7 @@ fn fake_v5_transaction_with_orchard_actions_has_flags() {
131131

132132
// Insert fake Orchard shielded data to the transaction, which has at least one action (this is
133133
// guaranteed structurally by `orchard::ShieldedData`)
134-
insert_fake_orchard_shielded_data(&mut transaction);
134+
insert_fake_v5_orchard_shielded_data(&mut transaction);
135135

136136
// The check will fail if the transaction has no flags
137137
assert_eq!(
@@ -140,17 +140,17 @@ fn fake_v5_transaction_with_orchard_actions_has_flags() {
140140
);
141141

142142
// If we add ENABLE_SPENDS flag it will pass.
143-
let shielded_data = insert_fake_orchard_shielded_data(&mut transaction);
143+
let shielded_data = insert_fake_v5_orchard_shielded_data(&mut transaction);
144144
shielded_data.flags = zebra_chain::orchard::Flags::ENABLE_SPENDS;
145145
assert!(check::has_enough_orchard_flags(&transaction).is_ok());
146146

147147
// If we add ENABLE_OUTPUTS flag instead, it will pass.
148-
let shielded_data = insert_fake_orchard_shielded_data(&mut transaction);
148+
let shielded_data = insert_fake_v5_orchard_shielded_data(&mut transaction);
149149
shielded_data.flags = zebra_chain::orchard::Flags::ENABLE_OUTPUTS;
150150
assert!(check::has_enough_orchard_flags(&transaction).is_ok());
151151

152152
// If we add BOTH ENABLE_SPENDS and ENABLE_OUTPUTS flags it will pass.
153-
let shielded_data = insert_fake_orchard_shielded_data(&mut transaction);
153+
let shielded_data = insert_fake_v5_orchard_shielded_data(&mut transaction);
154154
shielded_data.flags =
155155
zebra_chain::orchard::Flags::ENABLE_SPENDS | zebra_chain::orchard::Flags::ENABLE_OUTPUTS;
156156
assert!(check::has_enough_orchard_flags(&transaction).is_ok());
@@ -823,7 +823,7 @@ fn v5_coinbase_transaction_without_enable_spends_flag_passes_validation() {
823823
.find(|transaction| transaction.is_coinbase())
824824
.expect("At least one fake V5 coinbase transaction in the test vectors");
825825

826-
insert_fake_orchard_shielded_data(&mut transaction);
826+
insert_fake_v5_orchard_shielded_data(&mut transaction);
827827

828828
assert!(check::coinbase_tx_no_prevout_joinsplit_spend(&transaction).is_ok());
829829
}
@@ -838,7 +838,7 @@ fn v5_coinbase_transaction_with_enable_spends_flag_fails_validation() {
838838
.find(|transaction| transaction.is_coinbase())
839839
.expect("At least one fake V5 coinbase transaction in the test vectors");
840840

841-
let shielded_data = insert_fake_orchard_shielded_data(&mut transaction);
841+
let shielded_data = insert_fake_v5_orchard_shielded_data(&mut transaction);
842842

843843
shielded_data.flags = zebra_chain::orchard::Flags::ENABLE_SPENDS;
844844

@@ -2415,7 +2415,7 @@ fn v5_with_duplicate_orchard_action() {
24152415

24162416
// Insert fake Orchard shielded data to the transaction, which has at least one action (this is
24172417
// guaranteed structurally by `orchard::ShieldedData`)
2418-
let shielded_data = insert_fake_orchard_shielded_data(&mut transaction);
2418+
let shielded_data = insert_fake_v5_orchard_shielded_data(&mut transaction);
24192419

24202420
// Enable spends
24212421
shielded_data.flags = zebra_chain::orchard::Flags::ENABLE_SPENDS
@@ -2865,7 +2865,7 @@ fn coinbase_outputs_are_decryptable_for_fake_v5_blocks() {
28652865
})
28662866
.expect("At least one fake V5 transaction with no inputs and no outputs");
28672867

2868-
let shielded_data = insert_fake_orchard_shielded_data(&mut transaction);
2868+
let shielded_data = insert_fake_v5_orchard_shielded_data(&mut transaction);
28692869
shielded_data.flags = zebra_chain::orchard::Flags::ENABLE_SPENDS
28702870
| zebra_chain::orchard::Flags::ENABLE_OUTPUTS;
28712871

@@ -2907,7 +2907,7 @@ fn shielded_outputs_are_not_decryptable_for_fake_v5_blocks() {
29072907
})
29082908
.expect("At least one fake V5 transaction with no inputs and no outputs");
29092909

2910-
let shielded_data = insert_fake_orchard_shielded_data(&mut transaction);
2910+
let shielded_data = insert_fake_v5_orchard_shielded_data(&mut transaction);
29112911
shielded_data.flags = zebra_chain::orchard::Flags::ENABLE_SPENDS
29122912
| zebra_chain::orchard::Flags::ENABLE_OUTPUTS;
29132913

0 commit comments

Comments
 (0)