Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 49 additions & 18 deletions src/bundle/burn_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,43 +67,50 @@ impl fmt::Display for BurnError {
mod tests {
use crate::issuance::compute_asset_desc_hash;
use crate::value::NoteValue;
use nonempty::NonEmpty;

use super::*;

/// Creates an item of bundle burn list for a given asset description and value.
/// Creates an item of bundle burn list for a given asset description hash and value.
///
/// This function is deterministic and guarantees that each call with the same parameters
/// will return the same result. It achieves determinism by using a static `IssuanceAuthorizingKey`.
///
/// # Arguments
///
/// * `asset_desc` - The asset description string.
/// * `asset_desc_hash` - The asset description hash.
/// * `value` - The value for the burn.
///
/// # Returns
///
/// A tuple `(AssetBase, Amount)` representing the burn list item.
///
pub fn get_burn_tuple(asset_desc: &[u8], value: u64) -> (AssetBase, NoteValue) {
fn get_burn_tuple(asset_desc_hash: &[u8; 32], value: u64) -> (AssetBase, NoteValue) {
use crate::keys::{IssuanceAuthorizingKey, IssuanceValidatingKey};

let isk = IssuanceAuthorizingKey::from_bytes([1u8; 32]).unwrap();

(
AssetBase::derive(
&IssuanceValidatingKey::from(&isk),
&compute_asset_desc_hash(asset_desc).unwrap(),
),
AssetBase::derive(&IssuanceValidatingKey::from(&isk), asset_desc_hash),
NoteValue::from_raw(value),
)
}

#[test]
fn validate_bundle_burn_success() {
let bundle_burn = vec![
get_burn_tuple(b"Asset 1", 10),
get_burn_tuple(b"Asset 2", 20),
get_burn_tuple(b"Asset 3", 10),
get_burn_tuple(
&compute_asset_desc_hash(&NonEmpty::from_slice(b"Asset 1").unwrap()),
10,
),
get_burn_tuple(
&compute_asset_desc_hash(&NonEmpty::from_slice(b"Asset 2").unwrap()),
20,
),
get_burn_tuple(
&compute_asset_desc_hash(&NonEmpty::from_slice(b"Asset 3").unwrap()),
10,
),
];

let result = validate_bundle_burn(&bundle_burn);
Expand All @@ -114,9 +121,18 @@ mod tests {
#[test]
fn validate_bundle_burn_duplicate_asset() {
let bundle_burn = vec![
get_burn_tuple(b"Asset 1", 10),
get_burn_tuple(b"Asset 1", 20),
get_burn_tuple(b"Asset 3", 10),
get_burn_tuple(
&compute_asset_desc_hash(&NonEmpty::from_slice(b"Asset 1").unwrap()),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a From<> trait for NonEmpty in our code so we can do b"Asset 1".into() ?

Copy link
Collaborator Author

@ConstanceBeguier ConstanceBeguier May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not possible because neither From nor NonEmpty are defined in the current crate.

10,
),
get_burn_tuple(
&compute_asset_desc_hash(&NonEmpty::from_slice(b"Asset 1").unwrap()),
20,
),
get_burn_tuple(
&compute_asset_desc_hash(&NonEmpty::from_slice(b"Asset 3").unwrap()),
10,
),
];

let result = validate_bundle_burn(&bundle_burn);
Expand All @@ -127,9 +143,15 @@ mod tests {
#[test]
fn validate_bundle_burn_native_asset() {
let bundle_burn = vec![
get_burn_tuple(b"Asset 1", 10),
get_burn_tuple(
&compute_asset_desc_hash(&NonEmpty::from_slice(b"Asset 1").unwrap()),
10,
),
(AssetBase::native(), NoteValue::from_raw(20)),
get_burn_tuple(b"Asset 3", 10),
get_burn_tuple(
&compute_asset_desc_hash(&NonEmpty::from_slice(b"Asset 3").unwrap()),
10,
),
];

let result = validate_bundle_burn(&bundle_burn);
Expand All @@ -140,9 +162,18 @@ mod tests {
#[test]
fn validate_bundle_burn_zero_value() {
let bundle_burn = vec![
get_burn_tuple(b"Asset 1", 10),
get_burn_tuple(b"Asset 2", 0),
get_burn_tuple(b"Asset 3", 10),
get_burn_tuple(
&compute_asset_desc_hash(&NonEmpty::from_slice(b"Asset 1").unwrap()),
10,
),
get_burn_tuple(
&compute_asset_desc_hash(&NonEmpty::from_slice(b"Asset 2").unwrap()),
0,
),
get_burn_tuple(
&compute_asset_desc_hash(&NonEmpty::from_slice(b"Asset 3").unwrap()),
10,
),
];

let result = validate_bundle_burn(&bundle_burn);
Expand Down
Loading
Loading