Skip to content

Commit

Permalink
Merge pull request #318 from 0xPolygonMiden/frisitano-runtime-seed-ge…
Browse files Browse the repository at this point in the history
…neration

Generate test account seeds at runtime
  • Loading branch information
frisitano authored Nov 27, 2023
2 parents e3ecaeb + a671434 commit 967bcfe
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 108 deletions.
71 changes: 17 additions & 54 deletions miden-lib/src/tests/test_prologue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,7 @@ use crate::{
use assembly::ast::ProgramAst;
use miden_objects::transaction::{PreparedTransaction, TransactionScript};
use mock::{
constants::{
ACCOUNT_ID_FUNGIBLE_FAUCET_INVALID_INITIAL_BALANCE,
ACCOUNT_ID_FUNGIBLE_FAUCET_VALID_INITIAL_BALANCE,
ACCOUNT_ID_NON_FUNGIBLE_FAUCET_INVALID_RESERVED_SLOT,
ACCOUNT_ID_NON_FUNGIBLE_FAUCET_VALID_RESERVED_SLOT,
ACCOUNT_SEED_FUNGIBLE_FAUCET_INVALID_INITIAL_BALANCE,
ACCOUNT_SEED_FUNGIBLE_FAUCET_VALID_INITIAL_BALANCE,
ACCOUNT_SEED_NON_FUNGIBLE_FAUCET_INVALID_RESERVED_SLOT,
ACCOUNT_SEED_NON_FUNGIBLE_FAUCET_VALID_RESERVED_SLOT,
ACCOUNT_SEED_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN,
},
constants::{generate_account_seed, AccountSeedType},
consumed_note_data_ptr,
mock::{account::MockAccountType, notes::AssetPreservationStatus, transaction::mock_inputs},
prepare_transaction, run_tx,
Expand Down Expand Up @@ -339,6 +329,8 @@ fn consumed_notes_memory_assertions<A: AdviceProvider>(
#[cfg_attr(not(feature = "testing"), ignore)]
#[test]
pub fn test_prologue_create_account() {
let (_acct_id, account_seed) =
generate_account_seed(AccountSeedType::RegularAccountUpdatableCodeOnChain);
let (account, block_header, chain, notes) =
mock_inputs(MockAccountType::StandardNew, AssetPreservationStatus::Preserved);
let code = "
Expand All @@ -349,12 +341,6 @@ pub fn test_prologue_create_account() {
end
";

let account_seed: Word = ACCOUNT_SEED_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN
.iter()
.map(|x| Felt::new(*x))
.collect::<Vec<_>>()
.try_into()
.unwrap();
let transaction = prepare_transaction(
account,
Some(account_seed),
Expand All @@ -378,9 +364,11 @@ pub fn test_prologue_create_account() {
#[cfg_attr(not(feature = "testing"), ignore)]
#[test]
pub fn test_prologue_create_account_valid_fungible_faucet_reserved_slot() {
let (acct_id, account_seed) =
generate_account_seed(AccountSeedType::FungibleFaucetValidInitialBalance);
let (account, block_header, chain, notes) = mock_inputs(
MockAccountType::FungibleFaucet {
acct_id: ACCOUNT_ID_FUNGIBLE_FAUCET_VALID_INITIAL_BALANCE,
acct_id: acct_id.into(),
nonce: ZERO,
empty_reserved_slot: true,
},
Expand All @@ -394,12 +382,6 @@ pub fn test_prologue_create_account_valid_fungible_faucet_reserved_slot() {
end
";

let account_seed: Word = ACCOUNT_SEED_FUNGIBLE_FAUCET_VALID_INITIAL_BALANCE
.iter()
.map(|x| Felt::new(*x))
.collect::<Vec<_>>()
.try_into()
.unwrap();
let transaction = prepare_transaction(
account,
Some(account_seed),
Expand All @@ -423,9 +405,11 @@ pub fn test_prologue_create_account_valid_fungible_faucet_reserved_slot() {
#[cfg_attr(not(feature = "testing"), ignore)]
#[test]
pub fn test_prologue_create_account_invalid_fungible_faucet_reserved_slot() {
let (acct_id, account_seed) =
generate_account_seed(AccountSeedType::FungibleFaucetInvalidInitialBalance);
let (account, block_header, chain, notes) = mock_inputs(
MockAccountType::FungibleFaucet {
acct_id: ACCOUNT_ID_FUNGIBLE_FAUCET_INVALID_INITIAL_BALANCE,
acct_id: acct_id.into(),
nonce: ZERO,
empty_reserved_slot: false,
},
Expand All @@ -439,12 +423,6 @@ pub fn test_prologue_create_account_invalid_fungible_faucet_reserved_slot() {
end
";

let account_seed: Word = ACCOUNT_SEED_FUNGIBLE_FAUCET_INVALID_INITIAL_BALANCE
.iter()
.map(|x| Felt::new(*x))
.collect::<Vec<_>>()
.try_into()
.unwrap();
let transaction = prepare_transaction(
account,
Some(account_seed),
Expand All @@ -468,9 +446,11 @@ pub fn test_prologue_create_account_invalid_fungible_faucet_reserved_slot() {
#[cfg_attr(not(feature = "testing"), ignore)]
#[test]
pub fn test_prologue_create_account_valid_non_fungible_faucet_reserved_slot() {
let (acct_id, account_seed) =
generate_account_seed(AccountSeedType::NonFungibleFaucetValidReservedSlot);
let (account, block_header, chain, notes) = mock_inputs(
MockAccountType::NonFungibleFaucet {
acct_id: ACCOUNT_ID_NON_FUNGIBLE_FAUCET_VALID_RESERVED_SLOT,
acct_id: acct_id.into(),
nonce: ZERO,
empty_reserved_slot: true,
},
Expand All @@ -484,13 +464,6 @@ pub fn test_prologue_create_account_valid_non_fungible_faucet_reserved_slot() {
end
";

let account_seed: Word = ACCOUNT_SEED_NON_FUNGIBLE_FAUCET_VALID_RESERVED_SLOT
.iter()
.map(|x| Felt::new(*x))
.collect::<Vec<_>>()
.try_into()
.unwrap();

let transaction = prepare_transaction(
account,
Some(account_seed),
Expand All @@ -514,9 +487,11 @@ pub fn test_prologue_create_account_valid_non_fungible_faucet_reserved_slot() {
#[cfg_attr(not(feature = "testing"), ignore)]
#[test]
pub fn test_prologue_create_account_invalid_non_fungible_faucet_reserved_slot() {
let (acct_id, account_seed) =
generate_account_seed(AccountSeedType::NonFungibleFaucetInvalidReservedSlot);
let (account, block_header, chain, notes) = mock_inputs(
MockAccountType::NonFungibleFaucet {
acct_id: ACCOUNT_ID_NON_FUNGIBLE_FAUCET_INVALID_RESERVED_SLOT,
acct_id: acct_id.into(),
nonce: ZERO,
empty_reserved_slot: false,
},
Expand All @@ -530,13 +505,6 @@ pub fn test_prologue_create_account_invalid_non_fungible_faucet_reserved_slot()
end
";

let account_seed: Word = ACCOUNT_SEED_NON_FUNGIBLE_FAUCET_INVALID_RESERVED_SLOT
.iter()
.map(|x| Felt::new(*x))
.collect::<Vec<_>>()
.try_into()
.unwrap();

let transaction = prepare_transaction(
account,
Some(account_seed),
Expand All @@ -560,6 +528,8 @@ pub fn test_prologue_create_account_invalid_non_fungible_faucet_reserved_slot()
#[cfg_attr(not(feature = "testing"), ignore)]
#[test]
pub fn test_prologue_create_account_invalid_seed() {
let (_acct_id, account_seed) =
generate_account_seed(AccountSeedType::RegularAccountUpdatableCodeOnChain);
let (account, block_header, chain, notes) =
mock_inputs(MockAccountType::StandardNew, AssetPreservationStatus::Preserved);
let account_seed_key = [account.id().into(), ZERO, ZERO, ZERO];
Expand All @@ -572,13 +542,6 @@ pub fn test_prologue_create_account_invalid_seed() {
end
";

// we must provide a valid seed to `prepare_transaction` otherwise it will error
let account_seed: Word = ACCOUNT_SEED_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN
.iter()
.map(|x| Felt::new(*x))
.collect::<Vec<_>>()
.try_into()
.unwrap();
let transaction = prepare_transaction(
account,
Some(account_seed),
Expand Down
118 changes: 78 additions & 40 deletions mock/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,15 @@
use super::mock::account::{mock_account, mock_fungible_faucet, mock_non_fungible_faucet};
pub use super::mock::account::{
ACCOUNT_PROCEDURE_INCR_NONCE_PROC_IDX, ACCOUNT_PROCEDURE_SET_CODE_PROC_IDX,
ACCOUNT_PROCEDURE_SET_ITEM_PROC_IDX,
};
use miden_lib::assembler::assembler;
use miden_objects::{
accounts::{AccountId, StorageItem},
accounts::{AccountId, AccountType, StorageItem},
assets::{Asset, NonFungibleAsset, NonFungibleAssetDetails},
Felt, FieldElement,
Felt, FieldElement, Word, ZERO,
};
pub const ACCOUNT_SEED_FUNGIBLE_FAUCET_INVALID_INITIAL_BALANCE: [u64; 4] = [
5342472004481420725,
15139745540144612214,
7175220148257528278,
12185026252347356330,
];
pub const ACCOUNT_ID_FUNGIBLE_FAUCET_INVALID_INITIAL_BALANCE: u64 = 11808006999189383835;

pub const ACCOUNT_SEED_FUNGIBLE_FAUCET_VALID_INITIAL_BALANCE: [u64; 4] = [
2389651479266964250,
12570482780864789472,
3827181395997035738,
15484731259405424484,
];
pub const ACCOUNT_ID_FUNGIBLE_FAUCET_VALID_INITIAL_BALANCE: u64 = 13650031744811031251;

pub const ACCOUNT_SEED_NON_FUNGIBLE_FAUCET_INVALID_RESERVED_SLOT: [u64; 4] = [
6127036790776509692,
7202481422049357184,
4843524022082280619,
10803188976143115680,
];
pub const ACCOUNT_ID_NON_FUNGIBLE_FAUCET_INVALID_RESERVED_SLOT: u64 = 16992539250668925014;

pub const ACCOUNT_SEED_NON_FUNGIBLE_FAUCET_VALID_RESERVED_SLOT: [u64; 4] = [
1480622438474629078,
7551952530545164022,
14268335233603228254,
322360296984864845,
];
pub const ACCOUNT_ID_NON_FUNGIBLE_FAUCET_VALID_RESERVED_SLOT: u64 = 17114871292473597866;

pub const ACCOUNT_SEED_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN: [u64; 4] = [
4160082958698397031,
5735601916245113230,
4289496549565608242,
16940108222232119545,
];

pub const ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN: u64 = 3238098370154045919;

pub const ACCOUNT_ID_SENDER: u64 = 0b0110111011u64 << 54;
Expand Down Expand Up @@ -113,3 +78,76 @@ 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 = 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(Felt::ONE, None, &assembler),
AccountType::RegularAccountUpdatableCode,
),
};

let seed = AccountId::get_account_seed(
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)
}
21 changes: 7 additions & 14 deletions mock/src/mock/account.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::constants::{
non_fungible_asset, non_fungible_asset_2, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_1, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2,
ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN, ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN,
ACCOUNT_SEED_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN, CHILD_ROOT_PARENT_LEAF_INDEX,
generate_account_seed, non_fungible_asset, non_fungible_asset_2, AccountSeedType,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_1,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2, ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN, CHILD_ROOT_PARENT_LEAF_INDEX,
CHILD_SMT_DEPTH, CHILD_STORAGE_INDEX_0, CHILD_STORAGE_VALUE_0, FUNGIBLE_ASSET_AMOUNT,
FUNGIBLE_FAUCET_INITIAL_BALANCE, STORAGE_ITEM_0, STORAGE_ITEM_1,
};
Expand All @@ -12,7 +12,6 @@ use miden_objects::{
assembly::{Assembler, ModuleAst},
assets::{Asset, FungibleAsset},
crypto::merkle::{MerkleStore, SimpleSmt, TieredSmt},
utils::collections::Vec,
Felt, FieldElement, Word, ZERO,
};

Expand Down Expand Up @@ -130,17 +129,11 @@ pub fn mock_account_code(assembler: &Assembler) -> AccountCode {
}

pub fn mock_new_account(assembler: &Assembler) -> Account {
let (acct_id, _account_seed) =
generate_account_seed(AccountSeedType::RegularAccountUpdatableCodeOnChain);
let account_storage = mock_account_storage();
let account_code = mock_account_code(assembler);
let account_seed: Word = ACCOUNT_SEED_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN
.iter()
.map(|x| Felt::new(*x))
.collect::<Vec<_>>()
.try_into()
.unwrap();
let account_id =
AccountId::new(account_seed, account_code.root(), account_storage.root()).unwrap();
Account::new(account_id, AccountVault::default(), account_storage, account_code, Felt::ZERO)
Account::new(acct_id, AccountVault::default(), account_storage, account_code, Felt::ZERO)
}

pub fn mock_account(nonce: Felt, code: Option<AccountCode>, assembler: &Assembler) -> Account {
Expand Down

0 comments on commit 967bcfe

Please sign in to comment.