Skip to content

Commit

Permalink
Move events emitting after validation in the tx library (#660)
Browse files Browse the repository at this point in the history
* refactor: move event emitting after validation

In this commit refactoring was made for account procedures and incr_nonce procedure. set_item and set_map_item will be reworked in future commits.

* refactor: move emit in the incr_nonce
  • Loading branch information
Fumuran authored and bobbinth committed May 14, 2024
1 parent 2af529d commit f51f205
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
26 changes: 14 additions & 12 deletions miden-lib/asm/kernels/transaction/api.masm
Original file line number Diff line number Diff line change
Expand Up @@ -342,22 +342,24 @@ end
#! - If ASSET is a fungible asset, then ASSET' is the total fungible asset in the account vault
#! after ASSET was added to it.
export.account_vault_add_asset
# TODO: we execute `push.1 drop` before `emit` as decorators are not supported without other
# instructions - see: https://github.com/0xPolygonMiden/miden-vm/issues/1122
# emit event to signal that an asset is being added to the account vault
push.1 drop emit.ACCOUNT_VAULT_ADD_ASSET_EVENT

# authenticate that the procedure invocation originates from the account context
exec.authenticate_account_origin
# => [ASSET]

# duplicate the ASSET to be able to emit an event after an asset is being added
dupw
# => [ASSET, ASSET]

# fetch the vault root
exec.memory::get_acct_vault_root_ptr movdn.4
# => [ASSET, acct_vault_root_ptr]
# => [ASSET, acct_vault_root_ptr, ASSET]

# add the asset to the account vault
exec.asset_vault::add_asset
# => [ASSET']
# => [ASSET', ASSET]

# emit event to signal that an asset is being added to the account vault
swapw emit.ACCOUNT_VAULT_ADD_ASSET_EVENT dropw
end

#! Remove the specified asset from the vault.
Expand All @@ -372,11 +374,6 @@ end
#!
#! - ASSET is the asset to remove from the vault.
export.account_vault_remove_asset
# TODO: we execute `push.1 drop` before `emit` as decorators are not supported without other
# instructions - see: https://github.com/0xPolygonMiden/miden-vm/issues/1122
# emit event to signal that an asset is being removed from the account vault
push.1 drop emit.ACCOUNT_VAULT_REMOVE_ASSET_EVENT

# authenticate that the procedure invocation originates from the account context
exec.authenticate_account_origin
# => [ASSET]
Expand All @@ -388,6 +385,11 @@ export.account_vault_remove_asset
# remove the asset from the account vault
exec.asset_vault::remove_asset
# => [ASSET]

# TODO: we execute `push.1 drop` before `emit` as decorators are not supported without other
# instructions - see: https://github.com/0xPolygonMiden/miden-vm/issues/1122
# emit event to signal that an asset is being removed from the account vault
push.1 drop emit.ACCOUNT_VAULT_REMOVE_ASSET_EVENT
end

#! Returns the number of assets and vault hash of the note currently being processed. Panics if a
Expand Down
3 changes: 2 additions & 1 deletion miden-lib/asm/miden/kernels/tx/account.masm
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,11 @@ end
#! - value is the value to increment the nonce by. value can be at most 2^32 - 1 otherwise this
#! procedure panics.
export.incr_nonce
u32assert.err=ERR_ACCOUNT_NONCE_INCR_MUST_BE_U32

# emit event to signal that account nonce is being incremented
emit.ACCOUNT_INCREMENT_NONCE_EVENT

u32assert.err=ERR_ACCOUNT_NONCE_INCR_MUST_BE_U32
exec.memory::get_acct_nonce add
exec.memory::set_acct_nonce
end
Expand Down
2 changes: 1 addition & 1 deletion miden-lib/src/tests/test_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ fn test_set_item() {
# get the item
exec.account::set_item
# assert empty old value
# assert empty old value
padw assert_eqw
# get the new storage root
Expand Down
8 changes: 4 additions & 4 deletions mock/src/mock/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ pub fn mock_account_storage() -> AccountStorage {
// The MAST root of the default account's interface. Use these constants to interact with the
// account's procedures.
const MASTS: [&str; 9] = [
"0xe06a83054c72efc7e32698c4fc6037620cde834c9841afb038a5d39889e502b6",
"0x0cebd5225e35a53ff49cf57a1949f97a7b153c636db10b027f532132099917aa",
"0x74de7e94e5afc71e608f590c139ac51f446fc694da83f93d968b019d1d2b7306",
"0x3ffe8a2106040b02dcaefe87dc7f488b328f6b5760e863a90c0700e73cc190ac",
"0xd765111e22479256e87a57eaf3a27479d19cc876c9a715ee6c262e0a0d47a2ac",
"0x17b326d5403115afccc0727efa72bd929bfdc7bbf284c7c28a7aadade5d4cc9d",
"0x6682a0e0f4e49820e5c547f1b60a82cb326a56c972999e36bf6d45459393ac87",
Expand Down Expand Up @@ -168,7 +168,7 @@ pub fn mock_account_code(assembler: &Assembler) -> AccountCode {
# => [0]
end
# acct proc 3
# acct proc 3
export.set_item
exec.account::set_item
# => [R', V, 0, 0, 0]
Expand All @@ -177,7 +177,7 @@ pub fn mock_account_code(assembler: &Assembler) -> AccountCode {
# => [R', V]
end
# acct proc 4
# acct proc 4
export.set_map_item
exec.account::set_map_item
# => [R', V, 0, 0, 0]
Expand Down

0 comments on commit f51f205

Please sign in to comment.