2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-07-29 17:12:07 +02:00
|
|
|
use std::fmt;
|
2015-12-19 13:35:26 +01:00
|
|
|
use hashdb::*;
|
|
|
|
use nibbleslice::*;
|
|
|
|
use rlp::*;
|
2017-01-06 16:18:45 +01:00
|
|
|
use super::node::{Node, OwnedNode};
|
|
|
|
use super::lookup::Lookup;
|
|
|
|
use super::{Trie, TrieItem, TrieError, TrieIterator, Query};
|
2017-07-29 21:56:42 +02:00
|
|
|
use {ToPretty, Bytes, H256};
|
2015-12-19 13:35:26 +01:00
|
|
|
|
|
|
|
/// A `Trie` implementation using a generic `HashDB` backing database.
|
2016-03-07 14:33:00 +01:00
|
|
|
///
|
2017-01-06 16:18:45 +01:00
|
|
|
/// Use it as a `Trie` trait object. You can use `db()` to get the backing database object.
|
|
|
|
/// Use `get` and `contains` to query values associated with keys in the trie.
|
2015-12-19 13:35:26 +01:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// extern crate ethcore_util as util;
|
2016-09-01 13:36:32 +02:00
|
|
|
///
|
2015-12-19 13:35:26 +01:00
|
|
|
/// use util::trie::*;
|
|
|
|
/// use util::hashdb::*;
|
|
|
|
/// use util::memorydb::*;
|
|
|
|
/// use util::hash::*;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let mut memdb = MemoryDB::new();
|
|
|
|
/// let mut root = H256::new();
|
2016-08-03 18:35:48 +02:00
|
|
|
/// TrieDBMut::new(&mut memdb, &mut root).insert(b"foo", b"bar").unwrap();
|
2016-06-07 20:44:09 +02:00
|
|
|
/// let t = TrieDB::new(&memdb, &root).unwrap();
|
2016-08-03 18:35:48 +02:00
|
|
|
/// assert!(t.contains(b"foo").unwrap());
|
2016-10-26 13:53:47 +02:00
|
|
|
/// assert_eq!(t.get(b"foo").unwrap().unwrap(), DBValue::from_slice(b"bar"));
|
2015-12-19 13:35:26 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub struct TrieDB<'db> {
|
|
|
|
db: &'db HashDB,
|
|
|
|
root: &'db H256,
|
2016-02-03 14:51:45 +01:00
|
|
|
/// The number of hashes performed so far in operations on this trie.
|
2015-12-19 13:35:26 +01:00
|
|
|
pub hash_count: usize,
|
|
|
|
}
|
|
|
|
|
2016-03-11 11:16:49 +01:00
|
|
|
#[cfg_attr(feature="dev", allow(wrong_self_convention))]
|
2015-12-19 13:35:26 +01:00
|
|
|
impl<'db> TrieDB<'db> {
|
|
|
|
/// Create a new trie with the backing database `db` and `root`
|
2016-06-07 20:44:09 +02:00
|
|
|
/// Returns an error if `root` does not exist
|
2016-08-03 18:35:48 +02:00
|
|
|
pub fn new(db: &'db HashDB, root: &'db H256) -> super::Result<Self> {
|
2016-06-02 21:01:47 +02:00
|
|
|
if !db.contains(root) {
|
2016-08-03 18:35:48 +02:00
|
|
|
Err(Box::new(TrieError::InvalidStateRoot(*root)))
|
2016-06-07 20:44:09 +02:00
|
|
|
} else {
|
|
|
|
Ok(TrieDB {
|
|
|
|
db: db,
|
|
|
|
root: root,
|
|
|
|
hash_count: 0
|
|
|
|
})
|
2015-12-19 13:35:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the backing database.
|
2016-03-07 14:33:00 +01:00
|
|
|
pub fn db(&'db self) -> &'db HashDB {
|
|
|
|
self.db
|
2015-12-19 13:35:26 +01:00
|
|
|
}
|
|
|
|
|
2016-01-17 22:24:42 +01:00
|
|
|
/// Get the data of the root node.
|
2017-01-06 16:18:45 +01:00
|
|
|
fn root_data(&self) -> super::Result<DBValue> {
|
2016-08-03 18:35:48 +02:00
|
|
|
self.db.get(self.root).ok_or_else(|| Box::new(TrieError::InvalidStateRoot(*self.root)))
|
2015-12-19 13:35:26 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 16:18:45 +01:00
|
|
|
/// Indentation helper for `format_all`.
|
2015-12-19 13:35:26 +01:00
|
|
|
fn fmt_indent(&self, f: &mut fmt::Formatter, size: usize) -> fmt::Result {
|
2016-03-07 14:33:00 +01:00
|
|
|
for _ in 0..size {
|
2016-12-27 12:53:56 +01:00
|
|
|
write!(f, " ")?;
|
2015-12-19 13:35:26 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Recursion helper for implementation of formatting trait.
|
|
|
|
fn fmt_all(&self, node: Node, f: &mut fmt::Formatter, deepness: usize) -> fmt::Result {
|
|
|
|
match node {
|
2016-12-27 12:53:56 +01:00
|
|
|
Node::Leaf(slice, value) => writeln!(f, "'{:?}: {:?}.", slice, value.pretty())?,
|
2016-10-26 13:53:47 +02:00
|
|
|
Node::Extension(ref slice, ref item) => {
|
2016-12-27 12:53:56 +01:00
|
|
|
write!(f, "'{:?} ", slice)?;
|
2017-01-06 16:18:45 +01:00
|
|
|
if let Ok(node) = self.get_raw_or_lookup(&*item) {
|
|
|
|
self.fmt_all(Node::decoded(&node), f, deepness)?;
|
2016-08-03 18:35:48 +02:00
|
|
|
}
|
2015-12-19 13:35:26 +01:00
|
|
|
},
|
|
|
|
Node::Branch(ref nodes, ref value) => {
|
2016-12-27 12:53:56 +01:00
|
|
|
writeln!(f, "")?;
|
2016-10-26 13:53:47 +02:00
|
|
|
if let Some(ref v) = *value {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.fmt_indent(f, deepness + 1)?;
|
|
|
|
writeln!(f, "=: {:?}", v.pretty())?
|
2015-12-19 13:35:26 +01:00
|
|
|
}
|
|
|
|
for i in 0..16 {
|
2017-01-06 16:18:45 +01:00
|
|
|
let node = self.get_raw_or_lookup(&*nodes[i]);
|
|
|
|
match node.as_ref().map(|n| Node::decoded(&*n)) {
|
2016-08-03 18:35:48 +02:00
|
|
|
Ok(Node::Empty) => {},
|
|
|
|
Ok(n) => {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.fmt_indent(f, deepness + 1)?;
|
|
|
|
write!(f, "'{:x} ", i)?;
|
|
|
|
self.fmt_all(n, f, deepness + 1)?;
|
2015-12-19 13:35:26 +01:00
|
|
|
}
|
2016-08-03 18:35:48 +02:00
|
|
|
Err(e) => {
|
2016-12-27 12:53:56 +01:00
|
|
|
write!(f, "ERROR: {}", e)?;
|
2016-08-03 18:35:48 +02:00
|
|
|
}
|
2015-12-19 13:35:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// empty
|
|
|
|
Node::Empty => {
|
2016-12-27 12:53:56 +01:00
|
|
|
writeln!(f, "<empty>")?;
|
2015-12-19 13:35:26 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given some node-describing data `node`, return the actual node RLP.
|
|
|
|
/// This could be a simple identity operation in the case that the node is sufficiently small, but
|
|
|
|
/// may require a database lookup.
|
2017-01-06 16:18:45 +01:00
|
|
|
fn get_raw_or_lookup(&'db self, node: &'db [u8]) -> super::Result<DBValue> {
|
2015-12-19 13:35:26 +01:00
|
|
|
// check if its sha3 + len
|
|
|
|
let r = Rlp::new(node);
|
|
|
|
match r.is_data() && r.size() == 32 {
|
2016-08-03 18:35:48 +02:00
|
|
|
true => {
|
|
|
|
let key = r.as_val::<H256>();
|
|
|
|
self.db.get(&key).ok_or_else(|| Box::new(TrieError::IncompleteDatabase(key)))
|
|
|
|
}
|
2016-10-26 13:53:47 +02:00
|
|
|
false => Ok(DBValue::from_slice(node))
|
2015-12-19 13:35:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 16:18:45 +01:00
|
|
|
impl<'db> Trie for TrieDB<'db> {
|
|
|
|
fn iter<'a>(&'a self) -> super::Result<Box<TrieIterator<Item = TrieItem> + 'a>> {
|
|
|
|
TrieDBIterator::new(self).map(|iter| Box::new(iter) as Box<_>)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn root(&self) -> &H256 { self.root }
|
|
|
|
|
|
|
|
fn get_with<'a, 'key, Q: Query>(&'a self, key: &'key [u8], query: Q) -> super::Result<Option<Q::Item>>
|
|
|
|
where 'a: 'key
|
|
|
|
{
|
|
|
|
Lookup {
|
|
|
|
db: self.db,
|
|
|
|
query: query,
|
|
|
|
hash: self.root.clone(),
|
|
|
|
}.look_up(NibbleSlice::new(key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'db> fmt::Debug for TrieDB<'db> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
writeln!(f, "c={:?} [", self.hash_count)?;
|
|
|
|
let root_rlp = self.db.get(self.root).expect("Trie root not found!");
|
|
|
|
self.fmt_all(Node::decoded(&root_rlp), f, 0)?;
|
|
|
|
writeln!(f, "]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 22:18:47 +01:00
|
|
|
#[derive(Clone, Eq, PartialEq)]
|
|
|
|
enum Status {
|
|
|
|
Entering,
|
|
|
|
At,
|
|
|
|
AtChild(usize),
|
|
|
|
Exiting,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Eq, PartialEq)]
|
2016-10-26 13:53:47 +02:00
|
|
|
struct Crumb {
|
2017-01-06 16:18:45 +01:00
|
|
|
node: OwnedNode,
|
2016-01-17 22:18:47 +01:00
|
|
|
status: Status,
|
|
|
|
}
|
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
impl Crumb {
|
2016-01-17 22:18:47 +01:00
|
|
|
/// Move on to next status in the node's sequence.
|
|
|
|
fn increment(&mut self) {
|
|
|
|
self.status = match (&self.status, &self.node) {
|
2017-01-06 16:18:45 +01:00
|
|
|
(_, &OwnedNode::Empty) => Status::Exiting,
|
2016-01-17 22:18:47 +01:00
|
|
|
(&Status::Entering, _) => Status::At,
|
2017-01-06 16:18:45 +01:00
|
|
|
(&Status::At, &OwnedNode::Branch(_, _)) => Status::AtChild(0),
|
|
|
|
(&Status::AtChild(x), &OwnedNode::Branch(_, _)) if x < 15 => Status::AtChild(x + 1),
|
2016-01-17 22:18:47 +01:00
|
|
|
_ => Status::Exiting,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterator for going through all values in the trie.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct TrieDBIterator<'a> {
|
|
|
|
db: &'a TrieDB<'a>,
|
2016-10-26 13:53:47 +02:00
|
|
|
trail: Vec<Crumb>,
|
2016-01-17 22:18:47 +01:00
|
|
|
key_nibbles: Bytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TrieDBIterator<'a> {
|
|
|
|
/// Create a new iterator.
|
2016-09-21 12:56:13 +02:00
|
|
|
pub fn new(db: &'a TrieDB) -> super::Result<TrieDBIterator<'a>> {
|
2016-01-17 22:18:47 +01:00
|
|
|
let mut r = TrieDBIterator {
|
|
|
|
db: db,
|
|
|
|
trail: vec![],
|
|
|
|
key_nibbles: Vec::new(),
|
|
|
|
};
|
2016-09-21 12:56:13 +02:00
|
|
|
|
2017-01-06 16:18:45 +01:00
|
|
|
db.root_data().and_then(|root| r.descend(&root))?;
|
2016-09-21 12:56:13 +02:00
|
|
|
Ok(r)
|
2016-01-17 22:18:47 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 16:18:45 +01:00
|
|
|
fn seek_descend<'key>(&mut self, node_data: DBValue, key: &NibbleSlice<'key>) -> super::Result<()> {
|
|
|
|
let node = Node::decoded(&node_data);
|
|
|
|
match node {
|
2016-11-27 11:11:56 +01:00
|
|
|
Node::Leaf(ref slice, _) => {
|
|
|
|
if slice == key {
|
|
|
|
self.trail.push(Crumb {
|
|
|
|
status: Status::At,
|
2017-01-06 16:18:45 +01:00
|
|
|
node: node.clone().into(),
|
2016-11-27 11:11:56 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
self.trail.push(Crumb {
|
|
|
|
status: Status::Exiting,
|
2017-01-06 16:18:45 +01:00
|
|
|
node: node.clone().into(),
|
2016-11-27 11:11:56 +01:00
|
|
|
});
|
|
|
|
}
|
2017-01-06 16:18:45 +01:00
|
|
|
|
2016-11-27 18:26:23 +01:00
|
|
|
self.key_nibbles.extend(slice.iter());
|
2016-11-24 11:44:24 +01:00
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
Node::Extension(ref slice, ref item) => {
|
|
|
|
if key.starts_with(slice) {
|
|
|
|
self.trail.push(Crumb {
|
|
|
|
status: Status::At,
|
2017-01-06 16:18:45 +01:00
|
|
|
node: node.clone().into(),
|
2016-11-24 11:44:24 +01:00
|
|
|
});
|
|
|
|
self.key_nibbles.extend(slice.iter());
|
2017-01-06 16:18:45 +01:00
|
|
|
let data = self.db.get_raw_or_lookup(&*item)?;
|
|
|
|
self.seek_descend(data, &key.mid(slice.len()))
|
2016-11-24 11:44:24 +01:00
|
|
|
} else {
|
2017-01-06 16:18:45 +01:00
|
|
|
self.descend(&node_data)?;
|
2016-11-24 11:44:24 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Node::Branch(ref nodes, _) => match key.is_empty() {
|
|
|
|
true => {
|
|
|
|
self.trail.push(Crumb {
|
2016-11-27 11:11:56 +01:00
|
|
|
status: Status::At,
|
2017-01-06 16:18:45 +01:00
|
|
|
node: node.clone().into(),
|
2016-11-24 11:44:24 +01:00
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
false => {
|
2016-11-27 11:11:56 +01:00
|
|
|
let i = key.at(0);
|
2016-11-24 11:44:24 +01:00
|
|
|
self.trail.push(Crumb {
|
2016-11-27 11:11:56 +01:00
|
|
|
status: Status::AtChild(i as usize),
|
2017-01-06 16:18:45 +01:00
|
|
|
node: node.clone().into(),
|
2016-11-24 11:44:24 +01:00
|
|
|
});
|
2016-11-27 11:11:56 +01:00
|
|
|
self.key_nibbles.push(i);
|
2017-01-06 16:18:45 +01:00
|
|
|
let child = self.db.get_raw_or_lookup(&*nodes[i as usize])?;
|
|
|
|
self.seek_descend(child, &key.mid(1))
|
2016-11-24 11:44:24 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 22:18:47 +01:00
|
|
|
/// Descend into a payload.
|
2016-10-26 13:53:47 +02:00
|
|
|
fn descend(&mut self, d: &[u8]) -> super::Result<()> {
|
2017-08-21 18:35:39 +02:00
|
|
|
let node = Node::decoded(&self.db.get_raw_or_lookup(d)?).into();
|
|
|
|
Ok(self.descend_into_node(node))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Descend into a payload.
|
|
|
|
fn descend_into_node(&mut self, node: OwnedNode) {
|
2016-01-17 22:18:47 +01:00
|
|
|
self.trail.push(Crumb {
|
|
|
|
status: Status::Entering,
|
2017-08-21 18:35:39 +02:00
|
|
|
node: node,
|
2016-01-17 22:18:47 +01:00
|
|
|
});
|
2017-01-06 16:18:45 +01:00
|
|
|
match &self.trail.last().expect("just pushed item; qed").node {
|
|
|
|
&OwnedNode::Leaf(ref n, _) | &OwnedNode::Extension(ref n, _) => {
|
|
|
|
self.key_nibbles.extend((0..n.len()).map(|i| n.at(i)));
|
|
|
|
},
|
2016-01-17 22:18:47 +01:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The present key.
|
|
|
|
fn key(&self) -> Bytes {
|
|
|
|
// collapse the key_nibbles down to bytes.
|
2017-08-21 10:19:53 +02:00
|
|
|
let nibbles = &self.key_nibbles;
|
2017-08-21 14:36:59 +02:00
|
|
|
let mut i = 1;
|
2017-08-21 10:19:53 +02:00
|
|
|
let mut result = Bytes::with_capacity(nibbles.len() / 2);
|
2017-08-21 14:36:59 +02:00
|
|
|
let len = nibbles.len();
|
2017-08-21 10:19:53 +02:00
|
|
|
while i < len {
|
2017-08-21 14:36:59 +02:00
|
|
|
result.push(nibbles[i - 1] * 16 + nibbles[i]);
|
2017-08-21 10:19:53 +02:00
|
|
|
i += 2;
|
2017-08-17 16:05:26 +02:00
|
|
|
}
|
2017-08-21 10:19:53 +02:00
|
|
|
result
|
2016-01-17 22:18:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 11:44:24 +01:00
|
|
|
impl<'a> TrieIterator for TrieDBIterator<'a> {
|
|
|
|
/// Position the iterator on the first element with key >= `key`
|
|
|
|
fn seek(&mut self, key: &[u8]) -> super::Result<()> {
|
|
|
|
self.trail.clear();
|
|
|
|
self.key_nibbles.clear();
|
2017-01-06 16:18:45 +01:00
|
|
|
let root_rlp = self.db.root_data()?;
|
|
|
|
self.seek_descend(root_rlp, &NibbleSlice::new(key))
|
2016-11-24 11:44:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 22:18:47 +01:00
|
|
|
impl<'a> Iterator for TrieDBIterator<'a> {
|
2016-09-21 12:56:13 +02:00
|
|
|
type Item = TrieItem<'a>;
|
2016-01-17 22:18:47 +01:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2017-08-21 18:35:39 +02:00
|
|
|
enum IterStep {
|
|
|
|
Continue,
|
|
|
|
PopTrail,
|
|
|
|
Descend(super::Result<DBValue>),
|
|
|
|
}
|
|
|
|
|
2016-10-20 14:49:44 +02:00
|
|
|
loop {
|
2017-08-21 18:35:39 +02:00
|
|
|
let iter_step = {
|
|
|
|
match self.trail.last_mut() {
|
|
|
|
Some(b) => { b.increment(); },
|
|
|
|
None => return None,
|
|
|
|
}
|
|
|
|
|
|
|
|
let b = self.trail.last().expect("trail.last_mut().is_some(); qed");
|
|
|
|
|
|
|
|
match (b.status.clone(), &b.node) {
|
|
|
|
(Status::Exiting, n) => {
|
|
|
|
match *n {
|
|
|
|
OwnedNode::Leaf(ref n, _) | OwnedNode::Extension(ref n, _) => {
|
|
|
|
let l = self.key_nibbles.len();
|
|
|
|
self.key_nibbles.truncate(l - n.len());
|
|
|
|
},
|
|
|
|
OwnedNode::Branch(_, _) => { self.key_nibbles.pop(); },
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
IterStep::PopTrail
|
|
|
|
},
|
|
|
|
(Status::At, &OwnedNode::Leaf(_, ref v)) | (Status::At, &OwnedNode::Branch(_, Some(ref v))) => {
|
|
|
|
return Some(Ok((self.key(), v.clone())));
|
|
|
|
},
|
|
|
|
(Status::At, &OwnedNode::Extension(_, ref d)) => IterStep::Descend(self.db.get_raw_or_lookup(&*d)),
|
|
|
|
(Status::At, &OwnedNode::Branch(_, _)) => IterStep::Continue,
|
|
|
|
(Status::AtChild(i), &OwnedNode::Branch(ref children, _)) if children[i].len() > 0 => {
|
|
|
|
match i {
|
|
|
|
0 => self.key_nibbles.push(0),
|
|
|
|
i => *self.key_nibbles.last_mut()
|
|
|
|
.expect("pushed as 0; moves sequentially; removed afterwards; qed") = i as u8,
|
|
|
|
}
|
|
|
|
IterStep::Descend(self.db.get_raw_or_lookup(&*children[i]))
|
|
|
|
},
|
|
|
|
(Status::AtChild(i), &OwnedNode::Branch(_, _)) => {
|
|
|
|
if i == 0 {
|
|
|
|
self.key_nibbles.push(0);
|
|
|
|
}
|
|
|
|
IterStep::Continue
|
|
|
|
},
|
|
|
|
_ => panic!() // Should never see Entering or AtChild without a Branch here.
|
|
|
|
}
|
2016-10-20 14:49:44 +02:00
|
|
|
};
|
2017-08-21 18:35:39 +02:00
|
|
|
|
|
|
|
match iter_step {
|
|
|
|
IterStep::PopTrail => {
|
2016-10-20 14:49:44 +02:00
|
|
|
self.trail.pop();
|
|
|
|
},
|
2017-08-21 18:35:39 +02:00
|
|
|
IterStep::Descend(Ok(d)) => {
|
|
|
|
self.descend_into_node(Node::decoded(&d).into())
|
2016-10-20 14:49:44 +02:00
|
|
|
},
|
2017-08-21 18:35:39 +02:00
|
|
|
IterStep::Descend(Err(e)) => {
|
|
|
|
return Some(Err(e))
|
|
|
|
}
|
|
|
|
IterStep::Continue => {},
|
2016-10-20 14:49:44 +02:00
|
|
|
}
|
2016-01-17 22:18:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn iterator() {
|
|
|
|
use memorydb::*;
|
2016-08-03 18:35:48 +02:00
|
|
|
use super::TrieMut;
|
2016-01-17 22:18:47 +01:00
|
|
|
use super::triedbmut::*;
|
2016-03-07 14:33:00 +01:00
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
let d = vec![ DBValue::from_slice(b"A"), DBValue::from_slice(b"AA"), DBValue::from_slice(b"AB"), DBValue::from_slice(b"B") ];
|
2016-01-17 22:18:47 +01:00
|
|
|
|
|
|
|
let mut memdb = MemoryDB::new();
|
|
|
|
let mut root = H256::new();
|
|
|
|
{
|
|
|
|
let mut t = TrieDBMut::new(&mut memdb, &mut root);
|
|
|
|
for x in &d {
|
2016-08-03 18:35:48 +02:00
|
|
|
t.insert(x, x).unwrap();
|
2016-01-17 22:18:47 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-24 16:53:36 +02:00
|
|
|
|
|
|
|
let t = TrieDB::new(&memdb, &root).unwrap();
|
2017-06-28 14:16:53 +02:00
|
|
|
assert_eq!(d.iter().map(|i| i.clone().into_vec()).collect::<Vec<_>>(), t.iter().unwrap().map(|x| x.unwrap().0).collect::<Vec<_>>());
|
2016-09-21 12:56:13 +02:00
|
|
|
assert_eq!(d, t.iter().unwrap().map(|x| x.unwrap().1).collect::<Vec<_>>());
|
2016-01-17 22:18:47 +01:00
|
|
|
}
|
2016-11-24 11:44:24 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn iterator_seek() {
|
|
|
|
use memorydb::*;
|
|
|
|
use super::TrieMut;
|
|
|
|
use super::triedbmut::*;
|
|
|
|
|
|
|
|
let d = vec![ DBValue::from_slice(b"A"), DBValue::from_slice(b"AA"), DBValue::from_slice(b"AB"), DBValue::from_slice(b"B") ];
|
|
|
|
|
|
|
|
let mut memdb = MemoryDB::new();
|
|
|
|
let mut root = H256::new();
|
|
|
|
{
|
|
|
|
let mut t = TrieDBMut::new(&mut memdb, &mut root);
|
|
|
|
for x in &d {
|
|
|
|
t.insert(x, x).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let t = TrieDB::new(&memdb, &root).unwrap();
|
|
|
|
let mut iter = t.iter().unwrap();
|
2017-06-28 16:41:08 +02:00
|
|
|
assert_eq!(iter.next(), Some(Ok((b"A".to_vec(), DBValue::from_slice(b"A")))));
|
2016-11-27 11:11:56 +01:00
|
|
|
iter.seek(b"!").unwrap();
|
|
|
|
assert_eq!(d, iter.map(|x| x.unwrap().1).collect::<Vec<_>>());
|
2016-11-24 11:44:24 +01:00
|
|
|
let mut iter = t.iter().unwrap();
|
|
|
|
iter.seek(b"A").unwrap();
|
2016-11-27 11:11:56 +01:00
|
|
|
assert_eq!(&d[1..], &iter.map(|x| x.unwrap().1).collect::<Vec<_>>()[..]);
|
|
|
|
let mut iter = t.iter().unwrap();
|
|
|
|
iter.seek(b"AA").unwrap();
|
|
|
|
assert_eq!(&d[2..], &iter.map(|x| x.unwrap().1).collect::<Vec<_>>()[..]);
|
|
|
|
let mut iter = t.iter().unwrap();
|
|
|
|
iter.seek(b"A!").unwrap();
|
|
|
|
assert_eq!(&d[1..], &iter.map(|x| x.unwrap().1).collect::<Vec<_>>()[..]);
|
|
|
|
let mut iter = t.iter().unwrap();
|
|
|
|
iter.seek(b"AB").unwrap();
|
|
|
|
assert_eq!(&d[3..], &iter.map(|x| x.unwrap().1).collect::<Vec<_>>()[..]);
|
|
|
|
let mut iter = t.iter().unwrap();
|
|
|
|
iter.seek(b"AB!").unwrap();
|
|
|
|
assert_eq!(&d[3..], &iter.map(|x| x.unwrap().1).collect::<Vec<_>>()[..]);
|
|
|
|
let mut iter = t.iter().unwrap();
|
|
|
|
iter.seek(b"B").unwrap();
|
|
|
|
assert_eq!(&d[4..], &iter.map(|x| x.unwrap().1).collect::<Vec<_>>()[..]);
|
|
|
|
let mut iter = t.iter().unwrap();
|
|
|
|
iter.seek(b"C").unwrap();
|
|
|
|
assert_eq!(&d[4..], &iter.map(|x| x.unwrap().1).collect::<Vec<_>>()[..]);
|
2016-11-24 11:44:24 +01:00
|
|
|
}
|
2017-01-06 16:18:45 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_len() {
|
|
|
|
use memorydb::*;
|
|
|
|
use super::TrieMut;
|
|
|
|
use super::triedbmut::*;
|
|
|
|
|
|
|
|
let mut memdb = MemoryDB::new();
|
|
|
|
let mut root = H256::new();
|
|
|
|
{
|
|
|
|
let mut t = TrieDBMut::new(&mut memdb, &mut root);
|
|
|
|
t.insert(b"A", b"ABC").unwrap();
|
|
|
|
t.insert(b"B", b"ABCBA").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let t = TrieDB::new(&memdb, &root).unwrap();
|
|
|
|
assert_eq!(t.get_with(b"A", |x: &[u8]| x.len()), Ok(Some(3)));
|
|
|
|
assert_eq!(t.get_with(b"B", |x: &[u8]| x.len()), Ok(Some(5)));
|
|
|
|
assert_eq!(t.get_with(b"C", |x: &[u8]| x.len()), Ok(None));
|
|
|
|
}
|