This commit is contained in:
debris 2015-12-01 19:19:43 +01:00
commit 449e468778
2 changed files with 21 additions and 16 deletions

View File

@ -45,7 +45,10 @@ pub struct PrettySlice<'a> (&'a [u8]);
impl<'a> fmt::Debug for PrettySlice<'a> { impl<'a> fmt::Debug for PrettySlice<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for i in 0..self.0.len() { for i in 0..self.0.len() {
try!(write!(f, "{:02x}", self.0[i])); match i > 0 {
true => { try!(write!(f, "·{:02x}", self.0[i])); },
false => { try!(write!(f, "{:02x}", self.0[i])); },
}
} }
Ok(()) Ok(())
} }

View File

@ -392,7 +392,7 @@ impl TrieDB {
/// ///
/// **This operation will not insert the new node now destroy the original.** /// **This operation will not insert the new node now destroy the original.**
fn augmented(&self, old: &[u8], partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes { fn augmented(&self, old: &[u8], partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
trace!("augmented ({:?}, {:?}, {:?})", old.pretty(), partial, value.pretty()); trace!("augmented (old: {:?}, partial: {:?}, value: {:?})", old.pretty(), partial, value.pretty());
// already have an extension. either fast_forward, cleve or transmute_to_branch. // already have an extension. either fast_forward, cleve or transmute_to_branch.
let old_rlp = Rlp::new(old); let old_rlp = Rlp::new(old);
match old_rlp.prototype() { match old_rlp.prototype() {
@ -404,29 +404,29 @@ impl TrieDB {
Prototype::List(2) => { Prototype::List(2) => {
let existing_key_rlp = old_rlp.at(0); let existing_key_rlp = old_rlp.at(0);
let (existing_key, is_leaf) = NibbleSlice::from_encoded(existing_key_rlp.data()); let (existing_key, is_leaf) = NibbleSlice::from_encoded(existing_key_rlp.data());
match partial.common_prefix(&existing_key) { match (is_leaf, partial.common_prefix(&existing_key)) {
cp if partial.len() == existing_key.len() && cp == existing_key.len() && is_leaf => { (true, cp) if cp == existing_key.len() && partial.len() == existing_key.len() => {
// equivalent-leaf: replace // equivalent-leaf: replace
trace!("equivalent-leaf: REPLACE"); trace!("equivalent-leaf: REPLACE");
Self::compose_leaf(partial, value) Self::compose_leaf(partial, value)
}, },
0 => { (_, 0) => {
// one of us isn't empty: transmute to branch here // one of us isn't empty: transmute to branch here
trace!("no-common-prefix, not-both-empty (exist={:?}; new={:?}): TRANSMUTE,AUGMENT", existing_key.len(), partial.len()); trace!("no-common-prefix, not-both-empty (exist={:?}; new={:?}): TRANSMUTE,AUGMENT", existing_key.len(), partial.len());
self.transmuted_to_branch_and_augmented(is_leaf, &existing_key, old_rlp.at(1).raw(), partial, value, diff) self.transmuted_to_branch_and_augmented(is_leaf, &existing_key, old_rlp.at(1).raw(), partial, value, diff)
}, },
cp if cp == existing_key.len() => { (_, cp) if cp == existing_key.len() => {
trace!("complete-prefix (cp={:?}): AUGMENT-AT-END", cp); trace!("complete-prefix (cp={:?}): AUGMENT-AT-END", cp);
// fully-shared prefix for this extension: // fully-shared prefix for this extension:
// skip to the end of this extension and continue to augment there. // skip to the end of this extension and continue to augment there.
let n = self.take_node(&old_rlp.at(1), diff); let n = if is_leaf { old_rlp.at(1).raw() } else { self.take_node(&old_rlp.at(1), diff) };
let downstream_node = self.augmented(n, &partial.mid(cp), value, diff); let downstream_node = self.augmented(n, &partial.mid(cp), value, diff);
let mut s = RlpStream::new_list(2); let mut s = RlpStream::new_list(2);
s.append_raw(old_rlp.at(0).raw(), 1); s.append_raw(old_rlp.at(0).raw(), 1);
diff.new_node(downstream_node, &mut s); diff.new_node(downstream_node, &mut s);
s.out() s.out()
}, },
cp => { (_, cp) => {
// partially-shared prefix for this extension: // partially-shared prefix for this extension:
// split into two extensions, high and low, pass the // split into two extensions, high and low, pass the
// low through augment with the value before inserting the result // low through augment with the value before inserting the result
@ -448,7 +448,7 @@ impl TrieDB {
}, },
} }
}, },
Prototype::Data(0) => { Prototype::Data(_) => {
trace!("empty: COMPOSE"); trace!("empty: COMPOSE");
Self::compose_leaf(partial, value) Self::compose_leaf(partial, value)
}, },
@ -574,8 +574,8 @@ mod tests {
let 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")),
(From::from("horse"), From::from("stallion")), // (From::from("horse"), From::from("stallion")),
]; ];
for i in 0..v.len() { for i in 0..v.len() {
@ -584,11 +584,13 @@ mod tests {
t.insert(&key, &val); t.insert(&key, &val);
} }
//for i in 0..v.len() { assert_eq!(*t.root(), trie_root(v));
//let key: &[u8]= &v[i].0;
//let val: &[u8] = &v[i].1; /*for i in 0..v.len() {
//assert_eq!(t.at(&key).unwrap(), val); let key: &[u8]= &v[i].0;
//} let val: &[u8] = &v[i].1;
assert_eq!(t.at(&key).unwrap(), val);
}*/
} }
#[test] #[test]