Skip to content

Commit

Permalink
Organize account mock (#514)
Browse files Browse the repository at this point in the history
* mock: mock_account now requires account_id parameter

* mock: mock_account now requires account_code parameter

* mock: all account mock in the same module
  • Loading branch information
hackaugusto authored Mar 8, 2024
1 parent 0be5fc5 commit 5369f6b
Show file tree
Hide file tree
Showing 22 changed files with 250 additions and 219 deletions.
5 changes: 2 additions & 3 deletions miden-lib/src/tests/test_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ use miden_objects::{
};
use mock::{
constants::{
storage_item_0, storage_item_1, CHILD_ROOT_PARENT_LEAF_INDEX, CHILD_SMT_DEPTH,
CHILD_STORAGE_INDEX_0, CHILD_STORAGE_VALUE_0,
CHILD_ROOT_PARENT_LEAF_INDEX, CHILD_SMT_DEPTH, CHILD_STORAGE_INDEX_0, CHILD_STORAGE_VALUE_0,
},
mock::{
account::MockAccountType,
account::{storage_item_0, storage_item_1, MockAccountType},
host::MockHost,
notes::AssetPreservationStatus,
transaction::{mock_executed_tx, mock_inputs},
Expand Down
12 changes: 8 additions & 4 deletions miden-lib/src/tests/test_asset.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use mock::{
constants::{
non_fungible_asset, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN, FUNGIBLE_ASSET_AMOUNT, NON_FUNGIBLE_ASSET_DATA,
constants::{non_fungible_asset, FUNGIBLE_ASSET_AMOUNT, NON_FUNGIBLE_ASSET_DATA},
mock::{
account::{
MockAccountType, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN,
},
notes::AssetPreservationStatus,
transaction::mock_inputs,
},
mock::{account::MockAccountType, notes::AssetPreservationStatus, transaction::mock_inputs},
prepare_transaction,
procedures::prepare_word,
run_tx,
Expand Down
12 changes: 8 additions & 4 deletions miden-lib/src/tests/test_asset_vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ use miden_objects::{
assets::{Asset, FungibleAsset, NonFungibleAsset, NonFungibleAssetDetails},
};
use mock::{
constants::{
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN, ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN,
FUNGIBLE_ASSET_AMOUNT, NON_FUNGIBLE_ASSET_DATA,
constants::{FUNGIBLE_ASSET_AMOUNT, NON_FUNGIBLE_ASSET_DATA},
mock::{
account::{
MockAccountType, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN,
},
notes::AssetPreservationStatus,
transaction::mock_inputs,
},
mock::{account::MockAccountType, notes::AssetPreservationStatus, transaction::mock_inputs},
prepare_transaction,
procedures::prepare_word,
run_tx,
Expand Down
14 changes: 10 additions & 4 deletions miden-lib/src/tests/test_faucet.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use miden_objects::assets::FungibleAsset;
use mock::{
constants::{
non_fungible_asset, non_fungible_asset_2, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_1, ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN_1, CONSUMED_ASSET_1_AMOUNT, FUNGIBLE_ASSET_AMOUNT,
non_fungible_asset, non_fungible_asset_2, CONSUMED_ASSET_1_AMOUNT, FUNGIBLE_ASSET_AMOUNT,
FUNGIBLE_FAUCET_INITIAL_BALANCE,
},
mock::{account::MockAccountType, notes::AssetPreservationStatus, transaction::mock_inputs},
mock::{
account::{
MockAccountType, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_1, ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN_1,
},
notes::AssetPreservationStatus,
transaction::mock_inputs,
},
prepare_transaction,
procedures::prepare_word,
run_tx,
Expand Down
3 changes: 1 addition & 2 deletions miden-lib/src/tests/test_prologue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use miden_objects::{
Digest,
};
use mock::{
constants::{generate_account_seed, AccountSeedType},
consumed_note_data_ptr,
mock::{
account::MockAccountType,
account::{generate_account_seed, AccountSeedType, MockAccountType},
host::MockHost,
notes::AssetPreservationStatus,
transaction::{mock_inputs, mock_inputs_with_account_seed},
Expand Down
5 changes: 3 additions & 2 deletions miden-lib/src/tests/test_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use miden_objects::{
transaction::{OutputNote, OutputNotes},
};
use mock::{
constants::ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN,
mock::{
account::MockAccountType, host::MockHost, notes::AssetPreservationStatus,
account::{MockAccountType, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN},
host::MockHost,
notes::AssetPreservationStatus,
transaction::mock_inputs,
},
prepare_transaction,
Expand Down
18 changes: 12 additions & 6 deletions miden-tx/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ use miden_objects::{
use miden_prover::ProvingOptions;
use mock::{
constants::{
non_fungible_asset, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2, ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_PROCEDURE_INCR_NONCE_PROC_IDX, ACCOUNT_PROCEDURE_SET_CODE_PROC_IDX,
ACCOUNT_PROCEDURE_SET_ITEM_PROC_IDX, FUNGIBLE_ASSET_AMOUNT, MIN_PROOF_SECURITY_LEVEL,
STORAGE_INDEX_0,
non_fungible_asset, ACCOUNT_PROCEDURE_INCR_NONCE_PROC_IDX,
ACCOUNT_PROCEDURE_SET_CODE_PROC_IDX, ACCOUNT_PROCEDURE_SET_ITEM_PROC_IDX,
FUNGIBLE_ASSET_AMOUNT, MIN_PROOF_SECURITY_LEVEL,
},
mock::{
account::{
MockAccountType, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2, ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN,
STORAGE_INDEX_0,
},
notes::AssetPreservationStatus,
transaction::mock_inputs,
},
mock::{account::MockAccountType, notes::AssetPreservationStatus, transaction::mock_inputs},
utils::prepare_word,
};
use vm_processor::{
Expand Down
4 changes: 2 additions & 2 deletions miden-tx/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use miden_tx::{
DataStore, DataStoreError, TransactionProver, TransactionVerifier, TransactionVerifierError,
};
use mock::{
constants::{ACCOUNT_ID_SENDER, DEFAULT_ACCOUNT_CODE, MIN_PROOF_SECURITY_LEVEL},
constants::MIN_PROOF_SECURITY_LEVEL,
mock::{
account::MockAccountType,
account::{MockAccountType, ACCOUNT_ID_SENDER, DEFAULT_ACCOUNT_CODE},
notes::AssetPreservationStatus,
transaction::{mock_inputs, mock_inputs_with_existing},
},
Expand Down
2 changes: 1 addition & 1 deletion miden-tx/tests/integration/scripts/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use miden_objects::{
Felt, Word, ZERO,
};
use miden_tx::TransactionExecutor;
use mock::{constants::ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN, utils::prepare_word};
use mock::{mock::account::ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN, utils::prepare_word};

use crate::{
get_new_key_pair_with_advice_map, get_note_with_fungible_asset_and_script,
Expand Down
2 changes: 1 addition & 1 deletion miden-tx/tests/integration/scripts/p2id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use miden_objects::{
Felt,
};
use miden_tx::TransactionExecutor;
use mock::constants::{
use mock::mock::account::{
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2,
ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN, ACCOUNT_ID_SENDER, DEFAULT_AUTH_SCRIPT,
};
Expand Down
2 changes: 1 addition & 1 deletion miden-tx/tests/integration/scripts/p2idr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use miden_objects::{
Felt,
};
use miden_tx::TransactionExecutor;
use mock::constants::{
use mock::mock::account::{
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN, ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN,
ACCOUNT_ID_SENDER, DEFAULT_AUTH_SCRIPT,
};
Expand Down
2 changes: 1 addition & 1 deletion miden-tx/tests/integration/scripts/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use miden_objects::{
Felt,
};
use miden_tx::TransactionExecutor;
use mock::constants::{
use mock::mock::account::{
ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN, ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN,
ACCOUNT_ID_SENDER, DEFAULT_AUTH_SCRIPT,
};
Expand Down
2 changes: 1 addition & 1 deletion miden-tx/tests/integration/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use miden_objects::{
};
use miden_tx::TransactionExecutor;
use mock::{
constants::{
mock::account::{
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN, ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN,
ACCOUNT_ID_SENDER, DEFAULT_AUTH_SCRIPT,
},
Expand Down
2 changes: 1 addition & 1 deletion mock/src/builders/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rand::Rng;

use crate::{
builders::{str_to_account_code, AccountBuilderError, AccountIdBuilder, AccountStorageBuilder},
constants::DEFAULT_ACCOUNT_CODE,
mock::account::DEFAULT_ACCOUNT_CODE,
utils::{collections::*, string::*},
};

Expand Down
2 changes: 1 addition & 1 deletion mock/src/builders/account_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rand::Rng;

use crate::{
builders::{str_to_account_code, AccountBuilderError},
constants::DEFAULT_ACCOUNT_CODE,
mock::account::DEFAULT_ACCOUNT_CODE,
};

/// Builder for an `AccountId`, the builder can be configured and used multiple times.
Expand Down
128 changes: 2 additions & 126 deletions mock/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,19 @@
use miden_objects::{
accounts::{get_account_seed_single, AccountId, AccountType, SlotItem, StorageSlotType},
accounts::AccountId,
assets::{Asset, NonFungibleAsset, NonFungibleAssetDetails},
Felt, FieldElement, Word, ZERO,
Felt,
};

pub use super::mock::account::{
ACCOUNT_PROCEDURE_INCR_NONCE_PROC_IDX, ACCOUNT_PROCEDURE_SET_CODE_PROC_IDX,
ACCOUNT_PROCEDURE_SET_ITEM_PROC_IDX,
};
use super::{
mock::account::{mock_account, mock_fungible_faucet, mock_non_fungible_faucet},
TransactionKernel,
};

pub const ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN: u64 = 3238098370154045919;

pub const ACCOUNT_ID_SENDER: u64 = 0b0110111011u64 << 54;
pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN: u64 = 0b1010111100 << 54;
pub const ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN: u64 = 0b1110011100 << 54;
pub const ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN_1: u64 = 0b1110011101 << 54;
pub const FUNGIBLE_ASSET_AMOUNT: u64 = 100;
pub const FUNGIBLE_FAUCET_INITIAL_BALANCE: u64 = 50000;

pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_1: u64 =
0b1010010001111111010110100011011110101011010001101111110110111100u64;
pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2: u64 =
0b1010000101101010101101000110111101010110100011011110100011011101u64;
pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_3: u64 =
0b1010011001011010101101000110111101010110100011011101000110111100u64;

pub const MIN_PROOF_SECURITY_LEVEL: u32 = 96;

// Default account code
pub const DEFAULT_ACCOUNT_CODE: &str = "
use.miden::contracts::wallets::basic->basic_wallet
use.miden::contracts::auth::basic->basic_eoa
export.basic_wallet::receive_asset
export.basic_wallet::send_asset
export.basic_eoa::auth_tx_rpo_falcon512
";

// Default authentication code
pub const DEFAULT_AUTH_SCRIPT: &str = "
use.miden::contracts::auth::basic->auth_tx
begin
call.auth_tx::auth_tx_rpo_falcon512
end
";

pub const CONSUMED_ASSET_1_AMOUNT: u64 = 100;
pub const CONSUMED_ASSET_2_AMOUNT: u64 = 200;
pub const CONSUMED_ASSET_3_AMOUNT: u64 = 300;
Expand All @@ -58,21 +22,6 @@ pub const CONSUMED_ASSET_4_AMOUNT: u64 = 100;
pub const NON_FUNGIBLE_ASSET_DATA: [u8; 4] = [1, 2, 3, 4];
pub const NON_FUNGIBLE_ASSET_DATA_2: [u8; 4] = [5, 6, 7, 8];

pub const NONCE: Felt = Felt::ZERO;

pub const STORAGE_INDEX_0: u8 = 20;
pub const STORAGE_VALUE_0: Word = [Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)];
pub const STORAGE_INDEX_1: u8 = 30;
pub const STORAGE_VALUE_1: Word = [Felt::new(5), Felt::new(6), Felt::new(7), Felt::new(8)];

pub fn storage_item_0() -> SlotItem {
(STORAGE_INDEX_0, (StorageSlotType::Value { value_arity: 0 }, STORAGE_VALUE_0))
}

pub fn storage_item_1() -> SlotItem {
(STORAGE_INDEX_1, (StorageSlotType::Value { value_arity: 0 }, STORAGE_VALUE_1))
}

pub const CHILD_ROOT_PARENT_LEAF_INDEX: u8 = 10;
pub const CHILD_SMT_DEPTH: u8 = 64;
pub const CHILD_STORAGE_INDEX_0: u64 = 40;
Expand All @@ -99,76 +48,3 @@ pub fn non_fungible_asset_2(account_id: u64) -> Asset {
NonFungibleAsset::new(&non_fungible_asset_2_details).unwrap();
Asset::NonFungible(non_fungible_asset_2)
}

// ACCOUNT SEED GENERATION
// ================================================================================================

pub enum AccountSeedType {
FungibleFaucetInvalidInitialBalance,
FungibleFaucetValidInitialBalance,
NonFungibleFaucetInvalidReservedSlot,
NonFungibleFaucetValidReservedSlot,
RegularAccountUpdatableCodeOnChain,
}

/// Returns the account id and seed for the specified account type.
pub fn generate_account_seed(account_seed_type: AccountSeedType) -> (AccountId, Word) {
let assembler = TransactionKernel::assembler();
let init_seed: [u8; 32] = Default::default();

let (account, account_type) = match account_seed_type {
AccountSeedType::FungibleFaucetInvalidInitialBalance => (
mock_fungible_faucet(
ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN,
ZERO,
false,
&assembler,
),
AccountType::FungibleFaucet,
),
AccountSeedType::FungibleFaucetValidInitialBalance => (
mock_fungible_faucet(
ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN,
ZERO,
true,
&assembler,
),
AccountType::FungibleFaucet,
),
AccountSeedType::NonFungibleFaucetInvalidReservedSlot => (
mock_non_fungible_faucet(
ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN,
ZERO,
false,
&assembler,
),
AccountType::NonFungibleFaucet,
),
AccountSeedType::NonFungibleFaucetValidReservedSlot => (
mock_non_fungible_faucet(
ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN,
ZERO,
true,
&assembler,
),
AccountType::NonFungibleFaucet,
),
AccountSeedType::RegularAccountUpdatableCodeOnChain => (
mock_account(None, Felt::ONE, None, &assembler),
AccountType::RegularAccountUpdatableCode,
),
};

let seed = get_account_seed_single(
init_seed,
account_type,
true,
account.code().root(),
account.storage().root(),
)
.unwrap();

let account_id = AccountId::new(seed, account.code().root(), account.storage().root()).unwrap();

(account_id, seed)
}
6 changes: 3 additions & 3 deletions mock/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{fs::File, io::Write, path::PathBuf, time::Instant};

use clap::Parser;
use miden_mock::{
constants::DEFAULT_ACCOUNT_CODE,
mock::chain::{Immutable, MockChain, OnChain},
use miden_mock::mock::{
account::DEFAULT_ACCOUNT_CODE,
chain::{Immutable, MockChain, OnChain},
};
use miden_objects::{Digest, FieldElement, Word};
use rand::SeedableRng;
Expand Down
Loading

0 comments on commit 5369f6b

Please sign in to comment.