Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions hash-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub trait PlainDB<K, V>: Send + Sync + AsPlainDB<K, V> {
/// hash is not known.
fn get(&self, key: &K) -> Option<V>;

/// Check for the existance of a hash-key.
/// Check for the existence of a hash-key.
fn contains(&self, key: &K) -> bool;

/// Insert a datum item into the DB. Insertions are counted and the equivalent
Expand Down Expand Up @@ -113,7 +113,7 @@ pub trait HashDB<H: Hasher, T>: Send + Sync + AsHashDB<H, T> {
/// hash is not known.
fn get(&self, key: &H::Out, prefix: Prefix) -> Option<T>;

/// Check for the existance of a hash-key.
/// Check for the existence of a hash-key.
fn contains(&self, key: &H::Out, prefix: Prefix) -> bool;

/// Insert a datum item into the DB and return the datum's hash for a later lookup. Insertions
Expand Down
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
8 changes: 4 additions & 4 deletions trie-db/src/fatdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ where
let aux_hash = L::Hash::hash(&hash);
(
self.trie.db().get(&aux_hash, Default::default())
.expect("Missing fatdb hash")
.into_vec(),
.expect("Missing fatdb hash"),
value,
)
})
Expand All @@ -144,9 +143,10 @@ mod test {
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap();
}
let t = RefFatDB::new(&memdb, &root).unwrap();
assert_eq!(t.get(&[0x01u8, 0x23]).unwrap().unwrap(), DBValue::from_slice(&[0x01u8, 0x23]));
assert_eq!(t.get(&[0x01u8, 0x23]).unwrap().unwrap(), vec![0x01u8, 0x23]);
assert_eq!(
t.iter().unwrap().map(Result::unwrap).collect::<Vec<_>>(),
vec![(vec![0x01u8, 0x23], DBValue::from_slice(&[0x01u8, 0x23] as &[u8]))]);
vec![(vec![0x01u8, 0x23], vec![0x01u8, 0x23])]
);
}
}
8 changes: 4 additions & 4 deletions trie-db/src/fatdbmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ where
// insert if it doesn't exist.
if out.is_none() {
let aux_hash = L::Hash::hash(hash.as_ref());
db.emplace(aux_hash, EMPTY_PREFIX, DBValue::from_slice(key));
db.emplace(aux_hash, EMPTY_PREFIX, key.to_vec());
}
Ok(out)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ mod test {
let t = RefTrieDB::new(&memdb, &root).unwrap();
assert_eq!(
t.get(&KeccakHasher::hash(&[0x01u8, 0x23])),
Ok(Some(DBValue::from_slice(&[0x01u8, 0x23]))),
Ok(Some(vec![0x01u8, 0x23])),
);
}

Expand All @@ -140,8 +140,8 @@ mod test {
let aux_hash = KeccakHasher::hash(&key_hash);
let mut t = RefFatDBMut::new(&mut memdb, &mut root);
t.insert(&key, &val).unwrap();
assert_eq!(t.get(&key), Ok(Some(DBValue::from_slice(&val))));
assert_eq!(t.db().get(&aux_hash, EMPTY_PREFIX), Some(DBValue::from_slice(&key)));
assert_eq!(t.get(&key), Ok(Some(val.to_vec())));
assert_eq!(t.db().get(&aux_hash, EMPTY_PREFIX), Some(key.to_vec()));
t.remove(&key).unwrap();
assert_eq!(t.db().get(&aux_hash, EMPTY_PREFIX), None);
}
Expand Down
13 changes: 7 additions & 6 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 = Vec<u8>;

/// Trie Errors.
///
Expand Down Expand Up @@ -190,7 +191,7 @@ pub trait Query<H: Hasher> {

impl<'a, H: Hasher> Query<H> for &'a mut Recorder<H::Out> {
type Item = DBValue;
fn decode(self, value: &[u8]) -> DBValue { DBValue::from_slice(value) }
fn decode(self, value: &[u8]) -> DBValue { value.to_vec() }
fn record(&mut self, hash: &H::Out, data: &[u8], depth: u32) {
(&mut **self).record(hash, data, depth);
}
Expand Down Expand Up @@ -227,7 +228,7 @@ pub trait Trie<L: TrieLayout> {
&'a self,
key: &'key [u8],
) -> Result<Option<DBValue>, TrieHash<L>, CError<L>> where 'a: 'key {
self.get_with(key, DBValue::from_slice)
self.get_with(key, |v: &[u8]| v.to_vec() )
}

/// Search for the key with the given query parameter. See the docs of the `Query`
Expand Down Expand Up @@ -428,9 +429,9 @@ pub trait TrieLayout {
type Codec: NodeCodec<HashOut=<Self::Hash as Hasher>::Out>;
}

/// This traits associates a trie definition with prefered methods.
/// This trait associates a trie definition with preferred methods.
/// It also contains own default implementations and can be
/// use to allow switching implementation.
/// used to allow switching implementation.
pub trait TrieConfiguration: Sized + TrieLayout {
/// Operation to build a trie db from its ordered iterator over its key/values.
fn trie_build<DB, I, A, B>(db: &mut DB, input: I) -> <Self::Hash as Hasher>::Out where
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
2 changes: 1 addition & 1 deletion trie-db/src/sectriedb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ mod test {
t.insert(&KeccakHasher::hash(&[0x01u8, 0x23]), &[0x01u8, 0x23]).unwrap();
}
let t = RefSecTrieDB::new(&db, &root).unwrap();
assert_eq!(t.get(&[0x01u8, 0x23]).unwrap().unwrap(), DBValue::from_slice(&[0x01u8, 0x23]));
assert_eq!(t.get(&[0x01u8, 0x23]).unwrap().unwrap(), vec![0x01u8, 0x23]);
}
}
2 changes: 1 addition & 1 deletion trie-db/src/sectriedbmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ mod test {
let t = RefTrieDB::new(&memdb, &root).unwrap();
assert_eq!(
t.get(&KeccakHasher::hash(&[0x01u8, 0x23])).unwrap().unwrap(),
DBValue::from_slice(&[0x01u8, 0x23]),
vec![0x01u8, 0x23],
);
}
}
Loading