Skip to content

Commit 402cd87

Browse files
committed
Refactor insert_fake_orchard_shielded_data into separate V5 and V6 helpers
1 parent 63c5d2b commit 402cd87

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

zebra-chain/src/orchard/shielded_data_flavor.rs

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

zebra-chain/src/transaction/arbitrary.rs

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,22 +1162,13 @@ pub fn transactions_from_blocks<'a>(
11621162
})
11631163
}
11641164

1165-
// FIXME: make it a generic to support V6?
1166-
/// Modify a V5 transaction to insert fake Orchard shielded data.
1167-
///
11681165
/// Creates a fake instance of [`orchard::ShieldedData`] with one fake action. Note that both the
11691166
/// action and the shielded data are invalid and shouldn't be used in tests that require them to be
11701167
/// valid.
1171-
///
1172-
/// A mutable reference to the inserted shielded data is returned, so that the caller can further
1173-
/// customize it if required.
1174-
///
1175-
/// # Panics
1176-
///
1177-
/// Panics if the transaction to be modified is not V5.
1178-
pub fn insert_fake_orchard_shielded_data(
1179-
transaction: &mut Transaction,
1180-
) -> &mut orchard::ShieldedData<orchard::OrchardVanilla> {
1168+
pub fn create_fake_orchard_shielded_data<Flavor: orchard::ShieldedDataFlavor + 'static>(
1169+
) -> orchard::ShieldedData<Flavor>
1170+
//where <<Flavor as orchard::ShieldedDataFlavor>::EncryptedNote as Arbitrary>::Strategy: 'static
1171+
{
11811172
// Create a dummy action
11821173
let mut runner = TestRunner::default();
11831174
let dummy_action = orchard::Action::arbitrary()
@@ -1192,7 +1183,7 @@ pub fn insert_fake_orchard_shielded_data(
11921183
};
11931184

11941185
// Place the dummy action inside the Orchard shielded data
1195-
let dummy_shielded_data = orchard::ShieldedData::<orchard::OrchardVanilla> {
1186+
orchard::ShieldedData::<Flavor> {
11961187
flags: orchard::Flags::empty(),
11971188
value_balance: Amount::try_from(0).expect("invalid transaction amount"),
11981189
shared_anchor: orchard::tree::Root::default(),
@@ -1201,15 +1192,27 @@ pub fn insert_fake_orchard_shielded_data(
12011192
binding_sig: Signature::from([0u8; 64]),
12021193
#[cfg(all(zcash_unstable = "nu7", feature = "tx_v6"))]
12031194
burn: Default::default(),
1204-
};
1195+
}
1196+
}
12051197

1198+
/// Modify a V5 transaction to insert fake Orchard shielded data.
1199+
///
1200+
/// A mutable reference to the inserted shielded data is returned, so that the caller can further
1201+
/// customize it if required.
1202+
///
1203+
/// # Panics
1204+
///
1205+
/// Panics if the transaction to be modified is not V5.
1206+
pub fn insert_fake_v5_orchard_shielded_data(
1207+
transaction: &mut Transaction,
1208+
) -> &mut orchard::ShieldedData<orchard::OrchardVanilla> {
12061209
// Replace the shielded data in the transaction
12071210
match transaction {
12081211
Transaction::V5 {
12091212
orchard_shielded_data,
12101213
..
12111214
} => {
1212-
*orchard_shielded_data = Some(dummy_shielded_data);
1215+
*orchard_shielded_data = Some(create_fake_orchard_shielded_data());
12131216

12141217
orchard_shielded_data
12151218
.as_mut()
@@ -1218,3 +1221,31 @@ pub fn insert_fake_orchard_shielded_data(
12181221
_ => panic!("Fake V5 transaction is not V5"),
12191222
}
12201223
}
1224+
1225+
/// Modify a V6 transaction to insert fake Orchard shielded data.
1226+
///
1227+
/// A mutable reference to the inserted shielded data is returned, so that the caller can further
1228+
/// customize it if required.
1229+
///
1230+
/// # Panics
1231+
///
1232+
/// Panics if the transaction to be modified is not V6.
1233+
#[cfg(all(zcash_unstable = "nu7", feature = "tx_v6"))]
1234+
pub fn insert_fake_v6_orchard_shielded_data(
1235+
transaction: &mut Transaction,
1236+
) -> &mut orchard::ShieldedData<orchard::OrchardZSA> {
1237+
// Replace the shielded data in the transaction
1238+
match transaction {
1239+
Transaction::V6 {
1240+
orchard_shielded_data,
1241+
..
1242+
} => {
1243+
*orchard_shielded_data = Some(create_fake_orchard_shielded_data());
1244+
1245+
orchard_shielded_data
1246+
.as_mut()
1247+
.expect("shielded data was just inserted")
1248+
}
1249+
_ => panic!("Fake V6 transaction is not V6"),
1250+
}
1251+
}

zebra-consensus/src/transaction/tests.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ use zebra_chain::{
2828
sprout,
2929
transaction::{
3030
arbitrary::{
31-
insert_fake_orchard_shielded_data, test_transactions, transactions_from_blocks,
31+
insert_fake_v5_orchard_shielded_data, test_transactions, transactions_from_blocks,
3232
v5_transactions,
3333
},
3434
zip317, Hash, HashType, JoinSplitData, LockTime, Transaction,
3535
},
3636
transparent::{self, CoinbaseData, CoinbaseSpendRestriction},
3737
};
3838

39+
#[cfg(all(zcash_unstable = "nu7", feature = "tx_v6"))]
40+
use zebra_chain::transaction::arbitrary::insert_fake_v6_orchard_shielded_data;
41+
3942
use zebra_node_services::mempool;
4043
use zebra_state::ValidateContextError;
4144
use zebra_test::mock_service::MockService;
@@ -1218,7 +1221,7 @@ fn v5_coinbase_transaction_without_enable_spends_flag_passes_validation() {
12181221
.find(|transaction| transaction.is_coinbase())
12191222
.expect("V5 coinbase tx");
12201223

1221-
let shielded_data = insert_fake_orchard_shielded_data(&mut tx);
1224+
let shielded_data = insert_fake_v5_orchard_shielded_data(&mut tx);
12221225

12231226
assert!(!shielded_data.flags.contains(Flags::ENABLE_SPENDS));
12241227

@@ -1233,7 +1236,7 @@ fn v5_coinbase_transaction_with_enable_spends_flag_fails_validation() {
12331236
.find(|transaction| transaction.is_coinbase())
12341237
.expect("V5 coinbase tx");
12351238

1236-
let shielded_data = insert_fake_orchard_shielded_data(&mut tx);
1239+
let shielded_data = insert_fake_v5_orchard_shielded_data(&mut tx);
12371240

12381241
assert!(!shielded_data.flags.contains(Flags::ENABLE_SPENDS));
12391242

@@ -3461,7 +3464,7 @@ fn coinbase_outputs_are_decryptable_for_fake_v5_blocks() {
34613464
.find(|tx| tx.is_coinbase())
34623465
.expect("coinbase V5 tx");
34633466

3464-
let shielded_data = insert_fake_orchard_shielded_data(&mut transaction);
3467+
let shielded_data = insert_fake_v5_orchard_shielded_data(&mut transaction);
34653468
shielded_data.flags = Flags::ENABLE_OUTPUTS;
34663469

34673470
let action = fill_action_with_note_encryption_test_vector(
@@ -3494,7 +3497,7 @@ fn shielded_outputs_are_not_decryptable_for_fake_v5_blocks() {
34943497
.find(|tx| tx.is_coinbase())
34953498
.expect("V5 coinbase tx");
34963499

3497-
let shielded_data = insert_fake_orchard_shielded_data(&mut tx);
3500+
let shielded_data = insert_fake_v5_orchard_shielded_data(&mut tx);
34983501
shielded_data.flags = Flags::ENABLE_OUTPUTS;
34993502

35003503
let action = fill_action_with_note_encryption_test_vector(

0 commit comments

Comments
 (0)