Skip to content

Commit

Permalink
In-memory trie operations (openethereum#1408)
Browse files Browse the repository at this point in the history
* small cleanups in trie

* Memory trie skeleton

* decode nodes from RLP

* memorytrie -> memorytriedb

* implement Trie for MemoryTrie

* partially implement insert

* implement trie insertion

* don't load whole trie into memory, lookup across memory and db

* re-implement insertion and lazily load necessary nodes from DB

* node removal w/o fixing

* reduce churn in node storage

* finish remove implementation

* committing the in-memory trie

* reload root node after commit

* generate elastic arrays when converting nodes to rlp

* port triedbmut tests over to memorytriedb, fix a few bugs.

* hash count and dirty flag

* initial attempt for node inspection

* back to skeleton

* inspection framework

* implement insertion

* no panic paths in insert

* implement deletion without fixing

* node fixing

* commit nodes to db

* tracing targets and bugfix

* get tests to pass with a lot of tracing

* set playpen iterations to 10

* remove some tracing statements

* make TrieMut::root take &mut self

* replace triedbmut with memorytriedb

* treat empty insert value as removal

* add test for null insert

* fix some style concerns

* trie: use nodehandle for root_node, minor cleanup
  • Loading branch information
rphmeier authored and arkpar committed Jul 14, 2016
1 parent 598833d commit 517c705
Show file tree
Hide file tree
Showing 7 changed files with 823 additions and 707 deletions.
6 changes: 5 additions & 1 deletion util/src/trie/fatdbmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ impl<'db> FatDBMut<'db> {
}

impl<'db> TrieMut for FatDBMut<'db> {
fn root(&self) -> &H256 {
fn root(&mut self) -> &H256 {
self.raw.root()
}

fn is_empty(&self) -> bool {
self.raw.is_empty()
}

fn contains(&self, key: &[u8]) -> bool {
self.raw.contains(&key.sha3())
}
Expand Down
1 change: 0 additions & 1 deletion util/src/trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub mod sectriedb;
pub mod sectriedbmut;

mod fatdb;

mod fatdbmut;

pub use self::trietraits::{Trie, TrieMut};
Expand Down
6 changes: 3 additions & 3 deletions util/src/trie/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ pub enum Node<'a> {
/// Null trie node; could be an empty root or an empty branch entry.
Empty,
/// Leaf node; has key slice and value. Value may not be empty.
Leaf(NibbleSlice<'a>, &'a[u8]),
Leaf(NibbleSlice<'a>, &'a [u8]),
/// Extension node; has key slice and node data. Data may not be null.
Extension(NibbleSlice<'a>, &'a[u8]),
Extension(NibbleSlice<'a>, &'a [u8]),
/// Branch node; has array of 16 child nodes (each possibly null) and an optional immediate node data.
Branch([&'a[u8]; 16], Option<&'a [u8]>)
Branch([&'a [u8]; 16], Option<&'a [u8]>)
}

impl<'a> Node<'a> {
Expand Down
8 changes: 7 additions & 1 deletion util/src/trie/sectriedbmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ impl<'db> SecTrieDBMut<'db> {
}

impl<'db> TrieMut for SecTrieDBMut<'db> {
fn root(&self) -> &H256 { self.raw.root() }
fn root(&mut self) -> &H256 {
self.raw.root()
}

fn is_empty(&self) -> bool {
self.raw.is_empty()
}

fn contains(&self, key: &[u8]) -> bool {
self.raw.contains(&key.sha3())
Expand Down
5 changes: 2 additions & 3 deletions util/src/trie/triedb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ impl<'db> TrieDB<'db> {
pub fn to_map(hashes: Vec<H256>) -> HashMap<H256, u32> {
let mut r: HashMap<H256, u32> = HashMap::new();
for h in hashes.into_iter() {
let c = *r.get(&h).unwrap_or(&0);
r.insert(h, c + 1);
*r.entry(h).or_insert(0) += 1;
}
r
}
Expand Down Expand Up @@ -184,7 +183,7 @@ impl<'db> TrieDB<'db> {

/// Return optional data for a key given as a `NibbleSlice`. Returns `None` if no data exists.
fn do_lookup<'a, 'key>(&'a self, key: &NibbleSlice<'key>) -> Option<&'a [u8]> where 'a: 'key {
let root_rlp = self.db.get(&self.root).expect("Trie root not found!");
let root_rlp = self.root_data();
self.get_from_node(&root_rlp, key)
}

Expand Down
Loading

0 comments on commit 517c705

Please sign in to comment.