diff --git a/miden-lib/asm/kernels/transaction/api.masm b/miden-lib/asm/kernels/transaction/api.masm index 4df5ae665..ce54f693e 100644 --- a/miden-lib/asm/kernels/transaction/api.masm +++ b/miden-lib/asm/kernels/transaction/api.masm @@ -525,7 +525,7 @@ end #! tag is the tag to be included in the note. #! note_type is the note storage type #! RECIPIENT is the recipient of the note. -#! note_idx is the index of the the note. +#! note_idx is the index of the crated note. export.create_note # authenticate that the procedure invocation originates from the account context exec.authenticate_account_origin @@ -543,8 +543,7 @@ export.create_note # => [note_idx, 0, 0, 0, 0, 0] end -#! Adds an ASSET to the specified note and returns the index of the note -#! to which the asset is added. +#! Adds the ASSET to the note specified by the index. #! #! Inputs: [note_idx, ASSET] #! Outputs: [note_idx, 0, 0, 0, 0] diff --git a/miden-lib/asm/miden/kernels/tx/tx.masm b/miden-lib/asm/miden/kernels/tx/tx.masm index 92c0b9a09..610227730 100644 --- a/miden-lib/asm/miden/kernels/tx/tx.masm +++ b/miden-lib/asm/miden/kernels/tx/tx.masm @@ -251,14 +251,14 @@ proc.add_fungible_asset_to_note # => [note_ptr, note_idx] end -#! Creates a new note and returns the index of the created note. +#! Creates a new note and returns the index of the note. #! #! Inputs: [tag, note_type, RECIPIENT] #! Outputs: [note_idx] #! #! - tag is the tag to be included in the note. #! - RECIPIENT defines spend conditions for the note. -#! - note_idx is the index of the the note. +#! - note_idx is the index of the crated note. #! #! Panics if: #! - the note_type is not valid. @@ -312,13 +312,12 @@ export.create_note # => [note_idx] end -#! Adds an ASSET to the specified note and returns the index of the note -#! to which the asset is added. +#! Adds the ASSET to the note specified by the index. #! #! Inputs: [note_idx, ASSET] #! Outputs: [note_idx] #! -#! note_idx is the index of the the note to which the asset is added. +#! - note_idx is the index of the the note to which the asset is added. #! - ASSET can be a fungible or non-fungible asset. #! #! Panics if: diff --git a/miden-lib/asm/miden/tx.masm b/miden-lib/asm/miden/tx.masm index 6e6cf86a1..68fbe8f66 100644 --- a/miden-lib/asm/miden/tx.masm +++ b/miden-lib/asm/miden/tx.masm @@ -61,7 +61,7 @@ end #! tag is the tag to be included in the note. #! note_type is the storage type of the note #! RECIPIENT is the recipient of the note. -#! note_idx is the index of the the note to which the asset is added. +#! note_idx is the index of the crated note. export.create_note syscall.create_note # => [note_idx, ZERO, 0] @@ -71,13 +71,12 @@ export.create_note # => [note_idx] end -#! Adds an ASSET to the specified note and returns the index of the note -#! to which the asset is added. +#! Adds the ASSET to the note specified by the index. #! #! Inputs: [note_idx, ASSET] #! Outputs: [note_idx] #! -#! note_idx is the index of the the note to which the asset is added. +#! note_idx is the index of the note to which the asset is added. #! ASSET can be a fungible or non-fungible asset. export.add_asset_to_note syscall.add_asset_to_note diff --git a/miden-lib/src/transaction/errors.rs b/miden-lib/src/transaction/errors.rs index 3e3b7be73..6a9827435 100644 --- a/miden-lib/src/transaction/errors.rs +++ b/miden-lib/src/transaction/errors.rs @@ -23,7 +23,6 @@ pub enum TransactionKernelError { MalformedAssetOnAccountVaultUpdate(AssetError), MalformedNoteInputs(NoteError), MalformedNoteMetadata(NoteError), - MalformedNoteIndex(u64), MalformedNoteScript(Vec), MalformedNoteType(NoteError), MalformedRecipientData(Vec), @@ -68,9 +67,6 @@ impl fmt::Display for TransactionKernelError { TransactionKernelError::MalformedNoteMetadata(err) => { write!(f, "Note metadata created by the event handler is not well formed {err}") }, - TransactionKernelError::MalformedNoteIndex(err) => { - write!(f, "Note index is malformed {err}") - }, TransactionKernelError::MalformedNoteScript(data) => { write!( f, "Note script data extracted from the advice map by the event handler is not well formed {data:?}") }, diff --git a/miden-tx/src/host/mod.rs b/miden-tx/src/host/mod.rs index 8f978e494..6f20330f0 100644 --- a/miden-tx/src/host/mod.rs +++ b/miden-tx/src/host/mod.rs @@ -1,8 +1,7 @@ use alloc::{collections::BTreeMap, rc::Rc, string::ToString, vec::Vec}; use miden_lib::transaction::{ - memory::{ACCT_STORAGE_ROOT_PTR, CURRENT_CONSUMED_NOTE_PTR}, - TransactionEvent, TransactionKernelError, TransactionTrace, + memory::CURRENT_CONSUMED_NOTE_PTR, TransactionEvent, TransactionKernelError, TransactionTrace, }; use miden_objects::{ accounts::{AccountDelta, AccountId, AccountStorage, AccountStub}, @@ -118,6 +117,8 @@ impl TransactionHost { let note_idx: usize = stack[9].as_int() as usize; + assert_eq!(note_idx, self.output_notes.len(), "note index mismatch"); + let note_builder = OutputNoteBuilder::new(stack, &self.adv_provider)?; self.output_notes.insert(note_idx, note_builder); @@ -135,21 +136,16 @@ impl TransactionHost { let stack = process.get_stack_state(); //# => [ASSET, note_ptr, num_of_assets, note_idx] - // this is a safe conversion because the note index (in theory) is always a u64 - // in theory, the user can provide any note index. However, we already check in - // the kernel that the note index < MAX_OUTPUT_NOTES_PER_TX - let note_idx_u64 = stack[6].as_int(); - if note_idx_u64 >= usize::MAX as u64 { - return Err(TransactionKernelError::MalformedNoteIndex(note_idx_u64)); - } - let note_idx = note_idx_u64 as usize; + let note_idx = stack[6].as_int(); + assert!(note_idx < self.output_notes.len() as u64); + let node_idx = note_idx as usize; let asset = Asset::try_from(process.get_stack_word(0)) .map_err(TransactionKernelError::MalformedAsset)?; let note_builder = self .output_notes - .get_mut(¬e_idx) + .get_mut(&node_idx) .ok_or_else(|| TransactionKernelError::MissingNote(format!("{:?}", ¬e_idx)))?; note_builder.add_asset(asset)?;