no_std support for substrate trie (#2146)

* no_std trie compile in test_runtime (require to set nightly feature due
to the way hashbrown currently works).

* No nightly with hashmap_core.

* using crate elastic-array

* switch to publish trie crates

* fix default array decl

* bump impl_version for ci

* set all semver when possible wasm, and remove redundant code.

* Actually test use_trie function

* impl version +1

* Bump impl version
This commit is contained in:
cheme
2019-04-02 12:49:04 +02:00
committed by Bastian Köcher
parent 728f0393c7
commit 669e79181e
16 changed files with 150 additions and 65 deletions
+4
View File
@@ -6,7 +6,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[cfg(feature="std")]
use std::fmt;
#[cfg(feature="std")]
use std::error::Error as StdError;
#[derive(Debug, PartialEq, Eq, Clone)]
@@ -16,12 +18,14 @@ pub enum Error {
BadFormat,
}
#[cfg(feature="std")]
impl StdError for Error {
fn description(&self) -> &str {
"codec error"
}
}
#[cfg(feature="std")]
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self, f)
+5 -2
View File
@@ -16,13 +16,16 @@
//! Utility functions to interact with Substrate's Base-16 Modified Merkle Patricia tree ("trie").
// FIXME: no_std - https://github.com/paritytech/substrate/issues/1574
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(alloc))]
mod error;
mod node_header;
mod node_codec;
mod trie_stream;
use rstd::boxed::Box;
use rstd::vec::Vec;
use hash_db::Hasher;
/// Our `NodeCodec`-specific error.
pub use error::Error;
@@ -290,7 +293,7 @@ fn take<'a>(input: &mut &'a[u8], count: usize) -> Option<&'a[u8]> {
fn partial_to_key(partial: &[u8], offset: u8, big: u8) -> Vec<u8> {
let nibble_count = (partial.len() - 1) * 2 + if partial[0] & 16 == 16 { 1 } else { 0 };
let (first_byte_small, big_threshold) = (offset, (big - offset) as usize);
let mut output = vec![first_byte_small + nibble_count.min(big_threshold) as u8];
let mut output = [first_byte_small + nibble_count.min(big_threshold) as u8].to_vec();
if nibble_count >= big_threshold { output.push((nibble_count - big_threshold) as u8) }
if nibble_count % 2 == 1 {
output.push(partial[0] & 0x0f);
+5 -8
View File
@@ -16,7 +16,8 @@
//! `NodeCodec` implementation for Substrate's trie format.
use std::marker::PhantomData;
use rstd::marker::PhantomData;
use rstd::vec::Vec;
use codec::{Encode, Decode, Compact};
use hash_db::Hasher;
use trie_db::{self, DBValue, NibbleSlice, node::Node, ChildReference};
@@ -28,10 +29,6 @@ use super::{EMPTY_TRIE, LEAF_NODE_OFFSET, LEAF_NODE_BIG, EXTENSION_NODE_OFFSET,
#[derive(Default, Clone)]
pub struct NodeCodec<H: Hasher>(PhantomData<H>);
// NOTE: what we'd really like here is:
// `impl<H: Hasher> NodeCodec<H> for RlpNodeCodec<H> where H::Out: Decodable`
// but due to the current limitations of Rust const evaluation we can't
// do `const HASHED_NULL_NODE: H::Out = H::Out( … … )`. Perhaps one day soon?
impl<H: Hasher> trie_db::NodeCodec<H> for NodeCodec<H> {
type Error = Error;
@@ -39,7 +36,7 @@ impl<H: Hasher> trie_db::NodeCodec<H> for NodeCodec<H> {
H::hash(&[0u8][..])
}
fn decode(data: &[u8]) -> ::std::result::Result<Node, Self::Error> {
fn decode(data: &[u8]) -> ::rstd::result::Result<Node, Self::Error> {
use Error::BadFormat;
let input = &mut &*data;
match NodeHeader::decode(input).ok_or(BadFormat)? {
@@ -92,7 +89,7 @@ impl<H: Hasher> trie_db::NodeCodec<H> for NodeCodec<H> {
data == &[EMPTY_TRIE][..]
}
fn empty_node() -> Vec<u8> {
vec![EMPTY_TRIE]
[EMPTY_TRIE].to_vec()
}
// FIXME: refactor this so that `partial` isn't already encoded with HPE. Should just be an `impl Iterator<Item=u8>`.
@@ -117,7 +114,7 @@ impl<H: Hasher> trie_db::NodeCodec<H> for NodeCodec<H> {
fn branch_node<I>(children: I, maybe_value: Option<DBValue>) -> Vec<u8>
where I: IntoIterator<Item=Option<ChildReference<H::Out>>> + Iterator<Item=Option<ChildReference<H::Out>>>
{
let mut output = vec![0, 0, 0];
let mut output = [0, 0, 0].to_vec();
let have_value = if let Some(value) = maybe_value {
(&*value).encode_to(&mut output);
true
+2 -1
View File
@@ -16,10 +16,11 @@
//! `TrieStream` implementation for Substrate's trie format.
use std::iter::once;
use rstd::iter::once;
use hash_db::Hasher;
use trie_root;
use codec::Encode;
use rstd::vec::Vec;
use super::{EMPTY_TRIE, LEAF_NODE_OFFSET, LEAF_NODE_BIG, EXTENSION_NODE_OFFSET,
EXTENSION_NODE_BIG, branch_node};