Skip to content

Commit

Permalink
Add new function to get storage changes from snapshot or initial
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosNicolau committed Aug 20, 2024
1 parent 8747004 commit 2a28162
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/rollbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ impl<K: Clone + Hash + Eq, V: Clone> RollbackableHashMap<K, V> {
pub fn get_logs_after_snapshot(
&self,
snapshot: <RollbackableHashMap<K, V> as Rollbackable>::Snapshot,
) -> HashMap<K, V> {
) -> HashMap<K, (Option<V>, V)> {
let mut changes = HashMap::new();

for (key, value) in self.map.iter() {
if !snapshot.contains_key(key) || snapshot.get(key).is_none() {
changes.insert(key.clone(), value.clone());
}
changes.insert(key.clone(), (snapshot.get(key).cloned(), value.clone()));
}

changes
Expand Down
16 changes: 16 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,22 @@ impl VMState {
})
.collect()
}

/// returns the values that have actually changed from the snapshot storage or the initial if the latter does not exist
/// also, a flag is also retured indicating if the value existed on the initial storage
pub fn get_storage_changes_from_snapshot(
&self,
snapshot: <RollbackableHashMap<StorageKey, U256> as Rollbackable>::Snapshot,
) -> Vec<(StorageKey, (Option<U256>, U256, bool))> {
self.storage_changes
.get_logs_after_snapshot(snapshot)
.iter()
.map(|(key, (before, after))| {
let initial = self.initial_values.get(key).unwrap_or(&None);
(*key, (before.or(*initial), *after, initial.is_none()))
})
.collect()
}
}

#[derive(Clone, Default, PartialEq, Debug)]
Expand Down

0 comments on commit 2a28162

Please sign in to comment.