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

Commit

Permalink
Handle reorganizations in the state cache (#2490)
Browse files Browse the repository at this point in the history
* Handle reorganizations in the state cache

* Renamed and documented a few methods

* Basic test

* Renamed pending to buffered

* Updated cache on sealed block

* More renames and updated documentation

* Minor doc tweaks
  • Loading branch information
arkpar authored and gavofyork committed Oct 7, 2016
1 parent 5354a09 commit 72ec936
Show file tree
Hide file tree
Showing 3 changed files with 323 additions and 70 deletions.
12 changes: 8 additions & 4 deletions ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ impl Client {
// Enact Verified Block
let parent = chain_has_parent.unwrap();
let last_hashes = self.build_last_hashes(header.parent_hash().clone());
let is_canon = header.parent_hash() == &chain.best_block_hash();
let db = if is_canon { self.state_db.lock().boxed_clone_canon() } else { self.state_db.lock().boxed_clone() };
let db = self.state_db.lock().boxed_clone_canon(&header.parent_hash());

let enact_result = enact_verified(block, engine, self.tracedb.read().tracing_enabled(), db, &parent, last_hashes, self.factories.clone());
if let Err(e) = enact_result {
Expand Down Expand Up @@ -459,6 +458,8 @@ impl Client {
enacted: route.enacted.clone(),
retracted: route.retracted.len()
});
let is_canon = route.enacted.last().map_or(false, |h| h == hash);
state.sync_cache(&route.enacted, &route.retracted, is_canon);
// Final commit to the DB
self.db.read().write_buffered(batch);
chain.commit();
Expand Down Expand Up @@ -533,9 +534,11 @@ impl Client {

/// Get a copy of the best block's state.
pub fn state(&self) -> State {
let header = self.best_block_header();
let header = HeaderView::new(&header);
State::from_existing(
self.state_db.lock().boxed_clone(),
HeaderView::new(&self.best_block_header()).state_root(),
self.state_db.lock().boxed_clone_canon(&header.hash()),
header.state_root(),
self.engine.account_start_nonce(),
self.factories.clone())
.expect("State root of best block header always valid.")
Expand Down Expand Up @@ -1128,6 +1131,7 @@ impl MiningBlockChainClient for Client {
let block_data = block.rlp_bytes();
let route = self.commit_block(block, &h, &block_data);
trace!(target: "client", "Imported sealed block #{} ({})", number, h);
self.state_db.lock().sync_cache(&route.enacted, &route.retracted, false);

let (enacted, retracted) = self.calculate_enacted_retracted(&[route]);
self.miner.chain_new_blocks(self, &[h.clone()], &[], &enacted, &retracted);
Expand Down
11 changes: 6 additions & 5 deletions ethcore/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ impl AccountEntry {
/// use that.
/// ****************************************************************************
///
/// Upon destruction all the local cache data merged into the global cache.
/// The merge might be rejected if current state is non-canonical.
/// Upon destruction all the local cache data propagated into the global cache.
/// Propagated items might be rejected if current state is non-canonical.
///
/// State snapshotting.
///
Expand Down Expand Up @@ -318,7 +318,7 @@ impl State {

/// Destroy the current object and return root and database.
pub fn drop(mut self) -> (H256, StateDB) {
self.commit_cache();
self.propagate_to_global_cache();
(self.root, self.db)
}

Expand Down Expand Up @@ -533,11 +533,12 @@ impl State {
Ok(())
}

fn commit_cache(&mut self) {
/// Propagate local cache into shared canonical state cache.
fn propagate_to_global_cache(&mut self) {
let mut addresses = self.cache.borrow_mut();
trace!("Committing cache {:?} entries", addresses.len());
for (address, a) in addresses.drain().filter(|&(_, ref a)| a.state == AccountState::Committed || a.state == AccountState::CleanFresh) {
self.db.cache_account(address, a.account);
self.db.add_to_account_cache(address, a.account, a.state == AccountState::Committed);
}
}

Expand Down

0 comments on commit 72ec936

Please sign in to comment.