First few tests working.
This commit is contained in:
parent
87e458efed
commit
4e4b754eca
@ -174,7 +174,7 @@ impl<'a, 'view> Rlp<'a> where 'a: 'view {
|
|||||||
self.rlp.data()
|
self.rlp.data()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns number of rlp items.
|
/// Returns number of RLP items.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// extern crate ethcore_util as util;
|
/// extern crate ethcore_util as util;
|
||||||
@ -210,7 +210,7 @@ impl<'a, 'view> Rlp<'a> where 'a: 'view {
|
|||||||
self.rlp.size()
|
self.rlp.size()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get view onto rlp-slice at index.
|
/// Get view onto RLP-slice at index.
|
||||||
///
|
///
|
||||||
/// Caches offset to given index, so access to successive
|
/// Caches offset to given index, so access to successive
|
||||||
/// slices is faster.
|
/// slices is faster.
|
||||||
@ -337,7 +337,7 @@ impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The bare data of the rlp.
|
/// The bare data of the RLP.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// extern crate ethcore_util as util;
|
/// extern crate ethcore_util as util;
|
||||||
@ -355,7 +355,8 @@ impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn data(&'view self) -> &'a [u8] {
|
pub fn data(&'view self) -> &'a [u8] {
|
||||||
unimplemented!();
|
let ii = Self::item_info(self.bytes).unwrap();
|
||||||
|
&self.bytes[ii.prefix_len..(ii.prefix_len + ii.value_len)]
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns number of rlp items.
|
/// Returns number of rlp items.
|
||||||
|
105
src/trie.rs
105
src/trie.rs
@ -101,7 +101,7 @@ impl TrieDB {
|
|||||||
fn add(&mut self, key: &NibbleSlice, value: &[u8]) {
|
fn add(&mut self, key: &NibbleSlice, value: &[u8]) {
|
||||||
// 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.
|
||||||
let mut todo: Diff = Diff::new();
|
let mut todo: Diff = Diff::new();
|
||||||
let root_rlp = self.inject(self.db.lookup(&self.root).expect("Trie root not found!"), key, value, &mut todo);
|
let root_rlp = self.augmented(self.db.lookup(&self.root).expect("Trie root not found!"), key, value, &mut todo);
|
||||||
self.apply(todo);
|
self.apply(todo);
|
||||||
self.set_root_rlp(&root_rlp);
|
self.set_root_rlp(&root_rlp);
|
||||||
}
|
}
|
||||||
@ -134,11 +134,13 @@ impl TrieDB {
|
|||||||
/// the trie.
|
/// the trie.
|
||||||
fn take_node<'a, 'rlp_view>(&'a self, rlp: &'rlp_view Rlp<'a>, diff: &mut Diff) -> &'a [u8] where 'a: 'rlp_view {
|
fn take_node<'a, 'rlp_view>(&'a self, rlp: &'rlp_view Rlp<'a>, diff: &mut Diff) -> &'a [u8] where 'a: 'rlp_view {
|
||||||
if rlp.is_list() {
|
if rlp.is_list() {
|
||||||
|
trace!("take_node {:?} (inline)", rlp.raw());
|
||||||
rlp.raw()
|
rlp.raw()
|
||||||
}
|
}
|
||||||
else if rlp.is_data() && rlp.size() == 32 {
|
else if rlp.is_data() && rlp.size() == 32 {
|
||||||
let h = H256::decode(rlp);
|
let h = H256::decode(rlp);
|
||||||
let r = self.db.lookup(&h).expect("Trie root not found!");
|
let r = self.db.lookup(&h).expect("Trie root not found!");
|
||||||
|
trace!("take_node {:?} (indirect for {:?})", rlp.raw(), r);
|
||||||
diff.delete_node_sha3(h);
|
diff.delete_node_sha3(h);
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
@ -150,7 +152,8 @@ impl TrieDB {
|
|||||||
/// Transform an existing extension or leaf node to an invalid single-entry branch.
|
/// Transform an existing extension or leaf node to an invalid single-entry branch.
|
||||||
///
|
///
|
||||||
/// **This operation will not insert the new node nor destroy the original.**
|
/// **This operation will not insert the new node nor destroy the original.**
|
||||||
fn transmute_extension_to_branch(orig_partial: &NibbleSlice, orig_raw_payload: &[u8], diff: &mut Diff) -> Bytes {
|
fn transmuted_extension_to_branch(orig_partial: &NibbleSlice, orig_raw_payload: &[u8], diff: &mut Diff) -> Bytes {
|
||||||
|
trace!("transmuted_extension_to_branch");
|
||||||
let mut s = RlpStream::new_list(17);
|
let mut s = RlpStream::new_list(17);
|
||||||
assert!(!orig_partial.is_empty()); // extension nodes are not allowed to have empty partial keys.
|
assert!(!orig_partial.is_empty()); // extension nodes are not allowed to have empty partial keys.
|
||||||
let index = orig_partial.at(0);
|
let index = orig_partial.at(0);
|
||||||
@ -171,7 +174,8 @@ impl TrieDB {
|
|||||||
s.out()
|
s.out()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn transmute_leaf_to_branch(orig_partial: &NibbleSlice, orig_raw_payload: &[u8], diff: &mut Diff) -> Bytes {
|
fn transmuted_leaf_to_branch(orig_partial: &NibbleSlice, orig_raw_payload: &[u8], diff: &mut Diff) -> Bytes {
|
||||||
|
trace!("transmuted_leaf_to_branch");
|
||||||
let mut s = RlpStream::new_list(17);
|
let mut s = RlpStream::new_list(17);
|
||||||
let index = if orig_partial.is_empty() {16} else {orig_partial.at(0)};
|
let index = if orig_partial.is_empty() {16} else {orig_partial.at(0)};
|
||||||
// orig is leaf - orig_raw_payload is data representing the actual value.
|
// orig is leaf - orig_raw_payload is data representing the actual value.
|
||||||
@ -189,24 +193,26 @@ impl TrieDB {
|
|||||||
/// Transform an existing extension or leaf node plus a new partial/value to a two-entry branch.
|
/// Transform an existing extension or leaf node plus a new partial/value to a two-entry branch.
|
||||||
///
|
///
|
||||||
/// **This operation will not insert the new node nor destroy the original.**
|
/// **This operation will not insert the new node nor destroy the original.**
|
||||||
fn transmute_to_branch_and_inject(&self, orig_is_leaf: bool, orig_partial: &NibbleSlice, orig_raw_payload: &[u8], partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
|
fn transmuted_to_branch_and_augmented(&self, orig_is_leaf: bool, orig_partial: &NibbleSlice, orig_raw_payload: &[u8], partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
|
||||||
|
trace!("transmuted_to_branch_and_augmented");
|
||||||
let intermediate = match orig_is_leaf {
|
let intermediate = match orig_is_leaf {
|
||||||
true => Self::transmute_leaf_to_branch(orig_partial, orig_raw_payload, diff),
|
true => Self::transmuted_leaf_to_branch(orig_partial, orig_raw_payload, diff),
|
||||||
false => Self::transmute_extension_to_branch(orig_partial, orig_raw_payload, diff),
|
false => Self::transmuted_extension_to_branch(orig_partial, orig_raw_payload, diff),
|
||||||
};
|
};
|
||||||
self.inject(&intermediate, partial, value, diff)
|
self.augmented(&intermediate, partial, value, diff)
|
||||||
// TODO: implement without having to make an intermediate representation.
|
// TODO: implement without having to make an intermediate representation.
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Given a branch node's RLP `orig` together with a `partial` key and `value`, return the
|
/// Given a branch node's RLP `orig` together with a `partial` key and `value`, return the
|
||||||
/// RLP-encoded node that accomodates the trie with the new entry. Mutate `diff` so that
|
/// RLP-encoded node that accomodates the trie with the new entry. Mutate `diff` so that
|
||||||
/// once applied the returned node is valid.
|
/// once applied the returned node is valid.
|
||||||
fn injected_into_branch(&self, orig: &Rlp, partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
|
fn augmented_into_branch(&self, orig: &Rlp, partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
|
||||||
|
trace!("augmented_into_branch");
|
||||||
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 {
|
if index == i {
|
||||||
// this is node to inject into...
|
// this is node to augment into...
|
||||||
if orig.at(i).is_empty() {
|
if orig.at(i).is_empty() {
|
||||||
// easy - original had empty slot.
|
// 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(if i == 16 {0} else {1}), value), &mut s);
|
||||||
@ -216,7 +222,7 @@ impl TrieDB {
|
|||||||
diff.replace_node(&orig.at(i), new, &mut s);
|
diff.replace_node(&orig.at(i), new, &mut s);
|
||||||
} else {
|
} else {
|
||||||
// harder - original has something there already
|
// harder - original has something there already
|
||||||
let new = self.inject(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 {
|
} else {
|
||||||
@ -226,8 +232,6 @@ impl TrieDB {
|
|||||||
s.out()
|
s.out()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Determine the RLP of the node, assuming we're inserting `partial` into the
|
/// Determine the RLP of the node, assuming we're inserting `partial` into the
|
||||||
/// node currently of data `old`. This will *not* delete any hash of `old` from the database;
|
/// node currently of data `old`. This will *not* delete any hash of `old` from the database;
|
||||||
/// it will just return the new RLP that includes the new node.
|
/// it will just return the new RLP that includes the new node.
|
||||||
@ -236,31 +240,35 @@ impl TrieDB {
|
|||||||
/// and deleting nodes as necessary.
|
/// and deleting nodes as necessary.
|
||||||
///
|
///
|
||||||
/// **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 inject(&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, partial, value);
|
||||||
// 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() {
|
||||||
Prototype::List(17) => {
|
Prototype::List(17) => {
|
||||||
// already have a branch. route and inject.
|
// already have a branch. route and augment.
|
||||||
self.injected_into_branch(&old_rlp, partial, value, diff)
|
self.augmented_into_branch(&old_rlp, partial, value, diff)
|
||||||
},
|
},
|
||||||
Prototype::List(2) => {
|
Prototype::List(2) => {
|
||||||
let their_key_rlp = old_rlp.at(0);
|
let existing_key_rlp = old_rlp.at(0);
|
||||||
let (them, is_leaf) = NibbleSlice::from_encoded(their_key_rlp.data());
|
let (existing_key, is_leaf) = NibbleSlice::from_encoded(existing_key_rlp.data());
|
||||||
match partial.common_prefix(&them) {
|
match partial.common_prefix(&existing_key) {
|
||||||
0 if partial.is_empty() && them.is_empty() => {
|
cp if partial.len() == existing_key.len() && cp == existing_key.len() && is_leaf => {
|
||||||
// both empty: just replace.
|
// 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
|
||||||
self.transmute_to_branch_and_inject(is_leaf, &them, old_rlp.at(1).raw(), partial, value, diff)
|
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)
|
||||||
},
|
},
|
||||||
cp if cp == them.len() => {
|
cp if cp == existing_key.len() => {
|
||||||
|
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 the inject 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 = self.take_node(&old_rlp.at(1), diff);
|
||||||
let downstream_node = self.inject(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);
|
||||||
@ -269,19 +277,21 @@ impl TrieDB {
|
|||||||
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 inject with the value before inserting the result
|
// low through augment with the value before inserting the result
|
||||||
// into high to create the new.
|
// into high to create the new.
|
||||||
|
|
||||||
// TODO: optimise by doing this without creating injected_low.
|
// TODO: optimise by doing this without creating augmented_low.
|
||||||
|
|
||||||
|
trace!("partially-shared-prefix (exist={:?}; new={:?}; cp={:?}): AUGMENT-AT-END", existing_key.len(), partial.len(), cp);
|
||||||
|
|
||||||
// low (farther from root)
|
// low (farther from root)
|
||||||
let low = Self::compose_raw(&them.mid(cp), old_rlp.at(1).raw(), is_leaf);
|
let low = Self::compose_raw(&existing_key.mid(cp), old_rlp.at(1).raw(), is_leaf);
|
||||||
let injected_low = self.inject(&low, &partial.mid(cp), value, diff);
|
let augmented_low = self.augmented(&low, &partial.mid(cp), value, diff);
|
||||||
|
|
||||||
// high (closer to root)
|
// high (closer to root)
|
||||||
let mut s = RlpStream::new_list(2);
|
let mut s = RlpStream::new_list(2);
|
||||||
s.append(&them.encoded_leftmost(cp, false));
|
s.append(&existing_key.encoded_leftmost(cp, false));
|
||||||
diff.new_node(injected_low, &mut s);
|
diff.new_node(augmented_low, &mut s);
|
||||||
s.out()
|
s.out()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -314,21 +324,44 @@ impl Trie for TrieDB {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn playpen() {
|
mod tests {
|
||||||
use overlaydb::*;
|
use memorydb::*;
|
||||||
use triehash::*;
|
use triehash::*;
|
||||||
|
use super::*;
|
||||||
use env_logger;
|
use env_logger;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn playpen() {
|
||||||
env_logger::init().unwrap();
|
env_logger::init().unwrap();
|
||||||
|
|
||||||
(&[1, 2, 3]).starts_with(&[1, 2]);
|
let mut t = TrieDB::new(MemoryDB::new());
|
||||||
|
t.init();
|
||||||
|
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
|
||||||
|
}
|
||||||
|
|
||||||
let mut t = TrieDB::new(OverlayDB::new_temp());
|
#[test]
|
||||||
|
fn init() {
|
||||||
|
let mut t = TrieDB::new(MemoryDB::new());
|
||||||
t.init();
|
t.init();
|
||||||
assert_eq!(*t.root(), SHA3_NULL_RLP);
|
assert_eq!(*t.root(), SHA3_NULL_RLP);
|
||||||
assert!(t.is_empty());
|
assert!(t.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn insert_on_empty() {
|
||||||
|
let mut t = TrieDB::new(MemoryDB::new());
|
||||||
|
t.init();
|
||||||
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
|
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
|
||||||
assert_eq!(*t.root(), trie_root(vec![ (vec![1u8, 0x23], vec![1u8, 0x23]) ]));
|
assert_eq!(*t.root(), trie_root(vec![ (vec![0x01u8, 0x23], vec![0x01u8, 0x23]) ]));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn insert_replace_root() {
|
||||||
|
let mut t = TrieDB::new(MemoryDB::new());
|
||||||
|
t.init();
|
||||||
|
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
|
||||||
|
t.insert(&[0x01u8, 0x23], &[0x23u8, 0x45]);
|
||||||
|
assert_eq!(*t.root(), trie_root(vec![ (vec![0x01u8, 0x23], vec![0x23u8, 0x45]) ]));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user