diff --git a/crates/blockifier/src/execution/alias_keys.rs b/crates/blockifier/src/execution/alias_keys.rs index 58ef8141272..90e8ffb8b9a 100644 --- a/crates/blockifier/src/execution/alias_keys.rs +++ b/crates/blockifier/src/execution/alias_keys.rs @@ -5,11 +5,27 @@ use starknet_types_core::felt::Felt; use crate::state::cached_state::{CachedState, StorageEntry}; use crate::state::state_api::{StateReader, StateResult}; +/// Keys in contract addresses up to this address don't get aliases. +const N_SAVED_CONTRACT_ADDRESSES: u8 = 16; +/// The alias of a felt X up to this number is X. +/// This trivial mappings don't write to the alias contract. +const N_SELF_ALIASES: u8 = 128; + /// Returns the number of storage keys charged for new allocated aliases. +/// Counts keys that were previously empty and are now filled. pub fn n_charged_storage_keys( - _state: &CachedState, - _storage_changes: &HashMap, + state: &CachedState, + storage_changes: &HashMap, ) -> StateResult { - // TODO: Implement this function - Ok(0) + let mut n_storage_keys = 0; + for ((contract_address, storage_key), new_value) in storage_changes { + if contract_address.0.0 >= N_SAVED_CONTRACT_ADDRESSES.into() + && storage_key.0.0 >= N_SELF_ALIASES.into() + && state.get_storage_at(*contract_address, *storage_key)? == Felt::ZERO + && new_value != &Felt::ZERO + { + n_storage_keys += 1; + } + } + Ok(n_storage_keys) } diff --git a/crates/starknet_api/src/core.rs b/crates/starknet_api/src/core.rs index 8296075c8d4..c9bf5413fe9 100644 --- a/crates/starknet_api/src/core.rs +++ b/crates/starknet_api/src/core.rs @@ -347,7 +347,7 @@ pub struct StateDiffCommitment(pub PoseidonHash); derive_more:: Deref, )] #[display(fmt = "{}", "_0.to_fixed_hex_string()")] -pub struct PatriciaKey(StarkHash); +pub struct PatriciaKey(pub StarkHash); // 2**251 pub const PATRICIA_KEY_UPPER_BOUND: &str =