Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Preserve cache on reverting the snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
arkpar committed Oct 6, 2016
1 parent ecf098e commit 07a1c6c
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions ethcore/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ impl AccountEntry {
state: AccountState::Clean,
}
}

// Replace data with another entry but preserve storage cache
fn merge_snapshot(&mut self, other: AccountEntry) {
self.state = other.state;
match other.account {
None => self.account = None,
Some(acc) => match self.account {
Some(ref mut ours) => {
ours.merge_with(acc);
},
None => {},
},
}
}
}

/// Representation of the entire state of all accounts in the system.
Expand Down Expand Up @@ -210,8 +224,12 @@ impl State {
let last = self.snapshots.borrow_mut().pop();
if let Some(mut snapshot) = last {
if let Some(ref mut prev) = self.snapshots.borrow_mut().last_mut() {
for (k, v) in snapshot.drain() {
prev.entry(k).or_insert(v);
if prev.is_empty() {
**prev = snapshot;
} else {
for (k, v) in snapshot.drain() {
prev.entry(k).or_insert(v);
}
}
}
}
Expand All @@ -223,7 +241,16 @@ impl State {
for (k, v) in snapshot.drain() {
match v {
Some(v) => {
self.cache.borrow_mut().insert(k, v);
match self.cache.borrow_mut().entry(k) {
Entry::Occupied(mut e) => {
// Merge snapshotted changes back into the main account
// storage preserving the cache.
e.get_mut().merge_snapshot(v);
},
Entry::Vacant(e) => {
e.insert(v);
}
}
},
None => {
match self.cache.borrow_mut().entry(k) {
Expand Down Expand Up @@ -474,7 +501,7 @@ impl State {
},
None => {
try!(trie.remove(address));
},
}
}
}
}
Expand Down

0 comments on commit 07a1c6c

Please sign in to comment.