Skip to content

Commit

Permalink
fix: export expiration block number procedures (#925)
Browse files Browse the repository at this point in the history
* feat: export expiration procedure for wallet and faucet

* refactor: rename `update_expiration_block_num` to `update_expiration_delta`

* Clear the stack

* Test fix

* revert name change

* feat: export `get_expiration_block_delta`

* docs: add docs for exposed procedures

* refactor: rename user-facing procedure

* fix: address suggestions

---------

Co-authored-by: igamigo <[email protected]>
  • Loading branch information
tomyrd and igamigo authored Oct 24, 2024
1 parent 5ae103e commit ea1de49
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 68 deletions.
28 changes: 17 additions & 11 deletions miden-lib/asm/kernels/transaction/api.masm
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ proc.authenticate_account_origin
end

#! Verifies that the procedure to be invoked against the foreign account is part of its code.
#! TODO: it is a temporary solution. This and other `*_foreign` procedures should be removed after
#! the refactoring of the `DYN` operation (see miden-vm/#1091) and the fix of the CALLER operation.
#! TODO: it is a temporary solution. This and other `*_foreign` procedures should be removed after
#! the refactoring of the `DYN` operation (see miden-vm/#1091) and the fix of the CALLER operation.
#!
#! Stack: [FOREIGN_PROC_ROOT]
#! Output: [storage_offset, storage_size, ...]
Expand Down Expand Up @@ -230,7 +230,7 @@ end
#!
#! Where:
#! - KERNEL_PROCEDURE_HASH is the hash of this procedure.
#! - FOREIGN_PROC_ROOT is the hash of the foreign procedure which called this procedure.
#! - FOREIGN_PROC_ROOT is the hash of the foreign procedure which called this procedure.
#! - index is the index of the item to get.
#! - VALUE is the value of the item.
#!
Expand Down Expand Up @@ -356,7 +356,7 @@ end
#! Panics if:
#! - the index is out of bounds (>255).
#! - the requested storage slot type is not map
export.get_account_map_item_foreign
export.get_account_map_item_foreign
# drop the procedure's hash
dropw
# => [FOREIGN_PROC_ROOT, index, KEY, pad(6)]
Expand Down Expand Up @@ -976,10 +976,10 @@ end
#! - account_nonce is the nonce of the foreign account.
#! - VAULT_ROOT is the commitment of the foreign account's vault.
#! - STORAGE_ROOT is the commitment of the foreign account's storage.
#! - STORAGE_SLOT_DATA is the data contained in the storage slot which is constructed as follows:
#! - STORAGE_SLOT_DATA is the data contained in the storage slot which is constructed as follows:
#! [SLOT_VALUE, slot_type, 0, 0, 0]
#! - CODE_ROOT is the commitment of the foreign account's code.
#! - ACCOUNT_PROCEDURE_DATA is the information about account procedure which is constructed as
#! - ACCOUNT_PROCEDURE_DATA is the information about account procedure which is constructed as
#! follows: [PROCEDURE_MAST_ROOT, storage_offset, 0, 0, 0]
#!
#! Panics if:
Expand Down Expand Up @@ -1014,7 +1014,7 @@ export.start_foreign_context
# AS => [[foreign_account_id, 0, 0, account_nonce], VAULT_ROOT, STORAGE_ROOT, CODE_ROOT]

# store the id and nonce of the foreign account to the memory
dropw adv_loadw
dropw adv_loadw
exec.memory::set_acct_id_and_nonce dropw
# OS => []
# AS => [VAULT_ROOT, STORAGE_ROOT, CODE_ROOT]
Expand All @@ -1024,7 +1024,7 @@ export.start_foreign_context
# OS => []
# AS => [STORAGE_ROOT, CODE_ROOT]

# move the storage root and the code root to the operand stack
# move the storage root and the code root to the operand stack
adv_loadw padw adv_loadw
# OS => [CODE_ROOT, STORAGE_ROOT]
# AS => []
Expand All @@ -1050,7 +1050,7 @@ export.start_foreign_context
# AS => []
end

# make sure that the state of the loaded foreign account corresponds to this commitment in the
# make sure that the state of the loaded foreign account corresponds to this commitment in the
# account database
exec.account::validate_current_foreign_account
# => []
Expand All @@ -1076,23 +1076,29 @@ end
#! The input block height delta is added to the reference block in order to output an upper limit
#! up until which the transaction will be considered valid (not expired).
#!
#! Inputs: [block_height_delta, ...]
#! Inputs: [KERNEL_PROCEDURE_HASH, block_height_delta, ...]
#! Output: [...]
#!
#! Where:
#! - block_height_delta is the desired expiration time delta (1 to 0xFFFF).
export.update_expiration_block_num
# drop the procedure's hash
dropw

exec.tx::update_expiration_block_num
end

#! Gets the transaction expiration delta.
#!
#! Inputs: [...]
#! Output: [block_height_delta, ...]
#! Output: [KERNEL_PROCEDURE_HASH, block_height_delta, ...]
#!
#! Where:
#! - block_height_delta is the stored expiration time delta (1 to 0xFFFF).
export.get_expiration_delta
# drop the procedure's hash
dropw

exec.tx::get_expiration_delta
end

Expand Down
26 changes: 19 additions & 7 deletions miden-lib/asm/kernels/transaction/lib/tx.masm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const.EXPIRY_UPPER_LIMIT=0xFFFF+1
# The note type must be PUBLIC, unless the high bits are `0b11`. (See the table below.)
const.ALL_NOTE_TYPES_ALLOWED=3 # 0b11

# Max U32 value, used for initializing the expiration block number
const.MAX_BLOCK_NUM=0xFFFFFFFF

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

Expand Down Expand Up @@ -80,7 +83,7 @@ const.NOTE_AFTER_CREATED_EVENT=131084
const.NOTE_BEFORE_ADD_ASSET_EVENT=131085
# Event emitted after an ASSET is added to a note
const.NOTE_AFTER_ADD_ASSET_EVENT=131086

#! Returns the block hash of the reference block to memory.
#!
#! Stack: []
Expand Down Expand Up @@ -189,7 +192,7 @@ end
#!
#! The input block_height_delta is added to the block reference number in order to output an upper
#! limit at which the transaction will be considered valid (not expired).
#! This value can be later decreased, but not increased.
#! This value can be later decreased, but not increased.
#!
#! Inputs: [block_height_delta, ...]
#! Output: [...]
Expand All @@ -206,16 +209,16 @@ export.update_expiration_block_num
# => [absolute_expiration_num]

# Load the current stored delta from memory
dup exec.memory::get_expiration_block_num
dup exec.memory::get_expiration_block_num
# => [stored_expiration_block_num, absolute_expiration_num, absolute_expiration_num]

# Check if block_height_delta is greater
u32lt
if.true
# Set new expiration delta
exec.memory::set_expiration_block_num
exec.memory::set_expiration_block_num
else
drop
drop
end
end

Expand All @@ -227,8 +230,17 @@ end
#! Where:
#! - block_height_delta is the stored expiration time delta (1 to 0xFFFF).
export.get_expiration_delta
exec.memory::get_expiration_block_num exec.get_block_number
sub
exec.memory::get_expiration_block_num
# => [stored_expiration_block_num]

dup eq.MAX_BLOCK_NUM
if.true
# The delta was not set
drop push.0
else
# Calculate the delta
exec.get_block_number sub
end
end


Expand Down
Loading

0 comments on commit ea1de49

Please sign in to comment.