miden-client 0.17.0-rc.2

Client library that facilitates interaction with the Miden network
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
use std::boxed::Box;
use std::collections::BTreeMap;
use std::env::temp_dir;
use std::fs::OpenOptions;
use std::io::Write;
use std::ops::{Deref, DerefMut};
use std::path::PathBuf;
use std::string::{String, ToString};
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::vec::Vec;

use anyhow::{Context, Result};
use miden_protocol::account::auth::AuthSecretKey;
use miden_protocol::account::{Account, AccountId};
use miden_protocol::asset::{AssetAmount, FungibleAsset, TokenSymbol};
use miden_protocol::note::NoteType;
use miden_protocol::testing::account_id::ACCOUNT_ID_REGULAR_PRIVATE_ACCOUNT_UPDATABLE_CODE;
use miden_protocol::transaction::TransactionId;
use miden_standards::account::auth::{Approver, AuthSingleSig};
use miden_standards::account::faucets::TokenName;
use rand::Rng;
use tracing::{debug, info};
use uuid::Uuid;

use crate::account::component::{
    BasicWallet,
    BurnPolicy,
    FungibleFaucet,
    MintPolicy,
    TokenPolicyManager,
};
use crate::account::{AccountBuilder, AccountBuilderSchemaCommitmentExt, AccountType};
use crate::auth::{AuthSchemeId, ECDSA_K256_KECCAK_SCHEME_ID};
pub use crate::keystore::{FilesystemKeyStore, Keystore};
use crate::note::{Note, NoteConsumability, P2idNote};
use crate::rpc::RpcError;
use crate::store::{InputNoteRecord, NoteFilter, TransactionFilter};
use crate::sync::SyncSummary;
use crate::test_utils::fee::FeeFunder;
use crate::transaction::{
    NoteArgs,
    TransactionRequest,
    TransactionRequestBuilder,
    TransactionRequestError,
    TransactionResult,
    TransactionStatus,
};
use crate::{Client, ClientError};

// TEST CLIENT
// ================================================================================================

/// A [`Client`] wired for the test helpers, carrying the [`FeeFunder`] the account-creating helpers
/// pay deploys from when the chain charges transaction fees.
///
/// Dereferences to the wrapped [`Client`], so it is used exactly like one.
pub struct TestClient {
    client: Client<FilesystemKeyStore>,
    fee_funder: Option<Arc<dyn FeeFunder>>,
    /// Funding notes paid to accounts that have not spent them yet.
    ///
    /// A note's assets reach the vault before the fee is withdrawn, so folding one into an
    /// account's next transaction makes that transaction its deploy as well.
    pending_funding: BTreeMap<AccountId, Note>,
}

impl TestClient {
    /// Wraps `client` with no fee funder, which is all a fee-free chain needs.
    pub fn new(client: Client<FilesystemKeyStore>) -> Self {
        Self {
            client,
            fee_funder: None,
            pending_funding: BTreeMap::new(),
        }
    }

    /// Returns the keystore the client signs with, shared with it through the authenticator.
    pub fn keystore(&self) -> &FilesystemKeyStore {
        self.client
            .authenticator()
            .expect("test clients are always built with a keystore authenticator")
            .as_ref()
    }

    /// Records funding notes, to be folded into each account's next transaction.
    pub(crate) fn stash_funding(&mut self, funded: impl IntoIterator<Item = (AccountId, Note)>) {
        self.pending_funding.extend(funded);
    }

    /// Takes `account_id`'s funding note, opting it out of automatic folding.
    ///
    /// The caller must then consume it somewhere, or the account cannot pay a fee. Use it when a
    /// test needs the funding in a particular transaction — one asserting on what a sync reports,
    /// say.
    pub fn take_funding(&mut self, account_id: AccountId) -> Option<Note> {
        self.pending_funding.remove(&account_id)
    }

    /// Submits a transaction for `account_id`, folding in its funding note when it has one.
    ///
    /// Syncs first. A transaction expires a fixed number of blocks after its reference block, and
    /// the client takes that block from its sync height, so every block produced since the last
    /// sync is spent before proving starts.
    ///
    /// Shadows [`Client::submit_new_transaction`], still reachable through [`Deref`] for callers
    /// that want the unfunded path.
    pub async fn submit_new_transaction(
        &mut self,
        account_id: AccountId,
        transaction_request: TransactionRequest,
    ) -> Result<TransactionId, ClientError> {
        self.sync_state().await?;

        let transaction_request = self.fund_request(account_id, transaction_request);

        Box::pin(self.client.submit_new_transaction(account_id, transaction_request)).await
    }

    /// Executes a transaction for `account_id`, folding in its funding note when it has one.
    ///
    /// Syncs first. Execution fixes the reference block the expiration window is counted from.
    ///
    /// Takes `&mut self` where the wrapped method takes `&self`, since taking the note mutates.
    pub async fn execute_transaction(
        &mut self,
        account_id: AccountId,
        transaction_request: TransactionRequest,
    ) -> Result<TransactionResult, ClientError> {
        self.sync_state().await?;

        let transaction_request = self.fund_request(account_id, transaction_request);

        Box::pin(self.client.execute_transaction(account_id, transaction_request)).await
    }

    /// Returns `transaction_request` with `account_id`'s funding note folded in.
    ///
    /// Only needed for requests not going through [`Self::submit_new_transaction`] — notably a
    /// batch, which borrows the client, so the note must be taken before the batch is created.
    #[must_use]
    pub fn fund_request(
        &mut self,
        account_id: AccountId,
        mut transaction_request: TransactionRequest,
    ) -> TransactionRequest {
        if let Some(note) = self.take_funding(account_id) {
            transaction_request.add_unauthenticated_input_note(note);
        }

        transaction_request
    }

    /// Sets the funder the account-creating helpers draw the native fee asset from.
    #[must_use]
    pub fn with_fee_funder(mut self, fee_funder: Option<Arc<dyn FeeFunder>>) -> Self {
        self.fee_funder = fee_funder;
        self
    }

    /// Returns the fee funder, if one is set.
    pub fn fee_funder(&self) -> Option<&Arc<dyn FeeFunder>> {
        self.fee_funder.as_ref()
    }
}

impl From<Client<FilesystemKeyStore>> for TestClient {
    fn from(client: Client<FilesystemKeyStore>) -> Self {
        Self::new(client)
    }
}

impl Deref for TestClient {
    type Target = Client<FilesystemKeyStore>;

    fn deref(&self) -> &Self::Target {
        &self.client
    }
}

impl DerefMut for TestClient {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.client
    }
}

// ACCOUNT SETUP
// ================================================================================================

/// The standard component set a built test account is made of.
enum StandardComponents {
    /// A [`BasicWallet`].
    Wallet,
    /// A [`FungibleFaucet`] with permissive mint/burn policies, plus a [`BasicWallet`] for its
    /// `receive_asset` procedure, which `FungibleFaucet` does not export, so a P2ID note can fund
    /// the faucet's own minting fees. Minting is unaffected.
    Faucet,
}

/// How [`TestClient::insert_account`] gets the account it inserts.
enum AccountKind {
    /// The account is built from a standard component set.
    Standard {
        components: StandardComponents,
        account_type: AccountType,
        auth_scheme: AuthSchemeId,
    },
    /// The account was built by the caller.
    Prebuilt {
        account: Box<Account>,
        key: AuthSecretKey,
    },
}

/// Configuration for an account inserted through [`TestClient::insert_account`].
pub struct AccountSetup {
    kind: AccountKind,
    funded: bool,
    invitation_code: Option<String>,
}

impl AccountSetup {
    fn standard(components: StandardComponents, account_type: AccountType) -> Self {
        Self {
            kind: AccountKind::Standard {
                components,
                account_type,
                auth_scheme: ECDSA_K256_KECCAK_SCHEME_ID,
            },
            funded: true,
            invitation_code: None,
        }
    }

    /// A basic wallet account.
    pub fn wallet(account_type: AccountType) -> Self {
        Self::standard(StandardComponents::Wallet, account_type)
    }

    /// A fungible faucet account.
    pub fn faucet(account_type: AccountType) -> Self {
        Self::standard(StandardComponents::Faucet, account_type)
    }

    /// An account the caller built, with the key its auth component commits to.
    pub fn prebuilt(account: Account, key: AuthSecretKey) -> Self {
        Self {
            kind: AccountKind::Prebuilt { account: Box::new(account), key },
            funded: true,
            invitation_code: None,
        }
    }

    /// Signs with `auth_scheme` instead of the default [`ECDSA_K256_KECCAK_SCHEME_ID`].
    #[must_use]
    pub fn auth_scheme(mut self, auth_scheme: AuthSchemeId) -> Self {
        if let AccountKind::Standard { auth_scheme: scheme, .. } = &mut self.kind {
            *scheme = auth_scheme;
        }
        self
    }

    /// Skips funding the account on insertion.
    #[must_use]
    pub fn unfunded(mut self) -> Self {
        self.funded = false;
        self
    }

    /// Registers the account on the network allowlist with `invitation_code` when it is inserted.
    #[must_use]
    pub fn invitation_code(mut self, invitation_code: &str) -> Self {
        self.invitation_code = Some(invitation_code.to_string());
        self
    }
}

/// Creates a key pair for `auth_scheme`, and the authentication component that commits to it.
pub fn auth_component(auth_scheme: AuthSchemeId) -> Result<(AuthSingleSig, AuthSecretKey)> {
    let key_pair = match auth_scheme {
        AuthSchemeId::Falcon512Poseidon2 => AuthSecretKey::new_falcon512_poseidon2(),
        AuthSchemeId::EcdsaK256Keccak => AuthSecretKey::new_ecdsa_k256_keccak(),
        other => anyhow::bail!("unsupported auth scheme: {}", other.as_u8()),
    };
    let component =
        AuthSingleSig::new(Approver::new(key_pair.public_key().to_commitment(), auth_scheme));

    Ok((component, key_pair))
}

/// Creates the fungible faucet component the test faucets are built from, with the token policies
/// that go with it.
pub fn fungible_faucet_component() -> Result<(FungibleFaucet, TokenPolicyManager)> {
    let symbol = TokenSymbol::new("TEST").expect("TEST is a valid token symbol");
    let name = TokenName::new(&symbol.to_string()).expect("token symbol is a valid token name");
    let max_supply = 9_999_999_u64;
    let faucet = FungibleFaucet::builder()
        .name(name)
        .symbol(symbol)
        .decimals(10)
        .max_supply(AssetAmount::new(max_supply).expect("max supply is a valid amount"))
        .build()
        .context("failed to build the fungible faucet component")?;

    let policy_manager = TokenPolicyManager::builder()
        .active_mint_policy(MintPolicy::allow_all())
        .active_burn_policy(BurnPolicy::allow_all())
        .build();

    Ok((faucet, policy_manager))
}

impl TestClient {
    /// Inserts the account described by `setup` into the client and adds its key to the keystore.
    /// Unless [`AccountSetup::unfunded`] was set, the account is also funded so its first
    /// transaction can pay its own fee and double as its deploy.
    pub async fn insert_account(
        &mut self,
        setup: AccountSetup,
    ) -> Result<(Account, AuthSecretKey)> {
        let (account, key_pair) = match setup.kind {
            AccountKind::Prebuilt { account, key } => (*account, key),
            AccountKind::Standard { components, account_type, auth_scheme } => {
                let (auth, key_pair) = auth_component(auth_scheme)?;

                let mut init_seed = [0u8; 32];
                self.rng().fill_bytes(&mut init_seed);

                let mut builder =
                    AccountBuilder::new(init_seed).account_type(account_type).with_component(auth);

                match components {
                    StandardComponents::Wallet => {
                        builder = builder.with_component(BasicWallet);
                    },
                    StandardComponents::Faucet => {
                        let (faucet, policy_manager) = fungible_faucet_component()?;
                        builder = builder
                            .with_component(faucet)
                            .with_component(BasicWallet)
                            .with_components(policy_manager);
                    },
                }

                let account = builder
                    .build_with_schema_commitment()
                    .context("failed to build the test account")?;

                (account, key_pair)
            },
        };

        self.keystore()
            .add_key(&key_pair, account.id())
            .await
            .context("failed to add the account key to the keystore")?;

        self.add_account(&account, false).await?;
        if let Some(invitation_code) = setup.invitation_code.as_deref() {
            self.register_account(account.id(), invitation_code).await?;
        }

        info!(
            account_id = %account.id(),
            account_type = ?account.id().account_type(),
            "Inserted account"
        );

        if setup.funded {
            self.fund_if_needed(&[account.id()]).await?;
        }

        Ok((account, key_pair))
    }

    /// Inserts a new funded wallet account, signing with the default auth scheme.
    pub async fn insert_wallet(&mut self, account_type: AccountType) -> Result<Account> {
        let (account, _) = self.insert_account(AccountSetup::wallet(account_type)).await?;
        Ok(account)
    }

    /// Inserts a new funded fungible faucet account, signing with the default auth scheme.
    pub async fn insert_faucet(&mut self, account_type: AccountType) -> Result<Account> {
        let (account, _) = self.insert_account(AccountSetup::faucet(account_type)).await?;
        Ok(account)
    }

    /// Sets up a wallet account and a faucet account (in that order).
    pub async fn setup_wallet_and_faucet(
        &mut self,
        account_type: AccountType,
    ) -> Result<(Account, Account)> {
        let (faucet_account, _) = self
            .insert_account(AccountSetup::faucet(account_type).unfunded())
            .await
            .context("failed to insert new fungible faucet account")?;

        let (basic_account, _) = self
            .insert_account(AccountSetup::wallet(account_type).unfunded())
            .await
            .context("failed to insert new wallet account")?;

        self.fund_if_needed(&[faucet_account.id(), basic_account.id()])
            .await
            .context("failed to fund and deploy the created accounts")?;

        Ok((basic_account, faucet_account))
    }

    /// Sets up two wallet accounts and a faucet account (in that order), on a client that has to be
    /// in a clean state.
    pub async fn setup_two_wallets_and_faucet(
        &mut self,
        account_type: AccountType,
    ) -> Result<(Account, Account, Account)> {
        // Ensure clean state
        let account_headers = self
            .get_account_headers()
            .await
            .with_context(|| "failed to get account headers")?;
        anyhow::ensure!(
            account_headers.is_empty(),
            "Expected empty account headers for clean state"
        );

        let transactions = self
            .get_transactions(TransactionFilter::All)
            .await
            .with_context(|| "failed to get transactions")?;
        anyhow::ensure!(transactions.is_empty(), "Expected empty transactions for clean state");

        let input_notes = self
            .get_input_notes(NoteFilter::All)
            .await
            .with_context(|| "failed to get input notes")?;
        anyhow::ensure!(input_notes.is_empty(), "Expected empty input notes for clean state");

        let (faucet_account, _) = self
            .insert_account(AccountSetup::faucet(account_type).unfunded())
            .await
            .context("failed to insert new fungible faucet account")?;

        let (first_basic_account, _) = self
            .insert_account(AccountSetup::wallet(account_type).unfunded())
            .await
            .context("failed to insert first basic wallet account")?;

        let (second_basic_account, _) = self
            .insert_account(AccountSetup::wallet(account_type).unfunded())
            .await
            .context("failed to insert second basic wallet account")?;

        self.fund_if_needed(&[
            faucet_account.id(),
            first_basic_account.id(),
            second_basic_account.id(),
        ])
        .await
        .context("failed to fund and deploy the created accounts")?;

        info!(
            faucet_id = %faucet_account.id(),
            wallet_1_id = %first_basic_account.id(),
            wallet_2_id = %second_basic_account.id(),
            "Setup complete, syncing state"
        );
        self.sync_state().await.with_context(|| "failed to sync client state")?;

        Ok((first_basic_account, second_basic_account, faucet_account))
    }
}

// TRANSACTION HELPERS
// ================================================================================================

impl TestClient {
    /// Executes a transaction and asserts that it fails with the expected error.
    pub async fn execute_failing_tx(
        &mut self,
        account_id: AccountId,
        tx_request: TransactionRequest,
        expected_error: ClientError,
    ) {
        info!(account_id = %account_id, "Executing transaction (expecting failure)");
        // We compare string since we can't compare the error directly
        assert_eq!(
            self.submit_new_transaction(account_id, tx_request)
                .await
                .unwrap_err()
                .to_string(),
            expected_error.to_string()
        );
    }

    /// Executes a transaction and waits for it to be committed.
    pub async fn execute_tx_and_sync(
        &mut self,
        account_id: AccountId,
        tx_request: TransactionRequest,
    ) -> Result<()> {
        let transaction_id = self.submit_new_transaction(account_id, tx_request).await?;
        info!(tx_id = %transaction_id, account_id = %account_id, "Transaction submitted, waiting for commit");
        self.wait_for_tx(transaction_id).await?;
        Ok(())
    }

    /// Syncs the client and waits for the transaction to be committed.
    pub async fn wait_for_tx(&mut self, transaction_id: TransactionId) -> Result<()> {
        // wait until tx is committed
        let now = Instant::now();
        debug!(tx_id = %transaction_id, "Waiting for transaction to be committed");
        loop {
            self.sync_state()
                .await
                .with_context(|| "failed to sync client state while waiting for transaction")?;

            // Check if executed transaction got committed by the node
            let tracked_transaction = self
                .get_transactions(TransactionFilter::Ids(vec![transaction_id]))
                .await
                .with_context(|| format!("failed to get transaction with ID: {transaction_id}"))?
                .pop()
                .with_context(|| format!("transaction with ID {transaction_id} not found"))?;

            match tracked_transaction.status {
                TransactionStatus::Committed { block_number, .. } => {
                    info!(tx_id = %transaction_id, %block_number, "Transaction committed");
                    break;
                },
                TransactionStatus::Pending => {
                    // Cooldown between polling iterations to reduce pressure on the node's rate
                    // limiter when many integration tests poll concurrently.
                    tokio::time::sleep(Duration::from_millis(500)).await;
                },
                TransactionStatus::Discarded(cause) => {
                    anyhow::bail!("transaction was discarded with cause: {cause:?}");
                },
            }

            // Log wait time in a file if the env var is set This allows us to aggregate and measure
            // how long the tests are waiting for transactions to be committed
            if std::env::var("LOG_WAIT_TIMES") == Ok("true".to_string()) {
                let elapsed = now.elapsed();
                let wait_times_dir = std::path::PathBuf::from("wait_times");
                std::fs::create_dir_all(&wait_times_dir)
                    .with_context(|| "failed to create wait_times directory")?;

                let elapsed_time_file =
                    wait_times_dir.join(format!("wait_time_{}", Uuid::new_v4()));
                let mut file = OpenOptions::new()
                    .create(true)
                    .write(true)
                    .truncate(true)
                    .open(elapsed_time_file)
                    .with_context(|| "failed to create elapsed time file")?;
                writeln!(file, "{:?}", elapsed.as_millis())
                    .with_context(|| "failed to write elapsed time to file")?;
            }
        }
        Ok(())
    }

    /// Syncs until `amount_of_blocks` have been created onchain compared to client's sync height.
    pub async fn wait_for_blocks(&mut self, amount_of_blocks: u32) -> Result<SyncSummary> {
        let current_block = self.get_sync_height().await?;
        let final_block = current_block + amount_of_blocks;
        debug!(current_block = %current_block, target_block = %final_block, "Waiting for blocks");
        loop {
            let summary = self.sync_state().await?;
            debug!(sync_height = %summary.block_num, target_block = %final_block, "Synced");

            if summary.block_num >= final_block {
                return Ok(summary);
            }

            tokio::time::sleep(Duration::from_secs(3)).await;
        }
    }

    /// Idles until `amount_of_blocks` have been created onchain compared to client's sync height
    /// without advancing the client's sync height.
    pub async fn wait_for_blocks_no_sync(&mut self, amount_of_blocks: u32) -> Result<()> {
        let current_block = self.get_sync_height().await?;
        let final_block = current_block + amount_of_blocks;
        debug!(current_block = %current_block, target_block = %final_block, "Waiting for blocks (no sync)");
        loop {
            let (latest_block, _) =
                self.test_rpc_api().get_block_header_by_number(None, false).await?;
            debug!(
                chain_tip = %latest_block.block_num(),
                target_block = %final_block,
                "Waiting for blocks (no sync)"
            );

            if latest_block.block_num() >= final_block {
                return Ok(());
            }

            tokio::time::sleep(Duration::from_secs(3)).await;
        }
    }

    /// Syncs repeatedly until the given account has at least one consumable note, or until
    /// `max_blocks` have elapsed since the call. Returns the list of consumable notes once found.
    pub async fn wait_for_consumable_notes(
        &mut self,
        account_id: AccountId,
        max_blocks: u32,
    ) -> Result<Vec<(InputNoteRecord, Vec<NoteConsumability>)>> {
        let start_block = self.get_sync_height().await?;
        let deadline_block = start_block + max_blocks;
        debug!(
            %account_id,
            %start_block,
            %deadline_block,
            "Waiting for consumable notes"
        );

        loop {
            self.sync_state().await?;
            let notes = self.get_consumable_notes(Some(account_id)).await?;
            if !notes.is_empty() {
                let current_block = self.get_sync_height().await?;
                debug!(
                    %account_id,
                    count = notes.len(),
                    %current_block,
                    "Found consumable notes"
                );
                return Ok(notes);
            }

            let current_block = self.get_sync_height().await?;
            assert!(
                current_block < deadline_block,
                "account {account_id} has no consumable notes after waiting {max_blocks} blocks \
                 (from block {start_block} to {current_block})"
            );

            debug!(
                %account_id,
                %current_block,
                %deadline_block,
                "No consumable notes yet, waiting..."
            );
            std::thread::sleep(Duration::from_secs(3));
        }
    }

    /// Waits for node to be running.
    pub async fn wait_for_node(&mut self) {
        const NODE_TIME_BETWEEN_ATTEMPTS: u64 = 2;
        const NUMBER_OF_NODE_ATTEMPTS: u64 = 60;
        info!(
            "Waiting for node to be up (checking every {NODE_TIME_BETWEEN_ATTEMPTS}s, max {NUMBER_OF_NODE_ATTEMPTS} tries)"
        );
        for _try_number in 0..NUMBER_OF_NODE_ATTEMPTS {
            match self.sync_state().await {
                Err(ClientError::RpcError(
                    RpcError::ConnectionError(_) | RpcError::RequestError { .. },
                )) => {
                    tokio::time::sleep(Duration::from_secs(NODE_TIME_BETWEEN_ATTEMPTS)).await;
                },
                Err(other_error) => {
                    panic!("Unexpected error: {other_error}");
                },
                _ => return,
            }
        }

        panic!("Unable to connect to node");
    }

    /// Mints a note from `faucet_account_id` for `basic_account_id` and returns the executed
    /// transaction ID and the note with [`MINT_AMOUNT`] units of the corresponding fungible asset.
    pub async fn mint_note(
        &mut self,
        basic_account_id: AccountId,
        faucet_account_id: AccountId,
        note_type: NoteType,
    ) -> Result<(TransactionId, Note)> {
        // Create a Mint Tx for MINT_AMOUNT units of our fungible asset
        let fungible_asset = FungibleAsset::new(faucet_account_id, MINT_AMOUNT)?;
        info!(faucet_id = %faucet_account_id, target_id = %basic_account_id, amount = MINT_AMOUNT, "Minting asset");
        let tx_request = TransactionRequestBuilder::new().build_mint_fungible_asset(
            fungible_asset,
            basic_account_id,
            note_type,
            self.rng(),
        )?;
        let tx_id = self
            .submit_new_transaction(fungible_asset.faucet_id(), tx_request.clone())
            .await?;

        let note = tx_request
            .expected_output_own_notes()
            .pop()
            .context("the mint request should produce one output note")?;
        info!(tx_id = %tx_id, note_id = %note.id(), "Mint transaction submitted");
        Ok((tx_id, note))
    }

    /// Executes a transaction that consumes the provided notes and returns the transaction ID. This
    /// assumes the notes contain assets.
    pub async fn consume_notes(
        &mut self,
        account_id: AccountId,
        input_notes: &[Note],
    ) -> Result<TransactionId> {
        let note_ids: Vec<_> = input_notes.iter().map(|n| n.id().to_string()).collect();
        info!(account_id = %account_id, note_ids = %note_ids.join(", "), "Consuming notes");
        let tx_request =
            TransactionRequestBuilder::new().build_consume_notes(input_notes.to_vec())?;
        let tx_id = self.submit_new_transaction(account_id, tx_request).await?;
        info!(tx_id = %tx_id, "Consume transaction submitted");
        Ok(tx_id)
    }

    /// Executes a transaction and consumes the resulting unauthenticated notes immediately without
    /// waiting for the first transaction to be committed.
    pub async fn execute_tx_and_consume_output_notes(
        &mut self,
        tx_request: TransactionRequest,
        executor: AccountId,
        consumer: AccountId,
    ) -> Result<TransactionId> {
        let output_notes = tx_request
            .expected_output_own_notes()
            .into_iter()
            .map(|note| (note, None::<NoteArgs>))
            .collect::<Vec<(Note, Option<NoteArgs>)>>();

        self.submit_new_transaction(executor, tx_request).await?;

        let tx_request = TransactionRequestBuilder::new().input_notes(output_notes).build()?;
        Ok(self.submit_new_transaction(consumer, tx_request).await?)
    }

    /// Mints assets for the target account and consumes them immediately without waiting for the
    /// first transaction to be committed.
    pub async fn mint_and_consume(
        &mut self,
        basic_account_id: AccountId,
        faucet_account_id: AccountId,
        note_type: NoteType,
    ) -> Result<TransactionId> {
        info!(
            faucet_id = %faucet_account_id,
            target_id = %basic_account_id,
            amount = MINT_AMOUNT,
            "Minting and consuming asset"
        );
        let tx_request = TransactionRequestBuilder::new().build_mint_fungible_asset(
            FungibleAsset::new(faucet_account_id, MINT_AMOUNT)?,
            basic_account_id,
            note_type,
            self.rng(),
        )?;

        let tx_id = self
            .execute_tx_and_consume_output_notes(tx_request, faucet_account_id, basic_account_id)
            .await?;
        info!(tx_id = %tx_id, "Mint-and-consume transaction submitted");
        Ok(tx_id)
    }

    /// Creates a transaction request that mints assets for each `target_id` account.
    pub fn mint_multiple_fungible_asset(
        &mut self,
        asset: FungibleAsset,
        target_id: &[AccountId],
        note_type: NoteType,
    ) -> Result<TransactionRequest> {
        let rng = self.rng();
        let notes = target_id
            .iter()
            .map(|account_id| {
                Ok(P2idNote::builder()
                    .sender(asset.faucet_id())
                    .target(*account_id)
                    .asset(asset)
                    .note_type(note_type)
                    .generate_serial_number(rng)
                    .build()
                    .context("note creation failed")?
                    .into())
            })
            .collect::<Result<Vec<Note>>>()?;

        Ok(TransactionRequestBuilder::new().own_output_notes(notes).build()?)
    }
}

// ASSERTION HELPERS
// ================================================================================================

impl TestClient {
    /// Asserts that the account has a single asset with the expected amount.
    pub async fn assert_account_has_single_asset(
        &self,
        account_id: AccountId,
        faucet_id: AccountId,
        expected_amount: u64,
    ) {
        let balance = self
            .account_reader(account_id)
            .get_balance(faucet_id)
            .await
            .expect("Account should have the asset");
        assert_eq!(balance, AssetAmount::new(expected_amount).unwrap());
    }

    /// Tries to consume the note and asserts that the expected error is returned.
    pub async fn assert_note_cannot_be_consumed_twice(
        &mut self,
        consuming_account_id: AccountId,
        note_to_consume: Note,
    ) {
        // Check that we can't consume the P2ID note again
        info!(note_id = %note_to_consume.id(), account_id = %consuming_account_id, "Attempting double-consume (expecting failure)");

        // Double-spend error expected to be received since we are consuming the same note
        let tx_request = TransactionRequestBuilder::new()
            .build_consume_notes(vec![note_to_consume.clone()])
            .unwrap();

        match self.submit_new_transaction(consuming_account_id, tx_request).await {
            Err(ClientError::TransactionRequestError(
                TransactionRequestError::InputNoteAlreadyConsumed(_),
            )) => {},
            Ok(_) => panic!("Double-spend error: Note should not be consumable!"),
            err => {
                panic!("Unexpected error {:?} for note ID: {}", err, note_to_consume.id().to_hex())
            },
        }
    }
}

// CONSTANTS
// ================================================================================================

pub const ACCOUNT_ID_REGULAR: u128 = ACCOUNT_ID_REGULAR_PRIVATE_ACCOUNT_UPDATABLE_CODE;

/// Constant that represents the number of blocks until the p2id can be recalled. If this value is
/// too low, some tests might fail due to expected recall failures not happening.
pub const RECALL_HEIGHT_DELTA: u32 = 50;

pub const MINT_AMOUNT: u64 = 1000;
pub const TRANSFER_AMOUNT: u64 = 59;

// UTILITIES
// ================================================================================================

pub fn create_test_store_path() -> PathBuf {
    let mut temp_file = temp_dir();
    temp_file.push(format!("{}.sqlite3", Uuid::new_v4()));
    temp_file
}