Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DictStateReader #2897

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion crates/cheatnet/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ pub fn build_testing_state() -> DictStateReader {
DictStateReader {
address_to_class_hash,
class_hash_to_class,
..Default::default()
}
}

Expand Down
25 changes: 5 additions & 20 deletions crates/runtime/src/starknet/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use blockifier::execution::contract_class::ContractClass;
use blockifier::state::cached_state::StorageEntry;
use blockifier::state::errors::StateError;
use blockifier::state::state_api::{StateReader, StateResult};
use starknet_api::core::CompiledClassHash;
Expand All @@ -11,11 +10,8 @@ use std::collections::HashMap;
/// A simple implementation of `StateReader` using `HashMap`s as storage.
#[derive(Debug, Default)]
pub struct DictStateReader {
pub storage_view: HashMap<StorageEntry, Felt>,
pub address_to_nonce: HashMap<ContractAddress, Nonce>,
pub address_to_class_hash: HashMap<ContractAddress, ClassHash>,
pub class_hash_to_class: HashMap<ClassHash, ContractClass>,
pub class_hash_to_compiled_class_hash: HashMap<ClassHash, CompiledClassHash>,
}

impl StateReader for DictStateReader {
Expand All @@ -24,21 +20,15 @@ impl StateReader for DictStateReader {
contract_address: ContractAddress,
key: StorageKey,
) -> StateResult<Felt> {
self.storage_view
.get(&(contract_address, key))
.copied()
.ok_or(StateError::StateReadError(format!(
Err(StateError::StateReadError(format!(
"Unable to get storage at address: {contract_address:?} and key: {key:?} form DictStateReader"
)))
}

fn get_nonce_at(&self, contract_address: ContractAddress) -> StateResult<Nonce> {
self.address_to_nonce
.get(&contract_address)
.copied()
.ok_or(StateError::StateReadError(format!(
"Unable to get nonce at {contract_address:?} from DictStateReader"
)))
Err(StateError::StateReadError(format!(
"Unable to get nonce at {contract_address:?} from DictStateReader"
)))
}

fn get_class_hash_at(&self, contract_address: ContractAddress) -> StateResult<ClassHash> {
Expand All @@ -57,11 +47,6 @@ impl StateReader for DictStateReader {
}

fn get_compiled_class_hash(&self, class_hash: ClassHash) -> StateResult<CompiledClassHash> {
let compiled_class_hash = self
.class_hash_to_compiled_class_hash
.get(&class_hash)
.copied()
.unwrap_or_default();
Ok(compiled_class_hash)
Err(StateError::UndeclaredClassHash(class_hash))
}
}
Loading