Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event-based procedure index lookups #405

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion miden-lib/asm/kernels/transaction/api.masm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const.ADD_ASSET_TO_ACCOUNT_VAULT_EVENT=131072
# Event emitted to signal that an asset is being removed from the account vault.
const.REMOVE_ASSET_FROM_ACCOUNT_VAULT_EVENT=131073


# AUTHENTICATION
# =================================================================================================

Expand Down
13 changes: 10 additions & 3 deletions miden-lib/asm/miden/kernels/tx/account.masm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ const.SLOT_TYPES_COMMITMENT_STORAGE_SLOT=255
# The maximum value a slot type can take (An array of depth 64).
const.MAX_SLOT_TYPE=64

# EVENTS
# =================================================================================================

# Event emitted to push the index of the account procedure at the top of the operand stack onto
# the advice stack.
const.PUSH_ACCOUNT_PROCEDURE_INDEX_EVENT=131074

# CONSTANT ACCESSORS
# =================================================================================================

Expand Down Expand Up @@ -343,7 +350,7 @@ export.set_item
# => [V]
end

#! Authenticates the proedcure root is part of the account code Merkle treee. Panics if the
#! Verifies that the procedure root is part of the account code Merkle tree. Panics if the
#! procedure root is not part of the account code Merkle tree.
#!
#! Stack: [PROC_ROOT]
Expand All @@ -355,8 +362,8 @@ export.authenticate_procedure.1
exec.memory::get_acct_code_root swapw
# => [PROC_ROOT, CODE_ROOT]

# load the index of the procedure root onto the advice stack
adv.push_mapval adv_push.1 movdn.4
# load the index of the procedure root onto the advice stack, and move it to the operand stack
bobbinth marked this conversation as resolved.
Show resolved Hide resolved
emit.PUSH_ACCOUNT_PROCEDURE_INDEX_EVENT adv_push.1 movdn.4
# => [PROC_ROOT, index, CODE_ROOT]

# push the depth of the code Merkle tree onto the stack
Expand Down
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
Loading