fixed trie get and trie print

This commit is contained in:
debris 2015-12-01 23:14:38 +01:00
parent c1c3152094
commit 0cec68b59e

View File

@ -25,8 +25,7 @@ pub trait Trie {
#[derive(Eq, PartialEq, Debug)] #[derive(Eq, PartialEq, Debug)]
pub enum Node<'a> { pub enum Node<'a> {
Leaf(NibbleSlice<'a>, &'a[u8]), Leaf(NibbleSlice<'a>, &'a[u8]),
ExtensionRaw(NibbleSlice<'a>, &'a[u8]), Extension(NibbleSlice<'a>, &'a[u8]),
ExtensionSha3(NibbleSlice<'a>, &'a[u8]),
Branch(Option<[&'a[u8]; 16]>, &'a [u8]) Branch(Option<[&'a[u8]; 16]>, &'a [u8])
} }
@ -41,13 +40,7 @@ impl <'a>Node<'a> {
// fed back into this function or inline RLP which can be fed back into this function). // fed back into this function or inline RLP which can be fed back into this function).
Prototype::List(2) => match NibbleSlice::from_encoded(r.at(0).data()) { Prototype::List(2) => match NibbleSlice::from_encoded(r.at(0).data()) {
(slice, true) => Node::Leaf(slice, r.at(1).data()), (slice, true) => Node::Leaf(slice, r.at(1).data()),
(slice, false) => match r.at(1).raw().len() { (slice, false) => Node::Extension(slice, r.at(1).raw())
// its just raw extension
0...31 => Node::ExtensionRaw(slice, r.at(1).raw()),
// its SHA3 of raw extension + the length of sha3
33 => Node::ExtensionSha3(slice, r.at(1).data()),
_ => { panic!(); }
}
}, },
// branch - first 16 are nodes, 17th is a value (or empty). // branch - first 16 are nodes, 17th is a value (or empty).
Prototype::List(17) => { Prototype::List(17) => {
@ -62,6 +55,7 @@ impl <'a>Node<'a> {
} }
} }
// todo: should check length before encoding, cause it may just be sha3 of data
pub fn encoded(&self) -> Bytes { pub fn encoded(&self) -> Bytes {
match *self { match *self {
Node::Leaf(ref slice, ref value) => { Node::Leaf(ref slice, ref value) => {
@ -70,18 +64,12 @@ impl <'a>Node<'a> {
stream.append(value); stream.append(value);
stream.out() stream.out()
}, },
Node::ExtensionRaw(ref slice, ref raw_rlp) => { Node::Extension(ref slice, ref raw_rlp) => {
let mut stream = RlpStream::new_list(2); let mut stream = RlpStream::new_list(2);
stream.append(&slice.encoded(false)); stream.append(&slice.encoded(false));
stream.append_raw(raw_rlp, 1); stream.append_raw(raw_rlp, 1);
stream.out() stream.out()
}, },
Node::ExtensionSha3(ref slice, ref sha3) => {
let mut stream = RlpStream::new_list(2);
stream.append(&slice.encoded(false));
stream.append(sha3);
stream.out()
},
Node::Branch(Some(ref nodes), ref value) => { Node::Branch(Some(ref nodes), ref value) => {
let mut stream = RlpStream::new_list(17); let mut stream = RlpStream::new_list(17);
for i in 0..16 { stream.append_raw(nodes[i], 1); } for i in 0..16 { stream.append_raw(nodes[i], 1); }
@ -200,27 +188,21 @@ impl TrieDB {
let node = Node::decoded(node); let node = Node::decoded(node);
match node { match node {
Node::Leaf(slice, value) => try!(writeln!(f, "Leaf {:?}, {:?}", slice, value.pretty())), Node::Leaf(slice, value) => try!(writeln!(f, "Leaf {:?}, {:?}", slice, value.pretty())),
Node::ExtensionRaw(ref slice, ref item) => { Node::Extension(ref slice, ref item) => {
try!(write!(f, "Extension (raw): {:?} ", slice)); try!(write!(f, "Extension: {:?} ,", slice));
try!(self.fmt_all(item, f, deepness + 1)); try!(self.fmt_all(self.get_raw_or_lookup(item), f, deepness + 1));
},
Node::ExtensionSha3(ref slice, sha3) => {
try!(write!(f, "Extension (sha3): {:?} ", slice));
let rlp = self.db.lookup(&H256::from_slice(sha3)).expect("sha3 not found!");
try!(self.fmt_all(rlp, f, deepness + 1));
}, },
Node::Branch(Some(ref nodes), ref value) => { Node::Branch(Some(ref nodes), ref value) => {
try!(writeln!(f, "Branch: ")); try!(writeln!(f, "Branch: "));
try!(self.fmt_indent(f, deepness + 1)); try!(self.fmt_indent(f, deepness + 1));
try!(writeln!(f, ": {:?}", value.pretty())); try!(writeln!(f, ": {:?}", value.pretty()));
for i in 0..16 { for i in 0..16 {
match Node::decoded(nodes[i]) { let rlp = Rlp::new(nodes[i]);
Node::Branch(None, _) => (), if rlp.is_data() && rlp.size() != 0 {
_ => {
try!(self.fmt_indent(f, deepness + 1)); try!(self.fmt_indent(f, deepness + 1));
try!(write!(f, "{:x}: ", i)); try!(write!(f, "{:x}: ", i));
try!(self.fmt_all(nodes[i], f, deepness + 1)); try!(self.fmt_all(self.get_raw_or_lookup(nodes[i]), f, deepness + 1));
} try!(writeln!(f, ""));
} }
} }
}, },
@ -240,22 +222,25 @@ impl TrieDB {
fn get_from_node<'a>(&'a self, node: &'a [u8], key: &NibbleSlice<'a>) -> Option<&'a [u8]> { fn get_from_node<'a>(&'a self, node: &'a [u8], key: &NibbleSlice<'a>) -> Option<&'a [u8]> {
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::ExtensionRaw(ref slice, ref item) if key.starts_with(slice) => { Node::Extension(ref slice, ref item) if key.starts_with(slice) => {
self.get_from_node(item, &key.mid(slice.len())) self.get_from_node(self.get_raw_or_lookup(item), &key.mid(slice.len()))
},
Node::ExtensionSha3(ref slice, ref sha3) => {
// lookup for this item
let rlp = self.db.lookup(&H256::from_slice(sha3)).expect("sha3 not found!");
self.get_from_node(rlp, &key.mid(slice.len()))
}, },
Node::Branch(Some(ref nodes), ref value) => match key.is_empty() { Node::Branch(Some(ref nodes), ref value) => match key.is_empty() {
true => Some(value), true => Some(value),
false => self.get_from_node(nodes[key.at(0) as usize], &key.mid(1)) false => self.get_from_node(self.get_raw_or_lookup(nodes[key.at(0) as usize]), &key.mid(1))
}, },
_ => None _ => None
} }
} }
fn get_raw_or_lookup<'a>(&'a self, node: &'a [u8]) -> &'a [u8] {
// check if its sha3 + len
match node.len() > 32 {
true => self.db.lookup(&H256::from_slice(&node[1..])).expect("Not found!"),
false => node
}
}
fn add(&mut self, key: &NibbleSlice, value: &[u8]) { fn add(&mut self, key: &NibbleSlice, value: &[u8]) {
trace!("ADD: {:?} {:?}", key, value.pretty()); trace!("ADD: {:?} {:?}", key, value.pretty());
// determine what the new root is, insert new nodes and remove old as necessary. // determine what the new root is, insert new nodes and remove old as necessary.
@ -524,7 +509,7 @@ mod tests {
let v = rlp::encode(&"cat"); let v = rlp::encode(&"cat");
let (slice, is_leaf) = NibbleSlice::from_encoded(&k); let (slice, is_leaf) = NibbleSlice::from_encoded(&k);
assert_eq!(is_leaf, false); assert_eq!(is_leaf, false);
let ex = Node::ExtensionRaw(slice, &v); let ex = Node::Extension(slice, &v);
let rlp = ex.encoded(); let rlp = ex.encoded();
let ex2 = Node::decoded(&rlp); let ex2 = Node::decoded(&rlp);
assert_eq!(ex, ex2); assert_eq!(ex, ex2);
@ -604,15 +589,15 @@ mod tests {
t.insert(&key, &val); t.insert(&key, &val);
} }
// trace!("{:?}", t); println!("{:?}", t);
assert_eq!(*t.root(), trie_root(v)); for i in 0..v.len() {
/*for i in 0..v.len() {
let key: &[u8]= &v[i].0; let key: &[u8]= &v[i].0;
let val: &[u8] = &v[i].1; let val: &[u8] = &v[i].1;
assert_eq!(t.at(&key).unwrap(), val); assert_eq!(t.at(&key).unwrap(), val);
}*/ }
assert_eq!(*t.root(), trie_root(v));
} }
#[test] #[test]