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 9f797b5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
31 changes: 26 additions & 5 deletions objects/src/accounts/storage/header.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::ToOwned;

use alloc::vec::Vec;

use vm_core::{
Expand All @@ -6,12 +8,34 @@ use vm_core::{
};
use vm_processor::DeserializationError;

use super::{AccountStorage, Felt, StorageSlotType, Word};
use super::{AccountStorage, Felt, StorageSlotType, Word, StorageSlot, 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.to_owned(), 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 @@ -70,10 +94,7 @@ impl AccountStorageHeader {
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
StorageSlotHeader::new(slot).as_elements()
})
.collect()
}
Expand Down
11 changes: 9 additions & 2 deletions objects/src/accounts/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ 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] or [StorageSlotHeader] in
/// kernel memory.
pub const NUM_ELEMENTS_PER_STORAGE_SLOT: usize = 8;

// ACCOUNT STORAGE
// ================================================================================================
Expand Down Expand Up @@ -252,7 +259,7 @@ 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 9f797b5

Please sign in to comment.