Skip to content

Commit

Permalink
tests: implement MockHost
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbinth committed Jan 8, 2024
1 parent b797429 commit 3fcf698
Show file tree
Hide file tree
Showing 16 changed files with 487 additions and 507 deletions.
18 changes: 3 additions & 15 deletions miden-lib/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
use std::path::PathBuf;

use miden_objects::{
transaction::PreparedTransaction,
vm::{Program, StackInputs},
Felt, Hasher, Word, ONE, ZERO,
};
use vm_processor::{
AdviceProvider, ContextId, DefaultHost, MemAdviceProvider, Process, ProcessState,
};
use miden_objects::{vm::StackInputs, Felt, Hasher, Word, ONE, ZERO};
use vm_processor::{ContextId, MemAdviceProvider, Process, ProcessState};

use super::{transaction::ToTransactionKernelInputs, Library};
use super::Library;

mod test_account;
mod test_asset;
Expand Down Expand Up @@ -46,12 +40,6 @@ fn test_compile() {
// HELPER FUNCTIONS
// ================================================================================================

fn build_tx_inputs(tx: &PreparedTransaction) -> (Program, StackInputs, MemAdviceProvider) {
let (stack_inputs, advice_inputs) = tx.get_kernel_inputs();
let advice_provider = MemAdviceProvider::from(advice_inputs);
(tx.program().clone(), stack_inputs, advice_provider)
}

fn build_module_path(dir: &str, file: &str) -> PathBuf {
[env!("CARGO_MANIFEST_DIR"), "asm", dir, file].iter().collect()
}
75 changes: 28 additions & 47 deletions miden-lib/src/tests/test_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ use mock::{
},
mock::{
account::MockAccountType,
host::MockHost,
notes::AssetPreservationStatus,
transaction::{mock_executed_tx, mock_inputs},
},
prepare_transaction,
procedures::{output_notes_data_procedure, prepare_word},
run_tx, run_within_tx_kernel,
run_tx, run_within_host, run_within_tx_kernel,
};

use super::{
super::transaction::ToTransactionKernelInputs, build_tx_inputs, ContextId, Felt,
MemAdviceProvider, ProcessState, StackInputs, Word, ONE, ZERO,
super::transaction::ToTransactionKernelInputs, ContextId, Felt, MemAdviceProvider,
ProcessState, StackInputs, Word, ONE, ZERO,
};
use crate::transaction::memory::{ACCT_CODE_ROOT_PTR, ACCT_NEW_CODE_ROOT_PTR};

Expand All @@ -29,7 +30,7 @@ use crate::transaction::memory::{ACCT_CODE_ROOT_PTR, ACCT_NEW_CODE_ROOT_PTR};

#[test]
pub fn test_set_code_is_not_immediate() {
let (account, block_header, chain, notes) =
let tx_inputs =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);

let code = "
Expand All @@ -42,10 +43,8 @@ pub fn test_set_code_is_not_immediate() {
end
";

let transaction =
prepare_transaction(account, None, block_header, chain, notes, None, code, "", None);
let (program, stack_inputs, advice_provider) = build_tx_inputs(&transaction);
let process = run_tx(program, stack_inputs, advice_provider).unwrap();
let transaction = prepare_transaction(tx_inputs, None, code, None);
let process = run_tx(&transaction).unwrap();

// assert the code root is not changed
assert_eq!(
Expand Down Expand Up @@ -91,9 +90,8 @@ pub fn test_set_code_succeeds() {
);

let (stack_inputs, advice_inputs) = executed_transaction.get_kernel_inputs();
let process =
run_within_tx_kernel("", &code, stack_inputs, MemAdviceProvider::from(advice_inputs), None)
.unwrap();
let host = MockHost::new(executed_transaction.initial_account().into(), advice_inputs);
let process = run_within_host("", &code, stack_inputs, host, None).unwrap();

// assert the code root is changed after the epilogue
assert_eq!(
Expand Down Expand Up @@ -224,7 +222,7 @@ fn test_is_faucet_procedure() {
#[test]
fn test_get_item() {
for storage_item in [storage_item_0(), storage_item_1()] {
let (account, block_header, chain, notes) =
let tx_inputs =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);

let code = format!(
Expand All @@ -251,20 +249,18 @@ fn test_get_item() {
item_value = prepare_word(&storage_item.1 .1)
);

let transaction =
prepare_transaction(account, None, block_header, chain, notes, None, &code, "", None);
let (program, stack_inputs, advice_provider) = build_tx_inputs(&transaction);
let _process = run_tx(program, stack_inputs, advice_provider).unwrap();
let transaction = prepare_transaction(tx_inputs, None, &code, None);
let _process = run_tx(&transaction).unwrap();
}
}

#[test]
fn test_set_item() {
let (account, block_header, chain, notes) =
let tx_inputs =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);

// copy the initial account slots (SMT)
let mut account_smt = account.storage().slots().clone();
let mut account_smt = tx_inputs.account().storage().slots().clone();
let init_root = account_smt.root();

// insert a new leaf value
Expand Down Expand Up @@ -306,17 +302,15 @@ fn test_set_item() {
new_root = prepare_word(&account_smt.root()),
);

let transaction =
prepare_transaction(account, None, block_header, chain, notes, None, &code, "", None);
let (program, stack_inputs, advice_provider) = build_tx_inputs(&transaction);
let _process = run_tx(program, stack_inputs, advice_provider).unwrap();
let transaction = prepare_transaction(tx_inputs, None, &code, None);
let _process = run_tx(&transaction).unwrap();
}

// TODO: reenable once storage map support is implemented
#[ignore]
#[test]
fn test_get_map_item() {
let (account, block_header, chain, notes) =
let tx_inputs =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);

let code = format!(
Expand Down Expand Up @@ -347,29 +341,19 @@ fn test_get_map_item() {
child_value = prepare_word(&CHILD_STORAGE_VALUE_0)
);

let transaction = prepare_transaction(
account,
None,
block_header,
chain,
notes,
None,
code.as_str(),
"",
None,
);
let (program, stack_inputs, advice_provider) = build_tx_inputs(&transaction);
let _process = run_tx(program, stack_inputs, advice_provider).unwrap();
let transaction = prepare_transaction(tx_inputs, None, code.as_str(), None);
let _process = run_tx(&transaction).unwrap();
}

// ACCOUNT VAULT TESTS
// ================================================================================================

#[test]
fn test_get_vault_commitment() {
let (account, block_header, chain, notes) =
let tx_inputs =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);

let account = tx_inputs.account();
let code = format!(
"
use.miden::account
Expand All @@ -388,19 +372,18 @@ fn test_get_vault_commitment() {
expected_vault_commitment = prepare_word(&account.vault().commitment()),
);

let transaction =
prepare_transaction(account, None, block_header, chain, notes, None, &code, "", None);
let (program, stack_inputs, advice_provider) = build_tx_inputs(&transaction);
let _process = run_tx(program, stack_inputs, advice_provider).unwrap();
let transaction = prepare_transaction(tx_inputs, None, &code, None);
let _process = run_tx(&transaction).unwrap();
}

// PROCEDURE AUTHENTICATION TESTS
// ================================================================================================

#[test]
fn test_authenticate_procedure() {
let (account, ..) =
let tx_inputs =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);
let account = tx_inputs.account();

let test_cases = vec![
(account.code().procedure_tree().get_leaf(0).unwrap(), true),
Expand All @@ -409,7 +392,7 @@ fn test_authenticate_procedure() {
];

for (root, valid) in test_cases.into_iter() {
let (account, block_header, chain, notes) =
let tx_inputs =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);

let code = format!(
Expand All @@ -431,10 +414,8 @@ fn test_authenticate_procedure() {
root = prepare_word(&root)
);

let transaction =
prepare_transaction(account, None, block_header, chain, notes, None, &code, "", None);
let (program, stack_inputs, advice_provider) = build_tx_inputs(&transaction);
let process = run_tx(program, stack_inputs, advice_provider);
let transaction = prepare_transaction(tx_inputs, None, &code, None);
let process = run_tx(&transaction);

match valid {
true => assert!(process.is_ok()),
Expand Down
18 changes: 7 additions & 11 deletions miden-lib/src/tests/test_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use mock::{
run_tx,
};

use super::{build_tx_inputs, Hasher, Word, ONE};
use super::{Hasher, Word, ONE};

#[test]
fn test_create_fungible_asset_succeeds() {
let (account, block_header, chain, notes) = mock_inputs(
let tx_inputs = mock_inputs(
MockAccountType::FungibleFaucet {
acct_id: ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN,
nonce: ONE,
Expand Down Expand Up @@ -44,15 +44,13 @@ fn test_create_fungible_asset_succeeds() {
"
);

let transaction =
prepare_transaction(account, None, block_header, chain, notes, None, &code, "", None);
let (program, stack_inputs, advice_provider) = build_tx_inputs(&transaction);
let _process = run_tx(program, stack_inputs, advice_provider);
let transaction = prepare_transaction(tx_inputs, None, &code, None);
let _process = run_tx(&transaction);
}

#[test]
fn test_create_non_fungible_asset_succeeds() {
let (account, block_header, chain, notes) = mock_inputs(
let tx_inputs = mock_inputs(
MockAccountType::NonFungibleFaucet {
acct_id: ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN,
nonce: ONE,
Expand Down Expand Up @@ -84,8 +82,6 @@ fn test_create_non_fungible_asset_succeeds() {
expected_non_fungible_asset = prepare_word(&Word::from(non_fungible_asset))
);

let transaction =
prepare_transaction(account, None, block_header, chain, notes, None, &code, "", None);
let (program, stack_inputs, advice_provider) = build_tx_inputs(&transaction);
let _process = run_tx(program, stack_inputs, advice_provider).unwrap();
let transaction = prepare_transaction(tx_inputs, None, &code, None);
let _process = run_tx(&transaction).unwrap();
}
Loading

0 comments on commit 3fcf698

Please sign in to comment.