Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions trie-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "Apache-2.0"
[dependencies]
log = "0.4"
rand = { version = "0.6", default-features = false }
elastic-array = { version = "0.10", default-features = false }
smallvec = "1.0.0"
hash-db = { path = "../hash-db", default-features = false, version = "0.15.2"}
hashbrown = { version = "0.6.3", default-features = false }

Expand All @@ -29,7 +29,6 @@ parity-codec-derive = "3.0"
[features]
default = ["std"]
std = [
"elastic-array/std",
"hash-db/std",
"rand/std",
]
Expand Down
4 changes: 2 additions & 2 deletions trie-db/src/iter_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ mod test {
let key: &[u8]= &data[i].0;
let value: &[u8] = &data[i].1;
assert_eq!(k, key);
assert_eq!(v, value);
assert_eq!(v.as_ref(), value);
}
for (k, v) in data.into_iter() {
assert_eq!(&t.get(&k[..]).unwrap().unwrap()[..], &v[..]);
Expand All @@ -547,7 +547,7 @@ mod test {
let key: &[u8]= &data[i].0;
let value: &[u8] = &data[i].1;
assert_eq!(k, key);
assert_eq!(v, value);
assert_eq!(v.as_ref(), value);
}
for (k, v) in data.into_iter() {
assert_eq!(&t.get(&k[..]).unwrap().unwrap()[..], &v[..]);
Expand Down
5 changes: 3 additions & 2 deletions trie-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#[cfg(not(feature = "std"))]
extern crate alloc;

extern crate elastic_array;
extern crate smallvec;
extern crate hash_db;
extern crate rand;
#[macro_use]
Expand Down Expand Up @@ -106,7 +106,8 @@ pub use trie_codec::{decode_compact, encode_compact};
#[cfg(feature = "std")]
pub use iter_build::TrieRootPrint;

pub type DBValue = elastic_array::ElasticArray128<u8>;
/// Database value
pub type DBValue = smallvec::SmallVec<[u8; 128]>;

/// Trie Errors.
///
Expand Down
6 changes: 4 additions & 2 deletions trie-db/src/nibble/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

mod nibblevec;
mod nibbleslice;
use elastic_array::ElasticArray36;
use crate::node::NodeKey;
use core_::cmp;

Expand Down Expand Up @@ -146,13 +145,16 @@ pub mod nibble_ops {

}

/// Backing storage for `NibbleVec`s.
pub(crate) type BackingByteVec = smallvec::SmallVec<[u8; 36]>;

/// Owning, nibble-oriented byte vector. Counterpart to `NibbleSlice`.
/// Nibbles are always left aligned, so making a `NibbleVec` from
/// a `NibbleSlice` can get costy.
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub struct NibbleVec {
inner: ElasticArray36<u8>,
inner: BackingByteVec,
len: usize,
}

Expand Down
22 changes: 10 additions & 12 deletions trie-db/src/nibble/nibbleslice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

use ::core_::cmp::*;
use ::core_::fmt;
use super::{nibble_ops, NibbleSlice, NibbleSliceIterator};
use elastic_array::ElasticArray36;
use super::{nibble_ops, NibbleSlice, NibbleSliceIterator, BackingByteVec};
use node::NodeKey;
use node_codec::Partial;
use hash_db::Prefix;
Expand Down Expand Up @@ -78,13 +77,13 @@ impl<'a> NibbleSlice<'a> {
let end = (self.offset + nb) / nibble_ops::NIBBLE_PER_BYTE;
(
self.offset % nibble_ops::NIBBLE_PER_BYTE,
ElasticArray36::from_slice(&self.data[start..end]),
BackingByteVec::from_slice(&self.data[start..end]),
)
} else {
// unaligned
let start = self.offset / nibble_ops::NIBBLE_PER_BYTE;
let end = (self.offset + nb) / nibble_ops::NIBBLE_PER_BYTE;
let ea = ElasticArray36::from_slice(&self.data[start..=end]);
let ea = BackingByteVec::from_slice(&self.data[start..=end]);
let ea_offset = self.offset % nibble_ops::NIBBLE_PER_BYTE;
let n_offset = nibble_ops::number_padding(nb);
let mut result = (ea_offset, ea);
Expand Down Expand Up @@ -229,7 +228,7 @@ impl<'a> NibbleSlice<'a> {
}

/// Owned version of a `Prefix` from a `left` method call.
pub fn left_owned(&'a self) -> (ElasticArray36<u8>, Option<u8>) {
pub fn left_owned(&'a self) -> (BackingByteVec, Option<u8>) {
let (a, b) = self.left();
(a.into(), b)
}
Expand Down Expand Up @@ -285,8 +284,7 @@ impl<'a> fmt::Debug for NibbleSlice<'a> {

#[cfg(test)]
mod tests {
use crate::nibble::NibbleSlice;
use elastic_array::ElasticArray36;
use crate::nibble::{NibbleSlice, BackingByteVec};
static D: &'static [u8;3] = &[0x01u8, 0x23, 0x45];

#[test]
Expand Down Expand Up @@ -329,16 +327,16 @@ mod tests {
#[test]
fn encoded_pre() {
let n = NibbleSlice::new(D);
assert_eq!(n.to_stored(), (0, ElasticArray36::from_slice(&[0x01, 0x23, 0x45])));
assert_eq!(n.mid(1).to_stored(), (1, ElasticArray36::from_slice(&[0x01, 0x23, 0x45])));
assert_eq!(n.mid(2).to_stored(), (0, ElasticArray36::from_slice(&[0x23, 0x45])));
assert_eq!(n.mid(3).to_stored(), (1, ElasticArray36::from_slice(&[0x23, 0x45])));
assert_eq!(n.to_stored(), (0, BackingByteVec::from_slice(&[0x01, 0x23, 0x45])));
assert_eq!(n.mid(1).to_stored(), (1, BackingByteVec::from_slice(&[0x01, 0x23, 0x45])));
assert_eq!(n.mid(2).to_stored(), (0, BackingByteVec::from_slice(&[0x23, 0x45])));
assert_eq!(n.mid(3).to_stored(), (1, BackingByteVec::from_slice(&[0x23, 0x45])));
}

#[test]
fn from_encoded_pre() {
let n = NibbleSlice::new(D);
let stored: ElasticArray36<u8> = [0x01, 0x23, 0x45][..].into();
let stored: BackingByteVec = [0x01, 0x23, 0x45][..].into();
assert_eq!(n, NibbleSlice::from_stored(&(0, stored.clone())));
assert_eq!(n.mid(1), NibbleSlice::from_stored(&(1, stored)));
}
Expand Down
8 changes: 4 additions & 4 deletions trie-db/src/nibble/nibblevec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// limitations under the License.

//! An owning, nibble-oriented byte vector.
use elastic_array::ElasticArray36;
use nibble::NibbleSlice;

use nibble::{NibbleSlice, BackingByteVec};
use nibble::nibble_ops;
use hash_db::Prefix;
use node_codec::Partial;
Expand All @@ -30,7 +30,7 @@ impl NibbleVec {
/// Make a new `NibbleVec`.
pub fn new() -> Self {
NibbleVec {
inner: ElasticArray36::new(),
inner: BackingByteVec::new(),
len: 0,
}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ impl NibbleVec {
}
let pad = self.inner.len() * nibble_ops::NIBBLE_PER_BYTE - self.len;
if pad == 0 {
self.inner.append_slice(&sl[..]);
self.inner.extend_from_slice(&sl[..]);
} else {
let kend = self.inner.len() - 1;
if sl.len() > 0 {
Expand Down
5 changes: 2 additions & 3 deletions trie-db/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use elastic_array::ElasticArray36;
use hash_db::Hasher;
use nibble::NibbleSlice;
use nibble::{self, NibbleSlice};
use nibble::nibble_ops;
use node_codec::NodeCodec;

Expand All @@ -25,7 +24,7 @@ use alloc::vec::Vec;

/// Partial node key type: offset and owned value of a nibbleslice.
/// Offset is applied on first byte of array (bytes are right aligned).
pub type NodeKey = (usize, ElasticArray36<u8>);
pub type NodeKey = (usize, nibble::BackingByteVec);

/// A reference to a trie node which may be stored within another trie node.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
15 changes: 7 additions & 8 deletions trie-db/src/triedbmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ use node_codec::NodeCodec;
use super::{DBValue, node::NodeKey};

use hash_db::{HashDB, Hasher, Prefix, EMPTY_PREFIX};
use nibble::{NibbleVec, NibbleSlice, nibble_ops};
use elastic_array::ElasticArray36;
use nibble::{NibbleVec, NibbleSlice, nibble_ops, BackingByteVec};
use ::core_::convert::TryFrom;
use ::core_::mem;
use ::core_::ops::Index;
Expand Down Expand Up @@ -406,7 +405,7 @@ where
db: &'a mut dyn HashDB<L::Hash, DBValue>,
root: &'a mut TrieHash<L>,
root_handle: NodeHandle<TrieHash<L>>,
death_row: HashSet<(TrieHash<L>, (ElasticArray36<u8>, Option<u8>))>,
death_row: HashSet<(TrieHash<L>, (BackingByteVec, Option<u8>))>,
/// The number of hash operations this trie has performed.
/// Note that none are performed until changes are committed.
hash_count: usize,
Expand Down Expand Up @@ -1236,7 +1235,7 @@ where
let (start, alloc_start, prefix_end) = match key2.left() {
(start, None) => (start, None, Some(nibble_ops::push_at_left(0, a, 0))),
(start, Some(v)) => {
let mut so: ElasticArray36<u8> = start.into();
let mut so: BackingByteVec = start.into();
so.push(nibble_ops::pad_left(v) | a);
(start, Some(so), None)
},
Expand Down Expand Up @@ -1310,7 +1309,7 @@ where
let (start, alloc_start, prefix_end) = match key2.left() {
(start, None) => (start, None, Some(nibble_ops::push_at_left(0, last, 0))),
(start, Some(v)) => {
let mut so: ElasticArray36<u8> = start.into();
let mut so: BackingByteVec = start.into();
// Complete last byte with `last`.
so.push(nibble_ops::pad_left(v) | last);
(start, Some(so), None)
Expand Down Expand Up @@ -1601,9 +1600,9 @@ mod tests {
use memory_db::{MemoryDB, PrefixedKey};
use hash_db::{Hasher, HashDB};
use keccak_hasher::KeccakHasher;
use elastic_array::ElasticArray36;
use reference_trie::{RefTrieDBMutNoExt, RefTrieDBMut, TrieMut, NodeCodec,
ReferenceNodeCodec, reference_trie_root, reference_trie_root_no_extension};
use nibble::BackingByteVec;

fn populate_trie<'db>(
db: &'db mut dyn HashDB<KeccakHasher, DBValue>,
Expand Down Expand Up @@ -2056,9 +2055,9 @@ mod tests {

#[test]
fn combine_test() {
let a: ElasticArray36<u8> = [0x12, 0x34][..].into();
let a: BackingByteVec = [0x12, 0x34][..].into();
let b: &[u8] = [0x56, 0x78][..].into();
let test_comb = |a: (_, &ElasticArray36<_>), b, c| {
let test_comb = |a: (_, &BackingByteVec), b, c| {
let mut a = (a.0, a.1.clone());
super::combine_key(&mut a, b);
assert_eq!((a.0, &a.1[..]), c);
Expand Down