Skip to content

Commit

Permalink
refactor: use set_map_value in mint and burn faucet procs
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumuran committed Feb 6, 2025
1 parent 614536e commit 9872a21
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 54 deletions.
6 changes: 4 additions & 2 deletions crates/miden-lib/asm/kernels/transaction/api.masm
Original file line number Diff line number Diff line change
Expand Up @@ -625,17 +625,19 @@ export.faucet_get_total_fungible_asset_issuance
# => [total_issuance, pad(15)]
end

#! Returns a boolean indicating whether the non-fungible asset is issued.
#! Returns a boolean indicating whether the provided non-fungible asset has been already issued by
#! this faucet.
#!
#! Inputs: [ASSET, pad(12)]
#! Outputs: [is_issued, pad(15)]
#!
#! Where:
#! - ASSET is the non-fungible asset of interest.
#! - ASSET is the non-fungible asset that is being checked.
#! - is_issued is a boolean indicating whether the non-fungible asset has been issued.
#!
#! Panics if:
#! - the ASSET is a fungible asset.
#! - the ASSET is not associated with the faucet the transaction is being executed against.
#!
#! Invocation: dynexec
export.faucet_is_non_fungible_asset_issued
Expand Down
59 changes: 26 additions & 33 deletions crates/miden-lib/asm/kernels/transaction/lib/faucet.masm
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,21 @@ proc.mint_non_fungible_asset
# => [ASSET]

# fetch the root of the SMT containing the non-fungible assets
dupw exec.account::get_faucet_storage_data_slot exec.account::get_item
# => [SMT_ROOT, ASSET, ASSET]
dupw exec.account::get_faucet_storage_data_slot dup movdn.5 exec.account::get_item
# => [SMT_ROOT, ASSET, faucet_storage_data_slot, ASSET]

# prepare stack for insert of non-fungible asset into tracking SMT
swapw exec.asset_vault::build_non_fungible_asset_vault_key dupw.2
# => [ASSET, ASSET_KEY, SMT_ROOT, ASSET]
swapw dupw exec.asset_vault::build_non_fungible_asset_vault_key movup.12
# => [faucet_storage_data_slot, ASSET_KEY, ASSET, SMT_ROOT, ASSET]

# insert the non-fungible asset into the tracking SMT
exec.smt::set
# => [OLD_VAL, SMT_ROOT', ASSET]
exec.account::set_map_item dropw
# => [OLD_VAL, ASSET]

# assert the `OLD_VAL` is EMPTY_WORD, indicating that the non-fungible asset did not already
# exist we only need to check OLD_VAL[3] as this is always set to the faucet_id and can not be 0.
# Assert the `OLD_VAL` is an EMPTY_WORD, indicating that the non-fungible asset has not been
# issued yet. We only need to check OLD_VAL[3] as this is always set to the faucet_id_prefix
# and can not be 0.
eq.0 assert.err=ERR_FAUCET_NON_FUNGIBLE_ASSET_ALREADY_ISSUED drop drop drop
# => [SMT_ROOT', ASSET]

# update the root of the SMT containing the non-fungible assets
exec.account::get_faucet_storage_data_slot exec.account::set_item_raw dropw
# => [ASSET]

# add the non-fungible asset to the input vault for asset preservation checks
Expand Down Expand Up @@ -200,43 +197,40 @@ proc.burn_non_fungible_asset
# => [ASSET, ASSET]

# fetch the root of the SMT containing the non-fungible assets
exec.account::get_faucet_storage_data_slot exec.account::get_item
# => [SMT_ROOT, ASSET, ASSET]
exec.account::get_faucet_storage_data_slot dup movdn.5 exec.account::get_item
# => [SMT_ROOT, ASSET, faucet_storage_data_slot, ASSET]

# prepare stack for removal of non-fungible asset from tracking SMT
swapw exec.asset_vault::build_non_fungible_asset_vault_key padw
# => [EMPTY_WORD, ASSET_KEY, SMT_ROOT, ASSET]
swapw exec.asset_vault::build_non_fungible_asset_vault_key padw swapw movup.12
# => [faucet_storage_data_slot, ASSET_KEY, EMPTY_WORD, SMT_ROOT, ASSET]

# remove the non-fungible asset from the tracking SMT
exec.smt::set
# => [OLD_VAL, SMT_ROOT', ASSET]
exec.account::set_map_item dropw
# => [OLD_VAL, ASSET]

# assert the `OLD_VAL` is not EMPTY_WORD, indicating that the non-fungible asset exists.
# we only need to check OLD_VAL[3] as this is always set to the faucet_id and can not be 0.
# Assert the `OLD_VAL` is not an EMPTY_WORD, indicating that the non-fungible asset exists. We
# only need to check OLD_VAL[3] as this is always set to the faucet_id_prefix and can not be 0.
eq.0 not assert.err=ERR_FAUCET_NON_FUNGIBLE_ASSET_TO_BURN_NOT_FOUND drop drop drop
# => [SMT_ROOT', ASSET]

# update the root of the SMT containing the non-fungible assets
exec.account::get_faucet_storage_data_slot exec.account::set_item_raw dropw
# => [ASSET]

# remove the non-fungible asset from the input vault for asset preservation checks
exec.memory::get_input_vault_root_ptr movdn.4 exec.asset_vault::remove_non_fungible_asset
# => [ASSET]
end

#! Checks whether the provided non-fungible asset has been already issued by this faucet.
#! Returns a boolean indicating whether the provided non-fungible asset has been already issued by
#! this faucet.
#!
#! Inputs: [ASSET]
#! Outputs: [is_issued]
#!
#! Where:
#! - ASSET is the asset that is being checked.
#! - ASSET is the non-fungible asset that is being checked.
#! - is_issued is a boolean indicating whether the non-fungible asset has been issued.
#!
#! Panics if:
#! - the non-fungible asset being minted is not associated with the faucet the transaction is being
#! executed against.
#! - the ASSET is a fungible asset.
#! - the ASSET is not associated with the faucet the transaction is being executed against.
export.is_non_fungible_asset_issued
# assert that the asset is associated with the faucet the transaction is being executed against
# and that the asset is valid
Expand All @@ -253,12 +247,11 @@ export.is_non_fungible_asset_issued

# get the non-fungible asset from the tracking SMT
exec.smt::get
# => [VALUE, SMT_ROOT]
# => [ASSET, SMT_ROOT]

# Assert the `VALUE` is EMPTY_WORD, indicating that the non-fungible asset has not been issued
# yet.
# To do so we only need to check VALUE[3] as this is always set to the faucet_id and can not be
# 0 (in reversed stack order it will be top stack element)
# Assert the `ASSET` is an EMPTY_WORD, indicating that the non-fungible asset has not been
# issued yet. We only need to check ASSET[3] as this is always set to the faucet_id_prefix
# and can not be 0 (in reversed stack order it will be top stack element).
swapw dropw neq.0 movdn.3 drop drop drop
# => [is_issued]
end
Expand Down
6 changes: 4 additions & 2 deletions crates/miden-lib/asm/miden/faucet.masm
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,19 @@ export.get_total_issuance
# => [total_issuance]
end

#! Returns a boolean indicating whether the non-fungible asset is issued.
#! Returns a boolean indicating whether the provided non-fungible asset has been already issued by
#! this faucet.
#!
#! Inputs: [ASSET]
#! Outputs: [is_issued]
#!
#! Where:
#! - ASSET is the non-fungible asset of interest.
#! - ASSET is the non-fungible asset that is being checked.
#! - is_issued is a boolean indicating whether the non-fungible asset has been issued.
#!
#! Panics if:
#! - the ASSET is a fungible asset.
#! - the ASSET is not associated with the faucet the transaction is being executed against.
#!
#! Invocation: exec
export.is_non_fungible_asset_issued
Expand Down
4 changes: 2 additions & 2 deletions crates/miden-lib/src/transaction/procedures/kernel_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ pub const KERNEL0_PROCEDURES: [Digest; 35] = [
// account_has_non_fungible_asset
digest!("0x653ab7a20e5af62c0022850726fef4f5fd6468f9de4cfc43b8fb6b9ff12e6b32"),
// faucet_mint_asset
digest!("0x4ec3644abe3860fda12cb3a5fbbbd2fdc0ee9c6bbfadfb77415449775a24386b"),
digest!("0x2b755f5f43f1f8957225c7d4c2130a37a4ba15c1323703efe82d3ed676051535"),
// faucet_burn_asset
digest!("0x4bf9b04446547e5d54372c8933d1f37f33f7dec48bad6fd0207b5cba1ead99ed"),
digest!("0xdfa2005118b0bcefe94082b25b09bb3b47f525a026dffad60664d9bb679ca140"),
// faucet_get_total_fungible_asset_issuance
digest!("0x7c46ed8cc84a0c5439285f715d1c867eb71131e9f0b1bbd65acea9dddc35bd96"),
// faucet_is_non_fungible_asset_issued
Expand Down
15 changes: 0 additions & 15 deletions crates/miden-tx/src/tests/kernel_tests/test_faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ fn test_mint_non_fungible_asset_succeeds() {
let code = format!(
"
use.std::collections::smt
use.std::sys
use.kernel::account
use.kernel::asset_vault
Expand Down Expand Up @@ -536,11 +535,6 @@ fn test_burn_non_fungible_asset_fails_does_not_exist() {

let code = format!(
"
use.std::collections::smt
#use.kernel::account
use.kernel::asset_vault
use.kernel::memory
use.kernel::prologue
use.test::account
Expand All @@ -567,10 +561,6 @@ fn test_burn_non_fungible_asset_fails_not_faucet_account() {

let code = format!(
"
use.std::collections::smt
use.kernel::asset_vault
use.kernel::memory
use.kernel::prologue
use.test::account
Expand Down Expand Up @@ -606,10 +596,6 @@ fn test_burn_non_fungible_asset_fails_inconsistent_faucet_id() {

let code = format!(
"
use.std::collections::smt
use.kernel::asset_vault
use.kernel::memory
use.kernel::prologue
use.test::account
Expand Down Expand Up @@ -647,7 +633,6 @@ fn test_is_non_fungible_asset_issued_succeeds() {
"
use.kernel::prologue
use.miden::faucet
use.test::account
begin
exec.prologue::prepare_transaction
Expand Down

0 comments on commit 9872a21

Please sign in to comment.