Get rid of MemoryDB denote (#2881)
This commit is contained in:
committed by
Gav Wood
parent
436b7c213d
commit
5b978be034
@@ -16,7 +16,7 @@
|
||||
|
||||
use hash::H256;
|
||||
use sha3::Hashable;
|
||||
use hashdb::HashDB;
|
||||
use hashdb::{HashDB, DBValue};
|
||||
use super::{TrieDB, Trie, TrieDBIterator, TrieItem, Recorder};
|
||||
|
||||
/// A `Trie` implementation which hashes keys and uses a generic `HashDB` backing database.
|
||||
@@ -58,7 +58,7 @@ impl<'db> Trie for FatDB<'db> {
|
||||
self.raw.contains(&key.sha3())
|
||||
}
|
||||
|
||||
fn get_recorded<'a, 'b, R: 'b>(&'a self, key: &'b [u8], rec: &'b mut R) -> super::Result<Option<&'a [u8]>>
|
||||
fn get_recorded<'a, 'b, R: 'b>(&'a self, key: &'b [u8], rec: &'b mut R) -> super::Result<Option<DBValue>>
|
||||
where 'a: 'b, R: Recorder
|
||||
{
|
||||
self.raw.get_recorded(&key.sha3(), rec)
|
||||
@@ -88,7 +88,7 @@ impl<'db> Iterator for FatDBIterator<'db> {
|
||||
self.trie_iterator.next()
|
||||
.map(|res|
|
||||
res.map(|(hash, value)| {
|
||||
(self.trie.db().get_aux(&hash).expect("Missing fatdb hash"), value)
|
||||
(self.trie.db().get_aux(&hash).expect("Missing fatdb hash").to_vec(), value)
|
||||
})
|
||||
)
|
||||
}
|
||||
@@ -106,6 +106,6 @@ fn fatdb_to_trie() {
|
||||
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap();
|
||||
}
|
||||
let t = FatDB::new(&memdb, &root).unwrap();
|
||||
assert_eq!(t.get(&[0x01u8, 0x23]).unwrap().unwrap(), &[0x01u8, 0x23]);
|
||||
assert_eq!(t.iter().unwrap().map(Result::unwrap).collect::<Vec<_>>(), vec![(vec![0x01u8, 0x23], &[0x01u8, 0x23] as &[u8])]);
|
||||
assert_eq!(t.get(&[0x01u8, 0x23]).unwrap().unwrap(), DBValue::from_slice(&[0x01u8, 0x23]));
|
||||
assert_eq!(t.iter().unwrap().map(Result::unwrap).collect::<Vec<_>>(), vec![(vec![0x01u8, 0x23], DBValue::from_slice(&[0x01u8, 0x23] as &[u8]))]);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
use hash::H256;
|
||||
use sha3::Hashable;
|
||||
use hashdb::HashDB;
|
||||
use hashdb::{HashDB, DBValue};
|
||||
use super::{TrieDBMut, TrieMut};
|
||||
|
||||
/// A mutable `Trie` implementation which hashes keys and uses a generic `HashDB` backing database.
|
||||
@@ -66,7 +66,7 @@ impl<'db> TrieMut for FatDBMut<'db> {
|
||||
self.raw.contains(&key.sha3())
|
||||
}
|
||||
|
||||
fn get<'a, 'key>(&'a self, key: &'key [u8]) -> super::Result<Option<&'a [u8]>>
|
||||
fn get<'a, 'key>(&'a self, key: &'key [u8]) -> super::Result<Option<DBValue>>
|
||||
where 'a: 'key
|
||||
{
|
||||
self.raw.get(&key.sha3())
|
||||
@@ -98,5 +98,5 @@ fn fatdb_to_trie() {
|
||||
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap();
|
||||
}
|
||||
let t = TrieDB::new(&memdb, &root).unwrap();
|
||||
assert_eq!(t.get(&(&[0x01u8, 0x23]).sha3()).unwrap().unwrap(), &[0x01u8, 0x23]);
|
||||
assert_eq!(t.get(&(&[0x01u8, 0x23]).sha3()).unwrap().unwrap(), DBValue::from_slice(&[0x01u8, 0x23]));
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ use hashdb::*;
|
||||
/// Type of operation for the backing database - either a new node or a node deletion.
|
||||
#[derive(Debug)]
|
||||
enum Operation {
|
||||
New(H256, Bytes),
|
||||
New(H256, DBValue),
|
||||
Delete(H256),
|
||||
}
|
||||
|
||||
@@ -52,16 +52,16 @@ impl Journal {
|
||||
|
||||
/// Given the RLP that encodes a node, append a reference to that node `out` and leave `journal`
|
||||
/// such that the reference is valid, once applied.
|
||||
pub fn new_node(&mut self, rlp: Bytes, out: &mut RlpStream) {
|
||||
pub fn new_node(&mut self, rlp: DBValue, out: &mut RlpStream) {
|
||||
if rlp.len() >= 32 {
|
||||
let rlp_sha3 = rlp.sha3();
|
||||
|
||||
trace!("new_node: reference node {:?} => {:?}", rlp_sha3, rlp.pretty());
|
||||
trace!("new_node: reference node {:?} => {:?}", rlp_sha3, &*rlp);
|
||||
out.append(&rlp_sha3);
|
||||
self.0.push(Operation::New(rlp_sha3, rlp));
|
||||
}
|
||||
else {
|
||||
trace!("new_node: inline node {:?}", rlp.pretty());
|
||||
trace!("new_node: inline node {:?}", &*rlp);
|
||||
out.append_raw(&rlp, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
use std::fmt;
|
||||
use hash::H256;
|
||||
use hashdb::HashDB;
|
||||
use hashdb::{HashDB, DBValue};
|
||||
|
||||
/// Export the standardmap module.
|
||||
pub mod standardmap;
|
||||
@@ -76,7 +76,7 @@ impl fmt::Display for TrieError {
|
||||
pub type Result<T> = ::std::result::Result<T, Box<TrieError>>;
|
||||
|
||||
/// Trie-Item type.
|
||||
pub type TrieItem<'a> = Result<(Vec<u8>, &'a [u8])>;
|
||||
pub type TrieItem<'a> = Result<(Vec<u8>, DBValue)>;
|
||||
|
||||
/// A key-value datastore implemented as a database-backed modified Merkle tree.
|
||||
pub trait Trie {
|
||||
@@ -92,13 +92,13 @@ pub trait Trie {
|
||||
}
|
||||
|
||||
/// What is the value of the given key in this trie?
|
||||
fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Result<Option<&'a [u8]>> where 'a: 'key {
|
||||
fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Result<Option<DBValue>> where 'a: 'key {
|
||||
self.get_recorded(key, &mut recorder::NoOp)
|
||||
}
|
||||
|
||||
/// Query the value of the given key in this trie while recording visited nodes
|
||||
/// to the given recorder. If the query fails, the nodes passed to the recorder are unspecified.
|
||||
fn get_recorded<'a, 'b, R: 'b>(&'a self, key: &'b [u8], rec: &'b mut R) -> Result<Option<&'a [u8]>>
|
||||
fn get_recorded<'a, 'b, R: 'b>(&'a self, key: &'b [u8], rec: &'b mut R) -> Result<Option<DBValue>>
|
||||
where 'a: 'b, R: Recorder;
|
||||
|
||||
/// Returns an iterator over elements of trie.
|
||||
@@ -119,7 +119,7 @@ pub trait TrieMut {
|
||||
}
|
||||
|
||||
/// What is the value of the given key in this trie?
|
||||
fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Result<Option<&'a [u8]>> where 'a: 'key;
|
||||
fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Result<Option<DBValue>> where 'a: 'key;
|
||||
|
||||
/// Insert a `key`/`value` pair into the trie. An `empty` value is equivalent to removing
|
||||
/// `key` from the trie.
|
||||
@@ -188,7 +188,7 @@ impl<'db> Trie for TrieKinds<'db> {
|
||||
wrapper!(self, contains, key)
|
||||
}
|
||||
|
||||
fn get_recorded<'a, 'b, R: 'b>(&'a self, key: &'b [u8], r: &'b mut R) -> Result<Option<&'a [u8]>>
|
||||
fn get_recorded<'a, 'b, R: 'b>(&'a self, key: &'b [u8], r: &'b mut R) -> Result<Option<DBValue>>
|
||||
where 'a: 'b, R: Recorder {
|
||||
wrapper!(self, get_recorded, key, r)
|
||||
}
|
||||
|
||||
@@ -14,27 +14,51 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use elastic_array::ElasticArray36;
|
||||
use nibbleslice::*;
|
||||
use bytes::*;
|
||||
use rlp::*;
|
||||
use super::journal::*;
|
||||
use hashdb::DBValue;
|
||||
|
||||
/// Partial node key type.
|
||||
pub type NodeKey = ElasticArray36<u8>;
|
||||
|
||||
/// Type of node in the trie and essential information thereof.
|
||||
#[derive(Clone, Eq, PartialEq, Debug)]
|
||||
pub enum Node<'a> {
|
||||
#[derive(Eq, PartialEq, Debug)]
|
||||
pub enum Node {
|
||||
/// Null trie node; could be an empty root or an empty branch entry.
|
||||
Empty,
|
||||
/// Leaf node; has key slice and value. Value may not be empty.
|
||||
Leaf(NibbleSlice<'a>, &'a [u8]),
|
||||
Leaf(NodeKey, DBValue),
|
||||
/// Extension node; has key slice and node data. Data may not be null.
|
||||
Extension(NibbleSlice<'a>, &'a [u8]),
|
||||
Extension(NodeKey, DBValue),
|
||||
/// Branch node; has array of 16 child nodes (each possibly null) and an optional immediate node data.
|
||||
Branch([&'a [u8]; 16], Option<&'a [u8]>)
|
||||
Branch([NodeKey; 16], Option<DBValue>)
|
||||
}
|
||||
|
||||
impl<'a> Node<'a> {
|
||||
impl Clone for Node {
|
||||
fn clone(&self) -> Node {
|
||||
match *self {
|
||||
Node::Empty => Node::Empty,
|
||||
Node::Leaf(ref k, ref v) => Node::Leaf(k.clone(), v.clone()),
|
||||
Node::Extension(ref k, ref v) => Node::Extension(k.clone(), v.clone()),
|
||||
Node::Branch(ref k, ref v) => {
|
||||
let mut branch = [NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(),
|
||||
NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(),
|
||||
NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new()];
|
||||
for i in 0 .. 16 {
|
||||
branch[i] = k[i].clone();
|
||||
}
|
||||
Node::Branch(branch, v.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Node {
|
||||
/// Decode the `node_rlp` and return the Node.
|
||||
pub fn decoded(node_rlp: &'a [u8]) -> Node<'a> {
|
||||
pub fn decoded(node_rlp: &[u8]) -> Node {
|
||||
let r = Rlp::new(node_rlp);
|
||||
match r.prototype() {
|
||||
// either leaf or extension - decode first item with NibbleSlice::???
|
||||
@@ -43,16 +67,18 @@ impl<'a> Node<'a> {
|
||||
// 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).as_raw()),
|
||||
(slice, true) => Node::Leaf(slice.encoded(true), DBValue::from_slice(r.at(1).data())),
|
||||
(slice, false) => Node::Extension(slice.encoded(false), DBValue::from_slice(r.at(1).as_raw())),
|
||||
},
|
||||
// branch - first 16 are nodes, 17th is a value (or empty).
|
||||
Prototype::List(17) => {
|
||||
let mut nodes: [&'a [u8]; 16] = [&[], &[], &[], &[], &[], &[], &[], &[], &[], &[], &[], &[], &[], &[], &[], &[]];
|
||||
let mut nodes: [NodeKey; 16] = [NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(),
|
||||
NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(),
|
||||
NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new()];
|
||||
for i in 0..16 {
|
||||
nodes[i] = r.at(i).as_raw();
|
||||
nodes[i] = NodeKey::from_slice(r.at(i).as_raw());
|
||||
}
|
||||
Node::Branch(nodes, if r.at(16).is_empty() { None } else { Some(r.at(16).data()) })
|
||||
Node::Branch(nodes, if r.at(16).is_empty() { None } else { Some(DBValue::from_slice(r.at(16).data())) })
|
||||
},
|
||||
// an empty branch index.
|
||||
Prototype::Data(0) => Node::Empty,
|
||||
@@ -69,23 +95,23 @@ impl<'a> Node<'a> {
|
||||
match *self {
|
||||
Node::Leaf(ref slice, ref value) => {
|
||||
let mut stream = RlpStream::new_list(2);
|
||||
stream.append(&slice.encoded(true));
|
||||
stream.append(value);
|
||||
stream.append(&&**slice);
|
||||
stream.append(&&**value);
|
||||
stream.out()
|
||||
},
|
||||
Node::Extension(ref slice, raw_rlp) => {
|
||||
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.append(&&**slice);
|
||||
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);
|
||||
stream.append_raw(&*nodes[i], 1);
|
||||
}
|
||||
match *value {
|
||||
Some(n) => { stream.append(&n); },
|
||||
Some(ref n) => { stream.append(&&**n); },
|
||||
None => { stream.append_empty_data(); },
|
||||
}
|
||||
stream.out()
|
||||
@@ -100,26 +126,26 @@ impl<'a> Node<'a> {
|
||||
|
||||
/// Encode the node, adding it to `journal` if necessary and return the RLP valid for
|
||||
/// insertion into a parent node.
|
||||
pub fn encoded_and_added(&self, journal: &mut Journal) -> Bytes {
|
||||
pub fn encoded_and_added(&self, journal: &mut Journal) -> DBValue {
|
||||
let mut stream = RlpStream::new();
|
||||
match *self {
|
||||
Node::Leaf(ref slice, ref value) => {
|
||||
stream.begin_list(2);
|
||||
stream.append(&slice.encoded(true));
|
||||
stream.append(value);
|
||||
stream.append(&&**slice);
|
||||
stream.append(&&**value);
|
||||
},
|
||||
Node::Extension(ref slice, raw_rlp) => {
|
||||
Node::Extension(ref slice, ref raw_rlp) => {
|
||||
stream.begin_list(2);
|
||||
stream.append(&slice.encoded(false));
|
||||
stream.append_raw(raw_rlp, 1);
|
||||
stream.append(&&**slice);
|
||||
stream.append_raw(&&**raw_rlp, 1);
|
||||
},
|
||||
Node::Branch(ref nodes, ref value) => {
|
||||
stream.begin_list(17);
|
||||
for i in 0..16 {
|
||||
stream.append_raw(nodes[i], 1);
|
||||
stream.append_raw(&*nodes[i], 1);
|
||||
}
|
||||
match *value {
|
||||
Some(n) => { stream.append(&n); },
|
||||
Some(ref n) => { stream.append(&&**n); },
|
||||
None => { stream.append_empty_data(); },
|
||||
}
|
||||
},
|
||||
@@ -127,13 +153,13 @@ impl<'a> Node<'a> {
|
||||
stream.append_empty_data();
|
||||
}
|
||||
}
|
||||
let node = stream.out();
|
||||
let node = DBValue::from_slice(stream.as_raw());
|
||||
match node.len() {
|
||||
0 ... 31 => node,
|
||||
_ => {
|
||||
let mut stream = RlpStream::new();
|
||||
journal.new_node(node, &mut stream);
|
||||
stream.out()
|
||||
DBValue::from_slice(stream.as_raw())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
use hash::H256;
|
||||
use sha3::Hashable;
|
||||
use hashdb::HashDB;
|
||||
use hashdb::{HashDB, DBValue};
|
||||
use super::triedb::TrieDB;
|
||||
use super::{Trie, TrieItem, Recorder};
|
||||
|
||||
@@ -59,7 +59,7 @@ impl<'db> Trie for SecTrieDB<'db> {
|
||||
self.raw.contains(&key.sha3())
|
||||
}
|
||||
|
||||
fn get_recorded<'a, 'b, R: 'b>(&'a self, key: &'b [u8], rec: &'b mut R) -> super::Result<Option<&'a [u8]>>
|
||||
fn get_recorded<'a, 'b, R: 'b>(&'a self, key: &'b [u8], rec: &'b mut R) -> super::Result<Option<DBValue>>
|
||||
where 'a: 'b, R: Recorder
|
||||
{
|
||||
self.raw.get_recorded(&key.sha3(), rec)
|
||||
@@ -79,5 +79,5 @@ fn trie_to_sectrie() {
|
||||
t.insert(&(&[0x01u8, 0x23]).sha3(), &[0x01u8, 0x23]).unwrap();
|
||||
}
|
||||
let t = SecTrieDB::new(&memdb, &root).unwrap();
|
||||
assert_eq!(t.get(&[0x01u8, 0x23]).unwrap().unwrap(), &[0x01u8, 0x23]);
|
||||
assert_eq!(t.get(&[0x01u8, 0x23]).unwrap().unwrap(), DBValue::from_slice(&[0x01u8, 0x23]));
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
use hash::H256;
|
||||
use sha3::Hashable;
|
||||
use hashdb::HashDB;
|
||||
use hashdb::{HashDB, DBValue};
|
||||
use super::triedbmut::TrieDBMut;
|
||||
use super::TrieMut;
|
||||
|
||||
@@ -62,7 +62,7 @@ impl<'db> TrieMut for SecTrieDBMut<'db> {
|
||||
self.raw.contains(&key.sha3())
|
||||
}
|
||||
|
||||
fn get<'a, 'key>(&'a self, key: &'key [u8]) -> super::Result<Option<&'a [u8]>>
|
||||
fn get<'a, 'key>(&'a self, key: &'key [u8]) -> super::Result<Option<DBValue>>
|
||||
where 'a: 'key
|
||||
{
|
||||
self.raw.get(&key.sha3())
|
||||
@@ -90,5 +90,5 @@ fn sectrie_to_trie() {
|
||||
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap();
|
||||
}
|
||||
let t = TrieDB::new(&memdb, &root).unwrap();
|
||||
assert_eq!(t.get(&(&[0x01u8, 0x23]).sha3()).unwrap().unwrap(), &[0x01u8, 0x23]);
|
||||
assert_eq!(t.get(&(&[0x01u8, 0x23]).sha3()).unwrap().unwrap(), DBValue::from_slice(&[0x01u8, 0x23]));
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ use super::{Trie, TrieItem, TrieError};
|
||||
/// TrieDBMut::new(&mut memdb, &mut root).insert(b"foo", b"bar").unwrap();
|
||||
/// let t = TrieDB::new(&memdb, &root).unwrap();
|
||||
/// assert!(t.contains(b"foo").unwrap());
|
||||
/// assert_eq!(t.get(b"foo").unwrap().unwrap(), b"bar");
|
||||
/// assert_eq!(t.get(b"foo").unwrap().unwrap(), DBValue::from_slice(b"bar"));
|
||||
/// assert!(t.db_items_remaining().unwrap().is_empty());
|
||||
/// }
|
||||
/// ```
|
||||
@@ -119,8 +119,8 @@ impl<'db> TrieDB<'db> {
|
||||
};
|
||||
|
||||
match node {
|
||||
Node::Extension(_, payload) => try!(handle_payload(payload)),
|
||||
Node::Branch(payloads, _) => for payload in &payloads { try!(handle_payload(payload)) },
|
||||
Node::Extension(_, ref payload) => try!(handle_payload(payload)),
|
||||
Node::Branch(ref payloads, _) => for payload in payloads { try!(handle_payload(payload)) },
|
||||
_ => {},
|
||||
}
|
||||
|
||||
@@ -129,18 +129,18 @@ impl<'db> TrieDB<'db> {
|
||||
|
||||
/// Get the root node's RLP.
|
||||
fn root_node<R: Recorder>(&self, r: &mut R) -> super::Result<Node> {
|
||||
self.root_data(r).map(Node::decoded)
|
||||
self.root_data(r).map(|d| Node::decoded(&d))
|
||||
}
|
||||
|
||||
/// Get the data of the root node.
|
||||
fn root_data<'a, R: 'a + Recorder>(&self, r: &'a mut R) -> super::Result<&[u8]> {
|
||||
fn root_data<'a, R: 'a + Recorder>(&self, r: &'a mut R) -> super::Result<DBValue> {
|
||||
self.db.get(self.root).ok_or_else(|| Box::new(TrieError::InvalidStateRoot(*self.root)))
|
||||
.map(|node| { r.record(self.root, node, 0); node })
|
||||
.map(|node| { r.record(self.root, &*node, 0); node })
|
||||
}
|
||||
|
||||
/// Get the root node as a `Node`.
|
||||
fn get_node<'a, R: 'a + Recorder>(&'db self, node: &'db [u8], r: &'a mut R, depth: u32) -> super::Result<Node> {
|
||||
self.get_raw_or_lookup(node, r, depth).map(Node::decoded)
|
||||
self.get_raw_or_lookup(node, r, depth).map(|n| Node::decoded(&n))
|
||||
}
|
||||
|
||||
/// Indentation helper for `formal_all`.
|
||||
@@ -155,20 +155,20 @@ impl<'db> TrieDB<'db> {
|
||||
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::Extension(ref slice, item) => {
|
||||
Node::Extension(ref slice, ref item) => {
|
||||
try!(write!(f, "'{:?} ", slice));
|
||||
if let Ok(node) = self.get_node(item, &mut NoOp, 0) {
|
||||
if let Ok(node) = self.get_node(&*item, &mut NoOp, 0) {
|
||||
try!(self.fmt_all(node, f, deepness));
|
||||
}
|
||||
},
|
||||
Node::Branch(ref nodes, ref value) => {
|
||||
try!(writeln!(f, ""));
|
||||
if let Some(v) = *value {
|
||||
if let Some(ref v) = *value {
|
||||
try!(self.fmt_indent(f, deepness + 1));
|
||||
try!(writeln!(f, "=: {:?}", v.pretty()))
|
||||
}
|
||||
for i in 0..16 {
|
||||
match self.get_node(nodes[i], &mut NoOp, 0) {
|
||||
match self.get_node(&*nodes[i], &mut NoOp, 0) {
|
||||
Ok(Node::Empty) => {},
|
||||
Ok(n) => {
|
||||
try!(self.fmt_indent(f, deepness + 1));
|
||||
@@ -190,11 +190,11 @@ impl<'db> TrieDB<'db> {
|
||||
}
|
||||
|
||||
/// Return optional data for a key given as a `NibbleSlice`. Returns `None` if no data exists.
|
||||
fn do_lookup<'key, R: 'key>(&'db self, key: &NibbleSlice<'key>, r: &'key mut R) -> super::Result<Option<&'db [u8]>>
|
||||
fn do_lookup<'key, R: 'key>(&'db self, key: &NibbleSlice<'key>, r: &'key mut R) -> super::Result<Option<DBValue>>
|
||||
where 'db: 'key, R: Recorder
|
||||
{
|
||||
let root_rlp = try!(self.root_data(r));
|
||||
self.get_from_node(root_rlp, key, r, 1)
|
||||
self.get_from_node(&root_rlp, key, r, 1)
|
||||
}
|
||||
|
||||
/// Recursible function to retrieve the value given a `node` and a partial `key`. `None` if no
|
||||
@@ -207,18 +207,23 @@ impl<'db> TrieDB<'db> {
|
||||
key: &NibbleSlice<'key>,
|
||||
r: &'key mut R,
|
||||
d: u32
|
||||
) -> super::Result<Option<&'db [u8]>> where 'db: 'key, R: Recorder {
|
||||
) -> super::Result<Option<DBValue>> where 'db: 'key, R: Recorder {
|
||||
match Node::decoded(node) {
|
||||
Node::Leaf(ref slice, value) if key == slice => Ok(Some(value)),
|
||||
Node::Extension(ref slice, item) if key.starts_with(slice) => {
|
||||
let data = try!(self.get_raw_or_lookup(item, r, d));
|
||||
self.get_from_node(data, &key.mid(slice.len()), r, d + 1)
|
||||
Node::Leaf(ref slice, ref value) if NibbleSlice::from_encoded(slice).0 == *key => Ok(Some(value.clone())),
|
||||
Node::Extension(ref slice, ref item) => {
|
||||
let slice = &NibbleSlice::from_encoded(slice).0;
|
||||
if key.starts_with(slice) {
|
||||
let data = try!(self.get_raw_or_lookup(&*item, r, d));
|
||||
self.get_from_node(&data, &key.mid(slice.len()), r, d + 1)
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
},
|
||||
Node::Branch(ref nodes, value) => match key.is_empty() {
|
||||
true => Ok(value),
|
||||
Node::Branch(ref nodes, ref value) => match key.is_empty() {
|
||||
true => Ok(value.clone()),
|
||||
false => {
|
||||
let node = try!(self.get_raw_or_lookup(nodes[key.at(0) as usize], r, d));
|
||||
self.get_from_node(node, &key.mid(1), r, d + 1)
|
||||
let node = try!(self.get_raw_or_lookup(&*nodes[key.at(0) as usize], r, d));
|
||||
self.get_from_node(&node, &key.mid(1), r, d + 1)
|
||||
}
|
||||
},
|
||||
_ => Ok(None)
|
||||
@@ -228,16 +233,16 @@ impl<'db> TrieDB<'db> {
|
||||
/// 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.
|
||||
fn get_raw_or_lookup<R: Recorder>(&'db self, node: &'db [u8], rec: &mut R, d: u32) -> super::Result<&'db [u8]> {
|
||||
fn get_raw_or_lookup<R: Recorder>(&'db self, node: &'db [u8], rec: &mut R, d: u32) -> super::Result<DBValue> {
|
||||
// check if its sha3 + len
|
||||
let r = Rlp::new(node);
|
||||
match r.is_data() && r.size() == 32 {
|
||||
true => {
|
||||
let key = r.as_val::<H256>();
|
||||
self.db.get(&key).ok_or_else(|| Box::new(TrieError::IncompleteDatabase(key)))
|
||||
.map(|raw| { rec.record(&key, raw, d); raw })
|
||||
.map(|raw| { rec.record(&key, &raw, d); raw })
|
||||
}
|
||||
false => Ok(node)
|
||||
false => Ok(DBValue::from_slice(node))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -251,12 +256,12 @@ enum Status {
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
struct Crumb<'a> {
|
||||
node: Node<'a>,
|
||||
struct Crumb {
|
||||
node: Node,
|
||||
status: Status,
|
||||
}
|
||||
|
||||
impl<'a> Crumb<'a> {
|
||||
impl Crumb {
|
||||
/// Move on to next status in the node's sequence.
|
||||
fn increment(&mut self) {
|
||||
self.status = match (&self.status, &self.node) {
|
||||
@@ -273,7 +278,7 @@ impl<'a> Crumb<'a> {
|
||||
#[derive(Clone)]
|
||||
pub struct TrieDBIterator<'a> {
|
||||
db: &'a TrieDB<'a>,
|
||||
trail: Vec<Crumb<'a>>,
|
||||
trail: Vec<Crumb>,
|
||||
key_nibbles: Bytes,
|
||||
}
|
||||
|
||||
@@ -286,18 +291,18 @@ impl<'a> TrieDBIterator<'a> {
|
||||
key_nibbles: Vec::new(),
|
||||
};
|
||||
|
||||
try!(db.root_data(&mut NoOp).and_then(|root| r.descend(root)));
|
||||
try!(db.root_data(&mut NoOp).and_then(|root| r.descend(&root)));
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
/// Descend into a payload.
|
||||
fn descend(&mut self, d: &'a [u8]) -> super::Result<()> {
|
||||
fn descend(&mut self, d: &[u8]) -> super::Result<()> {
|
||||
self.trail.push(Crumb {
|
||||
status: Status::Entering,
|
||||
node: try!(self.db.get_node(d, &mut NoOp, 0)),
|
||||
});
|
||||
match self.trail.last().expect("just pushed item; qed").node {
|
||||
Node::Leaf(n, _) | Node::Extension(n, _) => { self.key_nibbles.extend(n.iter()); },
|
||||
Node::Leaf(ref n, _) | Node::Extension(ref n, _) => { self.key_nibbles.extend(NibbleSlice::from_encoded(n).0.iter()); },
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -325,7 +330,7 @@ impl<'a> Iterator for TrieDBIterator<'a> {
|
||||
match n {
|
||||
Node::Leaf(n, _) | Node::Extension(n, _) => {
|
||||
let l = self.key_nibbles.len();
|
||||
self.key_nibbles.truncate(l - n.len());
|
||||
self.key_nibbles.truncate(l - NibbleSlice::from_encoded(&*n).0.len());
|
||||
},
|
||||
Node::Branch(_, _) => { self.key_nibbles.pop(); },
|
||||
_ => {}
|
||||
@@ -337,19 +342,19 @@ impl<'a> Iterator for TrieDBIterator<'a> {
|
||||
return Some(Ok((self.key(), v)));
|
||||
},
|
||||
(Status::At, Node::Extension(_, d)) => {
|
||||
if let Err(e) = self.descend(d) {
|
||||
if let Err(e) = self.descend(&*d) {
|
||||
return Some(Err(e));
|
||||
}
|
||||
// continue
|
||||
},
|
||||
(Status::At, Node::Branch(_, _)) => {},
|
||||
(Status::AtChild(i), Node::Branch(children, _)) if children[i].len() > 0 => {
|
||||
(Status::AtChild(i), Node::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,
|
||||
}
|
||||
if let Err(e) = self.descend(children[i]) {
|
||||
if let Err(e) = self.descend(&*children[i]) {
|
||||
return Some(Err(e));
|
||||
}
|
||||
// continue
|
||||
@@ -373,7 +378,7 @@ impl<'db> Trie for TrieDB<'db> {
|
||||
|
||||
fn root(&self) -> &H256 { self.root }
|
||||
|
||||
fn get_recorded<'a, 'b, R: 'b>(&'a self, key: &'b [u8], rec: &'b mut R) -> super::Result<Option<&'a [u8]>>
|
||||
fn get_recorded<'a, 'b, R: 'b>(&'a self, key: &'b [u8], rec: &'b mut R) -> super::Result<Option<DBValue>>
|
||||
where 'a: 'b, R: Recorder
|
||||
{
|
||||
self.do_lookup(&NibbleSlice::new(key), rec)
|
||||
@@ -384,7 +389,7 @@ impl<'db> fmt::Debug for TrieDB<'db> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
try!(writeln!(f, "c={:?} [", self.hash_count));
|
||||
let root_rlp = self.db.get(self.root).expect("Trie root not found!");
|
||||
try!(self.fmt_all(Node::decoded(root_rlp), f, 0));
|
||||
try!(self.fmt_all(Node::decoded(&root_rlp), f, 0));
|
||||
writeln!(f, "]")
|
||||
}
|
||||
}
|
||||
@@ -395,7 +400,7 @@ fn iterator() {
|
||||
use super::TrieMut;
|
||||
use super::triedbmut::*;
|
||||
|
||||
let d = vec![ &b"A"[..], &b"AA"[..], &b"AB"[..], &b"B"[..] ];
|
||||
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();
|
||||
@@ -407,6 +412,6 @@ fn iterator() {
|
||||
}
|
||||
|
||||
let t = TrieDB::new(&memdb, &root).unwrap();
|
||||
assert_eq!(d.iter().map(|i|i.to_vec()).collect::<Vec<_>>(), t.iter().unwrap().map(|x| x.unwrap().0).collect::<Vec<_>>());
|
||||
assert_eq!(d.iter().map(|i| i.clone().to_vec()).collect::<Vec<_>>(), t.iter().unwrap().map(|x| x.unwrap().0).collect::<Vec<_>>());
|
||||
assert_eq!(d, t.iter().unwrap().map(|x| x.unwrap().1).collect::<Vec<_>>());
|
||||
}
|
||||
|
||||
@@ -18,12 +18,14 @@
|
||||
|
||||
use super::{TrieError, TrieMut};
|
||||
use super::node::Node as RlpNode;
|
||||
use super::node::NodeKey;
|
||||
|
||||
use ::{Bytes, HashDB, H256};
|
||||
use ::{HashDB, H256};
|
||||
use ::bytes::ToPretty;
|
||||
use ::nibbleslice::NibbleSlice;
|
||||
use ::rlp::{Rlp, RlpStream, View, Stream};
|
||||
use ::sha3::SHA3_NULL_RLP;
|
||||
use hashdb::DBValue;
|
||||
|
||||
use elastic_array::ElasticArray1024;
|
||||
|
||||
@@ -72,14 +74,14 @@ enum Node {
|
||||
/// A leaf node contains the end of a key and a value.
|
||||
/// This key is encoded from a `NibbleSlice`, meaning it contains
|
||||
/// a flag indicating it is a leaf.
|
||||
Leaf(Bytes, Bytes),
|
||||
Leaf(NodeKey, DBValue),
|
||||
/// An extension contains a shared portion of a key and a child node.
|
||||
/// The shared portion is encoded from a `NibbleSlice` meaning it contains
|
||||
/// a flag indicating it is an extension.
|
||||
/// The child node is always a branch.
|
||||
Extension(Bytes, NodeHandle),
|
||||
Extension(NodeKey, NodeHandle),
|
||||
/// A branch has up to 16 children and an optional value.
|
||||
Branch(Box<[Option<NodeHandle>; 16]>, Option<Bytes>)
|
||||
Branch(Box<[Option<NodeHandle>; 16]>, Option<DBValue>)
|
||||
}
|
||||
|
||||
impl Node {
|
||||
@@ -98,21 +100,18 @@ impl Node {
|
||||
fn from_rlp(rlp: &[u8], db: &HashDB, storage: &mut NodeStorage) -> Self {
|
||||
match RlpNode::decoded(rlp) {
|
||||
RlpNode::Empty => Node::Empty,
|
||||
RlpNode::Leaf(k, v) => Node::Leaf(k.encoded(true), v.to_owned()),
|
||||
RlpNode::Extension(partial, cb) => {
|
||||
let key = partial.encoded(false);
|
||||
|
||||
Node::Extension(key, Self::inline_or_hash(cb, db, storage))
|
||||
RlpNode::Leaf(k, v) => Node::Leaf(k, v),
|
||||
RlpNode::Extension(key, cb) => {
|
||||
Node::Extension(key, Self::inline_or_hash(&*cb, db, storage))
|
||||
}
|
||||
RlpNode::Branch(children_rlp, v) => {
|
||||
let val = v.map(|x| x.to_owned());
|
||||
RlpNode::Branch(children_rlp, val) => {
|
||||
let mut children = empty_children();
|
||||
|
||||
for i in 0..16 {
|
||||
let raw = children_rlp[i];
|
||||
let child_rlp = Rlp::new(raw);
|
||||
let raw = &children_rlp[i];
|
||||
let child_rlp = Rlp::new(&*raw);
|
||||
if !child_rlp.is_empty() {
|
||||
children[i] = Some(Self::inline_or_hash(raw, db, storage));
|
||||
children[i] = Some(Self::inline_or_hash(&*raw, db, storage));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,13 +133,13 @@ impl Node {
|
||||
}
|
||||
Node::Leaf(partial, value) => {
|
||||
let mut stream = RlpStream::new_list(2);
|
||||
stream.append(&partial);
|
||||
stream.append(&value);
|
||||
stream.append(&&*partial);
|
||||
stream.append(&&*value);
|
||||
stream.drain()
|
||||
}
|
||||
Node::Extension(partial, child) => {
|
||||
let mut stream = RlpStream::new_list(2);
|
||||
stream.append(&partial);
|
||||
stream.append(&&*partial);
|
||||
child_cb(child, &mut stream);
|
||||
stream.drain()
|
||||
}
|
||||
@@ -154,7 +153,7 @@ impl Node {
|
||||
}
|
||||
}
|
||||
if let Some(value) = value {
|
||||
stream.append(&value);
|
||||
stream.append(&&*value);
|
||||
} else {
|
||||
stream.append_empty_data();
|
||||
}
|
||||
@@ -276,7 +275,7 @@ impl<'a> Index<&'a StorageHandle> for NodeStorage {
|
||||
/// assert_eq!(*t.root(), ::util::sha3::SHA3_NULL_RLP);
|
||||
/// t.insert(b"foo", b"bar").unwrap();
|
||||
/// assert!(t.contains(b"foo").unwrap());
|
||||
/// assert_eq!(t.get(b"foo").unwrap().unwrap(), b"bar");
|
||||
/// assert_eq!(t.get(b"foo").unwrap().unwrap(), DBValue::from_slice(b"bar"));
|
||||
/// t.remove(b"foo").unwrap();
|
||||
/// assert!(!t.contains(b"foo").unwrap());
|
||||
/// }
|
||||
@@ -338,7 +337,7 @@ impl<'a> TrieDBMut<'a> {
|
||||
// cache a node by hash
|
||||
fn cache(&mut self, hash: H256) -> super::Result<StorageHandle> {
|
||||
let node_rlp = try!(self.db.get(&hash).ok_or_else(|| Box::new(TrieError::IncompleteDatabase(hash))));
|
||||
let node = Node::from_rlp(node_rlp, &*self.db, &mut self.storage);
|
||||
let node = Node::from_rlp(&node_rlp, &*self.db, &mut self.storage);
|
||||
Ok(self.storage.alloc(Stored::Cached(node, hash)))
|
||||
}
|
||||
|
||||
@@ -367,7 +366,7 @@ impl<'a> TrieDBMut<'a> {
|
||||
}
|
||||
|
||||
// walk the trie, attempting to find the key's node.
|
||||
fn lookup<'x, 'key>(&'x self, partial: NibbleSlice<'key>, handle: &NodeHandle) -> super::Result<Option<&'x [u8]>>
|
||||
fn lookup<'x, 'key>(&'x self, partial: NibbleSlice<'key>, handle: &NodeHandle) -> super::Result<Option<DBValue>>
|
||||
where 'x: 'key
|
||||
{
|
||||
match *handle {
|
||||
@@ -376,7 +375,7 @@ impl<'a> TrieDBMut<'a> {
|
||||
Node::Empty => Ok(None),
|
||||
Node::Leaf(ref key, ref value) => {
|
||||
if NibbleSlice::from_encoded(key).0 == partial {
|
||||
Ok(Some(value))
|
||||
Ok(Some(DBValue::from_slice(value)))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
@@ -391,7 +390,7 @@ impl<'a> TrieDBMut<'a> {
|
||||
}
|
||||
Node::Branch(ref children, ref value) => {
|
||||
if partial.is_empty() {
|
||||
Ok(value.as_ref().map(|v| &v[..]))
|
||||
Ok(value.as_ref().map(|v| DBValue::from_slice(v)))
|
||||
} else {
|
||||
let idx = partial.at(0);
|
||||
match children[idx as usize].as_ref() {
|
||||
@@ -405,28 +404,33 @@ impl<'a> TrieDBMut<'a> {
|
||||
}
|
||||
|
||||
/// Return optional data for a key given as a `NibbleSlice`. Returns `None` if no data exists.
|
||||
fn do_db_lookup<'x, 'key>(&'x self, hash: &H256, key: NibbleSlice<'key>) -> super::Result<Option<&'x [u8]>>
|
||||
fn do_db_lookup<'x, 'key>(&'x self, hash: &H256, key: NibbleSlice<'key>) -> super::Result<Option<DBValue>>
|
||||
where 'x: 'key
|
||||
{
|
||||
self.db.get(hash).ok_or_else(|| Box::new(TrieError::IncompleteDatabase(*hash)))
|
||||
.and_then(|node_rlp| self.get_from_db_node(node_rlp, key))
|
||||
.and_then(|node_rlp| self.get_from_db_node(&node_rlp, key))
|
||||
}
|
||||
|
||||
/// Recursible function to retrieve the value given a `node` and a partial `key`. `None` if no
|
||||
/// value exists for the key.
|
||||
///
|
||||
/// Note: Not a public API; use Trie trait functions.
|
||||
fn get_from_db_node<'x, 'key>(&'x self, node: &'x [u8], key: NibbleSlice<'key>) -> super::Result<Option<&'x [u8]>>
|
||||
fn get_from_db_node<'x, 'key>(&'x self, node: &'x [u8], key: NibbleSlice<'key>) -> super::Result<Option<DBValue>>
|
||||
where 'x: 'key
|
||||
{
|
||||
match RlpNode::decoded(node) {
|
||||
RlpNode::Leaf(ref slice, value) if &key == slice => Ok(Some(value)),
|
||||
RlpNode::Extension(ref slice, item) if key.starts_with(slice) => {
|
||||
self.get_from_db_node(try!(self.get_raw_or_lookup(item)), key.mid(slice.len()))
|
||||
RlpNode::Leaf(ref slice, ref value) if NibbleSlice::from_encoded(slice).0 == key => Ok(Some(value.clone())),
|
||||
RlpNode::Extension(ref slice, ref item) => {
|
||||
let slice = &NibbleSlice::from_encoded(slice).0;
|
||||
if key.starts_with(slice) {
|
||||
self.get_from_db_node(&try!(self.get_raw_or_lookup(&*item)), key.mid(slice.len()))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
},
|
||||
RlpNode::Branch(ref nodes, value) => match key.is_empty() {
|
||||
true => Ok(value),
|
||||
false => self.get_from_db_node(try!(self.get_raw_or_lookup(nodes[key.at(0) as usize])), key.mid(1))
|
||||
RlpNode::Branch(ref nodes, ref value) => match key.is_empty() {
|
||||
true => Ok(value.clone()),
|
||||
false => self.get_from_db_node(&try!(self.get_raw_or_lookup(&*nodes[key.at(0) as usize])), key.mid(1))
|
||||
},
|
||||
_ => Ok(None),
|
||||
}
|
||||
@@ -435,7 +439,7 @@ impl<'a> TrieDBMut<'a> {
|
||||
/// 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.
|
||||
fn get_raw_or_lookup<'x>(&'x self, node: &'x [u8]) -> super::Result<&'x [u8]> {
|
||||
fn get_raw_or_lookup<'x>(&'x self, node: &'x [u8]) -> super::Result<DBValue> {
|
||||
// check if its sha3 + len
|
||||
let r = Rlp::new(node);
|
||||
match r.is_data() && r.size() == 32 {
|
||||
@@ -443,12 +447,12 @@ impl<'a> TrieDBMut<'a> {
|
||||
let key = r.as_val::<H256>();
|
||||
self.db.get(&key).ok_or_else(|| Box::new(TrieError::IncompleteDatabase(key)))
|
||||
}
|
||||
false => Ok(node)
|
||||
false => Ok(DBValue::from_slice(node))
|
||||
}
|
||||
}
|
||||
|
||||
/// insert a key, value pair into the trie, creating new nodes if necessary.
|
||||
fn insert_at(&mut self, handle: NodeHandle, partial: NibbleSlice, value: Bytes) -> super::Result<(StorageHandle, bool)> {
|
||||
fn insert_at(&mut self, handle: NodeHandle, partial: NibbleSlice, value: DBValue) -> super::Result<(StorageHandle, bool)> {
|
||||
let h = match handle {
|
||||
NodeHandle::InMemory(h) => h,
|
||||
NodeHandle::Hash(h) => try!(self.cache(h)),
|
||||
@@ -463,7 +467,7 @@ impl<'a> TrieDBMut<'a> {
|
||||
|
||||
/// the insertion inspector.
|
||||
#[cfg_attr(feature = "dev", allow(cyclomatic_complexity))]
|
||||
fn insert_inspector(&mut self, node: Node, partial: NibbleSlice, value: Bytes) -> super::Result<InsertAction> {
|
||||
fn insert_inspector(&mut self, node: Node, partial: NibbleSlice, value: DBValue) -> super::Result<InsertAction> {
|
||||
trace!(target: "trie", "augmented (partial: {:?}, value: {:?})", partial, value.pretty());
|
||||
|
||||
Ok(match node {
|
||||
@@ -898,7 +902,7 @@ impl<'a> TrieMut for TrieDBMut<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn get<'x, 'key>(&'x self, key: &'key [u8]) -> super::Result<Option<&'x [u8]>> where 'x: 'key {
|
||||
fn get<'x, 'key>(&'x self, key: &'key [u8]) -> super::Result<Option<DBValue>> where 'x: 'key {
|
||||
self.lookup(NibbleSlice::new(key), &self.root_handle)
|
||||
}
|
||||
|
||||
@@ -911,7 +915,7 @@ impl<'a> TrieMut for TrieDBMut<'a> {
|
||||
trace!(target: "trie", "insert: key={:?}, value={:?}", key.pretty(), value.pretty());
|
||||
|
||||
let root_handle = self.root_handle();
|
||||
let (new_handle, changed) = try!(self.insert_at(root_handle, NibbleSlice::new(key), value.to_owned()));
|
||||
let (new_handle, changed) = try!(self.insert_at(root_handle, NibbleSlice::new(key), DBValue::from_slice(value)));
|
||||
|
||||
trace!(target: "trie", "insert: altered trie={}", changed);
|
||||
self.root_handle = NodeHandle::InMemory(new_handle);
|
||||
@@ -1180,9 +1184,9 @@ mod tests {
|
||||
let mut root = H256::new();
|
||||
let mut t = TrieDBMut::new(&mut memdb, &mut root);
|
||||
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap();
|
||||
assert_eq!(t.get(&[0x1, 0x23]).unwrap().unwrap(), &[0x1u8, 0x23]);
|
||||
assert_eq!(t.get(&[0x1, 0x23]).unwrap().unwrap(), DBValue::from_slice(&[0x1u8, 0x23]));
|
||||
t.commit();
|
||||
assert_eq!(t.get(&[0x1, 0x23]).unwrap().unwrap(), &[0x1u8, 0x23]);
|
||||
assert_eq!(t.get(&[0x1, 0x23]).unwrap().unwrap(), DBValue::from_slice(&[0x1u8, 0x23]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1193,14 +1197,14 @@ mod tests {
|
||||
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap();
|
||||
t.insert(&[0xf1u8, 0x23], &[0xf1u8, 0x23]).unwrap();
|
||||
t.insert(&[0x81u8, 0x23], &[0x81u8, 0x23]).unwrap();
|
||||
assert_eq!(t.get(&[0x01, 0x23]).unwrap().unwrap(), &[0x01u8, 0x23]);
|
||||
assert_eq!(t.get(&[0xf1, 0x23]).unwrap().unwrap(), &[0xf1u8, 0x23]);
|
||||
assert_eq!(t.get(&[0x81, 0x23]).unwrap().unwrap(), &[0x81u8, 0x23]);
|
||||
assert_eq!(t.get(&[0x01, 0x23]).unwrap().unwrap(), DBValue::from_slice(&[0x01u8, 0x23]));
|
||||
assert_eq!(t.get(&[0xf1, 0x23]).unwrap().unwrap(), DBValue::from_slice(&[0xf1u8, 0x23]));
|
||||
assert_eq!(t.get(&[0x81, 0x23]).unwrap().unwrap(), DBValue::from_slice(&[0x81u8, 0x23]));
|
||||
assert_eq!(t.get(&[0x82, 0x23]), Ok(None));
|
||||
t.commit();
|
||||
assert_eq!(t.get(&[0x01, 0x23]).unwrap().unwrap(), &[0x01u8, 0x23]);
|
||||
assert_eq!(t.get(&[0xf1, 0x23]).unwrap().unwrap(), &[0xf1u8, 0x23]);
|
||||
assert_eq!(t.get(&[0x81, 0x23]).unwrap().unwrap(), &[0x81u8, 0x23]);
|
||||
assert_eq!(t.get(&[0x01, 0x23]).unwrap().unwrap(), DBValue::from_slice(&[0x01u8, 0x23]));
|
||||
assert_eq!(t.get(&[0xf1, 0x23]).unwrap().unwrap(), DBValue::from_slice(&[0xf1u8, 0x23]));
|
||||
assert_eq!(t.get(&[0x81, 0x23]).unwrap().unwrap(), DBValue::from_slice(&[0x81u8, 0x23]));
|
||||
assert_eq!(t.get(&[0x82, 0x23]), Ok(None));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user