Skip to content

Commit

Permalink
refactor: impl StorageSlotHeader, move as_elements there
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumuran committed Nov 30, 2024
1 parent 61cb5fe commit d3a2db7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 32 deletions.
4 changes: 2 additions & 2 deletions miden-tx/src/tests/kernel_tests/test_prologue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use miden_lib::{
use miden_objects::{
accounts::{
AccountBuilder, AccountComponent, AccountProcedureInfo, AccountStorage, AccountType,
StorageSlot,
NUM_ELEMENTS_PER_STORAGE_SLOT,
},
testing::{
account_component::BASIC_WALLET_CODE,
Expand Down Expand Up @@ -277,7 +277,7 @@ fn account_data_memory_assertions(process: &Process<MockHost>, inputs: &Transact
.account()
.storage()
.as_elements()
.chunks(StorageSlot::NUM_ELEMENTS_PER_STORAGE_SLOT / 2)
.chunks(NUM_ELEMENTS_PER_STORAGE_SLOT / 2)
.enumerate()
{
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions miden-tx/src/tests/kernel_tests/test_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use miden_objects::{
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2,
},
Account, AccountBuilder, AccountComponent, AccountProcedureInfo, AccountStorage,
StorageSlot,
StorageSlot, NUM_ELEMENTS_PER_STORAGE_SLOT,
},
assets::NonFungibleAsset,
crypto::merkle::{LeafIndex, MerklePath},
Expand Down Expand Up @@ -1119,7 +1119,7 @@ fn foreign_account_data_memory_assertions(foreign_account: &Account, process: &P
for (i, elements) in foreign_account
.storage()
.as_elements()
.chunks(StorageSlot::NUM_ELEMENTS_PER_STORAGE_SLOT / 2)
.chunks(NUM_ELEMENTS_PER_STORAGE_SLOT / 2)
.enumerate()
{
assert_eq!(
Expand Down
5 changes: 4 additions & 1 deletion objects/src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ mod seed;
pub use seed::{get_account_seed, get_account_seed_single};

mod storage;
pub use storage::{AccountStorage, AccountStorageHeader, StorageMap, StorageSlot, StorageSlotType};
pub use storage::{
AccountStorage, AccountStorageHeader, StorageMap, StorageSlot, StorageSlotType,
NUM_ELEMENTS_PER_STORAGE_SLOT,
};

mod header;
pub use header::AccountHeader;
Expand Down
33 changes: 26 additions & 7 deletions objects/src/accounts/storage/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,36 @@ use vm_core::{
};
use vm_processor::DeserializationError;

use super::{AccountStorage, Felt, StorageSlotType, Word};
use super::{
AccountStorage, Felt, StorageSlot, StorageSlotType, Word, NUM_ELEMENTS_PER_STORAGE_SLOT,
};
use crate::AccountError;

// ACCOUNT STORAGE HEADER
// ================================================================================================

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StorageSlotHeader(StorageSlotType, Word);

impl From<&StorageSlot> for StorageSlotHeader {
fn from(value: &StorageSlot) -> Self {
Self(value.slot_type(), value.value())
}
}

impl StorageSlotHeader {
pub fn new(value: &(StorageSlotType, Word)) -> Self {
Self(value.0.clone(), value.1)
}

pub fn as_elements(&self) -> [Felt; NUM_ELEMENTS_PER_STORAGE_SLOT] {
let mut elements = [ZERO; NUM_ELEMENTS_PER_STORAGE_SLOT];
elements[0..4].copy_from_slice(&self.1);
elements[4..8].copy_from_slice(&self.0.as_word());
elements
}
}

/// Account storage header is a lighter version of the [AccountStorage] storing only the type and
/// the top-level value for each storage slot.
///
Expand Down Expand Up @@ -69,12 +93,7 @@ impl AccountStorageHeader {
pub fn as_elements(&self) -> Vec<Felt> {
self.slots
.iter()
.flat_map(|slot| {
let mut elements = [ZERO; 8];
elements[0..4].copy_from_slice(&slot.1);
elements[4..8].copy_from_slice(&slot.0.as_word());
elements
})
.flat_map(|slot| StorageSlotHeader::new(slot).as_elements())
.collect()
}
}
Expand Down
13 changes: 11 additions & 2 deletions objects/src/accounts/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ mod map;
pub use map::StorageMap;

mod header;
pub use header::AccountStorageHeader;
pub use header::{AccountStorageHeader, StorageSlotHeader};

// CONSTANTS
// ================================================================================================

/// The number of field elements needed to represent a [StorageSlot] in kernel memory.
pub const NUM_ELEMENTS_PER_STORAGE_SLOT: usize = 8;

// ACCOUNT STORAGE
// ================================================================================================
Expand Down Expand Up @@ -252,7 +258,10 @@ impl AccountStorage {

/// Converts given slots into field elements
fn slots_as_elements(slots: &[StorageSlot]) -> Vec<Felt> {
slots.iter().flat_map(|slot| slot.as_elements()).collect()
slots
.iter()
.flat_map(|slot| StorageSlotHeader::from(slot).as_elements())
.collect()
}

/// Computes the commitment to the given slots
Expand Down
20 changes: 2 additions & 18 deletions objects/src/accounts/storage/slot/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use vm_core::{
utils::{ByteReader, ByteWriter, Deserializable, Serializable},
EMPTY_WORD, ZERO,
EMPTY_WORD,
};
use vm_processor::DeserializationError;

use super::{map::EMPTY_STORAGE_MAP_ROOT, Felt, StorageMap, Word};
use super::{map::EMPTY_STORAGE_MAP_ROOT, StorageMap, Word};

mod r#type;
pub use r#type::StorageSlotType;
Expand All @@ -20,9 +20,6 @@ pub enum StorageSlot {
}

impl StorageSlot {
/// The number of field elements needed to represent a [StorageSlot] in kernel memory.
pub const NUM_ELEMENTS_PER_STORAGE_SLOT: usize = 8;

/// Returns true if this storage slot has a value equal the default of it's type
pub fn is_default(&self) -> bool {
match self {
Expand All @@ -49,19 +46,6 @@ impl StorageSlot {
StorageSlot::Map(StorageMap::new())
}

/// Returns this storage slot as field elements
///
/// This is done by converting this storage slot into 8 field elements as follows:
/// ```text
/// [SLOT_VALUE, slot_type, 0, 0, 0]
/// ```
pub fn as_elements(&self) -> [Felt; Self::NUM_ELEMENTS_PER_STORAGE_SLOT] {
let mut elements = [ZERO; 8];
elements[0..4].copy_from_slice(&self.value());
elements[4..8].copy_from_slice(&self.slot_type().as_word());
elements
}

/// Returns this storage slot value as a [Word]
///
/// Returns:
Expand Down

0 comments on commit d3a2db7

Please sign in to comment.