openethereum/src/trie.rs

742 lines
23 KiB
Rust
Raw Normal View History

2015-12-01 18:25:47 +01:00
use std::fmt;
2015-11-29 15:50:33 +01:00
use memorydb::*;
use sha3::*;
2015-11-29 15:50:33 +01:00
use hashdb::*;
use hash::*;
2015-11-30 02:57:02 +01:00
use nibbleslice::*;
use bytes::*;
use rlp::*;
2015-12-01 02:23:53 +01:00
//use log::*;
2015-11-29 15:50:33 +01:00
pub const NULL_RLP: [u8; 1] = [0x80; 1];
pub const SHA3_NULL_RLP: H256 = H256( [0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21] );
pub trait Trie {
fn root(&self) -> &H256;
fn is_empty(&self) -> bool { *self.root() == SHA3_NULL_RLP }
// TODO: consider returning &[u8]...
2015-11-30 02:57:02 +01:00
fn contains(&self, key: &[u8]) -> bool;
2015-12-01 16:39:44 +01:00
fn at<'a>(&'a self, key: &'a [u8]) -> Option<&'a [u8]>;
2015-11-30 02:57:02 +01:00
fn insert(&mut self, key: &[u8], value: &[u8]);
fn remove(&mut self, key: &[u8]);
2015-11-29 15:50:33 +01:00
}
2015-12-01 16:39:44 +01:00
#[derive(Eq, PartialEq, Debug)]
pub enum Node<'a> {
Leaf(NibbleSlice<'a>, &'a[u8]),
ExtensionRaw(NibbleSlice<'a>, &'a[u8]),
ExtensionSha3(NibbleSlice<'a>, &'a[u8]),
Branch(Option<[&'a[u8]; 16]>, &'a [u8])
}
impl <'a>Node<'a> {
pub fn decoded(node_rlp: &'a [u8]) -> Node<'a> {
let r = Rlp::new(node_rlp);
match r.prototype() {
// either leaf or extension - decode first item with NibbleSlice::???
// and use is_leaf return to figure out which.
// if leaf, second item is a value (is_data())
// if extension, second item is a node (either SHA3 to be looked up and
// 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()) {
(slice, true) => Node::Leaf(slice, r.at(1).data()),
(slice, false) => match r.at(1).raw().len() {
// 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).
Prototype::List(17) => {
let mut nodes: [&'a [u8]; 16] = unsafe { ::std::mem::uninitialized() };
for i in 0..16 { nodes[i] = r.at(i).raw(); }
Node::Branch(Some(nodes), r.at(16).data())
},
// an empty branch index.
Prototype::Data(0) => Node::Branch(None, r.data()),
// something went wrong.
_ => panic!("Rlp is not valid.")
}
}
pub fn encoded(&self) -> Bytes {
match *self {
Node::Leaf(ref slice, ref value) => {
let mut stream = RlpStream::new_list(2);
stream.append(&slice.encoded(true));
stream.append(value);
stream.out()
},
Node::ExtensionRaw(ref slice, ref raw_rlp) => {
let mut stream = RlpStream::new_list(2);
stream.append(&slice.encoded(false));
stream.append_raw(raw_rlp, 1);
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) => {
let mut stream = RlpStream::new_list(17);
for i in 0..16 { stream.append_raw(nodes[i], 1); }
stream.append(value);
stream.out()
},
Node::Branch(_, _) => {
let mut stream = RlpStream::new();
stream.append_empty_data();
stream.out()
}
}
}
}
2015-12-01 20:24:33 +01:00
//enum ValidationResult<'a> {
//Valid,
//Invalid { node: Node<'a>, depth: usize }
//}
2015-12-01 01:12:06 +01:00
enum Operation {
New(H256, Bytes),
Delete(H256),
2015-11-30 02:57:02 +01:00
}
struct Diff (Vec<Operation>);
2015-12-01 01:12:06 +01:00
2015-11-30 13:25:37 +01:00
impl Diff {
2015-12-01 01:12:06 +01:00
fn new() -> Diff { Diff(vec![]) }
/// Given the RLP that encodes a node, append a reference to that node `out` and leave `diff`
/// such that the reference is valid, once applied.
fn new_node(&mut self, rlp: Bytes, out: &mut RlpStream) {
2015-12-01 02:23:53 +01:00
if rlp.len() >= 32 {
2015-12-01 22:38:34 +01:00
trace!("new_node: reference node {:?}", rlp.pretty());
2015-12-01 01:12:06 +01:00
let rlp_sha3 = rlp.sha3();
out.append(&rlp_sha3);
self.0.push(Operation::New(rlp_sha3, rlp));
2015-12-01 01:12:06 +01:00
}
else {
2015-12-01 18:25:18 +01:00
trace!("new_node: inline node {:?}", rlp.pretty());
out.append_raw(&rlp, 1);
2015-12-01 01:12:06 +01:00
}
}
/// Given the RLP that encodes a now-unused node, leave `diff` in such a state that it is noted.
fn delete_node_sha3(&mut self, old_sha3: H256) {
self.0.push(Operation::Delete(old_sha3));
2015-12-01 01:12:06 +01:00
}
fn delete_node(&mut self, old: &Rlp) {
2015-12-01 02:23:53 +01:00
if old.is_data() && old.size() == 32 {
self.0.push(Operation::Delete(H256::decode(old)));
2015-12-01 01:12:06 +01:00
}
}
fn replace_node(&mut self, old: &Rlp, rlp: Bytes, out: &mut RlpStream) {
2015-12-01 01:12:06 +01:00
self.delete_node(old);
self.new_node(rlp, out);
2015-12-01 01:12:06 +01:00
}
2015-11-30 13:25:37 +01:00
}
pub struct TrieDB {
db: Box<HashDB>,
root: H256,
2015-11-30 13:25:37 +01:00
}
2015-12-01 18:25:47 +01:00
impl fmt::Debug for TrieDB {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(writeln!(f, "["));
2015-12-01 19:19:16 +01:00
let root_rlp = self.db.lookup(&self.root).expect("Trie root not found!");
2015-12-01 19:24:14 +01:00
try!(self.fmt_all(root_rlp, f, 0));
2015-12-01 18:25:47 +01:00
writeln!(f, "]")
}
}
2015-11-29 15:50:33 +01:00
impl TrieDB {
2015-12-01 12:10:36 +01:00
pub fn new_boxed(db_box: Box<HashDB>) -> Self { let mut r = TrieDB{ db: db_box, root: H256::new() }; r.set_root_rlp(&NULL_RLP); r }
2015-11-29 15:50:33 +01:00
2015-12-01 12:10:36 +01:00
pub fn new<T>(db: T) -> Self where T: HashDB + 'static { Self::new_boxed(Box::new(db)) }
2015-11-29 15:50:33 +01:00
2015-12-01 12:10:36 +01:00
pub fn new_memory() -> Self { Self::new(MemoryDB::new()) }
2015-11-29 15:50:33 +01:00
pub fn db(&self) -> &HashDB { self.db.as_ref() }
2015-11-30 02:57:02 +01:00
fn set_root_rlp(&mut self, root_data: &[u8]) {
2015-11-30 14:53:22 +01:00
self.db.kill(&self.root);
2015-11-30 02:57:02 +01:00
self.root = self.db.insert(root_data);
2015-12-01 18:25:18 +01:00
trace!("set_root_rlp {:?} {:?}", root_data.pretty(), self.root);
2015-11-30 02:57:02 +01:00
}
fn apply(&mut self, diff: Diff) {
2015-12-01 12:10:36 +01:00
trace!("applying {:?} changes", diff.0.len());
for d in diff.0.into_iter() {
2015-12-01 01:12:06 +01:00
match d {
Operation::Delete(h) => {
trace!("TrieDB::apply --- {:?}", &h);
self.db.kill(&h);
},
Operation::New(h, d) => {
2015-12-01 18:25:18 +01:00
trace!("TrieDB::apply +++ {:?} -> {:?}", &h, d.pretty());
2015-12-01 01:12:06 +01:00
self.db.emplace(h, d);
}
}
}
}
2015-12-01 01:44:18 +01:00
2015-12-01 19:19:16 +01:00
fn fmt_indent(&self, f: &mut fmt::Formatter, size: usize) -> fmt::Result {
2015-12-01 19:24:14 +01:00
for _ in 0..size {
2015-12-01 20:34:49 +01:00
try!(write!(f, " "));
2015-12-01 19:19:16 +01:00
}
Ok(())
}
fn fmt_all(&self, node: &[u8], f: &mut fmt::Formatter, deepness: usize) -> fmt::Result {
2015-12-01 18:25:47 +01:00
let node = Node::decoded(node);
match node {
2015-12-01 19:19:16 +01:00
Node::Leaf(slice, value) => try!(writeln!(f, "Leaf {:?}, {:?}", slice, value.pretty())),
2015-12-01 20:24:33 +01:00
Node::ExtensionRaw(ref slice, ref item) => {
try!(write!(f, "Extension (raw): {:?} ", slice));
2015-12-01 19:24:14 +01:00
try!(self.fmt_all(item, f, deepness + 1));
2015-12-01 18:25:47 +01:00
},
2015-12-01 20:24:33 +01:00
Node::ExtensionSha3(ref slice, sha3) => {
try!(write!(f, "Extension (sha3): {:?} ", slice));
2015-12-01 18:25:47 +01:00
let rlp = self.db.lookup(&H256::from_slice(sha3)).expect("sha3 not found!");
2015-12-01 19:24:14 +01:00
try!(self.fmt_all(rlp, f, deepness + 1));
2015-12-01 18:25:47 +01:00
},
2015-12-01 20:34:49 +01:00
Node::Branch(Some(ref nodes), ref value) => {
2015-12-01 19:19:16 +01:00
try!(writeln!(f, "Branch: "));
2015-12-01 20:34:49 +01:00
try!(self.fmt_indent(f, deepness + 1));
try!(writeln!(f, ": {:?}", value.pretty()));
2015-12-01 18:25:47 +01:00
for i in 0..16 {
2015-12-01 19:19:16 +01:00
match Node::decoded(nodes[i]) {
Node::Branch(None, _) => (),
_ => {
2015-12-01 19:24:14 +01:00
try!(self.fmt_indent(f, deepness + 1));
2015-12-01 19:19:16 +01:00
try!(write!(f, "{:x}: ", i));
2015-12-01 19:24:14 +01:00
try!(self.fmt_all(nodes[i], f, deepness + 1));
2015-12-01 19:19:16 +01:00
}
}
2015-12-01 18:25:47 +01:00
}
},
// empty
2015-12-01 19:24:14 +01:00
Node::Branch(_, _) => {
2015-12-01 18:25:47 +01:00
// do nothing
}
2015-12-01 19:19:16 +01:00
};
Ok(())
2015-12-01 18:25:47 +01:00
}
2015-12-01 16:39:44 +01:00
fn get<'a>(&'a self, key: &NibbleSlice<'a>) -> Option<&'a [u8]> {
let root_rlp = self.db.lookup(&self.root).expect("Trie root not found!");
self.get_from_node(&root_rlp, key)
}
fn get_from_node<'a>(&'a self, node: &'a [u8], key: &NibbleSlice<'a>) -> Option<&'a [u8]> {
match Node::decoded(node) {
Node::Leaf(ref slice, ref value) if key == slice => Some(value),
2015-12-01 17:03:54 +01:00
Node::ExtensionRaw(ref slice, ref item) if key.starts_with(slice) => {
self.get_from_node(item, &key.mid(slice.len()))
},
2015-12-01 16:39:44 +01:00
Node::ExtensionSha3(ref slice, ref sha3) => {
// lookup for this item
2015-12-01 18:25:47 +01:00
let rlp = self.db.lookup(&H256::from_slice(sha3)).expect("sha3 not found!");
2015-12-01 16:39:44 +01:00
self.get_from_node(rlp, &key.mid(slice.len()))
},
Node::Branch(Some(ref nodes), ref value) => match key.is_empty() {
true => Some(value),
false => self.get_from_node(nodes[key.at(0) as usize], &key.mid(1))
},
_ => None
}
}
2015-11-30 13:19:55 +01:00
fn add(&mut self, key: &NibbleSlice, value: &[u8]) {
2015-12-01 18:25:18 +01:00
trace!("ADD: {:?} {:?}", key, value.pretty());
2015-11-30 02:57:02 +01:00
// determine what the new root is, insert new nodes and remove old as necessary.
2015-12-01 01:44:18 +01:00
let mut todo: Diff = Diff::new();
2015-12-01 03:35:55 +01:00
let root_rlp = self.augmented(self.db.lookup(&self.root).expect("Trie root not found!"), key, value, &mut todo);
self.apply(todo);
self.set_root_rlp(&root_rlp);
2015-12-01 18:25:18 +01:00
trace!("/");
}
2015-12-01 01:44:18 +01:00
fn compose_leaf(partial: &NibbleSlice, value: &[u8]) -> Bytes {
2015-12-01 18:25:18 +01:00
trace!("compose_leaf {:?} {:?} ({:?})", partial, value.pretty(), partial.encoded(true).pretty());
let mut s = RlpStream::new_list(2);
2015-12-01 01:44:18 +01:00
s.append(&partial.encoded(true));
s.append(&value);
let r = s.out();
2015-12-01 18:25:18 +01:00
trace!("compose_leaf: -> {:?}", r.pretty());
r
}
2015-12-01 01:52:20 +01:00
fn compose_raw(partial: &NibbleSlice, raw_payload: &[u8], is_leaf: bool) -> Bytes {
2015-12-01 18:25:18 +01:00
println!("compose_raw {:?} {:?} {:?} ({:?})", partial, raw_payload.pretty(), is_leaf, partial.encoded(is_leaf));
let mut s = RlpStream::new_list(2);
2015-12-01 01:44:18 +01:00
s.append(&partial.encoded(is_leaf));
s.append_raw(raw_payload, 1);
let r = s.out();
2015-12-01 18:25:18 +01:00
println!("compose_raw: -> {:?}", r.pretty());
r
}
2015-12-01 20:24:47 +01:00
fn compose_stub_branch(value: &[u8]) -> Bytes {
let mut s = RlpStream::new_list(17);
for _ in 0..16 { s.append_empty_data(); }
s.append(&value);
s.out()
}
fn compose_extension(partial: &NibbleSlice, raw_payload: &[u8]) -> Bytes {
Self::compose_raw(partial, raw_payload, false)
}
2015-12-01 01:12:06 +01:00
2015-12-01 20:24:47 +01:00
fn create_extension(partial: &NibbleSlice, downstream_node: Bytes, diff: &mut Diff) -> Bytes {
trace!("create_extension partial: {:?}, downstream_node: {:?}", partial, downstream_node.pretty());
let mut s = RlpStream::new_list(2);
s.append(&partial.encoded(false));
diff.new_node(downstream_node, &mut s);
s.out()
}
2015-12-01 01:12:06 +01:00
/// Return the bytes encoding the node represented by `rlp`. It will be unlinked from
/// the trie.
2015-12-01 02:04:52 +01:00
fn take_node<'a, 'rlp_view>(&'a self, rlp: &'rlp_view Rlp<'a>, diff: &mut Diff) -> &'a [u8] where 'a: 'rlp_view {
2015-12-01 02:23:53 +01:00
if rlp.is_list() {
2015-12-01 18:25:18 +01:00
trace!("take_node {:?} (inline)", rlp.raw().pretty());
2015-12-01 02:04:52 +01:00
rlp.raw()
2015-11-30 02:57:02 +01:00
}
2015-12-01 02:23:53 +01:00
else if rlp.is_data() && rlp.size() == 32 {
2015-12-01 01:12:06 +01:00
let h = H256::decode(rlp);
2015-12-01 02:04:52 +01:00
let r = self.db.lookup(&h).expect("Trie root not found!");
2015-12-01 18:25:18 +01:00
trace!("take_node {:?} (indirect for {:?})", rlp.raw().pretty(), r);
2015-12-01 01:52:20 +01:00
diff.delete_node_sha3(h);
2015-12-01 01:12:06 +01:00
r
2015-11-30 02:57:02 +01:00
}
2015-12-01 01:52:20 +01:00
else {
2015-12-01 18:25:18 +01:00
trace!("take_node {:?} (???)", rlp.raw().pretty());
2015-12-01 01:52:20 +01:00
panic!("Empty or invalid node given?");
}
2015-11-30 02:57:02 +01:00
}
2015-12-01 01:12:06 +01:00
/// 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.**
2015-12-01 03:35:55 +01:00
fn transmuted_extension_to_branch(orig_partial: &NibbleSlice, orig_raw_payload: &[u8], diff: &mut Diff) -> Bytes {
trace!("transmuted_extension_to_branch");
2015-12-01 02:23:53 +01:00
let mut s = RlpStream::new_list(17);
2015-12-01 01:12:06 +01:00
assert!(!orig_partial.is_empty()); // extension nodes are not allowed to have empty partial keys.
let index = orig_partial.at(0);
2015-12-01 02:23:53 +01:00
// orig is extension - orig_raw_payload is a node itself.
2015-12-01 01:12:06 +01:00
for i in 0..17 {
if index == i {
if orig_partial.len() > 1 {
// still need an extension
2015-12-01 02:23:53 +01:00
diff.new_node(Self::compose_extension(&orig_partial.mid(1), orig_raw_payload), &mut s);
2015-12-01 01:12:06 +01:00
} else {
// was an extension of length 1 - just redirect the payload into here.
2015-12-01 02:23:53 +01:00
s.append_raw(orig_raw_payload, 1);
2015-12-01 01:12:06 +01:00
}
} else {
2015-12-01 02:23:53 +01:00
s.append_empty_data();
2015-12-01 01:12:06 +01:00
}
}
s.out()
}
2015-12-01 03:35:55 +01:00
fn transmuted_leaf_to_branch(orig_partial: &NibbleSlice, orig_raw_payload: &[u8], diff: &mut Diff) -> Bytes {
trace!("transmuted_leaf_to_branch");
2015-12-01 02:23:53 +01:00
let mut s = RlpStream::new_list(17);
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.
2015-12-01 01:12:06 +01:00
for i in 0..17 {
if index == i {
// this is our node.
2015-12-01 02:23:53 +01:00
diff.new_node(Self::compose_raw(&orig_partial.mid(if i == 16 {0} else {1}), orig_raw_payload, true), &mut s);
2015-12-01 01:12:06 +01:00
} else {
2015-12-01 02:23:53 +01:00
s.append_empty_data();
2015-12-01 01:12:06 +01:00
}
}
s.out()
}
2015-12-01 02:23:53 +01:00
/// 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.**
2015-12-01 03:35:55 +01:00
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");
2015-12-01 02:23:53 +01:00
let intermediate = match orig_is_leaf {
2015-12-01 03:35:55 +01:00
true => Self::transmuted_leaf_to_branch(orig_partial, orig_raw_payload, diff),
false => Self::transmuted_extension_to_branch(orig_partial, orig_raw_payload, diff),
2015-12-01 02:23:53 +01:00
};
2015-12-01 03:35:55 +01:00
self.augmented(&intermediate, partial, value, diff)
2015-12-01 02:23:53 +01:00
// TODO: implement without having to make an intermediate representation.
}
2015-12-01 01:12:06 +01:00
/// 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
/// once applied the returned node is valid.
2015-12-01 03:35:55 +01:00
fn augmented_into_branch(&self, orig: &Rlp, partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
trace!("augmented_into_branch");
2015-12-01 02:23:53 +01:00
let mut s = RlpStream::new_list(17);
let index = if partial.is_empty() {16} else {partial.at(0) as usize};
for i in 0usize..17 {
2015-12-01 12:57:08 +01:00
match (index == i, i) {
(true, 16) => // leaf entry - just replace.
{ s.append(&value); },
(true, i) if orig.at(i).is_empty() => // easy - original had empty slot.
diff.new_node(Self::compose_leaf(&partial.mid(1), value), &mut s),
(true, i) => { // harder - original has something there already
2015-12-01 03:35:55 +01:00
let new = self.augmented(orig.at(i).raw(), &partial.mid(1), value, diff);
2015-12-01 02:23:53 +01:00
diff.replace_node(&orig.at(i), new, &mut s);
2015-12-01 01:12:06 +01:00
}
2015-12-01 12:57:08 +01:00
(false, i) => { s.append_raw(orig.at(i).raw(), 1); },
2015-12-01 01:12:06 +01:00
}
}
2015-12-01 02:23:53 +01:00
s.out()
2015-12-01 01:12:06 +01:00
}
/// 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;
/// it will just return the new RLP that includes the new node.
2015-11-30 02:57:02 +01:00
///
/// The database will be updated so as to make the returned RLP valid through inserting
/// and deleting nodes as necessary.
2015-12-01 01:12:06 +01:00
///
/// **This operation will not insert the new node now destroy the original.**
2015-12-01 03:35:55 +01:00
fn augmented(&self, old: &[u8], partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
2015-12-01 19:09:48 +01:00
trace!("augmented (old: {:?}, partial: {:?}, value: {:?})", old.pretty(), partial, value.pretty());
2015-12-01 01:12:06 +01:00
// already have an extension. either fast_forward, cleve or transmute_to_branch.
let old_rlp = Rlp::new(old);
match old_rlp.prototype() {
2015-11-30 13:25:37 +01:00
Prototype::List(17) => {
2015-12-01 12:10:36 +01:00
trace!("branch: ROUTE,AUGMENT");
2015-12-01 03:35:55 +01:00
// already have a branch. route and augment.
self.augmented_into_branch(&old_rlp, partial, value, diff)
2015-11-30 02:57:02 +01:00
},
2015-11-30 13:25:37 +01:00
Prototype::List(2) => {
2015-12-01 03:35:55 +01:00
let existing_key_rlp = old_rlp.at(0);
let (existing_key, is_leaf) = NibbleSlice::from_encoded(existing_key_rlp.data());
2015-12-01 19:09:48 +01:00
match (is_leaf, partial.common_prefix(&existing_key)) {
(true, cp) if cp == existing_key.len() && partial.len() == existing_key.len() => {
2015-12-01 03:35:55 +01:00
// equivalent-leaf: replace
trace!("equivalent-leaf: REPLACE");
2015-12-01 02:23:53 +01:00
Self::compose_leaf(partial, value)
2015-12-01 01:12:06 +01:00
},
2015-12-01 19:09:48 +01:00
(_, 0) => {
2015-12-01 01:12:06 +01:00
// one of us isn't empty: transmute to branch here
2015-12-01 03:35:55 +01:00
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)
2015-11-30 16:06:29 +01:00
},
2015-12-01 19:09:48 +01:00
(_, cp) if cp == existing_key.len() => {
2015-12-01 03:35:55 +01:00
trace!("complete-prefix (cp={:?}): AUGMENT-AT-END", cp);
2015-12-01 01:12:06 +01:00
// fully-shared prefix for this extension:
2015-12-01 20:24:47 +01:00
// transform to an extension + augmented version of onward node.
let downstream_node: Bytes = if is_leaf {
// no onward node because we're a leaf - create fake stub and use that.
self.augmented(&Self::compose_stub_branch(old_rlp.at(1).data()), &partial.mid(cp), value, diff)
2015-12-01 19:45:30 +01:00
} else {
2015-12-01 20:24:47 +01:00
self.augmented(self.take_node(&old_rlp.at(1), diff), &partial.mid(cp), value, diff)
};
Self::create_extension(&existing_key, downstream_node, diff)
2015-11-30 16:06:29 +01:00
},
2015-12-01 19:09:48 +01:00
(_, cp) => {
2015-12-01 01:12:06 +01:00
// partially-shared prefix for this extension:
// split into two extensions, high and low, pass the
2015-12-01 03:35:55 +01:00
// low through augment with the value before inserting the result
2015-12-01 01:12:06 +01:00
// into high to create the new.
2015-12-01 03:35:55 +01:00
// 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);
2015-12-01 01:12:06 +01:00
// low (farther from root)
2015-12-01 03:35:55 +01:00
let low = Self::compose_raw(&existing_key.mid(cp), old_rlp.at(1).raw(), is_leaf);
let augmented_low = self.augmented(&low, &partial.mid(cp), value, diff);
2015-12-01 01:12:06 +01:00
// high (closer to root)
let mut s = RlpStream::new_list(2);
2015-12-01 03:35:55 +01:00
s.append(&existing_key.encoded_leftmost(cp, false));
diff.new_node(augmented_low, &mut s);
2015-12-01 01:12:06 +01:00
s.out()
2015-11-30 16:06:29 +01:00
},
2015-12-01 02:23:53 +01:00
}
2015-11-30 02:57:02 +01:00
},
2015-12-01 19:45:30 +01:00
Prototype::Data(0) => {
2015-12-01 12:10:36 +01:00
trace!("empty: COMPOSE");
2015-12-01 01:44:18 +01:00
Self::compose_leaf(partial, value)
2015-11-30 14:53:22 +01:00
},
2015-11-30 13:19:55 +01:00
_ => panic!("Invalid RLP for node."),
2015-11-30 13:25:37 +01:00
}
2015-11-30 02:57:02 +01:00
}
2015-11-29 15:50:33 +01:00
}
impl Trie for TrieDB {
fn root(&self) -> &H256 { &self.root }
2015-11-29 18:45:41 +01:00
2015-11-30 02:57:02 +01:00
fn contains(&self, _key: &[u8]) -> bool {
2015-11-29 18:45:41 +01:00
unimplemented!();
}
2015-12-01 16:39:44 +01:00
fn at<'a>(&'a self, key: &'a [u8]) -> Option<&'a [u8]> {
self.get(&NibbleSlice::new(key))
2015-11-29 18:45:41 +01:00
}
2015-11-30 02:57:02 +01:00
fn insert(&mut self, key: &[u8], value: &[u8]) {
2015-12-01 02:23:53 +01:00
self.add(&NibbleSlice::new(key), value);
2015-11-29 18:45:41 +01:00
}
2015-11-30 02:57:02 +01:00
fn remove(&mut self, _key: &[u8]) {
2015-11-29 18:45:41 +01:00
unimplemented!();
}
2015-11-29 15:50:33 +01:00
}
2015-12-01 03:35:55 +01:00
#[cfg(test)]
mod tests {
2015-11-30 14:53:22 +01:00
use triehash::*;
2015-12-01 03:35:55 +01:00
use super::*;
2015-12-01 16:39:44 +01:00
use nibbleslice::*;
use rlp;
2015-12-01 01:44:18 +01:00
use env_logger;
2015-12-01 16:39:44 +01:00
#[test]
fn test_node_leaf() {
let k = vec![0x20u8, 0x01, 0x23, 0x45];
let v: Vec<u8> = From::from("cat");
let (slice, is_leaf) = NibbleSlice::from_encoded(&k);
assert_eq!(is_leaf, true);
let leaf = Node::Leaf(slice, &v);
let rlp = leaf.encoded();
let leaf2 = Node::decoded(&rlp);
assert_eq!(leaf, leaf2);
}
#[test]
fn test_node_extension() {
let k = vec![0x00u8, 0x01, 0x23, 0x45];
// in extension, value must be valid rlp
let v = rlp::encode(&"cat");
let (slice, is_leaf) = NibbleSlice::from_encoded(&k);
assert_eq!(is_leaf, false);
let ex = Node::ExtensionRaw(slice, &v);
let rlp = ex.encoded();
let ex2 = Node::decoded(&rlp);
assert_eq!(ex, ex2);
}
#[test]
fn test_node_empty_branch() {
let branch = Node::Branch(None, &b""[..]);
let rlp = branch.encoded();
let branch2 = Node::decoded(&rlp);
assert_eq!(branch, branch2);
}
#[test]
fn test_node_branch() {
let k = rlp::encode(&"cat");
let mut nodes: [&[u8]; 16] = unsafe { ::std::mem::uninitialized() };
for i in 0..16 { nodes[i] = &k; }
let v: Vec<u8> = From::from("dog");
let branch = Node::Branch(Some(nodes), &v);
let rlp = branch.encoded();
let branch2 = Node::decoded(&rlp);
assert_eq!(branch, branch2);
}
#[test]
fn test_at_empty() {
let t = TrieDB::new_memory();
2015-12-01 16:39:44 +01:00
assert_eq!(t.at(&[0x5]), None);
}
#[test]
fn test_at_one() {
let mut t = TrieDB::new_memory();
2015-12-01 16:39:44 +01:00
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
assert_eq!(t.at(&[0x1, 0x23]).unwrap(), &[0x1u8, 0x23]);
}
2015-12-01 17:03:54 +01:00
#[test]
fn test_at_three() {
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.at(&[0x01, 0x23]).unwrap(), &[0x01u8, 0x23]);
assert_eq!(t.at(&[0xf1, 0x23]).unwrap(), &[0xf1u8, 0x23]);
assert_eq!(t.at(&[0x81, 0x23]).unwrap(), &[0x81u8, 0x23]);
assert_eq!(t.at(&[0x82, 0x23]), None);
}
2015-12-01 18:25:47 +01:00
#[test]
fn test_print_trie() {
let mut t = TrieDB::new_memory();
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
2015-12-01 18:43:44 +01:00
t.insert(&[0x02u8, 0x23], &[0x01u8, 0x23]);
2015-12-01 18:25:47 +01:00
t.insert(&[0xf1u8, 0x23], &[0xf1u8, 0x23]);
t.insert(&[0x81u8, 0x23], &[0x81u8, 0x23]);
println!("trie:");
println!("{:?}", t);
2015-12-01 19:20:48 +01:00
//assert!(false);
2015-12-01 18:25:47 +01:00
}
2015-12-01 17:03:54 +01:00
#[test]
fn test_at_dog() {
env_logger::init().ok();
2015-12-01 17:03:54 +01:00
let mut t = TrieDB::new_memory();
let v: Vec<(Vec<u8>, Vec<u8>)> = vec![
(From::from("do"), From::from("verb")),
(From::from("dog"), From::from("puppy")),
2015-12-01 22:38:34 +01:00
(From::from("doge"), From::from("coin")),
2015-12-01 22:22:52 +01:00
(From::from("horse"), From::from("stallion")),
2015-12-01 17:03:54 +01:00
];
for i in 0..v.len() {
let key: &[u8]= &v[i].0;
let val: &[u8] = &v[i].1;
t.insert(&key, &val);
}
2015-12-01 22:38:34 +01:00
// trace!("{:?}", t);
2015-12-01 20:24:47 +01:00
2015-12-01 19:09:48 +01:00
assert_eq!(*t.root(), trie_root(v));
/*for i in 0..v.len() {
let key: &[u8]= &v[i].0;
let val: &[u8] = &v[i].1;
assert_eq!(t.at(&key).unwrap(), val);
}*/
2015-12-01 17:03:54 +01:00
}
2015-12-01 03:35:55 +01:00
#[test]
fn playpen() {
env_logger::init().ok();
2015-11-29 15:50:33 +01:00
let big_value = b"00000000000000000000000000000000";
2015-12-01 12:10:36 +01:00
let mut t = TrieDB::new_memory();
t.insert(&[0x01u8, 0x23], big_value);
t.insert(&[0x11u8, 0x23], big_value);
2015-12-01 12:10:36 +01:00
assert_eq!(*t.root(), trie_root(vec![
(vec![0x01u8, 0x23], big_value.to_vec()),
(vec![0x11u8, 0x23], big_value.to_vec())
2015-12-01 12:10:36 +01:00
]));
2015-12-01 03:35:55 +01:00
}
2015-11-29 15:50:33 +01:00
2015-12-01 03:35:55 +01:00
#[test]
fn init() {
2015-12-01 12:10:36 +01:00
let t = TrieDB::new_memory();
2015-12-01 03:35:55 +01:00
assert_eq!(*t.root(), SHA3_NULL_RLP);
assert!(t.is_empty());
}
2015-12-01 01:44:18 +01:00
2015-12-01 03:35:55 +01:00
#[test]
fn insert_on_empty() {
2015-12-01 12:10:36 +01:00
let mut t = TrieDB::new_memory();
2015-12-01 03:35:55 +01:00
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
assert_eq!(*t.root(), trie_root(vec![ (vec![0x01u8, 0x23], vec![0x01u8, 0x23]) ]));
}
#[test]
fn insert_replace_root() {
2015-12-01 12:10:36 +01:00
let mut t = TrieDB::new_memory();
2015-12-01 03:35:55 +01:00
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]) ]));
}
2015-12-01 12:10:36 +01:00
#[test]
2015-12-01 12:57:08 +01:00
fn insert_make_branch_root() {
2015-12-01 12:10:36 +01:00
let mut t = TrieDB::new_memory();
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
t.insert(&[0x11u8, 0x23], &[0x11u8, 0x23]);
assert_eq!(*t.root(), trie_root(vec![
(vec![0x01u8, 0x23], vec![0x01u8, 0x23]),
(vec![0x11u8, 0x23], vec![0x11u8, 0x23])
]));
}
2015-12-01 12:57:08 +01:00
#[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]),
]));
}
#[test]
fn insert_value_into_branch_root() {
let mut t = TrieDB::new_memory();
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
t.insert(&[], &[0x0]);
assert_eq!(*t.root(), trie_root(vec![
(vec![], vec![0x0]),
(vec![0x01u8, 0x23], vec![0x01u8, 0x23]),
]));
}
#[test]
fn insert_split_leaf() {
let mut t = TrieDB::new_memory();
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
t.insert(&[0x01u8, 0x34], &[0x01u8, 0x34]);
assert_eq!(*t.root(), trie_root(vec![
(vec![0x01u8, 0x23], vec![0x01u8, 0x23]),
(vec![0x01u8, 0x34], vec![0x01u8, 0x34]),
]));
}
#[test]
fn insert_split_extenstion() {
let mut t = TrieDB::new_memory();
t.insert(&[0x01, 0x23, 0x45], &[0x01]);
t.insert(&[0x01, 0xf3, 0x45], &[0x02]);
t.insert(&[0x01, 0xf3, 0xf5], &[0x03]);
assert_eq!(*t.root(), trie_root(vec![
(vec![0x01, 0x23, 0x45], vec![0x01]),
(vec![0x01, 0xf3, 0x45], vec![0x02]),
(vec![0x01, 0xf3, 0xf5], vec![0x03]),
]));
}
#[test]
fn insert_big_value() {
let big_value0 = b"00000000000000000000000000000000";
let big_value1 = b"11111111111111111111111111111111";
let mut t = TrieDB::new_memory();
t.insert(&[0x01u8, 0x23], big_value0);
t.insert(&[0x11u8, 0x23], big_value1);
assert_eq!(*t.root(), trie_root(vec![
(vec![0x01u8, 0x23], big_value0.to_vec()),
(vec![0x11u8, 0x23], big_value1.to_vec())
]));
}
#[test]
fn insert_duplicate_value() {
let big_value = b"00000000000000000000000000000000";
let mut t = TrieDB::new_memory();
t.insert(&[0x01u8, 0x23], big_value);
t.insert(&[0x11u8, 0x23], big_value);
assert_eq!(*t.root(), trie_root(vec![
(vec![0x01u8, 0x23], big_value.to_vec()),
(vec![0x11u8, 0x23], big_value.to_vec())
]));
}
2015-12-01 16:39:44 +01:00
}