miden-testing 0.16.0-rc.2

Miden protocol testing tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
use core::num::NonZeroU16;
use core::slice;

use miden_protocol::account::auth::AuthScheme;
use miden_protocol::account::{Account, AccountCodeInterface, AccountComponentCode, AccountId};
use miden_protocol::asset::{Asset, FungibleAsset, NonFungibleAsset};
use miden_protocol::crypto::rand::{FeltRng, RandomCoin};
use miden_protocol::note::{
    Note,
    NoteAssets,
    NoteAttachment,
    NoteAttachmentScheme,
    NoteAttachments,
    NoteRecipient,
    NoteStorage,
    NoteTag,
    NoteType,
    PartialNote,
    PartialNoteMetadata,
};
use miden_protocol::testing::account_id::{
    ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET_1,
    ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_IMMUTABLE_CODE,
    ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_IMMUTABLE_CODE_2,
};
use miden_protocol::testing::note::DEFAULT_NOTE_SCRIPT;
use miden_protocol::transaction::{ExecutedTransaction, RawOutputNote};
use miden_protocol::{Felt, Hasher, Word};
use miden_standards::account::faucets::{FungibleFaucet, NonFungibleFaucet};
use miden_standards::account::wallets::BasicWallet;
use miden_standards::code_builder::CodeBuilder;
use miden_standards::errors::standards::ERR_SEND_NOTES_FAUCET_NOTE_REQUIRES_ONE_ASSET;
use miden_standards::note::P2idNote;
use miden_standards::tx_script::{
    SendFungibleFaucetNotesTransactionScript,
    SendNonFungibleFaucetNotesTransactionScript,
    SendNotesTransactionScript,
    SendNotesTransactionScriptError,
    SendWalletNotesTransactionScript,
};
use miden_testing::utils::create_p2any_note;
use miden_testing::{Auth, MockChain, assert_transaction_executor_error};
use miden_tx::TransactionExecutorError;

/// Tests the execution of the generated send_note transaction script in case the sending account
/// has the [`BasicWallet`][wallet] interface.
///
/// This tests consumes a SPAWN note first so that the note_idx in the send_note script is not zero
/// to make sure the note_idx is correctly kept on the stack.
///
/// The test also sends two assets to make sure the generated script deals correctly with multiple
/// assets.
///
/// [wallet]: miden_standards::account::interface::AccountComponentInterface::BasicWallet
#[tokio::test]
async fn test_send_note_script_basic_wallet() -> anyhow::Result<()> {
    let total_asset = FungibleAsset::mock(100);
    let sent_asset0 = NonFungibleAsset::mock(&[4, 5, 6]);

    let sent_asset1 = FungibleAsset::mock(10);
    let sent_asset2 = FungibleAsset::mock(40);

    let mut builder = MockChain::builder();

    let sender_basic_wallet_account = builder.add_existing_wallet_with_assets(
        Auth::BasicAuth {
            auth_scheme: AuthScheme::Falcon512Poseidon2,
        },
        [sent_asset0, total_asset],
    )?;
    let mut rng = RandomCoin::new(Word::from([1, 2, 3, 4u32]));
    let p2any_note = create_p2any_note(
        sender_basic_wallet_account.id(),
        NoteType::Private,
        [sent_asset1],
        &mut rng,
    );
    let spawn_note = builder.add_spawn_note([&p2any_note])?;
    let mock_chain = builder.build()?;

    let attachment_0 = NoteAttachment::with_words(
        NoteAttachmentScheme::new(42)?,
        vec![Word::from([9, 8, 7, 6u32]), Word::from([5, 4, 3, 2u32])],
    )?;
    let attachment_1 =
        NoteAttachment::with_word(NoteAttachmentScheme::new(43)?, Word::from([1, 2, 3, 4u32]));
    let attachment_2 = NoteAttachment::with_words(
        NoteAttachmentScheme::new(44)?,
        vec![Word::from([10, 11, 12, 13u32])],
    )?;
    let attachment_3 =
        NoteAttachment::with_word(NoteAttachmentScheme::new(45)?, Word::from([20, 21, 22, 23u32]));
    let attachments =
        NoteAttachments::new(vec![attachment_0, attachment_1, attachment_2, attachment_3])?;
    assert_eq!(
        attachments.num_attachments() as usize,
        NoteAttachments::MAX_COUNT,
        "test should use max num of attachments"
    );

    let p2id_note: Note = P2idNote::builder()
        .sender(sender_basic_wallet_account.id())
        .target(sender_basic_wallet_account.id())
        .asset(sent_asset0)
        .asset(sent_asset2)
        .attachments(attachments.iter().cloned())
        .note_type(NoteType::Public)
        .generate_serial_number(&mut rng)
        .build()?
        .into();
    let partial_note = PartialNote::from(p2id_note.clone());

    let expiration_delta = NonZeroU16::new(10).expect("10 is non-zero");
    let send_note_transaction_script = SendNotesTransactionScript::with_expiration_delta(
        &sender_basic_wallet_account.code_interface(),
        slice::from_ref(&partial_note),
        expiration_delta,
    )?;

    let executed_transaction = mock_chain
        .build_transaction(sender_basic_wallet_account.id())
        .authenticated_input_note(spawn_note.id())
        .send_notes_script(&send_note_transaction_script)
        .expected_output_note(RawOutputNote::Full(p2id_note.clone()))
        .build()?
        .execute()
        .await?;

    // Assert that the non-fungible asset was removed
    let vault_patch = executed_transaction.account_patch().vault();
    assert_eq!(
        vault_patch.removed_asset_ids().count(),
        1,
        "the non-fungible asset should have been completely removed"
    );
    assert_eq!(
        vault_patch.removed_asset_ids().next().unwrap(),
        &sent_asset0.id(),
        "the non-fungible asset should have been completely removed"
    );

    // Assert that the fungible asset's value was decremented
    assert_eq!(
        vault_patch.updated_assets().count(),
        1,
        "the fungible asset should have been updated"
    );
    // Expected value is total - (sent_asset1 + sent_asset2).
    let expected_removed = sent_asset1.unwrap_fungible().add(sent_asset2.unwrap_fungible())?;
    let expected_asset_value = total_asset.unwrap_fungible().sub(expected_removed)?.into();
    assert_eq!(
        vault_patch.updated_assets().next().unwrap(),
        expected_asset_value,
        "fungible asset should have been decremented"
    );

    assert_eq!(
        executed_transaction.output_notes().get_note(0),
        &RawOutputNote::Partial(p2any_note.into())
    );
    assert_eq!(executed_transaction.output_notes().get_note(1), &RawOutputNote::Full(p2id_note));

    assert_eq!(
        executed_transaction.expiration_block_num(),
        executed_transaction.block_header().block_num() + u32::from(expiration_delta.get()),
        "the payload-supplied expiration delta should be applied",
    );

    Ok(())
}

/// Creates a private note with the default note script and an empty asset list.
fn create_assetless_note(sender: miden_protocol::account::AccountId) -> anyhow::Result<Note> {
    let tag = NoteTag::with_account_target(sender);
    let metadata = PartialNoteMetadata::new(sender, NoteType::Private).with_tag(tag);
    let assets = NoteAssets::new(vec![])?;
    let note_script = CodeBuilder::default().compile_note_script(DEFAULT_NOTE_SCRIPT)?;
    let serial_num = RandomCoin::new(Word::from([1, 2, 3, 4u32])).draw_word();
    let recipient = NoteRecipient::new(serial_num, note_script, NoteStorage::default());
    Ok(Note::new(assets, metadata, recipient))
}

/// Tests that a basic wallet can send a note that carries no assets.
///
/// Regression test: the script must return at stack depth 16 even when the per-asset loop never
/// runs (zero-asset note), otherwise the VM rejects the transaction with
/// `InvalidStackDepthOnReturn`.
///
/// [wallet]: miden_standards::account::interface::AccountComponentInterface::BasicWallet
#[tokio::test]
async fn test_send_note_script_basic_wallet_without_assets() -> anyhow::Result<()> {
    let mut builder = MockChain::builder();
    let sender_basic_wallet_account = builder.add_existing_wallet(Auth::BasicAuth {
        auth_scheme: AuthScheme::Falcon512Poseidon2,
    })?;
    let mock_chain = builder.build()?;

    let assetless_note = create_assetless_note(sender_basic_wallet_account.id())?;
    let partial_note = PartialNote::from(assetless_note.clone());

    let send_note_transaction_script = SendNotesTransactionScript::new(
        &sender_basic_wallet_account.code_interface(),
        slice::from_ref(&partial_note),
    )?;

    let executed_transaction = mock_chain
        .build_transaction(sender_basic_wallet_account.id())
        .send_notes_script(&send_note_transaction_script)
        .expected_output_notes(vec![RawOutputNote::Full(assetless_note.clone())])
        .build()?
        .execute()
        .await?;

    assert_eq!(
        executed_transaction.output_notes().get_note(0),
        &RawOutputNote::Full(assetless_note)
    );

    Ok(())
}

/// Tests that the faucet path rejects assetless notes at script-build time.
#[tokio::test]
async fn test_send_note_script_fungible_faucet_without_assets() -> anyhow::Result<()> {
    let mut builder = MockChain::builder();
    let sender_fungible_faucet_account = builder.add_existing_basic_faucet(
        Auth::BasicAuth {
            auth_scheme: AuthScheme::Falcon512Poseidon2,
        },
        "POL",
        200,
        None,
    )?;
    builder.build()?;

    let assetless_note = create_assetless_note(sender_fungible_faucet_account.id())?;
    let partial_note = PartialNote::from(assetless_note);

    let result = SendNotesTransactionScript::new(
        &sender_fungible_faucet_account.code_interface(),
        slice::from_ref(&partial_note),
    );

    assert!(matches!(
        result,
        Err(SendNotesTransactionScriptError::FaucetNoteUnexpectedNumAssets)
    ));

    Ok(())
}

/// Builds the code interface of an account exposing `component`'s procedures, without
/// instantiating an account. Enough for the script builder, which only inspects the interface.
fn code_interface(
    account_id: u128,
    component: &AccountComponentCode,
) -> anyhow::Result<AccountCodeInterface> {
    let account_id = AccountId::try_from(account_id)?;
    Ok(AccountCodeInterface::new(account_id, component.procedure_roots().collect())?)
}

/// Tests that the wallet path rejects notes whose sender is not the sending account at
/// script-build time.
#[test]
fn test_send_note_script_rejects_sender_mismatch() -> anyhow::Result<()> {
    let sender_interface =
        code_interface(ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_IMMUTABLE_CODE, BasicWallet::code())?;
    let other_id = AccountId::try_from(ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_IMMUTABLE_CODE_2)?;

    let foreign_note = create_assetless_note(other_id)?;
    let partial_note = PartialNote::from(foreign_note);

    let result = SendNotesTransactionScript::new(&sender_interface, slice::from_ref(&partial_note));

    assert!(matches!(
        result,
        Err(SendNotesTransactionScriptError::InvalidSenderAccount(sender)) if sender == other_id
    ));

    Ok(())
}

/// Tests that embedding the payload in the script's MAST forest leaves the script root untouched,
/// so a single canonical root covers every set of output notes and stays allowlistable.
#[test]
fn test_send_note_script_root_is_independent_of_payload() -> anyhow::Result<()> {
    let wallet_interface =
        code_interface(ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_IMMUTABLE_CODE, BasicWallet::code())?;
    let note = create_assetless_note(wallet_interface.id())?;

    let script = SendNotesTransactionScript::new(&wallet_interface, &[note.clone().into()])?;
    assert!(
        matches!(script, SendNotesTransactionScript::Wallet(_)),
        "a wallet interface should select the wallet script"
    );
    assert_eq!(
        script.tx_script().root(),
        SendWalletNotesTransactionScript::script_root(),
        "the embedded payload must not change the script root"
    );
    assert!(
        SendNotesTransactionScript::script_roots()
            .contains(&SendWalletNotesTransactionScript::script_root()),
        "the canonical roots should include the wallet script"
    );

    // A different payload must still produce the same root.
    let other = SendNotesTransactionScript::with_expiration_delta(
        &wallet_interface,
        &[note.into()],
        NonZeroU16::new(10).expect("10 is non-zero"),
    )?;
    assert_eq!(script.tx_script().root(), other.tx_script().root());
    assert_ne!(
        script.tx_script_args(),
        other.tx_script_args(),
        "a different payload should still change the script arguments"
    );

    Ok(())
}

/// Tests the execution of the `send_notes` script in case the sending account has the
/// [`NonFungibleFaucet`] interface, which mints the note's asset from its commitment alone.
#[tokio::test]
async fn test_send_note_script_non_fungible_faucet() -> anyhow::Result<()> {
    let mut builder = MockChain::builder();
    let faucet_account = builder.add_existing_non_fungible_faucet(Auth::IncrNonce, "NFT")?;
    let mock_chain = builder.build()?;

    let commitment =
        NonFungibleFaucet::compute_asset_commitment(b"token #1", Word::from([7, 8, 9, 10u32]));
    let asset = Asset::NonFungible(NonFungibleAsset::from_parts(faucet_account.id(), commitment));

    let mut rng = RandomCoin::new(Word::from([1, 2, 3, 4u32]));
    let note = create_p2any_note(faucet_account.id(), NoteType::Public, [asset], &mut rng);

    let script =
        SendNotesTransactionScript::new(&faucet_account.code_interface(), &[note.clone().into()])?;
    assert!(
        matches!(script, SendNotesTransactionScript::NonFungible(_)),
        "a non-fungible faucet interface should select the non-fungible script"
    );
    assert_eq!(
        script.tx_script().root(),
        SendNonFungibleFaucetNotesTransactionScript::script_root()
    );

    let executed_transaction = mock_chain
        .build_transaction(faucet_account.id())
        .send_notes_script(&script)
        .expected_output_note(RawOutputNote::Full(note.clone()))
        .build()?
        .execute()
        .await?;

    assert_eq!(executed_transaction.output_notes().num_notes(), 1);
    assert_eq!(executed_transaction.output_notes().get_note(0), &RawOutputNote::Full(note));

    Ok(())
}

/// Tests that each dedicated script type rejects an interface it does not apply to, so building
/// one directly is as safe as letting [`SendNotesTransactionScript`] dispatch, and that the
/// dispatch picks the variant matching the interface.
#[test]
fn test_dedicated_send_notes_scripts_validate_their_interface() -> anyhow::Result<()> {
    let wallet_interface =
        code_interface(ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_IMMUTABLE_CODE, BasicWallet::code())?;
    let faucet_interface =
        code_interface(ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET_1, FungibleFaucet::code())?;

    // The faucet script needs a faucet interface.
    let wallet_note = create_assetless_note(wallet_interface.id())?;
    assert!(matches!(
        SendFungibleFaucetNotesTransactionScript::new(&wallet_interface, &[wallet_note.into()]),
        Err(SendNotesTransactionScriptError::UnsupportedAccountInterface)
    ));

    // The wallet script needs the wallet procedures.
    let own_asset = Asset::Fungible(FungibleAsset::new(faucet_interface.id(), 10)?);
    let mut rng = RandomCoin::new(Word::from([1, 2, 3, 4u32]));
    let faucet_note =
        create_p2any_note(faucet_interface.id(), NoteType::Private, [own_asset], &mut rng);
    assert!(matches!(
        SendWalletNotesTransactionScript::new(&faucet_interface, &[faucet_note.clone().into()]),
        Err(SendNotesTransactionScriptError::UnsupportedAccountInterface)
    ));

    // A faucet interface dispatches to the fungible faucet script.
    let dispatched = SendNotesTransactionScript::new(&faucet_interface, &[faucet_note.into()])?;
    assert!(matches!(dispatched, SendNotesTransactionScript::Fungible(_)));
    assert_eq!(
        dispatched.tx_script().root(),
        SendFungibleFaucetNotesTransactionScript::script_root()
    );

    Ok(())
}

/// Tests that the faucet path rejects notes carrying an asset issued by a different faucet at
/// script-build time.
#[test]
fn test_send_note_script_rejects_foreign_faucet_asset() -> anyhow::Result<()> {
    // The mock asset is issued by `ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET`, so the sending faucet must
    // be a different one for the asset to count as foreign.
    let faucet_interface =
        code_interface(ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET_1, FungibleFaucet::code())?;
    let foreign_asset = FungibleAsset::mock(10);
    let mut rng = RandomCoin::new(Word::from([1, 2, 3, 4u32]));
    let note =
        create_p2any_note(faucet_interface.id(), NoteType::Private, [foreign_asset], &mut rng);
    let partial_note = PartialNote::from(note);

    let result = SendNotesTransactionScript::new(&faucet_interface, slice::from_ref(&partial_note));

    assert!(matches!(
        result,
        Err(SendNotesTransactionScriptError::IssuanceFaucetMismatch(faucet_id))
            if faucet_id == foreign_asset.faucet_id()
    ));

    Ok(())
}

/// Tests that an account exposing both faucet interfaces builds the script matching the
/// composition of the assets it is sending, since either faucet could mint them.
#[test]
fn test_send_note_script_dual_faucet_dispatches_on_asset_composition() -> anyhow::Result<()> {
    let account_id = AccountId::try_from(ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET_1)?;
    let interface = AccountCodeInterface::new(
        account_id,
        FungibleFaucet::code()
            .procedure_roots()
            .chain(NonFungibleFaucet::code().procedure_roots())
            .collect(),
    )?;
    let mut rng = RandomCoin::new(Word::from([1, 2, 3, 4u32]));

    let non_fungible =
        Asset::NonFungible(NonFungibleAsset::from_parts(account_id, Word::from([5, 6, 7, 8u32])));
    let non_fungible_note =
        create_p2any_note(account_id, NoteType::Private, [non_fungible], &mut rng);
    assert!(matches!(
        SendNotesTransactionScript::new(&interface, &[non_fungible_note.into()])?,
        SendNotesTransactionScript::NonFungible(_)
    ));

    let fungible = Asset::Fungible(FungibleAsset::new(account_id, 10)?);
    let fungible_note = create_p2any_note(account_id, NoteType::Private, [fungible], &mut rng);
    assert!(matches!(
        SendNotesTransactionScript::new(&interface, &[fungible_note.into()])?,
        SendNotesTransactionScript::Fungible(_)
    ));

    Ok(())
}

/// Tests the execution of the generated send_note transaction script in case the sending account
/// has the [`FungibleFaucet`][faucet] interface.
///
/// [faucet]: miden_standards::account::interface::AccountComponentInterface::FungibleFaucet
#[tokio::test]
async fn test_send_note_script_fungible_faucet() -> anyhow::Result<()> {
    let mut builder = MockChain::builder();
    let sender_fungible_faucet_account = builder.add_existing_basic_faucet(
        Auth::BasicAuth {
            auth_scheme: AuthScheme::Falcon512Poseidon2,
        },
        "POL",
        200,
        None,
    )?;
    let mock_chain = builder.build()?;

    let tag = NoteTag::with_account_target(sender_fungible_faucet_account.id());
    let attachment = NoteAttachment::with_word(NoteAttachmentScheme::new(100)?, Word::empty());
    let metadata = PartialNoteMetadata::new(sender_fungible_faucet_account.id(), NoteType::Public)
        .with_tag(tag);
    let assets = NoteAssets::new(vec![Asset::Fungible(
        FungibleAsset::new(sender_fungible_faucet_account.id(), 10).unwrap(),
    )])?;
    let note_script = CodeBuilder::default().compile_note_script(DEFAULT_NOTE_SCRIPT).unwrap();
    let serial_num = RandomCoin::new(Word::from([1, 2, 3, 4u32])).draw_word();
    let recipient = NoteRecipient::new(serial_num, note_script, NoteStorage::default());
    let attachments = NoteAttachments::from(attachment);

    let note = Note::with_attachments(assets.clone(), metadata, recipient, attachments);
    let partial_note: PartialNote = note.clone().into();

    let expiration_delta = NonZeroU16::new(10).expect("10 is non-zero");
    let send_note_transaction_script = SendNotesTransactionScript::with_expiration_delta(
        &sender_fungible_faucet_account.code_interface(),
        slice::from_ref(&partial_note),
        expiration_delta,
    )?;

    let executed_transaction = mock_chain
        .build_transaction(sender_fungible_faucet_account.id())
        .send_notes_script(&send_note_transaction_script)
        .expected_output_note(RawOutputNote::Full(note.clone()))
        .build()?
        .execute()
        .await?;

    assert_eq!(executed_transaction.output_notes().get_note(0), &RawOutputNote::Full(note));

    assert_eq!(
        executed_transaction.expiration_block_num(),
        executed_transaction.block_header().block_num() + u32::from(expiration_delta.get()),
        "the payload-supplied expiration delta should be applied",
    );

    Ok(())
}

#[tokio::test]
async fn test_send_note_script_multiple_notes_basic_wallet() -> anyhow::Result<()> {
    let total_asset = FungibleAsset::mock(100);
    let non_fungible_asset = NonFungibleAsset::mock(&[7, 8, 9]);

    let mut builder = MockChain::builder();
    let sender_account = builder.add_existing_wallet_with_assets(
        Auth::BasicAuth {
            auth_scheme: AuthScheme::Falcon512Poseidon2,
        },
        [total_asset, non_fungible_asset],
    )?;
    let mock_chain = builder.build()?;

    let mut rng = RandomCoin::new(Word::from([1, 2, 3, 4u32]));

    // Note A: two assets and two attachments.
    let attachment_0 =
        NoteAttachment::with_word(NoteAttachmentScheme::new(7)?, Word::from([1, 2, 3, 4u32]));
    let attachment_1 = NoteAttachment::with_words(
        NoteAttachmentScheme::new(8)?,
        vec![Word::from([5, 6, 7, 8u32]), Word::from([9, 10, 11, 12u32])],
    )?;
    let note_a: Note = P2idNote::builder()
        .sender(sender_account.id())
        .target(sender_account.id())
        .asset(FungibleAsset::mock(10))
        .asset(non_fungible_asset)
        .attachments([attachment_0, attachment_1])
        .note_type(NoteType::Public)
        .generate_serial_number(&mut rng)
        .build()?
        .into();

    // Note B: one asset, no attachments.
    let note_b: Note = P2idNote::builder()
        .sender(sender_account.id())
        .target(sender_account.id())
        .asset(FungibleAsset::mock(40))
        .note_type(NoteType::Public)
        .generate_serial_number(&mut rng)
        .build()?
        .into();

    // Note C: no assets and no attachments (smallest possible record).
    let metadata = PartialNoteMetadata::new(sender_account.id(), NoteType::Public)
        .with_tag(NoteTag::with_account_target(sender_account.id()));
    let note_script = CodeBuilder::default().compile_note_script(DEFAULT_NOTE_SCRIPT).unwrap();
    let recipient = NoteRecipient::new(rng.draw_word(), note_script, NoteStorage::default());
    let note_c = Note::new(NoteAssets::default(), metadata, recipient);

    let notes: Vec<PartialNote> =
        vec![note_a.clone().into(), note_b.clone().into(), note_c.clone().into()];
    let script = SendNotesTransactionScript::new(&sender_account.code_interface(), &notes)?;

    let executed_transaction = mock_chain
        .build_transaction(sender_account.id())
        .send_notes_script(&script)
        .expected_output_notes(vec![
            RawOutputNote::Full(note_a.clone()),
            RawOutputNote::Full(note_b.clone()),
            RawOutputNote::Full(note_c.clone()),
        ])
        .build()?
        .execute()
        .await?;

    assert_eq!(executed_transaction.output_notes().num_notes(), 3);
    assert_eq!(executed_transaction.output_notes().get_note(0), &RawOutputNote::Full(note_a));
    assert_eq!(executed_transaction.output_notes().get_note(1), &RawOutputNote::Full(note_b));
    assert_eq!(executed_transaction.output_notes().get_note(2), &RawOutputNote::Full(note_c));

    // Both of note A's assets must have left the vault: the non-fungible one entirely, and the
    // fungible one decremented by note A's and note B's amounts.
    let vault_patch = executed_transaction.account_patch().vault();
    assert_eq!(
        vault_patch.removed_asset_ids().collect::<Vec<_>>(),
        vec![&non_fungible_asset.id()],
        "the non-fungible asset should have been completely removed"
    );

    let expected_removed = FungibleAsset::mock(10)
        .unwrap_fungible()
        .add(FungibleAsset::mock(40).unwrap_fungible())?;
    let expected_asset_value = total_asset.unwrap_fungible().sub(expected_removed)?.into();
    assert_eq!(
        vault_patch.updated_assets().collect::<Vec<_>>(),
        vec![expected_asset_value],
        "the fungible asset should have been decremented by both notes' amounts"
    );

    Ok(())
}

/// Builds a faucet account and the genuine `send_notes` payload for a single one-asset note,
/// returning the chain, the account, the script, and the payload elements.
async fn faucet_with_single_asset_payload()
-> anyhow::Result<(MockChain, Account, SendNotesTransactionScript, Vec<Felt>)> {
    let mut builder = MockChain::builder();
    let faucet_account = builder.add_existing_basic_faucet(
        Auth::BasicAuth {
            auth_scheme: AuthScheme::Falcon512Poseidon2,
        },
        "POL",
        200,
        None,
    )?;
    let mock_chain = builder.build()?;

    let mut rng = RandomCoin::new(Word::from([1, 2, 3, 4u32]));
    let own_asset = Asset::Fungible(FungibleAsset::new(faucet_account.id(), 10)?);
    let note = create_p2any_note(faucet_account.id(), NoteType::Public, [own_asset], &mut rng);

    let script = SendNotesTransactionScript::new(&faucet_account.code_interface(), &[note.into()])?;

    // The genuine payload is read back out of the script's embedded advice map.
    let payload = script
        .tx_script()
        .mast()
        .advice_map()
        .get(&script.tx_script_args())
        .expect("the script should embed its payload")
        .to_vec();

    Ok((mock_chain, faucet_account, script, payload))
}

/// Executes `payload` against `faucet_account` as a handcrafted `send_notes` payload, recomputing
/// the commitment so it passes the advice validation and reaches the script's own assertions.
async fn execute_with_payload(
    mock_chain: &MockChain,
    faucet_account: &Account,
    script: &SendNotesTransactionScript,
    payload: Vec<Felt>,
) -> anyhow::Result<Result<ExecutedTransaction, TransactionExecutorError>> {
    let tampered_args = Hasher::hash_elements(&payload);
    Ok(mock_chain
        .build_transaction(faucet_account.id())
        .tx_script(script.tx_script().clone())
        .tx_script_args(tampered_args)
        .add_advice_map_entry(tampered_args, payload)
        .build()?
        .execute()
        .await)
}

/// Tests that the faucet script rejects a payload whose note record carries more than one asset,
/// since the faucet mints exactly one asset per note.
#[tokio::test]
async fn test_send_note_script_faucet_rejects_multi_asset_payload() -> anyhow::Result<()> {
    let (mock_chain, faucet_account, script, mut payload) =
        faucet_with_single_asset_payload().await?;

    // Claim two assets and append a second asset's elements, so the record stays well formed and
    // the failure comes from the faucet's own one-asset assertion.
    payload[SendNotesTransactionScript::PAYLOAD_HEADER_NUM_ELEMENTS
        + SendNotesTransactionScript::NOTE_RECORD_NUM_ASSETS_OFFSET] = Felt::from(2u32);
    let first_asset_start = SendNotesTransactionScript::PAYLOAD_HEADER_NUM_ELEMENTS
        + SendNotesTransactionScript::NOTE_RECORD_ITEMS_OFFSET;
    let first_asset: Vec<Felt> = payload
        [first_asset_start..first_asset_start + SendNotesTransactionScript::ITEM_NUM_ELEMENTS]
        .to_vec();
    let insert_at = first_asset_start + SendNotesTransactionScript::ITEM_NUM_ELEMENTS;
    payload.splice(insert_at..insert_at, first_asset);

    let result = execute_with_payload(&mock_chain, &faucet_account, &script, payload).await?;

    assert_transaction_executor_error!(result, ERR_SEND_NOTES_FAUCET_NOTE_REQUIRES_ONE_ASSET);

    Ok(())
}