Fix clippy lints, impl treehash for slices

This commit is contained in:
Paul Hauner 2019-04-29 15:32:41 +10:00
parent 240d1e197a
commit f20314bd87
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
4 changed files with 64 additions and 54 deletions

View File

@ -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

View File

@ -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;

View File

@ -2,46 +2,53 @@ 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) => {
T: CachedTreeHash + TreeHash, impl<T> CachedTreeHash for $type
{ where
fn new_tree_hash_cache(&self, depth: usize) -> Result<TreeHashCache, Error> { T: CachedTreeHash + TreeHash,
let (mut cache, schema) = new_tree_hash_cache(self, depth)?; {
fn new_tree_hash_cache(&self, depth: usize) -> Result<TreeHashCache, Error> {
let (mut cache, schema) = new_tree_hash_cache(self, depth)?;
cache.add_length_nodes(schema.into_overlay(0).chunk_range(), self.len())?; cache.add_length_nodes(schema.into_overlay(0).chunk_range(), self.len())?;
Ok(cache) Ok(cache)
} }
fn num_tree_hash_cache_chunks(&self) -> usize { fn num_tree_hash_cache_chunks(&self) -> usize {
// Add two extra nodes to cater for the node before and after to allow mixing-in length. // Add two extra nodes to cater for the node before and after to allow mixing-in length.
BTreeOverlay::new(self, 0, 0).num_chunks() + 2 BTreeOverlay::new(self, 0, 0).num_chunks() + 2
} }
fn tree_hash_cache_schema(&self, depth: usize) -> BTreeSchema { fn tree_hash_cache_schema(&self, depth: usize) -> BTreeSchema {
produce_schema(self, depth) produce_schema(self, depth)
} }
fn update_tree_hash_cache(&self, cache: &mut TreeHashCache) -> Result<(), Error> { fn update_tree_hash_cache(&self, cache: &mut TreeHashCache) -> Result<(), Error> {
// Skip the length-mixed-in root node. // Skip the length-mixed-in root node.
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())?;
// Skip an extra node to clear the length node. // Skip an extra node to clear the length node.
cache.chunk_index += 1; cache.chunk_index += 1;
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())?;

View File

@ -87,31 +87,38 @@ impl TreeHash for H256 {
} }
} }
impl<T> TreeHash for Vec<T> macro_rules! impl_for_list {
where ($type: ty) => {
T: TreeHash, impl<T> TreeHash for $type
{ where
fn tree_hash_type() -> TreeHashType { T: TreeHash,
TreeHashType::List {
} fn tree_hash_type() -> TreeHashType {
TreeHashType::List
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> { fn tree_hash_packed_encoding(&self) -> Vec<u8> {
unreachable!("List should never be packed.") unreachable!("List should never be packed.")
} }
fn tree_hash_packing_factor() -> usize { fn tree_hash_packing_factor() -> usize {
unreachable!("List should never be packed.") unreachable!("List should never be packed.")
} }
fn tree_hash_root(&self) -> Vec<u8> { fn tree_hash_root(&self) -> Vec<u8> {
let mut root_and_len = Vec::with_capacity(HASHSIZE * 2); let mut root_and_len = Vec::with_capacity(HASHSIZE * 2);
root_and_len.append(&mut vec_tree_hash_root(self)); root_and_len.append(&mut vec_tree_hash_root(self));
root_and_len.append(&mut int_to_bytes32(self.len() as u64)); root_and_len.append(&mut int_to_bytes32(self.len() as u64));
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,