Skip to content

Commit

Permalink
Add test case to ensure spend adding fails when spends are disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
dmidem committed Jul 3, 2024
1 parent aa46d52 commit 1bffe59
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ impl BundleType {
pub fn flags(&self) -> Flags {
match self {
BundleType::Transactional { flags, .. } => *flags,
BundleType::Coinbase => Flags::SPENDS_DISABLED,
// FIXME: is it correct to use SPENDS_DISABLED_WITHOUT_ZSA (i.e does coinbase always have
// ZSA disabled)?
BundleType::Coinbase => Flags::SPENDS_DISABLED_WITHOUT_ZSA,
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,20 @@ impl Flags {
zsa_enabled: true,
};

/// The flag set with spends disabled.
pub const SPENDS_DISABLED: Flags = Flags {
/// The flag set with spends and ZSA disabled.
pub const SPENDS_DISABLED_WITHOUT_ZSA: Flags = Flags {
spends_enabled: false,
outputs_enabled: true,
zsa_enabled: false,
};

/// The flag set with spends disabled and ZSA enabled.
pub const SPENDS_DISABLED_WITH_ZSA: Flags = Flags {
spends_enabled: false,
outputs_enabled: true,
zsa_enabled: true,
};

/// The flag set with outputs disabled.
pub const OUTPUTS_DISABLED: Flags = Flags {
spends_enabled: true,
Expand Down
37 changes: 32 additions & 5 deletions tests/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ fn bundle_chain<FL: OrchardFlavor>() {

let mut builder = Builder::new(
BundleType::Transactional {
flags: Flags::SPENDS_DISABLED,
// FIXME: Should we use SPENDS_DISABLED_WITH_ZSA for FL=OrchardZSA case?
// The original zsa1 code used the following:
// let mut builder = Builder::new(Flags::from_parts(false, true, false), anchor);
flags: Flags::SPENDS_DISABLED_WITHOUT_ZSA,
bundle_required: false,
},
anchor,
Expand Down Expand Up @@ -100,20 +103,44 @@ fn bundle_chain<FL: OrchardFlavor>() {
// Verify the shielding bundle.
verify_bundle(&shielding_bundle, &vk, true);

// Create a shielded bundle spending the previous output.
let shielded_bundle: Bundle<_, i64, FL> = {
let note = {
let ivk = PreparedIncomingViewingKey::new(&fvk.to_ivk(Scope::External));
let (note, _, _) = shielding_bundle
shielding_bundle
.actions()
.iter()
.find_map(|action| {
let domain = OrchardDomain::for_action(action);
try_note_decryption(&domain, &ivk, action)
})
.unwrap();
.unwrap()
.0
};

// Test that spend adding attempt fails when spends are disabled.
// Note: We do not need a separate positive test for spends enabled
// as the following code adds spends with spends enabled.
{
let (merkle_path, anchor) = build_merkle_path(&note);

let mut builder = Builder::new(
BundleType::Transactional {
// FIXME: Should we use SPENDS_DISABLED_WITH_ZSA for FL=OrchardZSA case?
flags: Flags::SPENDS_DISABLED_WITHOUT_ZSA,
bundle_required: false,
},
anchor,
);

assert!(builder.add_spend(fvk.clone(), note, merkle_path).is_err());
}

// Create a shielded bundle spending the previous output.
let shielded_bundle: Bundle<_, i64, FL> = {
let (merkle_path, anchor) = build_merkle_path(&note);

// FIXME: Should we use DEFAULT_ZSA for FL=OrchardZSA case?
// The original zsa1 code used the following:
// let mut builder = Builder::new(Flags::from_parts(true, true, false), anchor);
let mut builder = Builder::new(BundleType::DEFAULT_VANILLA, anchor);
assert_eq!(builder.add_spend(fvk, note, merkle_path), Ok(()));
assert_eq!(
Expand Down

0 comments on commit 1bffe59

Please sign in to comment.