Tidy tree hash cache, add new trait

This commit is contained in:
Paul Hauner
2019-04-15 15:13:02 +10:00
parent 8e5b79452a
commit 354f823c16
6 changed files with 138 additions and 18 deletions

View File

@@ -12,7 +12,7 @@ pub struct BTreeOverlay {
impl BTreeOverlay {
pub fn new<T>(item: &T, initial_offset: usize) -> Result<Self, Error>
where
T: CachedTreeHash<T>,
T: CachedTreeHashSubtree<T>,
{
item.btree_overlay(initial_offset)
}

View File

@@ -15,7 +15,7 @@ impl Into<Vec<u8>> for TreeHashCache {
impl TreeHashCache {
pub fn new<T>(item: &T) -> Result<Self, Error>
where
T: CachedTreeHash<T>,
T: CachedTreeHashSubtree<T>,
{
item.new_cache()
}
@@ -32,7 +32,7 @@ impl TreeHashCache {
leaves_and_subtrees: Vec<Self>,
) -> Result<Self, Error>
where
T: CachedTreeHash<T>,
T: CachedTreeHashSubtree<T>,
{
let offset_handler = BTreeOverlay::new(item, 0)?;
@@ -55,7 +55,7 @@ impl TreeHashCache {
// Iterate through all of the leaves/subtrees, adding their root as a leaf node and then
// concatenating their merkle trees.
for t in leaves_and_subtrees {
leaves.append(&mut t.root()?);
leaves.append(&mut t.root().ok_or_else(|| Error::NoBytesForRoot)?.to_vec());
cache.append(&mut t.into_merkle_tree());
}
@@ -89,11 +89,8 @@ impl TreeHashCache {
self.cache.len()
}
pub fn root(&self) -> Result<Vec<u8>, Error> {
self.cache
.get(0..HASHSIZE)
.ok_or_else(|| Error::NoBytesForRoot)
.and_then(|slice| Ok(slice.to_vec()))
pub fn root(&self) -> Option<&[u8]> {
self.cache.get(0..HASHSIZE)
}
pub fn splice(&mut self, chunk_range: Range<usize>, replace_with: Self) {

View File

@@ -4,7 +4,7 @@ use ssz::ssz_encode;
mod vec;
impl CachedTreeHash<u64> for u64 {
impl CachedTreeHashSubtree<u64> for u64 {
fn item_type() -> ItemType {
ItemType::Basic
}

View File

@@ -1,8 +1,8 @@
use super::*;
impl<T> CachedTreeHash<Vec<T>> for Vec<T>
impl<T> CachedTreeHashSubtree<Vec<T>> for Vec<T>
where
T: CachedTreeHash<T>,
T: CachedTreeHashSubtree<T>,
{
fn item_type() -> ItemType {
ItemType::List
@@ -168,7 +168,7 @@ where
fn get_packed_leaves<T>(vec: &Vec<T>) -> Result<Vec<u8>, Error>
where
T: CachedTreeHash<T>,
T: CachedTreeHashSubtree<T>,
{
let num_packed_bytes = (BYTES_PER_CHUNK / T::packing_factor()) * vec.len();
let num_leaves = num_sanitized_leaves(num_packed_bytes);

View File

@@ -1,6 +1,5 @@
use hashing::hash;
use int_to_bytes::int_to_bytes32;
use std::fmt::Debug;
use std::ops::Range;
mod btree_overlay;
@@ -36,8 +35,15 @@ pub enum ItemType {
Composite,
}
// TODO: remove debug requirement.
pub trait CachedTreeHash<Item>: Debug {
pub trait CachedTreeHash<T>: CachedTreeHashSubtree<T> + Sized {
fn update_internal_tree_hash_cache(self, old: T) -> Result<(Self, Self), Error>;
fn cached_tree_hash_root(&self) -> Option<Vec<u8>>;
fn clone_without_tree_hash_cache(&self) -> Self;
}
pub trait CachedTreeHashSubtree<Item> {
fn item_type() -> ItemType;
fn btree_overlay(&self, chunk_offset: usize) -> Result<BTreeOverlay, Error>;