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

Allow updates to all item slots #737

Merged
merged 15 commits into from
Jun 11, 2024
12 changes: 3 additions & 9 deletions bench-tx/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub use alloc::collections::BTreeMap;

use miden_lib::transaction::TransactionKernel;
use miden_objects::{
accounts::{Account, AccountCode, AccountId, AccountStorage, SlotItem, StorageSlot},
accounts::{Account, AccountCode, AccountId, AccountStorage, SlotItem},
assembly::ModuleAst,
assets::{Asset, AssetVault},
Felt, Word,
Expand Down Expand Up @@ -88,14 +88,8 @@ pub fn get_account_with_default_account_code(
let account_assembler = TransactionKernel::assembler();

let account_code = AccountCode::new(account_code_ast.clone(), &account_assembler).unwrap();
let account_storage = AccountStorage::new(
vec![SlotItem {
index: 0,
slot: StorageSlot::new_value(public_key),
}],
vec![],
)
.unwrap();
let account_storage =
AccountStorage::new(vec![SlotItem::new_value(0, 0, public_key)], vec![]).unwrap();

let account_vault = match assets {
Some(asset) => AssetVault::new(&[asset]).unwrap(),
Expand Down
12 changes: 1 addition & 11 deletions miden-lib/src/accounts/faucets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use alloc::string::ToString;
use miden_objects::{
accounts::{
Account, AccountCode, AccountId, AccountStorage, AccountStorageType, AccountType, SlotItem,
StorageSlot,
},
assembly::LibraryPath,
assets::TokenSymbol,
Expand Down Expand Up @@ -72,16 +71,7 @@ pub fn create_basic_fungible_faucet(
// - slot 0: authentication data
// - slot 1: token metadata as [max_supply, decimals, token_symbol, 0]
let account_storage = AccountStorage::new(
vec![
SlotItem {
index: 0,
slot: StorageSlot::new_value(auth_data),
},
SlotItem {
index: 1,
slot: StorageSlot::new_value(metadata),
},
],
vec![SlotItem::new_value(0, 0, auth_data), SlotItem::new_value(1, 0, metadata)],
vec![],
)?;

Expand Down
12 changes: 3 additions & 9 deletions miden-lib/src/accounts/wallets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use alloc::string::{String, ToString};

use miden_objects::{
accounts::{
Account, AccountCode, AccountId, AccountStorage, AccountStorageType, AccountType,
StorageSlot,
Account, AccountCode, AccountId, AccountStorage, AccountStorageType, AccountType, SlotItem,
},
assembly::ModuleAst,
AccountError, Word,
Expand Down Expand Up @@ -58,13 +57,8 @@ pub fn create_basic_wallet(
let account_assembler = TransactionKernel::assembler();
let account_code = AccountCode::new(account_code_ast.clone(), &account_assembler)?;

let account_storage = AccountStorage::new(
vec![miden_objects::accounts::SlotItem {
index: 0,
slot: StorageSlot::new_value(storage_slot_0_data),
}],
vec![],
)?;
let account_storage =
AccountStorage::new(vec![SlotItem::new_value(0, 0, storage_slot_0_data)], vec![])?;

let account_seed = AccountId::get_account_seed(
init_seed,
Expand Down
20 changes: 14 additions & 6 deletions miden-tx/src/kernel_tests/test_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ use miden_objects::{
ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN,
ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_OFF_CHAIN,
},
AccountId, AccountType, StorageSlotType,
AccountId, AccountType, SlotItem, StorageSlotType,
},
crypto::{hash::rpo::RpoDigest, merkle::LeafIndex},
testing::{
account::MockAccountType,
notes::AssetPreservationStatus,
prepare_word,
storage::{
storage_item_0, storage_item_1, storage_item_2, storage_map_2, STORAGE_LEAVES_2,
storage_map_2, STORAGE_INDEX_0, STORAGE_INDEX_1, STORAGE_INDEX_2, STORAGE_LEAVES_2,
STORAGE_VALUE_0, STORAGE_VALUE_1,
},
},
};
Expand Down Expand Up @@ -246,7 +247,10 @@ fn test_is_faucet_procedure() {

#[test]
fn test_get_item() {
for storage_item in [storage_item_0(), storage_item_1()] {
for storage_item in [
SlotItem::new_value(STORAGE_INDEX_0, 0, STORAGE_VALUE_0),
SlotItem::new_value(STORAGE_INDEX_1, 0, STORAGE_VALUE_1),
] {
let (tx_inputs, tx_args) =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);

Expand Down Expand Up @@ -335,7 +339,11 @@ fn test_set_item() {
// Test different account storage types
#[test]
fn test_get_storage_data_type() {
for storage_item in [storage_item_0(), storage_item_1(), storage_item_2()] {
for storage_item in [
SlotItem::new_value(STORAGE_INDEX_0, 0, STORAGE_VALUE_0),
SlotItem::new_value(STORAGE_INDEX_1, 0, STORAGE_VALUE_1),
SlotItem::new_map(STORAGE_INDEX_2, 0, storage_map_2().root().into()),
] {
let (tx_inputs, tx_args) =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);

Expand Down Expand Up @@ -385,7 +393,7 @@ fn test_get_map_item() {
let (tx_inputs, tx_args) =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);

let storage_item = storage_item_2();
let storage_item = SlotItem::new_map(STORAGE_INDEX_2, 0, storage_map_2().root().into());
for (key, value) in STORAGE_LEAVES_2 {
let code = format!(
"
Expand Down Expand Up @@ -433,7 +441,7 @@ fn test_set_map_item() {
let (tx_inputs, tx_args) =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);

let storage_item = storage_item_2();
let storage_item = SlotItem::new_map(STORAGE_INDEX_2, 0, storage_map_2().root().into());

let code = format!(
"
Expand Down
12 changes: 3 additions & 9 deletions miden-tx/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use miden_lib::transaction::TransactionKernel;
use miden_objects::{
accounts::{
account_id::testing::ACCOUNT_ID_SENDER, Account, AccountCode, AccountId, AccountStorage,
SlotItem, StorageSlot,
SlotItem,
},
assembly::{ModuleAst, ProgramAst},
assets::{Asset, AssetVault, FungibleAsset},
Expand Down Expand Up @@ -78,14 +78,8 @@ pub fn get_account_with_default_account_code(
let account_assembler = TransactionKernel::assembler();

let account_code = AccountCode::new(account_code_ast.clone(), &account_assembler).unwrap();
let account_storage = AccountStorage::new(
vec![SlotItem {
index: 0,
slot: StorageSlot::new_value(public_key),
}],
vec![],
)
.unwrap();
let account_storage =
AccountStorage::new(vec![SlotItem::new_value(0, 0, public_key)], vec![]).unwrap();

let account_vault = match assets {
Some(asset) => AssetVault::new(&[asset]).unwrap(),
Expand Down
12 changes: 3 additions & 9 deletions miden-tx/tests/integration/scripts/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use miden_lib::{
use miden_objects::{
accounts::{
account_id::testing::ACCOUNT_ID_FUNGIBLE_FAUCET_OFF_CHAIN, Account, AccountCode, AccountId,
AccountStorage, AccountStorageType, SlotItem, StorageSlot,
AccountStorage, AccountStorageType, SlotItem,
},
assembly::{ModuleAst, ProgramAst},
assets::{Asset, AssetVault, FungibleAsset, TokenSymbol},
Expand Down Expand Up @@ -294,14 +294,8 @@ fn get_faucet_account_with_max_supply_and_total_issuance(
let faucet_storage_slot_1 = [Felt::new(max_supply), Felt::new(0), Felt::new(0), Felt::new(0)];
let mut faucet_account_storage = AccountStorage::new(
vec![
SlotItem {
index: 0,
slot: StorageSlot::new_value(public_key),
},
SlotItem {
index: 1,
slot: StorageSlot::new_value(faucet_storage_slot_1),
},
SlotItem::new_value(0, 0, public_key),
SlotItem::new_value(1, 0, faucet_storage_slot_1),
],
vec![],
)
Expand Down
22 changes: 5 additions & 17 deletions miden-tx/tests/integration/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use miden_objects::{
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN, ACCOUNT_ID_OFF_CHAIN_SENDER,
ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_OFF_CHAIN,
},
Account, AccountId, AccountStorage, SlotItem, StorageSlot,
Account, AccountId, AccountStorage, SlotItem,
},
assembly::ProgramAst,
assets::{Asset, AssetVault, FungibleAsset},
Expand Down Expand Up @@ -85,14 +85,8 @@ fn prove_receive_asset_via_wallet() {
assert_eq!(executed_transaction.account_delta().nonce(), Some(Felt::new(2)));

// clone account info
let account_storage = AccountStorage::new(
vec![SlotItem {
index: 0,
slot: StorageSlot::new_value(target_pub_key),
}],
vec![],
)
.unwrap();
let account_storage =
AccountStorage::new(vec![SlotItem::new_value(0, 0, target_pub_key)], vec![]).unwrap();
let account_code = target_account.code().clone();
// vault delta
let target_account_after: Account = Account::from_parts(
Expand Down Expand Up @@ -167,14 +161,8 @@ fn prove_send_asset_via_wallet() {
assert!(prove_and_verify_transaction(executed_transaction.clone()).is_ok());

// clones account info
let sender_account_storage = AccountStorage::new(
vec![SlotItem {
index: 0,
slot: StorageSlot::new_value(sender_pub_key),
}],
vec![],
)
.unwrap();
let sender_account_storage =
AccountStorage::new(vec![SlotItem::new_value(0, 0, sender_pub_key)], vec![]).unwrap();
let sender_account_code = sender_account.code().clone();

// vault delta
Expand Down
17 changes: 16 additions & 1 deletion objects/src/accounts/storage/map.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use vm_core::Felt;

use super::{
AccountError, ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable, Word,
};
Expand All @@ -9,6 +11,13 @@ use crate::crypto::{
// ACCOUNT STORAGE MAP
// ================================================================================================

pub const EMPTY_STORAGE_MAP: Word = [
Felt::new(15321474589252129342),
Felt::new(17373224439259377994),
Felt::new(15071539326562317628),
Felt::new(3312677166725950353),
];

/// Account storage map is a Sparse Merkle Tree of depth 64. It can be used to store more data as
/// there is in plain usage of the storage slots. The root of the SMT consumes one account storage
/// slot.
Expand Down Expand Up @@ -119,7 +128,7 @@ impl Deserializable for StorageMap {
mod tests {
use miden_crypto::{hash::rpo::RpoDigest, Felt};

use super::{Deserializable, Serializable, StorageMap, Word};
use super::{Deserializable, Serializable, StorageMap, Word, EMPTY_STORAGE_MAP};

#[test]
fn account_storage_serialization() {
Expand All @@ -144,4 +153,10 @@ mod tests {
let bytes = storage_map.to_bytes();
assert_eq!(storage_map, StorageMap::read_from_bytes(&bytes).unwrap());
}

#[test]
fn test_empty_storage_map_constants() {
// If these values don't match, update the constants.
assert_eq!(*StorageMap::default().root(), EMPTY_STORAGE_MAP);
}
}
Loading
Loading