Merge branch 'master' of https://github.com/gavofyork/ethcore-util
This commit is contained in:
commit
fe4141237e
@ -30,6 +30,8 @@ use bytes::*;
|
||||
pub struct NibbleSlice<'a> {
|
||||
data: &'a [u8],
|
||||
offset: usize,
|
||||
data_encode_suffix: &'a [u8],
|
||||
offset_encode_suffix: usize,
|
||||
}
|
||||
|
||||
impl<'a, 'view> NibbleSlice<'a> where 'a: 'view {
|
||||
@ -37,7 +39,26 @@ impl<'a, 'view> NibbleSlice<'a> where 'a: 'view {
|
||||
pub fn new(data: &[u8]) -> NibbleSlice { NibbleSlice::new_offset(data, 0) }
|
||||
|
||||
/// Create a new nibble slice with the given byte-slice with a nibble offset.
|
||||
pub fn new_offset(data: &'a [u8], offset: usize) -> NibbleSlice { NibbleSlice{data: data, offset: offset} }
|
||||
pub fn new_offset(data: &'a [u8], offset: usize) -> NibbleSlice { NibbleSlice{data: data, offset: offset, data_encode_suffix: &b""[..], offset_encode_suffix: 0} }
|
||||
|
||||
///
|
||||
pub fn new_composed(a: &'a NibbleSlice, b: &'a NibbleSlice) -> NibbleSlice<'a> { NibbleSlice{data: a.data, offset: a.offset, data_encode_suffix: b.data, offset_encode_suffix: b.offset} }
|
||||
|
||||
/*pub fn new_composed_bytes_offset(a: &NibbleSlice, b: &NibbleSlice) -> (Bytes, usize) {
|
||||
let r: Vec<u8>::with_capacity((a.len() + b.len() + 1) / 2);
|
||||
let mut i = (a.len() + b.len()) % 2;
|
||||
while i < a.len() {
|
||||
match i % 2 {
|
||||
0 => ,
|
||||
1 => ,
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
while i < a.len() + b.len() {
|
||||
i += 1;
|
||||
}
|
||||
(r, a.len() + b.len())
|
||||
}*/
|
||||
|
||||
/// Create a new nibble slice from the given HPE encoded data (e.g. output of `encoded()`).
|
||||
pub fn from_encoded(data: &'a [u8]) -> (NibbleSlice, bool) {
|
||||
@ -48,10 +69,12 @@ impl<'a, 'view> NibbleSlice<'a> where 'a: 'view {
|
||||
pub fn is_empty(&self) -> bool { self.len() == 0 }
|
||||
|
||||
/// Get the length (in nibbles, naturally) of this slice.
|
||||
pub fn len(&self) -> usize { self.data.len() * 2 - self.offset }
|
||||
pub fn len(&self) -> usize { (self.data.len() + self.data_encode_suffix.len()) * 2 - self.offset - self.offset_encode_suffix }
|
||||
|
||||
/// Get the nibble at position `i`.
|
||||
pub fn at(&self, i: usize) -> u8 {
|
||||
let l = self.data.len() * 2 - self.offset;
|
||||
if i < l {
|
||||
if (self.offset + i) & 1 == 1 {
|
||||
self.data[(self.offset + i) / 2] & 15u8
|
||||
}
|
||||
@ -59,9 +82,19 @@ impl<'a, 'view> NibbleSlice<'a> where 'a: 'view {
|
||||
self.data[(self.offset + i) / 2] >> 4
|
||||
}
|
||||
}
|
||||
else {
|
||||
let i = i - l;
|
||||
if (self.offset_encode_suffix + i) & 1 == 1 {
|
||||
self.data_encode_suffix[(self.offset_encode_suffix + i) / 2] & 15u8
|
||||
}
|
||||
else {
|
||||
self.data_encode_suffix[(self.offset_encode_suffix + i) / 2] >> 4
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Return object which represents a view on to this slice (further) offset by `i` nibbles.
|
||||
pub fn mid(&'view self, i: usize) -> NibbleSlice<'a> { NibbleSlice{ data: self.data, offset: self.offset + i} }
|
||||
pub fn mid(&'view self, i: usize) -> NibbleSlice<'a> { NibbleSlice{ data: self.data, offset: self.offset + i, data_encode_suffix: &b""[..], offset_encode_suffix: 0 } }
|
||||
|
||||
/// Do we start with the same nibbles as the whole of `them`?
|
||||
pub fn starts_with(&self, them: &Self) -> bool { self.common_prefix(them) == them.len() }
|
||||
|
830
src/trie.rs
830
src/trie.rs
@ -27,83 +27,12 @@ pub trait Trie {
|
||||
|
||||
#[derive(Eq, PartialEq, Debug)]
|
||||
pub enum Node<'a> {
|
||||
NullRoot,
|
||||
Empty,
|
||||
Leaf(NibbleSlice<'a>, &'a[u8]),
|
||||
Extension(NibbleSlice<'a>, &'a[u8]),
|
||||
Branch([Option<&'a[u8]>; 16], Option<&'a [u8]>)
|
||||
Branch([&'a[u8]; 16], Option<&'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) => Node::Extension(slice, r.at(1).raw()),
|
||||
},
|
||||
// branch - first 16 are nodes, 17th is a value (or empty).
|
||||
Prototype::List(17) => {
|
||||
let mut nodes: [Option<&'a [u8]>; 16] = unsafe { ::std::mem::uninitialized() };
|
||||
for i in 0..16 {
|
||||
nodes[i] = if r.at(i).is_empty() { None } else { Some(r.at(i).raw()) }
|
||||
}
|
||||
Node::Branch(nodes, if r.at(16).is_empty() { None } else { Some(r.at(16).data()) })
|
||||
},
|
||||
// an empty branch index.
|
||||
Prototype::Data(0) => Node::NullRoot,
|
||||
// something went wrong.
|
||||
_ => panic!("Rlp is not valid.")
|
||||
}
|
||||
}
|
||||
|
||||
// todo: should check length before encoding, cause it may just be sha3 of data
|
||||
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::Extension(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::Branch(ref nodes, ref value) => {
|
||||
let mut stream = RlpStream::new_list(17);
|
||||
for i in 0..16 {
|
||||
match nodes[i] {
|
||||
Some(n) => { stream.append_raw(n, 1); },
|
||||
None => { stream.append_empty_data(); },
|
||||
}
|
||||
}
|
||||
match *value {
|
||||
Some(n) => { stream.append(&n); },
|
||||
None => { stream.append_empty_data(); },
|
||||
}
|
||||
stream.out()
|
||||
},
|
||||
Node::NullRoot => {
|
||||
let mut stream = RlpStream::new();
|
||||
stream.append_empty_data();
|
||||
stream.out()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//enum ValidationResult<'a> {
|
||||
//Valid,
|
||||
//Invalid { node: Node<'a>, depth: usize }
|
||||
//}
|
||||
|
||||
enum Operation {
|
||||
New(H256, Bytes),
|
||||
Delete(H256),
|
||||
@ -140,12 +69,125 @@ impl Diff {
|
||||
}
|
||||
}
|
||||
|
||||
fn delete_node_from_slice(&mut self, old: &[u8]) {
|
||||
let r = Rlp::new(old);
|
||||
if r.is_data() && r.size() == 32 {
|
||||
self.0.push(Operation::Delete(H256::decode(&r)));
|
||||
}
|
||||
}
|
||||
|
||||
fn replace_node(&mut self, old: &Rlp, rlp: Bytes, out: &mut RlpStream) {
|
||||
self.delete_node(old);
|
||||
self.new_node(rlp, out);
|
||||
}
|
||||
}
|
||||
|
||||
impl <'a>Node<'a> {
|
||||
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) => Node::Extension(slice, r.at(1).raw()),
|
||||
},
|
||||
// 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(nodes, if r.at(16).is_empty() { None } else { Some(r.at(16).data()) })
|
||||
},
|
||||
// an empty branch index.
|
||||
Prototype::Data(0) => Node::Empty,
|
||||
// something went wrong.
|
||||
_ => panic!("Rlp is not valid.")
|
||||
}
|
||||
}
|
||||
|
||||
// todo: should check length before encoding, cause it may just be sha3 of data
|
||||
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::Extension(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::Branch(ref nodes, ref value) => {
|
||||
let mut stream = RlpStream::new_list(17);
|
||||
for i in 0..16 {
|
||||
stream.append_raw(nodes[i], 1);
|
||||
}
|
||||
match *value {
|
||||
Some(n) => { stream.append(&n); },
|
||||
None => { stream.append_empty_data(); },
|
||||
}
|
||||
stream.out()
|
||||
},
|
||||
Node::Empty => {
|
||||
let mut stream = RlpStream::new();
|
||||
stream.append_empty_data();
|
||||
stream.out()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn encoded_and_added(&self, diff: &mut Diff) -> Bytes {
|
||||
let mut stream = RlpStream::new();
|
||||
match *self {
|
||||
Node::Leaf(ref slice, ref value) => {
|
||||
stream.append_list(2);
|
||||
stream.append(&slice.encoded(true));
|
||||
stream.append(value);
|
||||
},
|
||||
Node::Extension(ref slice, ref raw_rlp) => {
|
||||
stream.append_list(2);
|
||||
stream.append(&slice.encoded(false));
|
||||
stream.append_raw(raw_rlp, 1);
|
||||
},
|
||||
Node::Branch(ref nodes, ref value) => {
|
||||
stream.append_list(17);
|
||||
for i in 0..16 {
|
||||
stream.append_raw(nodes[i], 1);
|
||||
}
|
||||
match *value {
|
||||
Some(n) => { stream.append(&n); },
|
||||
None => { stream.append_empty_data(); },
|
||||
}
|
||||
},
|
||||
Node::Empty => {
|
||||
stream.append_empty_data();
|
||||
}
|
||||
}
|
||||
let node = stream.out();
|
||||
match node.len() {
|
||||
0 ... 31 => node,
|
||||
_ => {
|
||||
let mut stream = RlpStream::new();
|
||||
diff.new_node(node, &mut stream);
|
||||
stream.out()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//enum ValidationResult<'a> {
|
||||
//Valid,
|
||||
//Invalid { node: Node<'a>, depth: usize }
|
||||
//}
|
||||
|
||||
pub struct TrieDB {
|
||||
db: Box<HashDB>,
|
||||
root: H256,
|
||||
@ -155,11 +197,16 @@ impl fmt::Debug for TrieDB {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
try!(writeln!(f, "["));
|
||||
let root_rlp = self.db.lookup(&self.root).expect("Trie root not found!");
|
||||
try!(self.fmt_all(root_rlp, f, 0));
|
||||
try!(self.fmt_all(Node::decoded(root_rlp), f, 0));
|
||||
writeln!(f, "]")
|
||||
}
|
||||
}
|
||||
|
||||
enum MaybeChanged<'a> {
|
||||
Same(Node<'a>),
|
||||
Changed(Bytes),
|
||||
}
|
||||
|
||||
impl TrieDB {
|
||||
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 }
|
||||
|
||||
@ -198,13 +245,16 @@ impl TrieDB {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn fmt_all(&self, node: &[u8], f: &mut fmt::Formatter, deepness: usize) -> fmt::Result {
|
||||
let node = Node::decoded(node);
|
||||
fn get_node<'a>(&'a self, node: &'a [u8]) -> Node {
|
||||
Node::decoded(self.get_raw_or_lookup(node))
|
||||
}
|
||||
|
||||
fn fmt_all(&self, node: Node, f: &mut fmt::Formatter, deepness: usize) -> fmt::Result {
|
||||
match node {
|
||||
Node::Leaf(slice, value) => try!(writeln!(f, "-{:?}: {:?}.", slice, value.pretty())),
|
||||
Node::Leaf(slice, value) => try!(writeln!(f, "'{:?}: {:?}.", slice, value.pretty())),
|
||||
Node::Extension(ref slice, ref item) => {
|
||||
try!(write!(f, "-{:?}- ", slice));
|
||||
try!(self.fmt_all(self.get_raw_or_lookup(item), f, deepness));
|
||||
try!(write!(f, "'{:?} ", slice));
|
||||
try!(self.fmt_all(self.get_node(item), f, deepness));
|
||||
},
|
||||
Node::Branch(ref nodes, ref value) => {
|
||||
try!(writeln!(f, ""));
|
||||
@ -216,18 +266,18 @@ impl TrieDB {
|
||||
&None => {}
|
||||
}
|
||||
for i in 0..16 {
|
||||
match nodes[i] {
|
||||
Some(n) => {
|
||||
match self.get_node(nodes[i]) {
|
||||
Node::Empty => {},
|
||||
n => {
|
||||
try!(self.fmt_indent(f, deepness + 1));
|
||||
try!(write!(f, "{:x}: ", i));
|
||||
try!(self.fmt_all(self.get_raw_or_lookup(n), f, deepness + 1));
|
||||
},
|
||||
None => {},
|
||||
try!(write!(f, "'{:x} ", i));
|
||||
try!(self.fmt_all(n, f, deepness + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
// empty
|
||||
Node::NullRoot => {
|
||||
Node::Empty => {
|
||||
try!(writeln!(f, "<empty>"));
|
||||
}
|
||||
};
|
||||
@ -247,18 +297,13 @@ impl TrieDB {
|
||||
},
|
||||
Node::Branch(ref nodes, value) => match key.is_empty() {
|
||||
true => value,
|
||||
false => match nodes[key.at(0) as usize] {
|
||||
Some(payload) => {
|
||||
self.get_from_node(self.get_raw_or_lookup(payload), &key.mid(1))
|
||||
},
|
||||
None => None
|
||||
}
|
||||
false => self.get_from_node(self.get_raw_or_lookup(nodes[key.at(0) as usize]), &key.mid(1))
|
||||
},
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
fn get_raw_or_lookup<'a>(&'a self, node: &'a [u8]) -> &'a [u8] {
|
||||
fn get_raw_or_lookup<'a, 'b>(&'a self, node: &'b [u8]) -> &'b [u8] where 'a: 'b {
|
||||
// check if its sha3 + len
|
||||
let r = Rlp::new(node);
|
||||
match r.is_data() && r.size() == 32 {
|
||||
@ -277,6 +322,22 @@ impl TrieDB {
|
||||
trace!("/");
|
||||
}
|
||||
|
||||
fn delete(&mut self, key: &NibbleSlice) {
|
||||
trace!("DELETE: {:?}", key);
|
||||
// determine what the new root is, insert new nodes and remove old as necessary.
|
||||
let mut todo: Diff = Diff::new();
|
||||
match self.cleared_from_slice(self.db.lookup(&self.root).expect("Trie root not found!"), key, &mut todo) {
|
||||
Some(root_rlp) => {
|
||||
self.apply(todo);
|
||||
self.set_root_rlp(&root_rlp);
|
||||
},
|
||||
None => {
|
||||
trace!("no change needed");
|
||||
}
|
||||
}
|
||||
trace!("/");
|
||||
}
|
||||
|
||||
fn compose_leaf(partial: &NibbleSlice, value: &[u8]) -> Bytes {
|
||||
trace!("compose_leaf {:?} {:?} ({:?})", partial, value.pretty(), partial.encoded(true).pretty());
|
||||
let mut s = RlpStream::new_list(2);
|
||||
@ -288,12 +349,12 @@ impl TrieDB {
|
||||
}
|
||||
|
||||
fn compose_raw(partial: &NibbleSlice, raw_payload: &[u8], is_leaf: bool) -> Bytes {
|
||||
println!("compose_raw {:?} {:?} {:?} ({:?})", partial, raw_payload.pretty(), is_leaf, partial.encoded(is_leaf));
|
||||
trace!("compose_raw {:?} {:?} {:?} ({:?})", partial, raw_payload.pretty(), is_leaf, partial.encoded(is_leaf));
|
||||
let mut s = RlpStream::new_list(2);
|
||||
s.append(&partial.encoded(is_leaf));
|
||||
s.append_raw(raw_payload, 1);
|
||||
let r = s.out();
|
||||
println!("compose_raw: -> {:?}", r.pretty());
|
||||
trace!("compose_raw: -> {:?}", r.pretty());
|
||||
r
|
||||
}
|
||||
|
||||
@ -361,17 +422,18 @@ impl TrieDB {
|
||||
s.out()
|
||||
}
|
||||
|
||||
fn transmuted_leaf_to_branch(orig_partial: &NibbleSlice, orig_raw_payload: &[u8], diff: &mut Diff) -> Bytes {
|
||||
fn transmuted_leaf_to_branch(orig_partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
|
||||
trace!("transmuted_leaf_to_branch");
|
||||
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.
|
||||
for i in 0..17 {
|
||||
if index == i {
|
||||
// this is our node.
|
||||
diff.new_node(Self::compose_raw(&orig_partial.mid(if i == 16 {0} else {1}), orig_raw_payload, true), &mut s);
|
||||
} else {
|
||||
s.append_empty_data();
|
||||
match (index == i, i) {
|
||||
(true, 16) => // leaf entry - just replace.
|
||||
{ s.append(&value); },
|
||||
(true, _) => // easy - original had empty slot.
|
||||
diff.new_node(Self::compose_leaf(&orig_partial.mid(1), value), &mut s),
|
||||
(false, _) => { s.append_empty_data(); }
|
||||
}
|
||||
}
|
||||
s.out()
|
||||
@ -383,7 +445,7 @@ impl TrieDB {
|
||||
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 {
|
||||
true => Self::transmuted_leaf_to_branch(orig_partial, orig_raw_payload, diff),
|
||||
true => Self::transmuted_leaf_to_branch(orig_partial, Rlp::new(orig_raw_payload).data(), diff),
|
||||
false => Self::transmuted_extension_to_branch(orig_partial, orig_raw_payload, diff),
|
||||
};
|
||||
self.augmented(&intermediate, partial, value, diff)
|
||||
@ -420,7 +482,7 @@ impl TrieDB {
|
||||
/// The database will be updated so as to make the returned RLP valid through inserting
|
||||
/// 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 nor destroy the original.**
|
||||
fn augmented(&self, old: &[u8], partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
|
||||
trace!("augmented (old: {:?}, partial: {:?}, value: {:?})", old.pretty(), partial, value.pretty());
|
||||
// already have an extension. either fast_forward, cleve or transmute_to_branch.
|
||||
@ -486,6 +548,155 @@ impl TrieDB {
|
||||
_ => panic!("Invalid RLP for node: {:?}", old.pretty()),
|
||||
}
|
||||
}
|
||||
|
||||
fn encoded(n: MaybeChanged) -> Bytes {
|
||||
match n {
|
||||
MaybeChanged::Same(n) => n.encoded(),
|
||||
MaybeChanged::Changed(b) => b,
|
||||
}
|
||||
}
|
||||
|
||||
fn fixed_indirection<'a>(n: Node<'a>, diff: &mut Diff) -> MaybeChanged<'a> {
|
||||
match n {
|
||||
Node::Extension(partial, payload) if payload.len() >= 32 => {
|
||||
// make indirect
|
||||
MaybeChanged::Changed(Node::Extension(partial, &Node::decoded(payload).encoded_and_added(diff)).encoded())
|
||||
},
|
||||
Node::Branch(nodes, node_value) => {
|
||||
// check each child isn't too big
|
||||
// TODO OPTIMISE - should really check at the point of (re-)constructing the branch.
|
||||
for i in 0..16 {
|
||||
if nodes[i].len() >= 32 {
|
||||
let n = Node::decoded(nodes[i]).encoded_and_added(diff);
|
||||
let mut new_nodes = nodes;
|
||||
new_nodes[i] = &n;
|
||||
return MaybeChanged::Changed(Node::Branch(new_nodes, node_value).encoded())
|
||||
}
|
||||
}
|
||||
MaybeChanged::Same(n)
|
||||
}
|
||||
_ => MaybeChanged::Same(n),
|
||||
}
|
||||
}
|
||||
|
||||
/// Given a node `n` which may be in an _invalid state_, fix it such that it is then in a valid
|
||||
/// state.
|
||||
///
|
||||
/// _invalid state_ means:
|
||||
/// - Branch node where there is only a single entry;
|
||||
/// - Extension node followed by anything other than a Branch node.
|
||||
/// - Extension node with a child which has too many bytes to be inline.
|
||||
///
|
||||
/// **This operation will not insert the new node nor destroy the original.**
|
||||
fn fixed<'a, 'b>(&'a self, n: Node<'b>, diff: &mut Diff) -> MaybeChanged<'b> where 'a: 'b {
|
||||
match n {
|
||||
Node::Branch(nodes, node_value) => {
|
||||
// if only a single value, transmute to leaf/extension and feed through fixed.
|
||||
let mut index: [u8; 1] = [16; 1];
|
||||
// 0-15 -> index of a non-null branch
|
||||
// 16 -> no non-null branch
|
||||
// 17 -> multiple non-null branches
|
||||
for i in 0..16 {
|
||||
match (nodes[i] == NULL_RLP, index[0]) {
|
||||
(false, _) => {},
|
||||
(true, 16) => index[0] = i as u8,
|
||||
(true, _) => index[0] = 17,
|
||||
}
|
||||
}
|
||||
match (index[0], node_value) {
|
||||
(16, None) => panic!("Branch with no subvalues. Something went wrong."),
|
||||
(0 ... 15, None) => { // one onward node
|
||||
// transmute to extension.
|
||||
// TODO: OPTIMISE: - don't call fixed again but put the right node in straight away here.
|
||||
// call fixed again since the transmute may cause invalidity.
|
||||
MaybeChanged::Changed(Self::encoded(self.fixed(Node::Extension(NibbleSlice::new_offset(&index[..], 1), nodes[index[0] as usize]), diff)))
|
||||
},
|
||||
(16, Some(value)) => { // one leaf value
|
||||
// transmute to leaf.
|
||||
// call fixed again since the transmute may cause invalidity.
|
||||
MaybeChanged::Changed(Self::encoded(self.fixed(Node::Leaf(NibbleSlice::new(&b""[..]), value), diff)))
|
||||
}
|
||||
_ => { // onwards node(s) and/or leaf
|
||||
// no transmute needed, but should still fix the indirection.
|
||||
Self::fixed_indirection(Node::Branch(nodes, node_value), diff)
|
||||
},
|
||||
}
|
||||
},
|
||||
Node::Extension(partial, payload) => {
|
||||
match Node::decoded(self.get_raw_or_lookup(payload)) {
|
||||
Node::Extension(sub_partial, sub_payload) => {
|
||||
// combine with node below
|
||||
diff.delete_node_from_slice(payload);
|
||||
MaybeChanged::Changed(Self::encoded(Self::fixed_indirection(Node::Extension(NibbleSlice::new_composed(&partial, &sub_partial), sub_payload), diff)))
|
||||
},
|
||||
Node::Leaf(sub_partial, sub_value) => {
|
||||
// combine with node below
|
||||
diff.delete_node_from_slice(payload);
|
||||
MaybeChanged::Changed(Self::encoded(Self::fixed_indirection(Node::Leaf(NibbleSlice::new_composed(&partial, &sub_partial), sub_value), diff)))
|
||||
},
|
||||
// no change, might still have an oversize node inline - fix indirection
|
||||
_ => Self::fixed_indirection(n, diff),
|
||||
}
|
||||
},
|
||||
// leaf or empty. no change.
|
||||
n => { MaybeChanged::Same(n) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Determine the RLP of the node, assuming we're removing `partial` from 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 represents the new node.
|
||||
/// `None` may be returned should no change be needed.
|
||||
///
|
||||
/// The database will be updated so as to make the returned RLP valid through inserting
|
||||
/// and deleting nodes as necessary.
|
||||
///
|
||||
/// **This operation will not insert the new node nor destroy the original.**
|
||||
fn cleared_from_slice(&self, old: &[u8], partial: &NibbleSlice, diff: &mut Diff) -> Option<Bytes> {
|
||||
self.cleared(Node::decoded(old), partial, diff)
|
||||
}
|
||||
|
||||
fn cleared(&self, n: Node, partial: &NibbleSlice, diff: &mut Diff) -> Option<Bytes> {
|
||||
trace!("cleared (old: {:?}, partial: {:?})", n, partial);
|
||||
|
||||
match (n, partial.is_empty()) {
|
||||
(Node::Empty, _) => None,
|
||||
(Node::Branch(_, None), true) => { None },
|
||||
(Node::Branch(nodes, _), true) => Some(Self::encoded(self.fixed(Node::Branch(nodes, None), diff))), // matched as leaf-branch - give back fixed branch with it.
|
||||
(Node::Branch(nodes, value), false) => {
|
||||
// Branch with partial left - route, clear, fix.
|
||||
let i: usize = partial.at(0) as usize;
|
||||
self.cleared(self.get_node(nodes[i]), &partial.mid(1), diff).map(|new_payload| {
|
||||
// downsteam node needed to be changed.
|
||||
diff.delete_node_from_slice(nodes[i]);
|
||||
// return fixed up new node.
|
||||
let mut new_nodes = nodes;
|
||||
new_nodes[i] = &new_payload;
|
||||
Self::encoded(self.fixed(Node::Branch(new_nodes, value), diff))
|
||||
})
|
||||
},
|
||||
(Node::Leaf(node_partial, _), _) => {
|
||||
match node_partial.common_prefix(partial) {
|
||||
cp if cp == partial.len() => Some(Node::Empty.encoded()), // leaf to be deleted - delete it :)
|
||||
_ => None, // anything else and the key doesn't exit - no change.
|
||||
}
|
||||
},
|
||||
(Node::Extension(node_partial, node_payload), _) => {
|
||||
match node_partial.common_prefix(partial) {
|
||||
cp if cp < partial.len() => None, // key in the middle of an extension - doesn't exist.
|
||||
_ => {
|
||||
// key at end of extension - skip, clear, fix
|
||||
self.cleared(self.get_node(node_payload), &partial.mid(node_partial.len()), diff).map(|new_payload| {
|
||||
// downsteam node needed to be changed.
|
||||
diff.delete_node_from_slice(node_payload);
|
||||
// return fixed up new node.
|
||||
Self::encoded(self.fixed(Node::Extension(node_partial, &new_payload), diff))
|
||||
})
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Trie for TrieDB {
|
||||
@ -503,8 +714,8 @@ impl Trie for TrieDB {
|
||||
self.add(&NibbleSlice::new(key), value);
|
||||
}
|
||||
|
||||
fn remove(&mut self, _key: &[u8]) {
|
||||
unimplemented!();
|
||||
fn remove(&mut self, key: &[u8]) {
|
||||
self.delete(&NibbleSlice::new(key));
|
||||
}
|
||||
}
|
||||
|
||||
@ -512,209 +723,29 @@ impl Trie for TrieDB {
|
||||
mod tests {
|
||||
use rustc_serialize::hex::FromHex;
|
||||
use triehash::*;
|
||||
use hash::*;
|
||||
use super::*;
|
||||
use nibbleslice::*;
|
||||
use rlp;
|
||||
use env_logger;
|
||||
use rand::random;
|
||||
|
||||
#[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::Extension(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; 16], None);
|
||||
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: [Option<&[u8]>; 16] = unsafe { ::std::mem::uninitialized() };
|
||||
for i in 0..16 { nodes[i] = Some(&k); }
|
||||
let v: Vec<u8> = From::from("dog");
|
||||
let branch = Node::Branch(nodes, Some(&v));
|
||||
let rlp = branch.encoded();
|
||||
let branch2 = Node::decoded(&rlp);
|
||||
assert_eq!(branch, branch2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_at_empty() {
|
||||
let t = TrieDB::new_memory();
|
||||
assert_eq!(t.at(&[0x5]), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_at_one() {
|
||||
let mut t = TrieDB::new_memory();
|
||||
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
|
||||
assert_eq!(t.at(&[0x1, 0x23]).unwrap(), &[0x1u8, 0x23]);
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_print_trie() {
|
||||
let mut t = TrieDB::new_memory();
|
||||
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
|
||||
t.insert(&[0x02u8, 0x23], &[0x01u8, 0x23]);
|
||||
t.insert(&[0xf1u8, 0x23], &[0xf1u8, 0x23]);
|
||||
t.insert(&[0x81u8, 0x23], &[0x81u8, 0x23]);
|
||||
println!("trie:");
|
||||
println!("{:?}", t);
|
||||
//assert!(false);
|
||||
}
|
||||
|
||||
fn test_all(v: Vec<(Vec<u8>, Vec<u8>)>) {
|
||||
let mut t = TrieDB::new_memory();
|
||||
|
||||
for i in 0..v.len() {
|
||||
let key: &[u8]= &v[i].0;
|
||||
let val: &[u8] = &v[i].1;
|
||||
t.insert(&key, &val);
|
||||
}
|
||||
|
||||
// trace!("{:?}", t);
|
||||
// println!("{:?}", t);
|
||||
|
||||
// check lifetime
|
||||
// let _q = t.at(&[b'd', b'o']).unwrap();
|
||||
|
||||
assert_eq!(*t.root(), trie_root(v));
|
||||
}
|
||||
|
||||
fn random_key() -> Vec<u8> {
|
||||
let chars = b"abcdefgrstuvwABCDEFGRSTUVW";
|
||||
let mut ret: Vec<u8> = Vec::new();
|
||||
let r = random::<u8>() % 4 + 1;
|
||||
for _ in 0..r {
|
||||
ret.push(chars[random::<usize>() % chars.len()]);
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stress() {
|
||||
for _ in 0..1000 {
|
||||
let mut x: Vec<(Vec<u8>, Vec<u8>)> = Vec::new();
|
||||
for j in 0..100u32 {
|
||||
let key = random_key();
|
||||
x.push((key, rlp::encode(&j)));
|
||||
}
|
||||
let real = trie_root(x.clone());
|
||||
|
||||
let mem = trie_root_mem(&x);
|
||||
assert_eq!(mem, real);
|
||||
}
|
||||
}
|
||||
|
||||
fn trie_root_mem(v: &Vec<(Vec<u8>, Vec<u8>)>) -> H256 {
|
||||
let mut t = TrieDB::new_memory();
|
||||
|
||||
for i in 0..v.len() {
|
||||
let key: &[u8]= &v[i].0;
|
||||
let val: &[u8] = &v[i].1;
|
||||
t.insert(&key, &val);
|
||||
}
|
||||
|
||||
t.root().clone()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_at_dog() {
|
||||
env_logger::init().ok();
|
||||
let v = vec![
|
||||
(From::from("do"), From::from("verb")),
|
||||
(From::from("dog"), From::from("puppy")),
|
||||
(From::from("doge"), From::from("coin")),
|
||||
(From::from("horse"), From::from("stallion")),
|
||||
];
|
||||
|
||||
test_all(v);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_more_data() {
|
||||
let v = vec![
|
||||
|
||||
("0000000000000000000000000000000000000000000000000000000000000045".from_hex().unwrap(),
|
||||
"22b224a1420a802ab51d326e29fa98e34c4f24ea".from_hex().unwrap()),
|
||||
|
||||
("0000000000000000000000000000000000000000000000000000000000000046".from_hex().unwrap(),
|
||||
"67706c2076330000000000000000000000000000000000000000000000000000".from_hex().unwrap()),
|
||||
|
||||
("000000000000000000000000697c7b8c961b56f675d570498424ac8de1a918f6".from_hex().unwrap(),
|
||||
"6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000".from_hex().unwrap()),
|
||||
|
||||
("0000000000000000000000007ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap(),
|
||||
"4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap()),
|
||||
|
||||
("000000000000000000000000ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap(),
|
||||
"4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap()),
|
||||
|
||||
("4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap(),
|
||||
"7ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap()),
|
||||
|
||||
("4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap(),
|
||||
"ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap()),
|
||||
|
||||
("6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000".from_hex().unwrap(),
|
||||
"697c7b8c961b56f675d570498424ac8de1a918f6".from_hex().unwrap())
|
||||
|
||||
];
|
||||
|
||||
test_all(v);
|
||||
}
|
||||
use bytes::ToPretty;
|
||||
|
||||
#[test]
|
||||
fn playpen() {
|
||||
env_logger::init().ok();
|
||||
|
||||
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())
|
||||
]));
|
||||
let mut t1 = TrieDB::new_memory();
|
||||
t1.insert(&[0x01], &[0]);
|
||||
t1.insert(&[0x01, 0x23], &[1]);
|
||||
t1.insert(&[0x01, 0x34], &[2]);
|
||||
t1.remove(&[0x01]);
|
||||
let mut t2 = TrieDB::new_memory();
|
||||
t2.insert(&[0x01, 0x23], &[1]);
|
||||
t2.insert(&[0x01, 0x34], &[2]);
|
||||
/*if t1.root() != t2.root()*/ {
|
||||
trace!("{:?}", t1);
|
||||
trace!("{:?}", t2);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -731,6 +762,10 @@ mod tests {
|
||||
assert_eq!(*t.root(), trie_root(vec![ (vec![0x01u8, 0x23], vec![0x01u8, 0x23]) ]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_to_empty() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_replace_root() {
|
||||
let mut t = TrieDB::new_memory();
|
||||
@ -824,5 +859,204 @@ mod tests {
|
||||
(vec![0x11u8, 0x23], big_value.to_vec())
|
||||
]));
|
||||
}
|
||||
|
||||
#[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::Extension(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([&b""[..]; 16], None);
|
||||
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(nodes, Some(&v));
|
||||
let rlp = branch.encoded();
|
||||
let branch2 = Node::decoded(&rlp);
|
||||
assert_eq!(branch, branch2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_at_empty() {
|
||||
let t = TrieDB::new_memory();
|
||||
assert_eq!(t.at(&[0x5]), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_at_one() {
|
||||
let mut t = TrieDB::new_memory();
|
||||
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
|
||||
assert_eq!(t.at(&[0x1, 0x23]).unwrap(), &[0x1u8, 0x23]);
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_print_trie() {
|
||||
let mut t = TrieDB::new_memory();
|
||||
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
|
||||
t.insert(&[0x02u8, 0x23], &[0x01u8, 0x23]);
|
||||
t.insert(&[0xf1u8, 0x23], &[0xf1u8, 0x23]);
|
||||
t.insert(&[0x81u8, 0x23], &[0x81u8, 0x23]);
|
||||
println!("trie:");
|
||||
println!("{:?}", t);
|
||||
//assert!(false);
|
||||
}
|
||||
|
||||
fn test_all(v: Vec<(Vec<u8>, Vec<u8>)>) {
|
||||
let mut t = TrieDB::new_memory();
|
||||
|
||||
for i in 0..v.len() {
|
||||
let key: &[u8]= &v[i].0;
|
||||
let val: &[u8] = &v[i].1;
|
||||
t.insert(&key, &val);
|
||||
}
|
||||
|
||||
// trace!("{:?}", t);
|
||||
// println!("{:?}", t);
|
||||
|
||||
// check lifetime
|
||||
// let _q = t.at(&[b'd', b'o']).unwrap();
|
||||
|
||||
assert_eq!(*t.root(), trie_root(v));
|
||||
}
|
||||
|
||||
fn random_key() -> Vec<u8> {
|
||||
let chars = b"abcdefgrstuvwABCDEFGRSTUVW";
|
||||
let mut ret: Vec<u8> = Vec::new();
|
||||
let r = random::<u8>() % 4 + 1;
|
||||
for _ in 0..r {
|
||||
ret.push(chars[random::<usize>() % chars.len()]);
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stress() {
|
||||
for _ in 0..5000 {
|
||||
let mut x: Vec<(Vec<u8>, Vec<u8>)> = Vec::new();
|
||||
for j in 0..4u32 {
|
||||
let key = random_key();
|
||||
x.push((key, rlp::encode(&j)));
|
||||
}
|
||||
let real = trie_root(x.clone());
|
||||
let memtrie = trie_root_mem(&x);
|
||||
let mut y = x.clone();
|
||||
y.sort_by(|ref a, ref b| a.0.cmp(&b.0));
|
||||
let memtrie_sorted = trie_root_mem(&y);
|
||||
if *memtrie.root() != real || *memtrie_sorted.root() != real {
|
||||
println!("TRIE MISMATCH");
|
||||
println!("");
|
||||
println!("ORIGINAL... {:?}", memtrie.root());
|
||||
for i in x.iter() {
|
||||
println!("{:?} -> {:?}", i.0.pretty(), i.1.pretty());
|
||||
}
|
||||
println!("{:?}", memtrie);
|
||||
println!("SORTED... {:?}", memtrie_sorted.root());
|
||||
for i in y.iter() {
|
||||
println!("{:?} -> {:?}", i.0.pretty(), i.1.pretty());
|
||||
}
|
||||
println!("{:?}", memtrie_sorted);
|
||||
}
|
||||
assert_eq!(*memtrie.root(), real);
|
||||
assert_eq!(*memtrie_sorted.root(), real);
|
||||
}
|
||||
}
|
||||
|
||||
fn trie_root_mem(v: &Vec<(Vec<u8>, Vec<u8>)>) -> TrieDB {
|
||||
let mut t = TrieDB::new_memory();
|
||||
|
||||
for i in 0..v.len() {
|
||||
let key: &[u8]= &v[i].0;
|
||||
let val: &[u8] = &v[i].1;
|
||||
t.insert(&key, &val);
|
||||
}
|
||||
|
||||
t
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_at_dog() {
|
||||
env_logger::init().ok();
|
||||
let v = vec![
|
||||
(From::from("do"), From::from("verb")),
|
||||
(From::from("dog"), From::from("puppy")),
|
||||
(From::from("doge"), From::from("coin")),
|
||||
(From::from("horse"), From::from("stallion")),
|
||||
];
|
||||
|
||||
test_all(v);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_more_data() {
|
||||
let v = vec![
|
||||
|
||||
("0000000000000000000000000000000000000000000000000000000000000045".from_hex().unwrap(),
|
||||
"22b224a1420a802ab51d326e29fa98e34c4f24ea".from_hex().unwrap()),
|
||||
|
||||
("0000000000000000000000000000000000000000000000000000000000000046".from_hex().unwrap(),
|
||||
"67706c2076330000000000000000000000000000000000000000000000000000".from_hex().unwrap()),
|
||||
|
||||
("000000000000000000000000697c7b8c961b56f675d570498424ac8de1a918f6".from_hex().unwrap(),
|
||||
"6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000".from_hex().unwrap()),
|
||||
|
||||
("0000000000000000000000007ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap(),
|
||||
"4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap()),
|
||||
|
||||
("000000000000000000000000ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap(),
|
||||
"4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap()),
|
||||
|
||||
("4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap(),
|
||||
"7ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap()),
|
||||
|
||||
("4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap(),
|
||||
"ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap()),
|
||||
|
||||
("6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000".from_hex().unwrap(),
|
||||
"697c7b8c961b56f675d570498424ac8de1a918f6".from_hex().unwrap())
|
||||
|
||||
];
|
||||
|
||||
test_all(v);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user