|
| 1 | +//! Tests for the `miden::standards::attachments::network_account_target` module. |
| 2 | +
|
| 3 | +use miden_protocol::Felt; |
| 4 | +use miden_protocol::account::AccountStorageMode; |
| 5 | +use miden_protocol::note::{NoteAttachment, NoteExecutionHint, NoteMetadata, NoteTag, NoteType}; |
| 6 | +use miden_protocol::testing::account_id::AccountIdBuilder; |
| 7 | +use miden_standards::note::NetworkAccountTarget; |
| 8 | + |
| 9 | +use crate::executor::CodeExecutor; |
| 10 | + |
| 11 | +#[tokio::test] |
| 12 | +async fn network_account_target_get_id() -> anyhow::Result<()> { |
| 13 | + let target_id = AccountIdBuilder::new() |
| 14 | + .storage_mode(AccountStorageMode::Network) |
| 15 | + .build_with_rng(&mut rand::rng()); |
| 16 | + let exec_hint = NoteExecutionHint::Always; |
| 17 | + |
| 18 | + let attachment = NoteAttachment::from(NetworkAccountTarget::new(target_id, exec_hint)?); |
| 19 | + let metadata = |
| 20 | + NoteMetadata::new(target_id, NoteType::Public, NoteTag::with_account_target(target_id)) |
| 21 | + .with_attachment(attachment.clone()); |
| 22 | + let metadata_header = metadata.to_header_word(); |
| 23 | + |
| 24 | + let source = format!( |
| 25 | + r#" |
| 26 | + use miden::standards::attachments::network_account_target |
| 27 | + use miden::protocol::note |
| 28 | +
|
| 29 | + begin |
| 30 | + push.{attachment_word} |
| 31 | + push.{metadata_header} |
| 32 | + exec.note::extract_attachment_info_from_metadata |
| 33 | + # => [attachment_kind, attachment_scheme, NOTE_ATTACHMENT] |
| 34 | + exec.network_account_target::get_id |
| 35 | + # cleanup stack |
| 36 | + movup.2 drop movup.2 drop |
| 37 | + end |
| 38 | + "#, |
| 39 | + metadata_header = metadata_header, |
| 40 | + attachment_word = attachment.content().to_word(), |
| 41 | + ); |
| 42 | + |
| 43 | + let exec_output = CodeExecutor::with_default_host().run(&source).await?; |
| 44 | + |
| 45 | + assert_eq!(exec_output.stack[0], target_id.prefix().as_felt()); |
| 46 | + assert_eq!(exec_output.stack[1], target_id.suffix()); |
| 47 | + |
| 48 | + Ok(()) |
| 49 | +} |
| 50 | + |
| 51 | +#[tokio::test] |
| 52 | +async fn network_account_target_new_attachment() -> anyhow::Result<()> { |
| 53 | + let target_id = AccountIdBuilder::new() |
| 54 | + .storage_mode(AccountStorageMode::Network) |
| 55 | + .build_with_rng(&mut rand::rng()); |
| 56 | + let exec_hint = NoteExecutionHint::Always; |
| 57 | + |
| 58 | + let attachment = NoteAttachment::from(NetworkAccountTarget::new(target_id, exec_hint)?); |
| 59 | + let attachment_word = attachment.content().to_word(); |
| 60 | + let expected_attachment_kind = Felt::from(attachment.attachment_kind().as_u8()); |
| 61 | + |
| 62 | + let source = format!( |
| 63 | + r#" |
| 64 | + use miden::standards::attachments::network_account_target |
| 65 | +
|
| 66 | + begin |
| 67 | + push.{exec_hint} |
| 68 | + push.{target_id_suffix} |
| 69 | + push.{target_id_prefix} |
| 70 | + # => [target_id_prefix, target_id_suffix, exec_hint] |
| 71 | + exec.network_account_target::new |
| 72 | + # => [attachment_scheme, attachment_kind, ATTACHMENT, pad(16)] |
| 73 | +
|
| 74 | + # cleanup stack |
| 75 | + swapdw dropw dropw |
| 76 | + end |
| 77 | + "#, |
| 78 | + target_id_prefix = target_id.prefix().as_felt(), |
| 79 | + target_id_suffix = target_id.suffix(), |
| 80 | + exec_hint = Felt::from(exec_hint), |
| 81 | + ); |
| 82 | + |
| 83 | + let exec_output = CodeExecutor::with_default_host().run(&source).await?; |
| 84 | + |
| 85 | + assert_eq!(exec_output.stack[0], expected_attachment_kind); |
| 86 | + assert_eq!( |
| 87 | + exec_output.stack[1], |
| 88 | + Felt::from(NetworkAccountTarget::ATTACHMENT_SCHEME.as_u32()) |
| 89 | + ); |
| 90 | + |
| 91 | + assert_eq!(exec_output.stack.get_stack_word_be(2).unwrap(), attachment_word); |
| 92 | + |
| 93 | + Ok(()) |
| 94 | +} |
| 95 | + |
| 96 | +#[tokio::test] |
| 97 | +async fn network_account_target_attachment_round_trip() -> anyhow::Result<()> { |
| 98 | + let target_id = AccountIdBuilder::new() |
| 99 | + .storage_mode(AccountStorageMode::Network) |
| 100 | + .build_with_rng(&mut rand::rng()); |
| 101 | + let exec_hint = NoteExecutionHint::Always; |
| 102 | + |
| 103 | + let source = format!( |
| 104 | + r#" |
| 105 | + use miden::standards::attachments::network_account_target |
| 106 | +
|
| 107 | + begin |
| 108 | + push.{exec_hint} |
| 109 | + push.{target_id_suffix} |
| 110 | + push.{target_id_prefix} |
| 111 | + # => [target_id_prefix, target_id_suffix, exec_hint] |
| 112 | + exec.network_account_target::new |
| 113 | + # => [attachment_scheme, attachment_kind, ATTACHMENT] |
| 114 | + exec.network_account_target::get_id |
| 115 | + # => [target_id_prefix, target_id_suffix] |
| 116 | + movup.2 drop movup.2 drop |
| 117 | + end |
| 118 | + "#, |
| 119 | + target_id_prefix = target_id.prefix().as_felt(), |
| 120 | + target_id_suffix = target_id.suffix(), |
| 121 | + exec_hint = Felt::from(exec_hint), |
| 122 | + ); |
| 123 | + |
| 124 | + let exec_output = CodeExecutor::with_default_host().run(&source).await?; |
| 125 | + |
| 126 | + assert_eq!(exec_output.stack[0], target_id.prefix().as_felt()); |
| 127 | + assert_eq!(exec_output.stack[1], target_id.suffix()); |
| 128 | + |
| 129 | + Ok(()) |
| 130 | +} |
0 commit comments