Skip to content

Commit

Permalink
add method for getting storage changes after a snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
joonazan committed May 6, 2024
1 parent 3c90515 commit 8be7c39
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/modified_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ impl ModifiedWorld {
}
}

pub fn get_storage_changes(&self) -> &BTreeMap<(H160, U256), U256> {
self.storage_changes.as_ref()
}

pub fn get_storage_changes_after(
&self,
snapshot: &Snapshot,
) -> BTreeMap<(H160, U256), (Option<U256>, U256)> {
self.storage_changes.changes_after(snapshot.storage_changes)
}

pub fn prepaid_for_write(&self, address: H160, key: U256) -> u32 {
self.paid_changes
.as_ref()
Expand All @@ -121,10 +132,6 @@ impl ModifiedWorld {
self.paid_changes.insert((address, key), price)
}

pub fn get_storage_changes(&self) -> &BTreeMap<(H160, U256), U256> {
self.storage_changes.as_ref()
}

pub(crate) fn record_event(&mut self, event: Event) {
self.events.push(event);
}
Expand Down
16 changes: 15 additions & 1 deletion src/rollback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,25 @@ pub struct RollbackableMap<K: Ord, V> {
old_entries: Vec<(K, Option<V>)>,
}

impl<K: Ord + Clone, V> RollbackableMap<K, V> {
impl<K: Ord + Clone, V: Clone> RollbackableMap<K, V> {
pub fn insert(&mut self, key: K, value: V) {
self.old_entries
.push((key.clone(), self.map.insert(key, value)));
}

pub(crate) fn changes_after(
&self,
snapshot: <Self as Rollback>::Snapshot,
) -> BTreeMap<K, (Option<V>, V)> {
let mut changes = BTreeMap::new();
for (key, old_value) in self.old_entries[snapshot..].iter().rev() {
changes
.entry(key.clone())
.and_modify(|(old, _)| *old = old_value.clone())
.or_insert((old_value.clone(), self.map.get(key).unwrap().clone()));
}
changes
}
}

impl<K: Ord, V> Rollback for RollbackableMap<K, V> {
Expand Down

0 comments on commit 8be7c39

Please sign in to comment.