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/>.
|
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
use elastic_array::ElasticArray36;
|
2017-10-10 20:01:27 +02:00
|
|
|
use nibbleslice::NibbleSlice;
|
2017-01-06 16:18:45 +01:00
|
|
|
use nibblevec::NibbleVec;
|
2015-12-19 13:35:26 +01:00
|
|
|
use bytes::*;
|
|
|
|
use rlp::*;
|
2016-10-26 13:53:47 +02:00
|
|
|
use hashdb::DBValue;
|
|
|
|
|
|
|
|
/// Partial node key type.
|
|
|
|
pub type NodeKey = ElasticArray36<u8>;
|
2015-12-19 13:35:26 +01:00
|
|
|
|
|
|
|
/// Type of node in the trie and essential information thereof.
|
2017-01-06 16:18:45 +01:00
|
|
|
#[derive(Eq, PartialEq, Debug, Clone)]
|
|
|
|
pub enum Node<'a> {
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Null trie node; could be an empty root or an empty branch entry.
|
2015-12-19 13:35:26 +01:00
|
|
|
Empty,
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Leaf node; has key slice and value. Value may not be empty.
|
2017-01-06 16:18:45 +01:00
|
|
|
Leaf(NibbleSlice<'a>, &'a [u8]),
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Extension node; has key slice and node data. Data may not be null.
|
2017-01-06 16:18:45 +01:00
|
|
|
Extension(NibbleSlice<'a>, &'a [u8]),
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Branch node; has array of 16 child nodes (each possibly null) and an optional immediate node data.
|
2017-01-06 16:18:45 +01:00
|
|
|
Branch([&'a [u8]; 16], Option<&'a [u8]>)
|
2015-12-19 13:35:26 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 16:18:45 +01:00
|
|
|
impl<'a> Node<'a> {
|
2016-05-23 11:46:01 +02:00
|
|
|
/// Decode the `node_rlp` and return the Node.
|
2017-01-06 16:18:45 +01:00
|
|
|
pub fn decoded(node_rlp: &'a [u8]) -> Self {
|
2015-12-19 13:35:26 +01:00
|
|
|
let r = Rlp::new(node_rlp);
|
|
|
|
match r.prototype() {
|
2016-05-23 11:46:01 +02:00
|
|
|
// either leaf or extension - decode first item with NibbleSlice::???
|
2015-12-19 13:35:26 +01:00
|
|
|
// and use is_leaf return to figure out which.
|
|
|
|
// if leaf, second item is a value (is_data())
|
2016-05-23 11:46:01 +02:00
|
|
|
// if extension, second item is a node (either SHA3 to be looked up and
|
2015-12-19 13:35:26 +01:00
|
|
|
// 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()) {
|
2017-01-06 16:18:45 +01:00
|
|
|
(slice, true) => Node::Leaf(slice, r.at(1).data()),
|
|
|
|
(slice, false) => Node::Extension(slice, r.at(1).as_raw()),
|
2015-12-19 13:35:26 +01:00
|
|
|
},
|
|
|
|
// branch - first 16 are nodes, 17th is a value (or empty).
|
|
|
|
Prototype::List(17) => {
|
2017-01-06 16:18:45 +01:00
|
|
|
let mut nodes = [&[] as &[u8]; 16];
|
2015-12-19 13:35:26 +01:00
|
|
|
for i in 0..16 {
|
2017-01-06 16:18:45 +01:00
|
|
|
nodes[i] = r.at(i).as_raw();
|
2015-12-19 13:35:26 +01:00
|
|
|
}
|
2017-01-06 16:18:45 +01:00
|
|
|
Node::Branch(nodes, if r.at(16).is_empty() { None } else { Some(r.at(16).data()) })
|
2015-12-19 13:35:26 +01:00
|
|
|
},
|
|
|
|
// an empty branch index.
|
|
|
|
Prototype::Data(0) => Node::Empty,
|
|
|
|
// something went wrong.
|
|
|
|
_ => panic!("Rlp is not valid.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Encode the node into RLP.
|
|
|
|
///
|
|
|
|
/// Will always return the direct node RLP even if it's 32 or more bytes. To get the
|
|
|
|
/// RLP which would be valid for using in another node, use `encoded_and_added()`.
|
|
|
|
pub fn encoded(&self) -> Bytes {
|
|
|
|
match *self {
|
|
|
|
Node::Leaf(ref slice, ref value) => {
|
|
|
|
let mut stream = RlpStream::new_list(2);
|
2017-01-06 16:18:45 +01:00
|
|
|
stream.append(&&*slice.encoded(true));
|
|
|
|
stream.append(value);
|
2015-12-19 13:35:26 +01:00
|
|
|
stream.out()
|
|
|
|
},
|
2016-10-26 13:53:47 +02:00
|
|
|
Node::Extension(ref slice, ref raw_rlp) => {
|
2015-12-19 13:35:26 +01:00
|
|
|
let mut stream = RlpStream::new_list(2);
|
2017-01-06 16:18:45 +01:00
|
|
|
stream.append(&&*slice.encoded(false));
|
|
|
|
stream.append_raw(raw_rlp, 1);
|
2015-12-19 13:35:26 +01:00
|
|
|
stream.out()
|
|
|
|
},
|
|
|
|
Node::Branch(ref nodes, ref value) => {
|
|
|
|
let mut stream = RlpStream::new_list(17);
|
|
|
|
for i in 0..16 {
|
2017-01-06 16:18:45 +01:00
|
|
|
stream.append_raw(nodes[i], 1);
|
2015-12-19 13:35:26 +01:00
|
|
|
}
|
|
|
|
match *value {
|
2017-01-06 16:18:45 +01:00
|
|
|
Some(ref n) => { stream.append(n); },
|
2015-12-19 13:35:26 +01:00
|
|
|
None => { stream.append_empty_data(); },
|
|
|
|
}
|
|
|
|
stream.out()
|
|
|
|
},
|
|
|
|
Node::Empty => {
|
|
|
|
let mut stream = RlpStream::new();
|
|
|
|
stream.append_empty_data();
|
|
|
|
stream.out()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 16:18:45 +01:00
|
|
|
|
|
|
|
/// An owning node type. Useful for trie iterators.
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum OwnedNode {
|
|
|
|
/// Empty trie node.
|
|
|
|
Empty,
|
|
|
|
/// Leaf node: partial key and value.
|
|
|
|
Leaf(NibbleVec, DBValue),
|
|
|
|
/// Extension node: partial key and child node.
|
|
|
|
Extension(NibbleVec, DBValue),
|
|
|
|
/// Branch node: 16 children and an optional value.
|
|
|
|
Branch([NodeKey; 16], Option<DBValue>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for OwnedNode {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
match *self {
|
|
|
|
OwnedNode::Empty => OwnedNode::Empty,
|
|
|
|
OwnedNode::Leaf(ref k, ref v) => OwnedNode::Leaf(k.clone(), v.clone()),
|
|
|
|
OwnedNode::Extension(ref k, ref c) => OwnedNode::Extension(k.clone(), c.clone()),
|
|
|
|
OwnedNode::Branch(ref c, ref v) => {
|
|
|
|
let mut children = [
|
|
|
|
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 (owned, borrowed) in children.iter_mut().zip(c.iter()) {
|
|
|
|
*owned = borrowed.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
OwnedNode::Branch(children, v.as_ref().cloned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<Node<'a>> for OwnedNode {
|
|
|
|
fn from(node: Node<'a>) -> Self {
|
|
|
|
match node {
|
|
|
|
Node::Empty => OwnedNode::Empty,
|
|
|
|
Node::Leaf(k, v) => OwnedNode::Leaf(k.into(), DBValue::from_slice(v)),
|
|
|
|
Node::Extension(k, child) => OwnedNode::Extension(k.into(), DBValue::from_slice(child)),
|
|
|
|
Node::Branch(c, val) => {
|
|
|
|
let mut children = [
|
|
|
|
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 (owned, borrowed) in children.iter_mut().zip(c.iter()) {
|
|
|
|
*owned = NodeKey::from_slice(borrowed)
|
|
|
|
}
|
|
|
|
|
|
|
|
OwnedNode::Branch(children, val.map(DBValue::from_slice))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|