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

feat(blockifier): add n_allocated_keys #2149

Merged
merged 1 commit into from
Nov 26, 2024
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
37 changes: 35 additions & 2 deletions crates/blockifier/src/state/cached_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@ impl StateCache {
/// reads. Assumes (and enforces) all initial reads are cached.
pub fn to_state_diff(&self) -> StateChanges {
let state_maps = self.writes.diff(&self.initial_reads);
StateChanges { state_maps }
let allocated_keys =
AllocatedKeys::from_storage_diff(&self.writes.storage, &self.initial_reads.storage);
StateChanges { state_maps, allocated_keys }
}

fn declare_contract(&mut self, class_hash: ClassHash) {
Expand Down Expand Up @@ -677,11 +679,42 @@ impl StateChangesKeys {
}
}

#[cfg_attr(any(feature = "testing", test), derive(Clone))]
#[derive(Debug, Default, Eq, PartialEq)]
pub struct AllocatedKeys(HashSet<StorageEntry>);

impl AllocatedKeys {
pub fn update(&mut self, state_change: &StateChanges) {
self.0.extend(&state_change.allocated_keys.0);
// TODO: Remove keys that are set back to zero.
}

pub fn len(&self) -> usize {
self.0.len()
}

pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

/// Collect entries that turn zero -> nonzero.
pub fn from_storage_diff(
_updated_storage: &HashMap<StorageEntry, Felt>,
_base_storage: &HashMap<StorageEntry, Felt>,
) -> Self {
Self(
HashSet::new(),
// TODO: Calculate the difference between the updated_storage and the base_storage.
)
}
}

/// Holds the state changes.
#[cfg_attr(any(feature = "testing", test), derive(Clone))]
#[derive(Debug, Default, Eq, PartialEq)]
pub struct StateChanges {
pub state_maps: StateMaps,
pub allocated_keys: AllocatedKeys,
}

impl StateChanges {
Expand All @@ -691,8 +724,8 @@ impl StateChanges {
let mut merged_state_changes = Self::default();
for state_change in state_changes {
merged_state_changes.state_maps.extend(&state_change.state_maps);
merged_state_changes.allocated_keys.update(&state_change);
}

merged_state_changes
}

Expand Down
Loading