Skip to content

Commit

Permalink
refactor: move back the constant, add doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumuran committed Dec 1, 2024
1 parent d3a2db7 commit 5091ba2
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 28 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,
NUM_ELEMENTS_PER_STORAGE_SLOT,
StorageSlot,
},
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(NUM_ELEMENTS_PER_STORAGE_SLOT / 2)
.chunks(StorageSlot::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, NUM_ELEMENTS_PER_STORAGE_SLOT,
StorageSlot,
},
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(NUM_ELEMENTS_PER_STORAGE_SLOT / 2)
.chunks(StorageSlot::NUM_ELEMENTS_PER_STORAGE_SLOT / 2)
.enumerate()
{
assert_eq!(
Expand Down
5 changes: 1 addition & 4 deletions objects/src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ mod seed;
pub use seed::{get_account_seed, get_account_seed_single};

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

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

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

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

#[derive(Debug, Clone, PartialEq, Eq)]
/// Storage slot header is a lighter version of the [StorageSlot] storing only the type and the
/// top-level value for the slot, and being, in fact, just a thin wrapper around a tuple.
///
/// That is, for complex storage slot (e.g., storage map), the header contains only the commitment
/// to the underlying data.
#[derive(Debug, Clone, Copy, 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 {
/// Returns a new instance of storage slot header from the provided storage slot type and value.
pub fn new(value: &(StorageSlotType, Word)) -> Self {
Self(value.0.clone(), value.1)
Self(value.0, value.1)
}

pub fn as_elements(&self) -> [Felt; NUM_ELEMENTS_PER_STORAGE_SLOT] {
let mut elements = [ZERO; NUM_ELEMENTS_PER_STORAGE_SLOT];
/// Returns this storage slot header 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; StorageSlot::NUM_ELEMENTS_PER_STORAGE_SLOT] {
let mut elements = [ZERO; StorageSlot::NUM_ELEMENTS_PER_STORAGE_SLOT];
elements[0..4].copy_from_slice(&self.1);
elements[4..8].copy_from_slice(&self.0.as_word());
elements
}
}

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

/// 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
6 changes: 0 additions & 6 deletions objects/src/accounts/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ pub use map::StorageMap;
mod header;
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
3 changes: 3 additions & 0 deletions objects/src/accounts/storage/slot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ 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 Down
2 changes: 1 addition & 1 deletion objects/src/accounts/storage/slot/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use vm_processor::DeserializationError;
// ================================================================================================

/// An object that represents the type of a storage slot.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StorageSlotType {
/// Represents a slot that contains a value.
Value,
Expand Down

0 comments on commit 5091ba2

Please sign in to comment.