From c61da07516bfddad7850ec7767a8e5ea362852c2 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Wed, 31 Aug 2016 14:28:56 +0200 Subject: [PATCH] always process trie death row on commit, add more tracing --- ethcore/src/block.rs | 2 +- util/src/trie/triedbmut.rs | 32 ++++++++++++++++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/ethcore/src/block.rs b/ethcore/src/block.rs index 784e71dc0..d3b4ac168 100644 --- a/ethcore/src/block.rs +++ b/ethcore/src/block.rs @@ -498,7 +498,7 @@ pub fn enact( { if ::log::max_log_level() >= ::log::LogLevel::Trace { let s = try!(State::from_existing(db.boxed_clone(), parent.state_root().clone(), engine.account_start_nonce(), factories.clone())); - trace!("enact(): root={}, author={}, author_balance={}\n", s.root(), header.author(), s.balance(&header.author())); + trace!(target: "enact", "num={}, root={}, author={}, author_balance={}\n", header.number(), s.root(), header.author(), s.balance(&header.author())); } } diff --git a/util/src/trie/triedbmut.rs b/util/src/trie/triedbmut.rs index ca5fb015f..8d3d7d3d3 100644 --- a/util/src/trie/triedbmut.rs +++ b/util/src/trie/triedbmut.rs @@ -56,11 +56,11 @@ impl From for NodeHandle { } } -fn empty_children() -> [Option; 16] { - [ +fn empty_children() -> Box<[Option; 16]> { + Box::new([ None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, - ] + ]) } /// Node types in the Trie. @@ -78,7 +78,7 @@ enum Node { /// The child node is always a branch. Extension(Bytes, NodeHandle), /// A branch has up to 16 children and an optional value. - Branch([Option; 16], Option) + Branch(Box<[Option; 16]>, Option) } impl Node { @@ -820,19 +820,19 @@ impl<'a> TrieDBMut<'a> { /// Commit the in-memory changes to disk, freeing their storage and /// updating the state root. pub fn commit(&mut self) { - let handle = match self.root_handle() { - NodeHandle::Hash(_) => return, // no changes necessary. - NodeHandle::InMemory(h) => h, - }; - trace!(target: "trie", "Committing trie changes to db."); - // kill all the nodes on death row. + // always kill all the nodes on death row. trace!(target: "trie", "{:?} nodes to remove from db", self.death_row.len()); for hash in self.death_row.drain() { self.db.remove(&hash); } + let handle = match self.root_handle() { + NodeHandle::Hash(_) => return, // no changes necessary. + NodeHandle::InMemory(h) => h, + }; + match self.storage.destroy(handle) { Stored::New(node) => { let root_rlp = node.into_rlp(|child, stream| self.commit_node(child, stream)); @@ -906,21 +906,29 @@ impl<'a> TrieMut for TrieDBMut<'a> { return self.remove(key); } + trace!(target: "trie", "insert: key={:?}, value={:?}", key.pretty(), value.pretty()); + let root_handle = self.root_handle(); - let (new_handle, _) = try!(self.insert_at(root_handle, NibbleSlice::new(key), value.to_owned())); + let (new_handle, changed) = try!(self.insert_at(root_handle, NibbleSlice::new(key), value.to_owned())); + + trace!(target: "trie", "insert: altered trie={}", changed); self.root_handle = NodeHandle::InMemory(new_handle); Ok(()) } fn remove(&mut self, key: &[u8]) -> super::Result<()> { + trace!(target: "trie", "remove: key={:?}", key.pretty()); + let root_handle = self.root_handle(); let key = NibbleSlice::new(key); match try!(self.remove_at(root_handle, key)) { - Some((handle, _)) => { + Some((handle, changed)) => { + trace!(target: "trie", "remove: altered trie={}", changed); self.root_handle = NodeHandle::InMemory(handle); } None => { + trace!(target: "trie", "remove: obliterated trie"); self.root_handle = NodeHandle::Hash(SHA3_NULL_RLP); *self.root = SHA3_NULL_RLP; }