Random cleanups / improvements to a state (#6472)

This commit is contained in:
Marek Kotewicz 2017-09-19 11:34:13 +02:00 committed by Gav Wood
parent 9196c7268a
commit f38d34919b
2 changed files with 35 additions and 37 deletions

View File

@ -450,22 +450,19 @@ impl<B: Backend> State<B> {
// //
// In all other cases account is read as clean first, and after that made // In all other cases account is read as clean first, and after that made
// dirty in and added to the checkpoint with `note_cache`. // dirty in and added to the checkpoint with `note_cache`.
if account.is_dirty() { let is_dirty = account.is_dirty();
let old_value = self.cache.borrow_mut().insert(*address, account);
if is_dirty {
if let Some(ref mut checkpoint) = self.checkpoints.borrow_mut().last_mut() { if let Some(ref mut checkpoint) = self.checkpoints.borrow_mut().last_mut() {
if !checkpoint.contains_key(address) { checkpoint.entry(*address).or_insert(old_value);
checkpoint.insert(address.clone(), self.cache.borrow_mut().insert(address.clone(), account));
return;
} }
} }
} }
self.cache.borrow_mut().insert(address.clone(), account);
}
fn note_cache(&self, address: &Address) { fn note_cache(&self, address: &Address) {
if let Some(ref mut checkpoint) = self.checkpoints.borrow_mut().last_mut() { if let Some(ref mut checkpoint) = self.checkpoints.borrow_mut().last_mut() {
if !checkpoint.contains_key(address) { checkpoint.entry(*address)
checkpoint.insert(address.clone(), self.cache.borrow().get(address).map(AccountEntry::clone_dirty)); .or_insert_with(|| self.cache.borrow().get(address).map(AccountEntry::clone_dirty));
}
} }
} }
@ -559,9 +556,8 @@ impl<B: Backend> State<B> {
} }
}); });
match trie_res { if let Some(res) = trie_res {
None => {} return res;
Some(res) => return res,
} }
// otherwise cache the account localy and cache storage key there. // otherwise cache the account localy and cache storage key there.
@ -868,11 +864,15 @@ impl<B: Backend> State<B> {
// load required account data from the databases. // load required account data from the databases.
fn update_account_cache(require: RequireCache, account: &mut Account, state_db: &B, db: &HashDB) { fn update_account_cache(require: RequireCache, account: &mut Account, state_db: &B, db: &HashDB) {
match (account.is_cached(), require) { if let RequireCache::None = require {
(true, _) | (false, RequireCache::None) => {} return;
(false, require) => { }
// if there's already code in the global cache, always cache it
// locally. if account.is_cached() {
return;
}
// if there's already code in the global cache, always cache it localy
let hash = account.code_hash(); let hash = account.code_hash();
match state_db.get_cached_code(&hash) { match state_db.get_cached_code(&hash) {
Some(code) => account.cache_given_code(code), Some(code) => account.cache_given_code(code),
@ -884,15 +884,13 @@ impl<B: Backend> State<B> {
// the global code cache. // the global code cache.
state_db.cache_code(hash, code) state_db.cache_code(hash, code)
} }
} },
RequireCache::CodeSize => { RequireCache::CodeSize => {
account.cache_code_size(db); account.cache_code_size(db);
} }
} }
} }
} }
}
}
/// Check caches for required data /// Check caches for required data
/// First searches for account in the local, then the shared cache. /// First searches for account in the local, then the shared cache.
@ -938,7 +936,7 @@ impl<B: Backend> State<B> {
/// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too. /// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too.
fn require<'a>(&'a self, a: &Address, require_code: bool) -> trie::Result<RefMut<'a, Account>> { fn require<'a>(&'a self, a: &Address, require_code: bool) -> trie::Result<RefMut<'a, Account>> {
self.require_or_from(a, require_code, || Account::new_basic(U256::from(0u8), self.account_start_nonce), |_|{}) self.require_or_from(a, require_code, || Account::new_basic(0u8.into(), self.account_start_nonce), |_|{})
} }
/// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too. /// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too.

View File

@ -50,11 +50,11 @@ impl Substate {
/// Merge secondary substate `s` into self, accruing each element correspondingly. /// Merge secondary substate `s` into self, accruing each element correspondingly.
pub fn accrue(&mut self, s: Substate) { pub fn accrue(&mut self, s: Substate) {
self.suicides.extend(s.suicides.into_iter()); self.suicides.extend(s.suicides);
self.touched.extend(s.touched.into_iter()); self.touched.extend(s.touched);
self.logs.extend(s.logs.into_iter()); self.logs.extend(s.logs);
self.sstore_clears_count = self.sstore_clears_count + s.sstore_clears_count; self.sstore_clears_count = self.sstore_clears_count + s.sstore_clears_count;
self.contracts_created.extend(s.contracts_created.into_iter()); self.contracts_created.extend(s.contracts_created);
} }
/// Get the cleanup mode object from this. /// Get the cleanup mode object from this.