Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
arkpar committed Aug 12, 2023
1 parent d9ac5aa commit 00b4fa9
Show file tree
Hide file tree
Showing 20 changed files with 327 additions and 203 deletions.
10 changes: 3 additions & 7 deletions memory-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@
#[cfg(not(feature = "std"))]
extern crate alloc;

use hash_db::{
HashDB, Hasher as KeyHasher, MaybeDebug,
Prefix,
};
use hash_db::{HashDB, Hasher as KeyHasher, MaybeDebug, Prefix};
#[cfg(feature = "std")]
use std::{
cmp::Eq, collections::hash_map::Entry, collections::HashMap as Map, hash,
marker::PhantomData, mem,
cmp::Eq, collections::hash_map::Entry, collections::HashMap as Map, hash, marker::PhantomData,
mem,
};

#[cfg(not(feature = "std"))]
Expand Down Expand Up @@ -451,7 +448,6 @@ where
},
}
}

}

#[cfg(test)]
Expand Down
45 changes: 33 additions & 12 deletions test-support/reference-trie/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,11 @@ impl<H: Hasher> NodeCodec for ReferenceNodeCodec<H> {
&[EMPTY_TRIE]
}

fn leaf_node<L>(partial: impl Iterator<Item = u8>, number_nibble: usize, value: Value<L>) -> Vec<u8> {
fn leaf_node<L>(
partial: impl Iterator<Item = u8>,
number_nibble: usize,
value: Value<L>,
) -> Vec<u8> {
let mut output =
partial_from_iterator_to_key(partial, number_nibble, LEAF_NODE_OFFSET, LEAF_NODE_OVER);
match value {
Expand Down Expand Up @@ -835,7 +839,11 @@ impl<H: Hasher> NodeCodec for ReferenceNodeCodecNoExt<H> {
&[EMPTY_TRIE_NO_EXT]
}

fn leaf_node<L>(partial: impl Iterator<Item = u8>, number_nibble: usize, value: Value<L>) -> Vec<u8> {
fn leaf_node<L>(
partial: impl Iterator<Item = u8>,
number_nibble: usize,
value: Value<L>,
) -> Vec<u8> {
let mut output = partial_from_iterator_encode(partial, number_nibble, NodeKindNoExt::Leaf);
match value {
Value::Inline(value) => {
Expand Down Expand Up @@ -1002,7 +1010,10 @@ where
}

/// Trie builder trie building utility.
pub fn calc_root_build<T, I, A, B, K>(data: I, memdb: &mut memory_db::MemoryDB<T::Hash, K, DBValue>) -> TrieHash<T>
pub fn calc_root_build<T, I, A, B, K>(
data: I,
memdb: &mut memory_db::MemoryDB<T::Hash, K, DBValue>,
) -> TrieHash<T>
where
T: TrieLayout,
I: IntoIterator<Item = (A, B)>,
Expand Down Expand Up @@ -1065,9 +1076,8 @@ where

/// Testing utility that uses some periodic removal over
/// its input test data.
pub fn compare_insert_remove<T, K>(
data: Vec<(bool, Vec<u8>, Vec<u8>)>,
) where
pub fn compare_insert_remove<T, K>(data: Vec<(bool, Vec<u8>, Vec<u8>)>)
where
T: TrieLayout,
K: memory_db::KeyFunction<T::Hash> + Send + Sync,
{
Expand Down Expand Up @@ -1134,11 +1144,18 @@ impl<L: TrieLayout> Default for TestTrieCache<L> {
}

impl<L: TrieLayout> trie_db::TrieCache<L::Codec, L::Location> for TestTrieCache<L> {
fn lookup_value_for_key(&mut self, key: &[u8]) -> Option<&trie_db::CachedValue<TrieHash<L>, L::Location>> {
fn lookup_value_for_key(
&mut self,
key: &[u8],
) -> Option<&trie_db::CachedValue<TrieHash<L>, L::Location>> {
self.value_cache.get(key)
}

fn cache_value_for_key(&mut self, key: &[u8], value: trie_db::CachedValue<TrieHash<L>, L::Location>) {
fn cache_value_for_key(
&mut self,
key: &[u8],
value: trie_db::CachedValue<TrieHash<L>, L::Location>,
) {
self.value_cache.insert(key.to_vec(), value);
}

Expand All @@ -1161,12 +1178,15 @@ impl<L: TrieLayout> trie_db::TrieCache<L::Codec, L::Location> for TestTrieCache<
}
}

fn get_node(&mut self, hash: &TrieHash<L>, _location: L::Location) -> Option<&NodeOwned<TrieHash<L>, L::Location>> {
fn get_node(
&mut self,
hash: &TrieHash<L>,
_location: L::Location,
) -> Option<&NodeOwned<TrieHash<L>, L::Location>> {
self.node_cache.get(hash)
}

fn insert_new_node(&mut self, _hash: &TrieHash<L>) {
}
fn insert_new_node(&mut self, _hash: &TrieHash<L>) {}
}

#[cfg(test)]
Expand Down Expand Up @@ -1206,7 +1226,8 @@ mod tests {
input.len() * NIBBLE_PER_BYTE,
Value::<()>::Inline(&[1]),
);
let dec = <ReferenceNodeCodecNoExt<RefHasher> as NodeCodec>::decode(&enc, &[] as &[()]).unwrap();
let dec =
<ReferenceNodeCodecNoExt<RefHasher> as NodeCodec>::decode(&enc, &[] as &[()]).unwrap();
let o_sl = if let Node::Leaf(sl, _) = dec { Some(sl) } else { None };
assert!(o_sl.is_some());
}
Expand Down
6 changes: 5 additions & 1 deletion test-support/reference-trie/src/substrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,11 @@ where
&[trie_constants::EMPTY_TRIE]
}

fn leaf_node<L>(partial: impl Iterator<Item = u8>, number_nibble: usize, value: Value<L>) -> Vec<u8> {
fn leaf_node<L>(
partial: impl Iterator<Item = u8>,
number_nibble: usize,
value: Value<L>,
) -> Vec<u8> {
let contains_hash = matches!(&value, Value::Node(..));
let mut output = if contains_hash {
partial_from_iterator_encode(partial, number_nibble, NodeKind::HashedValueLeaf)
Expand Down
6 changes: 5 additions & 1 deletion test-support/reference-trie/src/substrate_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ where
&[trie_constants::EMPTY_TRIE]
}

fn leaf_node<L>(partial: impl Iterator<Item = u8>, number_nibble: usize, value: Value<L>) -> Vec<u8> {
fn leaf_node<L>(
partial: impl Iterator<Item = u8>,
number_nibble: usize,
value: Value<L>,
) -> Vec<u8> {
let contains_hash = matches!(&value, Value::Node(..));
let mut output = if contains_hash {
partial_from_iterator_encode(partial, number_nibble, NodeKind::HashedValueLeaf)
Expand Down
3 changes: 2 additions & 1 deletion trie-db/src/iter_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ impl<'a, T: TrieLayout, K: memory_db::KeyFunction<T::Hash> + Send + Sync> TrieBu
}
}

impl<'a, T, K: memory_db::KeyFunction<T::Hash> + Send + Sync> ProcessEncodedNode<TrieHash<T>> for TrieBuilder<'a, T, K>
impl<'a, T, K: memory_db::KeyFunction<T::Hash> + Send + Sync> ProcessEncodedNode<TrieHash<T>>
for TrieBuilder<'a, T, K>
where
T: TrieLayout,
{
Expand Down
18 changes: 11 additions & 7 deletions trie-db/src/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,11 @@ impl<L: TrieLayout> TrieDBRawIterator<L> {
}

let value = match value {
Value::Node(hash, location) => match Self::fetch_value(db, &hash, (key_slice, None), location) {
Ok(value) => value,
Err(err) => return Some(Err(err)),
},
Value::Node(hash, location) =>
match Self::fetch_value(db, &hash, (key_slice, None), location) {
Ok(value) => value,
Err(err) => return Some(Err(err)),
},
Value::Inline(value) => value.to_vec(),
};

Expand All @@ -504,7 +505,7 @@ impl<L: TrieLayout> TrieDBRawIterator<L> {

let mut prefix = prefix.clone();
match node.node() {
Node::Leaf(partial, _) => {
Node::Leaf(partial, _) => {
prefix.append_partial(partial.right());
},
Node::Branch(_, value) =>
Expand Down Expand Up @@ -593,8 +594,11 @@ impl<'a, 'cache, L: TrieLayout> TrieIterator<L> for TrieDBNodeIterator<'a, 'cach
}

impl<'a, 'cache, L: TrieLayout> Iterator for TrieDBNodeIterator<'a, 'cache, L> {
type Item =
Result<(NibbleVec, Option<TrieHash<L>>, Arc<OwnedNode<DBValue, L::Location>>), TrieHash<L>, CError<L>>;
type Item = Result<
(NibbleVec, Option<TrieHash<L>>, Arc<OwnedNode<DBValue, L::Location>>),
TrieHash<L>,
CError<L>,
>;

fn next(&mut self) -> Option<Self::Item> {
self.raw_iter.next_raw_item(self.db).map(|result| {
Expand Down
11 changes: 7 additions & 4 deletions trie-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ pub use self::{
recorder::Recorder,
triedb::{TrieDB, TrieDBBuilder, TrieDBIterator, TrieDBKeyIterator},
triedbmut::{
ChildReference, TrieDBMut, TrieDBMutBuilder, Value,
Changeset, ChangesetNodeRef, NewChangesetNode, ExistingChangesetNode
Changeset, ChangesetNodeRef, ChildReference, ExistingChangesetNode, NewChangesetNode,
TrieDBMut, TrieDBMutBuilder, Value,
},
};
pub use crate::{
Expand Down Expand Up @@ -329,15 +329,18 @@ pub trait TrieLayout {
type Hash: Hasher;
/// Codec to use (needs to match hasher and nibble ops).
type Codec: NodeCodec<HashOut = <Self::Hash as Hasher>::Out>;
type Location: Copy + Default + Eq + PartialEq;
type Location: Copy + Default + Eq + PartialEq;
}

/// This trait associates a trie definition with preferred methods.
/// It also contains own default implementations and can be
/// 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<I, A, B>(db: &mut memory_db::MemoryDB<Self::Hash, memory_db::HashKey<Self::Hash>, DBValue>, input: I) -> <Self::Hash as Hasher>::Out
fn trie_build<I, A, B>(
db: &mut memory_db::MemoryDB<Self::Hash, memory_db::HashKey<Self::Hash>, DBValue>,
input: I,
) -> <Self::Hash as Hasher>::Out
where
I: IntoIterator<Item = (A, B)>,
A: AsRef<[u8]> + Ord,
Expand Down
46 changes: 25 additions & 21 deletions trie-db/src/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ where
full_key,
cache,
|value, _, full_key, _, _, recorder| match value {
ValueOwned::Inline(value, hash) => Ok((hash, Some(value.clone()), Default::default())),
ValueOwned::Inline(value, hash) =>
Ok((hash, Some(value.clone()), Default::default())),
ValueOwned::Node(hash, location) => {
if let Some(recoder) = recorder.as_mut() {
recoder.record(TrieAccess::Hash { full_key });
Expand All @@ -231,7 +232,8 @@ where
match &hash_and_value {
Some((hash, Some(value), _location)) =>
cache.cache_value_for_key(full_key, (value.clone(), *hash).into()),
Some((hash, None, location)) => cache.cache_value_for_key(full_key, CachedValue::ExistingHash(*hash, *location)),
Some((hash, None, location)) =>
cache.cache_value_for_key(full_key, CachedValue::ExistingHash(*hash, *location)),
None => cache.cache_value_for_key(full_key, CachedValue::NonExisting),
}

Expand Down Expand Up @@ -285,7 +287,7 @@ where
let res = match value_cache_allowed.then(|| cache.lookup_value_for_key(full_key)).flatten()
{
Some(CachedValue::NonExisting) => None,
Some(CachedValue::ExistingHash(hash, location)) => {
Some(CachedValue::ExistingHash(hash, location)) => {
let data = Self::load_owned_value(
// If we only have the hash cached, this can only be a value node.
// For inline nodes we cache them directly as `CachedValue::Existing`.
Expand Down Expand Up @@ -352,14 +354,15 @@ where
// this loop iterates through non-inline nodes.
for depth in 0.. {
let mut node = cache.get_or_insert_node(hash, location, &mut || {
let (node_data, locations) = match self.db.get(&hash, nibble_key.mid(key_nibbles).left(), location) {
Some(value) => value,
None =>
return Err(Box::new(match depth {
0 => TrieError::InvalidStateRoot(hash),
_ => TrieError::IncompleteDatabase(hash),
})),
};
let (node_data, locations) =
match self.db.get(&hash, nibble_key.mid(key_nibbles).left(), location) {
Some(value) => value,
None =>
return Err(Box::new(match depth {
0 => TrieError::InvalidStateRoot(hash),
_ => TrieError::IncompleteDatabase(hash),
})),
};

let decoded = match L::Codec::decode(&node_data[..], &locations) {
Ok(node) => node,
Expand Down Expand Up @@ -528,14 +531,15 @@ where

// this loop iterates through non-inline nodes.
for depth in 0.. {
let (node_data, locations) = match self.db.get(&hash, nibble_key.mid(key_nibbles).left(), location) {
Some(value) => value,
None =>
return Err(Box::new(match depth {
0 => TrieError::InvalidStateRoot(hash),
_ => TrieError::IncompleteDatabase(hash),
})),
};
let (node_data, locations) =
match self.db.get(&hash, nibble_key.mid(key_nibbles).left(), location) {
Some(value) => value,
None =>
return Err(Box::new(match depth {
0 => TrieError::InvalidStateRoot(hash),
_ => TrieError::IncompleteDatabase(hash),
})),
};

self.record(|| TrieAccess::EncodedNode {
hash,
Expand Down Expand Up @@ -596,7 +600,7 @@ where
Ok(None)
}
} else {
let i = partial.at(0) as usize;
let i = partial.at(0) as usize;
match children[i] {
Some(x) => {
partial = partial.mid(1);
Expand Down Expand Up @@ -634,7 +638,7 @@ where
Ok(None)
}
} else {
let i = partial.at(slice.len()) as usize;
let i = partial.at(slice.len()) as usize;
match children[i] {
Some(x) => {
partial = partial.mid(slice.len() + 1);
Expand Down
Loading

0 comments on commit 00b4fa9

Please sign in to comment.