Fix clippy lints, impl treehash for slices
This commit is contained in:
parent
240d1e197a
commit
f20314bd87
@ -4,9 +4,6 @@ version = "0.1.0"
|
|||||||
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[profile.release]
|
|
||||||
debug = true
|
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "benches"
|
name = "benches"
|
||||||
harness = false
|
harness = false
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
use cached_tree_hash::TreeHashCache;
|
|
||||||
use ethereum_types::H256 as Hash256;
|
use ethereum_types::H256 as Hash256;
|
||||||
use tree_hash::TreeHash;
|
use tree_hash::TreeHash;
|
||||||
|
|
||||||
|
@ -2,10 +2,12 @@ use super::*;
|
|||||||
use crate::btree_overlay::LeafNode;
|
use crate::btree_overlay::LeafNode;
|
||||||
use crate::merkleize::{merkleize, num_sanitized_leaves, sanitise_bytes};
|
use crate::merkleize::{merkleize, num_sanitized_leaves, sanitise_bytes};
|
||||||
|
|
||||||
impl<T> CachedTreeHash for Vec<T>
|
macro_rules! impl_for_list {
|
||||||
where
|
($type: ty) => {
|
||||||
|
impl<T> CachedTreeHash for $type
|
||||||
|
where
|
||||||
T: CachedTreeHash + TreeHash,
|
T: CachedTreeHash + TreeHash,
|
||||||
{
|
{
|
||||||
fn new_tree_hash_cache(&self, depth: usize) -> Result<TreeHashCache, Error> {
|
fn new_tree_hash_cache(&self, depth: usize) -> Result<TreeHashCache, Error> {
|
||||||
let (mut cache, schema) = new_tree_hash_cache(self, depth)?;
|
let (mut cache, schema) = new_tree_hash_cache(self, depth)?;
|
||||||
|
|
||||||
@ -28,7 +30,7 @@ where
|
|||||||
cache.chunk_index += 1;
|
cache.chunk_index += 1;
|
||||||
|
|
||||||
// Update the cache, returning the new overlay.
|
// Update the cache, returning the new overlay.
|
||||||
let new_overlay = update_tree_hash_cache(self, cache)?;
|
let new_overlay = update_tree_hash_cache(&self, cache)?;
|
||||||
|
|
||||||
// Mix in length
|
// Mix in length
|
||||||
cache.mix_in_length(new_overlay.chunk_range(), self.len())?;
|
cache.mix_in_length(new_overlay.chunk_range(), self.len())?;
|
||||||
@ -38,10 +40,15 @@ where
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl_for_list!(Vec<T>);
|
||||||
|
impl_for_list!(&[T]);
|
||||||
|
|
||||||
pub fn new_tree_hash_cache<T: CachedTreeHash>(
|
pub fn new_tree_hash_cache<T: CachedTreeHash>(
|
||||||
vec: &Vec<T>,
|
vec: &[T],
|
||||||
depth: usize,
|
depth: usize,
|
||||||
) -> Result<(TreeHashCache, BTreeSchema), Error> {
|
) -> Result<(TreeHashCache, BTreeSchema), Error> {
|
||||||
let schema = vec.tree_hash_cache_schema(depth);
|
let schema = vec.tree_hash_cache_schema(depth);
|
||||||
@ -58,7 +65,7 @@ pub fn new_tree_hash_cache<T: CachedTreeHash>(
|
|||||||
.map(|item| TreeHashCache::new(item, depth + 1))
|
.map(|item| TreeHashCache::new(item, depth + 1))
|
||||||
.collect::<Result<Vec<TreeHashCache>, _>>()?;
|
.collect::<Result<Vec<TreeHashCache>, _>>()?;
|
||||||
|
|
||||||
TreeHashCache::from_leaves_and_subtrees(vec, subtrees, depth)
|
TreeHashCache::from_leaves_and_subtrees(&vec, subtrees, depth)
|
||||||
}
|
}
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
@ -91,11 +98,11 @@ pub fn produce_schema<T: CachedTreeHash>(vec: &[T], depth: usize) -> BTreeSchema
|
|||||||
|
|
||||||
#[allow(clippy::range_plus_one)] // Minor readability lint requiring structural changes; not worth it.
|
#[allow(clippy::range_plus_one)] // Minor readability lint requiring structural changes; not worth it.
|
||||||
pub fn update_tree_hash_cache<T: CachedTreeHash>(
|
pub fn update_tree_hash_cache<T: CachedTreeHash>(
|
||||||
vec: &Vec<T>,
|
vec: &[T],
|
||||||
cache: &mut TreeHashCache,
|
cache: &mut TreeHashCache,
|
||||||
) -> Result<BTreeOverlay, Error> {
|
) -> Result<BTreeOverlay, Error> {
|
||||||
let old_overlay = cache.get_overlay(cache.schema_index, cache.chunk_index)?;
|
let old_overlay = cache.get_overlay(cache.schema_index, cache.chunk_index)?;
|
||||||
let new_overlay = BTreeOverlay::new(vec, cache.chunk_index, old_overlay.depth);
|
let new_overlay = BTreeOverlay::new(&vec, cache.chunk_index, old_overlay.depth);
|
||||||
|
|
||||||
cache.replace_overlay(cache.schema_index, cache.chunk_index, new_overlay.clone())?;
|
cache.replace_overlay(cache.schema_index, cache.chunk_index, new_overlay.clone())?;
|
||||||
|
|
||||||
|
@ -87,10 +87,12 @@ impl TreeHash for H256 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> TreeHash for Vec<T>
|
macro_rules! impl_for_list {
|
||||||
where
|
($type: ty) => {
|
||||||
|
impl<T> TreeHash for $type
|
||||||
|
where
|
||||||
T: TreeHash,
|
T: TreeHash,
|
||||||
{
|
{
|
||||||
fn tree_hash_type() -> TreeHashType {
|
fn tree_hash_type() -> TreeHashType {
|
||||||
TreeHashType::List
|
TreeHashType::List
|
||||||
}
|
}
|
||||||
@ -110,8 +112,13 @@ where
|
|||||||
|
|
||||||
hash(&root_and_len)
|
hash(&root_and_len)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl_for_list!(Vec<T>);
|
||||||
|
impl_for_list!(&[T]);
|
||||||
|
|
||||||
pub fn vec_tree_hash_root<T>(vec: &[T]) -> Vec<u8>
|
pub fn vec_tree_hash_root<T>(vec: &[T]) -> Vec<u8>
|
||||||
where
|
where
|
||||||
T: TreeHash,
|
T: TreeHash,
|
||||||
|
Loading…
Reference in New Issue
Block a user