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

Commit

Permalink
Added documentation to state/state_cache module (#859)
Browse files Browse the repository at this point in the history
* added comments on fucntions

* added documentation to state_cache

---------

Co-authored-by: fannyguthmann <fanny.guthmann@post.idc.ac.il>
Co-authored-by: Juan Bono <juanbono94@gmail.com>
  • Loading branch information
3 people authored Jul 31, 2023
1 parent be4abb5 commit 644a776
Showing 1 changed file with 13 additions and 0 deletions.
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

0 comments on commit 644a776

Please sign in to comment.