Merge branch 'master' of github.com:gavofyork/ethcore-util

This commit is contained in:
Gav Wood 2015-12-02 00:19:58 +01:00
commit 88655203b5
2 changed files with 12 additions and 9 deletions

View File

@ -17,7 +17,7 @@ pub trait Trie {
// TODO: consider returning &[u8]... // TODO: consider returning &[u8]...
fn contains(&self, key: &[u8]) -> bool; fn contains(&self, key: &[u8]) -> bool;
fn at<'a>(&'a self, key: &'a [u8]) -> Option<&'a [u8]>; fn at<'a, 'key>(&'a self, key: &'key [u8]) -> Option<&'a [u8]> where 'a: 'key;
fn insert(&mut self, key: &[u8], value: &[u8]); fn insert(&mut self, key: &[u8], value: &[u8]);
fn remove(&mut self, key: &[u8]); fn remove(&mut self, key: &[u8]);
} }
@ -204,7 +204,7 @@ impl TrieDB {
try!(self.fmt_all(self.get_raw_or_lookup(item), f, deepness)); try!(self.fmt_all(self.get_raw_or_lookup(item), f, deepness));
}, },
Node::Branch(ref nodes, ref value) => { Node::Branch(ref nodes, ref value) => {
try!(writeln!(f, "-< ")); try!(writeln!(f, ""));
match value { match value {
&Some(v) => { &Some(v) => {
try!(self.fmt_indent(f, deepness + 1)); try!(self.fmt_indent(f, deepness + 1));
@ -225,18 +225,18 @@ impl TrieDB {
}, },
// empty // empty
Node::NullRoot => { Node::NullRoot => {
writeln!(f, "<empty>"); try!(writeln!(f, "<empty>"));
} }
}; };
Ok(()) Ok(())
} }
fn get<'a>(&'a self, key: &NibbleSlice<'a>) -> Option<&'a [u8]> { fn get<'a, 'key>(&'a self, key: &NibbleSlice<'key>) -> Option<&'a [u8]> where 'a: 'key {
let root_rlp = self.db.lookup(&self.root).expect("Trie root not found!"); let root_rlp = self.db.lookup(&self.root).expect("Trie root not found!");
self.get_from_node(&root_rlp, key) self.get_from_node(&root_rlp, key)
} }
fn get_from_node<'a>(&'a self, node: &'a [u8], key: &NibbleSlice<'a>) -> Option<&'a [u8]> { fn get_from_node<'a, 'key>(&'a self, node: &'a [u8], key: &NibbleSlice<'key>) -> Option<&'a [u8]> where 'a: 'key {
match Node::decoded(node) { match Node::decoded(node) {
Node::Leaf(ref slice, ref value) if key == slice => Some(value), Node::Leaf(ref slice, ref value) if key == slice => Some(value),
Node::Extension(ref slice, ref item) if key.starts_with(slice) => { Node::Extension(ref slice, ref item) if key.starts_with(slice) => {
@ -492,7 +492,7 @@ impl Trie for TrieDB {
unimplemented!(); unimplemented!();
} }
fn at<'a>(&'a self, key: &'a [u8]) -> Option<&'a [u8]> { fn at<'a, 'key>(&'a self, key: &'key [u8]) -> Option<&'a [u8]> where 'a: 'key {
self.get(&NibbleSlice::new(key)) self.get(&NibbleSlice::new(key))
} }
@ -599,7 +599,7 @@ mod tests {
fn test_at_dog() { fn test_at_dog() {
env_logger::init().ok(); env_logger::init().ok();
let mut t = TrieDB::new_memory(); let mut t = TrieDB::new_memory();
let mut v: Vec<(Vec<u8>, Vec<u8>)> = vec![ let v: Vec<(Vec<u8>, Vec<u8>)> = vec![
(From::from("do"), From::from("verb")), (From::from("do"), From::from("verb")),
(From::from("dog"), From::from("puppy")), (From::from("dog"), From::from("puppy")),
(From::from("doge"), From::from("coin")), (From::from("doge"), From::from("coin")),
@ -615,7 +615,9 @@ mod tests {
trace!("{:?}", t); trace!("{:?}", t);
v.sort(); // check lifetime
let _q = t.at(&[b'd', b'o']).unwrap();
assert_eq!(*t.root(), trie_root(v)); assert_eq!(*t.root(), trie_root(v));
} }

View File

@ -62,7 +62,8 @@ pub fn ordered_trie_root(input: Vec<Vec<u8>>) -> H256 {
/// assert_eq!(trie_root(v), H256::from_str(root).unwrap()); /// assert_eq!(trie_root(v), H256::from_str(root).unwrap());
/// } /// }
/// ``` /// ```
pub fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> H256 { pub fn trie_root(mut input: Vec<(Vec<u8>, Vec<u8>)>) -> H256 {
input.sort();
let gen_input = input let gen_input = input
.into_iter() .into_iter()
.map(|(k, v)| (as_nibbles(&k), v)) .map(|(k, v)| (as_nibbles(&k), v))