Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Added documentation to state/state_cache module #859

Merged
merged 6 commits into from
Jul 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/state/state_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::collections::{HashMap, HashSet};
// TODO: Change [u8; 32] to Felt252.
pub type StorageEntry = (Address, [u8; 32]);

/// Struct that keeps track of initial and written state of contracts
#[derive(Default, Clone, Debug, Eq, Getters, MutGetters, PartialEq)]
pub struct StateCache {
// Reader's cached information; initial values, read before any write operation (per cell)
Expand Down Expand Up @@ -38,6 +39,8 @@ pub struct StateCache {

impl StateCache {
#[allow(clippy::too_many_arguments)]

/// Create a new StateCache with given initial and written values for testing
pub fn new(
class_hash_initial_values: HashMap<Address, ClassHash>,
compiled_class_hash_initial_values: HashMap<ClassHash, CompiledClass>,
Expand All @@ -62,6 +65,7 @@ impl StateCache {
}
}

/// Define a default state for testing
pub(crate) fn default() -> Self {
Self {
class_hash_initial_values: HashMap::new(),
Expand All @@ -76,6 +80,7 @@ impl StateCache {
}
}

/// Creates a new instance of `StateCache` for testing purposes with the provided initial values and writes.
#[allow(clippy::too_many_arguments)]
pub fn new_for_testing(
class_hash_initial_values: HashMap<Address, [u8; 32]>,
Expand All @@ -101,13 +106,15 @@ impl StateCache {
}
}

/// Get the class hash for a given address
pub(crate) fn get_class_hash(&self, contract_address: &Address) -> Option<&ClassHash> {
if self.class_hash_writes.contains_key(contract_address) {
return self.class_hash_writes.get(contract_address);
}
self.class_hash_initial_values.get(contract_address)
}

/// Get the compiled hash for a given class hash
#[allow(dead_code)]
pub(crate) fn get_compiled_class_hash(&self, class_hash: &ClassHash) -> Option<&CompiledClass> {
if self.compiled_class_hash_writes.contains_key(class_hash) {
Expand All @@ -116,20 +123,23 @@ impl StateCache {
self.compiled_class_hash_initial_values.get(class_hash)
}

/// Get the nonce for a given address
pub(crate) fn get_nonce(&self, contract_address: &Address) -> Option<&Felt252> {
if self.nonce_writes.contains_key(contract_address) {
return self.nonce_writes.get(contract_address);
}
self.nonce_initial_values.get(contract_address)
}

/// Get the storage for a given storage entry
pub(crate) fn get_storage(&self, storage_entry: &StorageEntry) -> Option<&Felt252> {
if self.storage_writes.contains_key(storage_entry) {
return self.storage_writes.get(storage_entry);
}
self.storage_initial_values.get(storage_entry)
}

/// Update written values
pub(crate) fn update_writes(
&mut self,
address_to_class_hash: &HashMap<Address, ClassHash>,
Expand All @@ -144,6 +154,7 @@ impl StateCache {
self.storage_writes.extend(storage_updates.clone());
}

/// Set initial values
pub fn set_initial_values(
&mut self,
address_to_class_hash: &HashMap<Address, ClassHash>,
Expand All @@ -170,6 +181,7 @@ impl StateCache {
}

// TODO: Remove warning inhibitor when finally used.
/// Get all contract addresses that have been accessed
#[allow(dead_code)]
pub(crate) fn get_accessed_contract_addresses(&self) -> HashSet<Address> {
let mut set: HashSet<Address> = HashSet::with_capacity(self.class_hash_writes.len());
Expand Down Expand Up @@ -204,6 +216,7 @@ impl StateCache {
}
}

/// Unit tests for StateCache
#[cfg(test)]
mod tests {
use crate::services::api::contract_classes::deprecated_contract_class::ContractClass;
Expand Down