Skip to content

Commit

Permalink
feat: Test build_note_metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGackstatter committed Jan 10, 2025
1 parent 40b7633 commit 9fb5f1e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
4 changes: 3 additions & 1 deletion miden-lib/asm/kernels/transaction/lib/tx.masm
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,9 @@ end
#! or off-chain).
#! - execution_hint is the hint which specifies when a note is ready to be consumed.
#! - NOTE_METADATA is the metadata associated with a note.
proc.build_note_metadata
export.build_note_metadata
# This procedure is only exported so it can be tested.

# Validate the note type.
# --------------------------------------------------------------------------------------------

Expand Down
69 changes: 67 additions & 2 deletions miden-tx/src/tests/kernel_tests/test_note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ use miden_lib::{
transaction::memory::CURRENT_INPUT_NOTE_PTR,
};
use miden_objects::{
notes::Note, testing::prepare_word, transaction::TransactionArgs, Hasher, WORD_SIZE,
accounts::AccountId,
notes::{Note, NoteExecutionHint, NoteExecutionMode, NoteMetadata, NoteTag, NoteType},
testing::{account_id::ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_OFF_CHAIN, prepare_word},
transaction::TransactionArgs,
Hasher, WORD_SIZE,
};
use vm_processor::{ProcessState, EMPTY_WORD, ONE};
use vm_processor::{ProcessState, Word, EMPTY_WORD, ONE};

use super::{Felt, Process, ZERO};
use crate::{
Expand Down Expand Up @@ -544,3 +548,64 @@ fn test_get_current_script_hash() {
let script_hash = tx_context.input_notes().get_note(0).note().script().hash();
assert_eq!(process.stack.get_word(0), script_hash.as_elements());
}

#[test]
fn test_build_note_metadata() {
let tx_context = TransactionContextBuilder::with_standard_account(ONE)
.with_mock_notes_preserved()
.build();
let sender = tx_context.account().id();
let receiver =
AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_OFF_CHAIN).unwrap();

let test_metadata1 = NoteMetadata::new(
sender,
NoteType::Private,
NoteTag::from_account_id(receiver, NoteExecutionMode::Local).unwrap(),
NoteExecutionHint::after_block(500).unwrap(),
Felt::try_from(1u64 << 63).unwrap(),
)
.unwrap();
let test_metadata2 = NoteMetadata::new(
sender,
NoteType::Public,
// Use largest allowed use_case_id.
NoteTag::for_public_use_case((1 << 14) - 1, u16::MAX, NoteExecutionMode::Local).unwrap(),
NoteExecutionHint::on_block_slot(u8::MAX, u8::MAX, u8::MAX),
Felt::try_from(0u64).unwrap(),
)
.unwrap();

for (iteration, test_metadata) in [test_metadata1, test_metadata2].into_iter().enumerate() {
let code = format!(
"
use.kernel::prologue
use.kernel::tx
begin
exec.prologue::prepare_transaction
push.{execution_hint}.{note_type}.{aux}.{tag}
exec.tx::build_note_metadata
# truncate the stack
swapw dropw
end
",
execution_hint = Felt::from(test_metadata.execution_hint()),
note_type = Felt::from(test_metadata.note_type()),
aux = test_metadata.aux(),
tag = test_metadata.tag(),
);

let process = tx_context.execute_code(&code).unwrap();

let metadata_word = [
process.stack.get(3),
process.stack.get(2),
process.stack.get(1),
process.stack.get(0),
];

assert_eq!(Word::from(test_metadata), metadata_word, "failed in iteration {iteration}");
}
}

0 comments on commit 9fb5f1e

Please sign in to comment.