Switch to shiny new fast, RLP-less trie (#795)

* Bump codec

* Fix tests

* Patricia trie builds

* Introduce trie

* Some yak shaving.

* Some fixes

* Remove RLP ref

* Fixes

* It builds!

* Some tests fixed

* Another test fix

* Rejig more hashes

* substrate-trie::iterator_works test

* Update lock

* Polish

* Docs

* Undo incorrect "fix" for tests

* Fix nits
This commit is contained in:
Gav Wood
2018-09-25 15:32:22 +01:00
committed by Arkadiy Paronyan
parent b02c274374
commit 82d6ca3484
90 changed files with 1977 additions and 1129 deletions
-47
View File
@@ -21,10 +21,6 @@ use serde::{Serialize, Serializer, Deserialize, Deserializer};
#[cfg(feature = "std")]
use bytes;
#[cfg(feature = "std")]
use core::cmp;
#[cfg(feature = "std")]
use rlp::{Rlp, RlpStream, DecoderError};
macro_rules! impl_rest {
($name: ident, $len: expr) => {
@@ -53,29 +49,6 @@ macro_rules! impl_rest {
<[u8; $len] as ::codec::Decode>::decode(input).map($name)
}
}
#[cfg(feature = "std")]
impl ::rlp::Encodable for $name {
fn rlp_append(&self, s: &mut RlpStream) {
s.encoder().encode_value(self);
}
}
#[cfg(feature = "std")]
impl ::rlp::Decodable for $name {
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
rlp.decoder().decode_value(|bytes| match bytes.len().cmp(&$len) {
cmp::Ordering::Less => Err(DecoderError::RlpIsTooShort),
cmp::Ordering::Greater => Err(DecoderError::RlpIsTooBig),
cmp::Ordering::Equal => {
let mut t = [0u8; $len];
t.copy_from_slice(bytes);
Ok($name(t))
}
})
}
}
}
}
@@ -90,26 +63,6 @@ impl_rest!(H512, 64);
mod tests {
use super::*;
use substrate_serializer as ser;
use rlp::{Encodable, RlpStream};
#[test]
fn test_hash_is_encodable() {
let h = H160::from(21);
let mut s = RlpStream::new();
h.rlp_append(&mut s);
assert_eq!(s.drain().into_vec(), &[148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21]);
}
#[test]
fn test_hash_is_decodable() {
let data = vec![148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123];
let res = ::rlp::decode::<H160>(&data);
assert!(res.is_ok());
assert_eq!(res.unwrap(), H160::from(123));
let res = ::rlp::decode::<H256>(&data);
assert!(res.is_err());
}
#[test]
fn test_h160() {
+4 -4
View File
@@ -16,12 +16,12 @@
//! Polkadot Blake2b Hasher implementation
use hashdb::Hasher;
use plain_hasher::PlainHasher;
use hash_db::Hasher;
use hash256_std_hasher::Hash256StdHasher;
use hash::H256;
pub mod blake2 {
use super::{Hasher, PlainHasher, H256};
use super::{Hasher, Hash256StdHasher, H256};
#[cfg(feature = "std")]
use hashing::blake2_256;
@@ -44,7 +44,7 @@ pub mod blake2 {
impl Hasher for Blake2Hasher {
type Out = H256;
type StdHasher = PlainHasher;
type StdHasher = Hash256StdHasher;
const LENGTH: usize = 32;
fn hash(x: &[u8]) -> Self::Out {
blake2_256(x).into()
+2 -11
View File
@@ -35,8 +35,6 @@ extern crate parity_codec_derive;
extern crate rustc_hex;
extern crate byteorder;
extern crate parity_codec as codec;
#[cfg(feature = "std")]
extern crate rlp;
#[cfg(feature = "std")]
extern crate serde;
@@ -62,10 +60,8 @@ extern crate serde_derive;
extern crate core;
#[cfg(feature = "std")]
extern crate wasmi;
extern crate hashdb;
extern crate plain_hasher;
#[cfg(feature = "std")]
extern crate patricia_trie;
extern crate hash_db;
extern crate hash256_std_hasher;
#[cfg(feature = "std")]
extern crate elastic_array;
@@ -110,8 +106,6 @@ pub mod sandbox;
pub mod storage;
pub mod uint;
mod authority_id;
#[cfg(feature = "std")]
mod rlp_codec;
mod changes_trie;
#[cfg(test)]
@@ -126,9 +120,6 @@ pub use changes_trie::ChangesTrieConfiguration;
// pub use self::hasher::blake::BlakeHasher;
pub use self::hasher::blake2::Blake2Hasher;
#[cfg(feature = "std")]
pub use self::rlp_codec::RlpCodec;
/// A 512-bit value interpreted as a signature.
pub type Signature = hash::H512;
-123
View File
@@ -1,123 +0,0 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Polkadot Blake2b (trie) NodeCodec implementation
use elastic_array::{ElasticArray1024, ElasticArray128};
use hashdb::Hasher;
use rlp::{DecoderError, RlpStream, Rlp, Prototype};
use core::marker::PhantomData;
use patricia_trie::{NibbleSlice, NodeCodec, node::Node, ChildReference};
use hash::H256;
use Blake2Hasher;
/// Concrete implementation of a `NodeCodec` with Rlp encoding, generic over the `Hasher`
pub struct RlpNodeCodec<H: Hasher> {mark: PhantomData<H>}
/// Convenience type for a Blake2_256/Rlp flavoured NodeCodec
pub type RlpCodec = RlpNodeCodec<Blake2Hasher>;
impl NodeCodec<Blake2Hasher> for RlpCodec {
type Error = DecoderError;
const HASHED_NULL_NODE : H256 = H256( [0x45, 0xb0, 0xcf, 0xc2, 0x20, 0xce, 0xec, 0x5b, 0x7c, 0x1c, 0x62, 0xc4, 0xd4, 0x19, 0x3d, 0x38, 0xe4, 0xeb, 0xa4, 0x8e, 0x88, 0x15, 0x72, 0x9c, 0xe7, 0x5f, 0x9c, 0xa, 0xb0, 0xe4, 0xc1, 0xc0] );
fn decode(data: &[u8]) -> ::std::result::Result<Node, Self::Error> {
let r = Rlp::new(data);
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) => Ok(Node::Leaf(slice, r.at(1)?.data()?)),
(slice, false) => Ok(Node::Extension(slice, r.at(1)?.as_raw())),
},
// branch - first 16 are nodes, 17th is a value (or empty).
Prototype::List(17) => {
let mut nodes = [&[] as &[u8]; 16];
for i in 0..16 {
nodes[i] = r.at(i)?.as_raw();
}
Ok(Node::Branch(nodes, if r.at(16)?.is_empty() { None } else { Some(r.at(16)?.data()?) }))
},
// an empty branch index.
Prototype::Data(0) => Ok(Node::Empty),
// something went wrong.
_ => Err(DecoderError::Custom("Rlp is not valid."))
}
}
fn try_decode_hash(data: &[u8]) -> Option<<Blake2Hasher as Hasher>::Out> {
let r = Rlp::new(data);
if r.is_data() && r.size() == Blake2Hasher::LENGTH {
Some(r.as_val().expect("Hash is the correct size; qed"))
} else {
None
}
}
fn is_empty_node(data: &[u8]) -> bool {
Rlp::new(data).is_empty()
}
fn empty_node() -> ElasticArray1024<u8> {
let mut stream = RlpStream::new();
stream.append_empty_data();
stream.drain()
}
fn leaf_node(partial: &[u8], value: &[u8]) -> ElasticArray1024<u8> {
let mut stream = RlpStream::new_list(2);
stream.append(&partial);
stream.append(&value);
stream.drain()
}
fn ext_node(partial: &[u8], child_ref: ChildReference<<Blake2Hasher as Hasher>::Out>) -> ElasticArray1024<u8> {
let mut stream = RlpStream::new_list(2);
stream.append(&partial);
match child_ref {
ChildReference::Hash(h) => stream.append(&h),
ChildReference::Inline(inline_data, len) => {
let bytes = &AsRef::<[u8]>::as_ref(&inline_data)[..len];
stream.append_raw(bytes, 1)
},
};
stream.drain()
}
fn branch_node<I>(children: I, value: Option<ElasticArray128<u8>>) -> ElasticArray1024<u8>
where I: IntoIterator<Item=Option<ChildReference<<Blake2Hasher as Hasher>::Out>>>
{
let mut stream = RlpStream::new_list(17);
for child_ref in children {
match child_ref {
Some(c) => match c {
ChildReference::Hash(h) => stream.append(&h),
ChildReference::Inline(inline_data, len) => {
let bytes = &AsRef::<[u8]>::as_ref(&inline_data)[..len];
stream.append_raw(bytes, 1)
},
},
None => stream.append_empty_data()
};
}
if let Some(value) = value {
stream.append(&&*value);
} else {
stream.append_empty_data();
}
stream.drain()
}
}