From f38d34919bfcee6583b358ebf7d488763f082304 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Tue, 19 Sep 2017 11:34:13 +0200 Subject: [PATCH] Random cleanups / improvements to a state (#6472) --- ethcore/src/state/mod.rs | 64 +++++++++++++++++------------------ ethcore/src/state/substate.rs | 8 ++--- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/ethcore/src/state/mod.rs b/ethcore/src/state/mod.rs index 3935d7d86..2472557a4 100644 --- a/ethcore/src/state/mod.rs +++ b/ethcore/src/state/mod.rs @@ -450,22 +450,19 @@ impl State { // // In all other cases account is read as clean first, and after that made // 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 !checkpoint.contains_key(address) { - checkpoint.insert(address.clone(), self.cache.borrow_mut().insert(address.clone(), account)); - return; - } + checkpoint.entry(*address).or_insert(old_value); } } - self.cache.borrow_mut().insert(address.clone(), account); } fn note_cache(&self, address: &Address) { if let Some(ref mut checkpoint) = self.checkpoints.borrow_mut().last_mut() { - if !checkpoint.contains_key(address) { - checkpoint.insert(address.clone(), self.cache.borrow().get(address).map(AccountEntry::clone_dirty)); - } + checkpoint.entry(*address) + .or_insert_with(|| self.cache.borrow().get(address).map(AccountEntry::clone_dirty)); } } @@ -559,9 +556,8 @@ impl State { } }); - match trie_res { - None => {} - Some(res) => return res, + if let Some(res) = trie_res { + return res; } // otherwise cache the account localy and cache storage key there. @@ -868,27 +864,29 @@ impl State { // load required account data from the databases. fn update_account_cache(require: RequireCache, account: &mut Account, state_db: &B, db: &HashDB) { - match (account.is_cached(), require) { - (true, _) | (false, RequireCache::None) => {} - (false, require) => { - // if there's already code in the global cache, always cache it - // locally. - let hash = account.code_hash(); - match state_db.get_cached_code(&hash) { - Some(code) => account.cache_given_code(code), - None => match require { - RequireCache::None => {}, - RequireCache::Code => { - if let Some(code) = account.cache_code(db) { - // propagate code loaded from the database to - // the global code cache. - state_db.cache_code(hash, code) - } - } - RequireCache::CodeSize => { - account.cache_code_size(db); - } + if let RequireCache::None = require { + return; + } + + if account.is_cached() { + return; + } + + // if there's already code in the global cache, always cache it localy + let hash = account.code_hash(); + match state_db.get_cached_code(&hash) { + Some(code) => account.cache_given_code(code), + None => match require { + RequireCache::None => {}, + RequireCache::Code => { + if let Some(code) = account.cache_code(db) { + // propagate code loaded from the database to + // the global code cache. + state_db.cache_code(hash, code) } + }, + RequireCache::CodeSize => { + account.cache_code_size(db); } } } @@ -938,7 +936,7 @@ impl State { /// 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> { - 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. diff --git a/ethcore/src/state/substate.rs b/ethcore/src/state/substate.rs index 4acc54114..8d90c64d1 100644 --- a/ethcore/src/state/substate.rs +++ b/ethcore/src/state/substate.rs @@ -50,11 +50,11 @@ impl Substate { /// Merge secondary substate `s` into self, accruing each element correspondingly. pub fn accrue(&mut self, s: Substate) { - self.suicides.extend(s.suicides.into_iter()); - self.touched.extend(s.touched.into_iter()); - self.logs.extend(s.logs.into_iter()); + self.suicides.extend(s.suicides); + self.touched.extend(s.touched); + self.logs.extend(s.logs); 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.