Skip to content

Commit

Permalink
Merge pull request #320 from 0xPolygonMiden/frisitano-account-code-pr…
Browse files Browse the repository at this point in the history
…ocedure-ordering

Change account procedure ordering
  • Loading branch information
frisitano authored Nov 22, 2023
2 parents 4606817 + ae86bd0 commit e3ecaeb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
24 changes: 19 additions & 5 deletions miden-tx/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ 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_MAST_ROOT, ACCOUNT_PROCEDURE_SET_CODE_MAST_ROOT,
ACCOUNT_PROCEDURE_SET_ITEM_MAST_ROOT, CHILD_ROOT_PARENT_LEAF_INDEX, CHILD_SMT_DEPTH,
ACCOUNT_PROCEDURE_INCR_NONCE_PROC_IDX, ACCOUNT_PROCEDURE_SET_CODE_PROC_IDX,
ACCOUNT_PROCEDURE_SET_ITEM_PROC_IDX, CHILD_ROOT_PARENT_LEAF_INDEX, CHILD_SMT_DEPTH,
CHILD_STORAGE_INDEX_0, FUNGIBLE_ASSET_AMOUNT,
},
mock::{account::MockAccountType, notes::AssetPreservationStatus, transaction::mock_inputs},
utils::prepare_word,
};
use vm_core::utils::to_hex;
use vm_processor::MemAdviceProvider;

// TESTS
Expand Down Expand Up @@ -102,6 +103,19 @@ fn test_transaction_result_account_delta() {
let removed_asset_3 = non_fungible_asset(ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN);
let removed_assets = vec![removed_asset_1, removed_asset_2, removed_asset_3];

let account_procedure_incr_nonce_mast_root = to_hex(
&data_store.account.code().procedures()[ACCOUNT_PROCEDURE_INCR_NONCE_PROC_IDX].as_bytes(),
)
.unwrap();
let account_procedure_set_code_mast_root = to_hex(
&data_store.account.code().procedures()[ACCOUNT_PROCEDURE_SET_CODE_PROC_IDX].as_bytes(),
)
.unwrap();
let account_procedure_set_item_mast_root = to_hex(
&data_store.account.code().procedures()[ACCOUNT_PROCEDURE_SET_ITEM_PROC_IDX].as_bytes(),
)
.unwrap();

let tx_script = format!(
"\
use.miden::sat::account
Expand All @@ -114,20 +128,20 @@ fn test_transaction_result_account_delta() {
push.0 movdn.5 push.0 movdn.5 push.0 movdn.5
# => [index, V', 0, 0, 0]
call.{ACCOUNT_PROCEDURE_SET_ITEM_MAST_ROOT}
call.0x{account_procedure_set_item_mast_root}
# => [R', V]
end
proc.set_code
call.{ACCOUNT_PROCEDURE_SET_CODE_MAST_ROOT}
call.0x{account_procedure_set_code_mast_root}
# => [0, 0, 0, 0]
dropw
# => []
end
proc.incr_nonce
call.{ACCOUNT_PROCEDURE_INCR_NONCE_MAST_ROOT}
call.0x{account_procedure_incr_nonce_mast_root}
# => [0]
drop
Expand Down
11 changes: 4 additions & 7 deletions mock/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
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_objects::{
accounts::{AccountId, StorageItem},
assets::{Asset, NonFungibleAsset, NonFungibleAssetDetails},
Expand Down Expand Up @@ -67,13 +71,6 @@ pub const DEFAULT_ACCOUNT_CODE: &str = "
export.basic_eoa::auth_tx_rpo_falcon512
";

pub const ACCOUNT_PROCEDURE_INCR_NONCE_MAST_ROOT: &str =
"0xd765111e22479256e87a57eaf3a27479d19cc876c9a715ee6c262e0a0d47a2ac";
pub const ACCOUNT_PROCEDURE_SET_CODE_MAST_ROOT: &str =
"0x9d221abcc386973775499406d126764cdf4530ccf8084e27091f7e9f28177bbe";
pub const ACCOUNT_PROCEDURE_SET_ITEM_MAST_ROOT: &str =
"0x49935297f029f8b229fe86c6c47b9d291d063b8558fe90319128fb60dbda3d1b";

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 Down
13 changes: 13 additions & 0 deletions mock/src/mock/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,23 @@ pub fn mock_account_storage() -> AccountStorage {
.unwrap()
}

// Constants that define the indexes of the account procedures of interest
pub const ACCOUNT_PROCEDURE_INCR_NONCE_PROC_IDX: usize = 2;
pub const ACCOUNT_PROCEDURE_SET_ITEM_PROC_IDX: usize = 3;
pub const ACCOUNT_PROCEDURE_SET_CODE_PROC_IDX: usize = 4;

pub fn mock_account_code(assembler: &Assembler) -> AccountCode {
let account_code = "\
use.miden::sat::account
use.miden::sat::tx
use.miden::wallets::basic->wallet
# acct proc 0
export.wallet::receive_asset
# acct proc 1
export.wallet::send_asset
# acct proc 2
export.incr_nonce
push.0 swap
# => [value, 0]
Expand All @@ -75,6 +83,7 @@ pub fn mock_account_code(assembler: &Assembler) -> AccountCode {
# => [0]
end
# acct proc 3
export.set_item
exec.account::set_item
# => [R', V, 0, 0, 0]
Expand All @@ -83,6 +92,7 @@ pub fn mock_account_code(assembler: &Assembler) -> AccountCode {
# => [R', V]
end
# acct proc 4
export.set_code
padw swapw
# => [CODE_ROOT, 0, 0, 0, 0]
Expand All @@ -91,6 +101,7 @@ pub fn mock_account_code(assembler: &Assembler) -> AccountCode {
# => [0, 0, 0, 0]
end
# acct proc 5
export.create_note
# apply padding
repeat.8
Expand All @@ -102,11 +113,13 @@ pub fn mock_account_code(assembler: &Assembler) -> AccountCode {
# => [ptr, 0, 0, 0, 0, 0, 0, 0, 0]
end
# acct proc 6
export.account_procedure_1
push.1.2
add
end
# acct proc 7
export.account_procedure_2
push.2.1
sub
Expand Down
12 changes: 8 additions & 4 deletions objects/src/accounts/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl AccountCode {
/// - The number of procedures exported from the provided module is greater than 256.
pub fn new(account_module: ModuleAst, assembler: &Assembler) -> Result<Self, AccountError> {
// compile the module and make sure the number of exported procedures is within the limit
let mut procedure_digests = assembler
let procedure_digests = assembler
.compile_module(&account_module, None, &mut AssemblyContext::for_module(false))
.map_err(AccountError::AccountCodeAssemblerError)?;

Expand All @@ -53,9 +53,6 @@ impl AccountCode {
});
}

// sort the procedure digests so that their order is stable
procedure_digests.sort_by_key(|a| a.as_bytes());

Ok(Self {
procedure_tree: build_procedure_tree(&procedure_digests),
module: account_module,
Expand Down Expand Up @@ -163,6 +160,13 @@ mod serialization {
// ================================================================================================

fn build_procedure_tree(procedures: &[Digest]) -> SimpleSmt {
// order the procedure digests to achieve a reproducible tree
let procedures = {
let mut procedures = procedures.to_vec();
procedures.sort_by_key(|a| a.as_bytes());
procedures
};

SimpleSmt::with_leaves(
ACCOUNT_CODE_TREE_DEPTH,
procedures
Expand Down

0 comments on commit e3ecaeb

Please sign in to comment.