Additional trie test and fix.

This commit is contained in:
Gav Wood 2015-12-01 12:57:08 +01:00
parent 6c21bc7b95
commit 9041f81cba

View File

@ -213,22 +213,16 @@ impl TrieDB {
let mut s = RlpStream::new_list(17); let mut s = RlpStream::new_list(17);
let index = if partial.is_empty() {16} else {partial.at(0) as usize}; let index = if partial.is_empty() {16} else {partial.at(0) as usize};
for i in 0usize..17 { for i in 0usize..17 {
if index == i { match (index == i, i) {
// this is node to augment into... (true, 16) => // leaf entry - just replace.
if orig.at(i).is_empty() { { s.append(&value); },
// easy - original had empty slot. (true, i) if orig.at(i).is_empty() => // easy - original had empty slot.
diff.new_node(Self::compose_leaf(&partial.mid(if i == 16 {0} else {1}), value), &mut s); diff.new_node(Self::compose_leaf(&partial.mid(1), value), &mut s),
} else if i == 16 { (true, i) => { // harder - original has something there already
// leaf entry - just replace.
let new = Self::compose_leaf(&partial.mid(if i == 16 {0} else {1}), value);
diff.replace_node(&orig.at(i), new, &mut s);
} else {
// harder - original has something there already
let new = self.augmented(orig.at(i).raw(), &partial.mid(1), value, diff); let new = self.augmented(orig.at(i).raw(), &partial.mid(1), value, diff);
diff.replace_node(&orig.at(i), new, &mut s); diff.replace_node(&orig.at(i), new, &mut s);
} }
} else { (false, i) => { s.append_raw(orig.at(i).raw(), 1); },
s.append_raw(orig.at(i).raw(), 1);
} }
} }
s.out() s.out()
@ -340,21 +334,10 @@ mod tests {
let mut t = TrieDB::new_memory(); let mut t = TrieDB::new_memory();
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]); t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
t.insert(&[0xf1u8, 0x23], &[0xf1u8, 0x23]); t.insert(&[], &[0x0]);
t.insert(&[0x81u8, 0x23], &[0x81u8, 0x23]);
trace!("trieroot with 2 items: {:?}", trie_root(vec![
(vec![0x01u8, 0x23], vec![0x01u8, 0x23]),
(vec![0xf1u8, 0x23], vec![0xf1u8, 0x23]),
]));
trace!("trieroot with 3 items: {:?}", trie_root(vec![
(vec![0x01u8, 0x23], vec![0x01u8, 0x23]),
(vec![0xf1u8, 0x23], vec![0xf1u8, 0x23]),
(vec![0x81u8, 0x23], vec![0x81u8, 0x23]),
]));
assert_eq!(*t.root(), trie_root(vec![ assert_eq!(*t.root(), trie_root(vec![
(vec![], vec![0x0]),
(vec![0x01u8, 0x23], vec![0x01u8, 0x23]), (vec![0x01u8, 0x23], vec![0x01u8, 0x23]),
(vec![0xf1u8, 0x23], vec![0xf1u8, 0x23]),
(vec![0x81u8, 0x23], vec![0x81u8, 0x23]),
])); ]));
} }
@ -381,7 +364,7 @@ mod tests {
} }
#[test] #[test]
fn insert_branch_root() { fn insert_make_branch_root() {
let mut t = TrieDB::new_memory(); let mut t = TrieDB::new_memory();
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]); t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
t.insert(&[0x11u8, 0x23], &[0x11u8, 0x23]); t.insert(&[0x11u8, 0x23], &[0x11u8, 0x23]);
@ -390,4 +373,17 @@ mod tests {
(vec![0x11u8, 0x23], vec![0x11u8, 0x23]) (vec![0x11u8, 0x23], vec![0x11u8, 0x23])
])); ]));
} }
#[test]
fn insert_into_branch_root() {
let mut t = TrieDB::new_memory();
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
t.insert(&[0xf1u8, 0x23], &[0xf1u8, 0x23]);
t.insert(&[0x81u8, 0x23], &[0x81u8, 0x23]);
assert_eq!(*t.root(), trie_root(vec![
(vec![0x01u8, 0x23], vec![0x01u8, 0x23]),
(vec![0x81u8, 0x23], vec![0x81u8, 0x23]),
(vec![0xf1u8, 0x23], vec![0xf1u8, 0x23]),
]));
}
} }