Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tx kernel events errors #819

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Renamed `NoteExecutionHint` to `NoteExecutionMode` and added new `NoteExecutionHint` to `NoteMetadata` (#812).
- [BREAKING] Refactored and simplified `NoteOrigin` and `NoteInclusionProof` structs (#810, #814).
- Made `miden_lib::notes::build_swap_tag()` function public (#817).
- Refactor tx kernel events (all events to api.masm) and errors (subsequent files) (#768)

## 0.4.0 (2024-07-03)

Expand Down
117 changes: 64 additions & 53 deletions miden-lib/asm/kernels/transaction/api.masm
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ use.miden::kernels::tx::memory
use.miden::kernels::tx::note
use.miden::kernels::tx::tx

# ERRORS
# =================================================================================================

# For faucets the slot FAUCET_STORAGE_DATA_SLOT is reserved and can not be used with set_account_item
const.ERR_FAUCET_RESERVED_DATA_SLOT=0x00020000

# Procedure can only be called from faucet accounts
const.ERR_ACCT_MUST_BE_A_FAUCET=0x00020001

# Getting a map item on a non-map slot
const.ERR_READING_MAP_VALUE_FROM_NON_MAP_SLOT=0x00020049

# EVENTS
# =================================================================================================

Expand All @@ -33,6 +21,21 @@ const.ACCOUNT_VAULT_BEFORE_REMOVE_ASSET_EVENT=131074
# Event emitted after an asset is removed from the account vault.
const.ACCOUNT_VAULT_AFTER_REMOVE_ASSET_EVENT=131075

# Event emitted before an account storage item is updated.
const.ACCOUNT_STORAGE_BEFORE_SET_ITEM_EVENT=131076
# Event emitted after an account storage item is updated.
const.ACCOUNT_STORAGE_AFTER_SET_ITEM_EVENT=131077

# Event emitted before an account storage map item is updated.
const.ACCOUNT_STORAGE_BEFORE_SET_MAP_ITEM_EVENT=131078
# Event emitted after an account storage map item is updated.
const.ACCOUNT_STORAGE_AFTER_SET_MAP_ITEM_EVENT=131079

# Event emitted before an account nonce is incremented.
const.ACCOUNT_BEFORE_INCREMENT_NONCE_EVENT=131080
# Event emitted after an account nonce is incremented.
const.ACCOUNT_AFTER_INCREMENT_NONCE_EVENT=131081

# AUTHENTICATION
# =================================================================================================

Expand Down Expand Up @@ -137,9 +140,17 @@ export.incr_account_nonce
push.0 swap
# => [value, 0]

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

# increment the account nonce
exec.account::incr_nonce
# => [0]

# emit event to signal that account nonce has been incremented
push.0 drop # TODO: remove line, see miden-vm/#1122
emit.ACCOUNT_AFTER_INCREMENT_NONCE_EVENT
# => [0]
end

#! Gets an item from the account storage. Panics if the index is out of bounds.
Expand All @@ -161,32 +172,36 @@ end

#! Sets an item in the account storage. Panics if the index is out of bounds.
#!
#! Stack: [index, V', 0, 0, 0]
#! Output: [R', V]
#! Stack: [index, NEW_VALUE]
#! Output: [OLD_VALUE]
#!
#! - index is the index of the item to set.
#! - V' is the value to set.
#! - V is the previous value of the item.
#! - R' is the new storage root.
export.set_account_item
# if the transaction is being executed against a faucet account then assert
# index != FAUCET_STORAGE_DATA_SLOT (reserved slot)
dup exec.account::get_faucet_storage_data_slot eq
exec.account::get_id exec.account::is_faucet
and assertz.err=ERR_FAUCET_RESERVED_DATA_SLOT
# => [index, V', 0, 0, 0]

#! - NEW_VALUE is the value to set.
#! - OLD_VALUE is the previous value of the item.
export.set_account_item.2
# authenticate that the procedure invocation originates from the account context
exec.authenticate_account_origin
# => [index, V', 0, 0, 0]
# => [index, NEW_VALUE]

push.0 drop # TODO: remove line, see miden-vm/#1122
emit.ACCOUNT_STORAGE_BEFORE_SET_ITEM_EVENT
# => [index, NEW_VALUE]

# store index and NEW_VALUE for later to emit event. Note: loc_store pops the Felt value
loc_store.0 loc_storew.1 loc_load.0
# => [index, NEW_VALUE]

# set the account storage item
exec.account::set_item
# => [R', V, 0, 0, 0]
# => [OLD_VALUE]

# emit event to signal that an account storage item is being updated
padw loc_loadw.1 loc_load.0 emit.ACCOUNT_STORAGE_AFTER_SET_ITEM_EVENT drop dropw
# => [OLD_VALUE]
Comment on lines +198 to +200
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I would probably move drop dropw to line 203 below and update the comments accordingly.


# organize the stack for return
movup.8 drop movup.8 drop movup.8 drop
# => [R', V]
movup.4 drop movup.4 drop movup.4 drop
# => [OLD_VALUE]
end

#! Returns VALUE located under specified KEY in map in specified account storage slot.
Expand All @@ -200,21 +215,9 @@ end
#! - index is the index of the item to get.
#! - VALUE is the value of the item.
export.get_account_map_item
# check if storage type is map
dup exec.account::get_storage_slot_type_info drop
# => [slot_type, index, KEY, ...]

# fails if slot_type is not 1 = map
exec.constants::get_storage_slot_type_map eq assert.err=ERR_READING_MAP_VALUE_FROM_NON_MAP_SLOT
# => [index, KEY, ...]

# fetch the account storage item, which is ROOT of the map
exec.account::get_item swapw
# => [KEY, ROOT ...]

# fetch the VALUE located under KEY in the tree
exec.smt::get
# => [VALUE, ROOT, ...]
# set the account storage item
exec.account::get_map_item
# => [VALUE, ROOT]

# prepare the stack for return
swapw dropw
Expand All @@ -236,26 +239,38 @@ end
#! - KEY is the key of the new item.
#! - OLD_MAP_ROOT is the root of the old map before insertion
#! - NEW_MAP_ROOT is the root of the new map after insertion.
export.set_account_map_item.1
export.set_account_map_item.3
# authenticate that the procedure invocation originates from the account context
exec.authenticate_account_origin
# => [index, KEY, NEW_VALUE, ...]

# store index for later
dup loc_store.0
# store index (pos 0), KEY (pos 1), NEW_VALUE (pos 2) for later to emit event
dup loc_store.0 movdn.8 loc_storew.1 swapw loc_storew.2 swapw movup.8
# => [index, KEY, NEW_VALUE, ...]

# fetch the account storage item, which is ROOT of the map
exec.account::get_item movdnw.2
# => [KEY, NEW_VALUE, OLD_MAP_ROOT, ...]

# emit event before the map item is set
loc_load.0 emit.ACCOUNT_STORAGE_BEFORE_SET_MAP_ITEM_EVENT
# => [index, KEY, NEW_VALUE, OLD_MAP_ROOT, ...]

# set the new map item
loc_load.0 exec.account::set_map_item
exec.account::set_map_item
# => [OLD_MAP_ROOT, OLD_VALUE, ...]

# load all data to emit event after the map item is set
padw loc_loadw.2 padw loc_loadw.1 loc_load.0
# => [index, KEY, NEW_VALUE, OLD_MAP_ROOT, OLD_VALUE, ...]

# emit event to signal that an account storage map item is being updated
emit.ACCOUNT_STORAGE_AFTER_SET_MAP_ITEM_EVENT drop dropw dropw
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar nit as above.

# => [OLD_MAP_ROOT, OLD_VALUE, ...]

# organize the stack for return (16 elements)
movupw.2 dropw
# => [OLD_MAP_ROOT, OLD_MAP_VALUE, 0, ...]
# => [OLD_MAP_ROOT, OLD_VALUE, 0, ...]
end

#! Sets the code of the account the transaction is being executed against. This procedure can only
Expand Down Expand Up @@ -631,10 +646,6 @@ end
#! - total_issuance is the total issuance of the fungible faucet the transaction is being executed
#! against.
export.get_fungible_faucet_total_issuance
# assert that we are executing a transaction against a fungible faucet (access checks)
exec.account::get_id exec.account::is_fungible_faucet assert.err=ERR_ACCT_MUST_BE_A_FAUCET
# => [0]

# get the total issuance
exec.faucet::get_total_issuance
# => [total_issuance]
Expand Down
Loading
Loading