Update naming for tree_hash fns/structs/traits
This commit is contained in:
parent
d311b48a9f
commit
8a1bde3e2f
@ -32,11 +32,11 @@ pub trait CachedTreeHash<T>: CachedTreeHashSubTree<T> + Sized {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait CachedTreeHashSubTree<Item>: TreeHash {
|
pub trait CachedTreeHashSubTree<Item>: TreeHash {
|
||||||
fn btree_overlay(&self, chunk_offset: usize) -> Result<BTreeOverlay, Error>;
|
fn tree_hash_cache_overlay(&self, chunk_offset: usize) -> Result<BTreeOverlay, Error>;
|
||||||
|
|
||||||
fn new_cache(&self) -> Result<TreeHashCache, Error>;
|
fn new_tree_hash_cache(&self) -> Result<TreeHashCache, Error>;
|
||||||
|
|
||||||
fn update_cache(
|
fn update_tree_hash_cache(
|
||||||
&self,
|
&self,
|
||||||
other: &Item,
|
other: &Item,
|
||||||
cache: &mut TreeHashCache,
|
cache: &mut TreeHashCache,
|
||||||
@ -136,7 +136,7 @@ impl TreeHashCache {
|
|||||||
where
|
where
|
||||||
T: CachedTreeHashSubTree<T>,
|
T: CachedTreeHashSubTree<T>,
|
||||||
{
|
{
|
||||||
item.new_cache()
|
item.new_tree_hash_cache()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_elems(cache: Vec<u8>, chunk_modified: Vec<bool>) -> Self {
|
pub fn from_elems(cache: Vec<u8>, chunk_modified: Vec<bool>) -> Self {
|
||||||
|
@ -14,7 +14,7 @@ impl BTreeOverlay {
|
|||||||
where
|
where
|
||||||
T: CachedTreeHashSubTree<T>,
|
T: CachedTreeHashSubTree<T>,
|
||||||
{
|
{
|
||||||
item.btree_overlay(initial_offset)
|
item.tree_hash_cache_overlay(initial_offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_lengths(offset: usize, mut lengths: Vec<usize>) -> Result<Self, Error> {
|
pub fn from_lengths(offset: usize, mut lengths: Vec<usize>) -> Result<Self, Error> {
|
||||||
|
@ -5,18 +5,18 @@ use ssz::ssz_encode;
|
|||||||
mod vec;
|
mod vec;
|
||||||
|
|
||||||
impl CachedTreeHashSubTree<u64> for u64 {
|
impl CachedTreeHashSubTree<u64> for u64 {
|
||||||
fn new_cache(&self) -> Result<TreeHashCache, Error> {
|
fn new_tree_hash_cache(&self) -> Result<TreeHashCache, Error> {
|
||||||
Ok(TreeHashCache::from_bytes(
|
Ok(TreeHashCache::from_bytes(
|
||||||
merkleize(ssz_encode(self)),
|
merkleize(ssz_encode(self)),
|
||||||
false,
|
false,
|
||||||
)?)
|
)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn btree_overlay(&self, chunk_offset: usize) -> Result<BTreeOverlay, Error> {
|
fn tree_hash_cache_overlay(&self, chunk_offset: usize) -> Result<BTreeOverlay, Error> {
|
||||||
BTreeOverlay::from_lengths(chunk_offset, vec![1])
|
BTreeOverlay::from_lengths(chunk_offset, vec![1])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_cache(
|
fn update_tree_hash_cache(
|
||||||
&self,
|
&self,
|
||||||
other: &Self,
|
other: &Self,
|
||||||
cache: &mut TreeHashCache,
|
cache: &mut TreeHashCache,
|
||||||
|
@ -4,7 +4,7 @@ impl<T> CachedTreeHashSubTree<Vec<T>> for Vec<T>
|
|||||||
where
|
where
|
||||||
T: CachedTreeHashSubTree<T> + TreeHash,
|
T: CachedTreeHashSubTree<T> + TreeHash,
|
||||||
{
|
{
|
||||||
fn new_cache(&self) -> Result<TreeHashCache, Error> {
|
fn new_tree_hash_cache(&self) -> Result<TreeHashCache, Error> {
|
||||||
match T::tree_hash_type() {
|
match T::tree_hash_type() {
|
||||||
TreeHashType::Basic => {
|
TreeHashType::Basic => {
|
||||||
TreeHashCache::from_bytes(merkleize(get_packed_leaves(self)?), false)
|
TreeHashCache::from_bytes(merkleize(get_packed_leaves(self)?), false)
|
||||||
@ -20,7 +20,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn btree_overlay(&self, chunk_offset: usize) -> Result<BTreeOverlay, Error> {
|
fn tree_hash_cache_overlay(&self, chunk_offset: usize) -> Result<BTreeOverlay, Error> {
|
||||||
let lengths = match T::tree_hash_type() {
|
let lengths = match T::tree_hash_type() {
|
||||||
TreeHashType::Basic => vec![1; self.len() / T::tree_hash_packing_factor()],
|
TreeHashType::Basic => vec![1; self.len() / T::tree_hash_packing_factor()],
|
||||||
TreeHashType::Composite | TreeHashType::List => {
|
TreeHashType::Composite | TreeHashType::List => {
|
||||||
@ -37,7 +37,7 @@ where
|
|||||||
BTreeOverlay::from_lengths(chunk_offset, lengths)
|
BTreeOverlay::from_lengths(chunk_offset, lengths)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_cache(
|
fn update_tree_hash_cache(
|
||||||
&self,
|
&self,
|
||||||
other: &Vec<T>,
|
other: &Vec<T>,
|
||||||
cache: &mut TreeHashCache,
|
cache: &mut TreeHashCache,
|
||||||
@ -104,7 +104,7 @@ where
|
|||||||
match (other.get(i), self.get(i)) {
|
match (other.get(i), self.get(i)) {
|
||||||
// The item existed in the previous list and exsits in the current list.
|
// The item existed in the previous list and exsits in the current list.
|
||||||
(Some(old), Some(new)) => {
|
(Some(old), Some(new)) => {
|
||||||
new.update_cache(old, cache, start_chunk)?;
|
new.update_tree_hash_cache(old, cache, start_chunk)?;
|
||||||
}
|
}
|
||||||
// The item existed in the previous list but does not exist in this list.
|
// The item existed in the previous list but does not exist in this list.
|
||||||
//
|
//
|
||||||
|
@ -5,8 +5,8 @@ pub const BYTES_PER_CHUNK: usize = 32;
|
|||||||
pub const HASHSIZE: usize = 32;
|
pub const HASHSIZE: usize = 32;
|
||||||
pub const MERKLE_HASH_CHUNCK: usize = 2 * BYTES_PER_CHUNK;
|
pub const MERKLE_HASH_CHUNCK: usize = 2 * BYTES_PER_CHUNK;
|
||||||
|
|
||||||
pub use cached_tree_hash::CachedTreeHashSubTree;
|
pub use cached_tree_hash::{BTreeOverlay, CachedTreeHashSubTree, Error, TreeHashCache};
|
||||||
pub use standard_tree_hash::TreeHash;
|
pub use standard_tree_hash::{efficient_merkleize, TreeHash};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub enum TreeHashType {
|
pub enum TreeHashType {
|
||||||
|
@ -40,9 +40,9 @@ impl CachedTreeHash<InternalCache> for InternalCache {
|
|||||||
old.cache = None;
|
old.cache = None;
|
||||||
|
|
||||||
if let Some(ref mut local_cache) = local_cache {
|
if let Some(ref mut local_cache) = local_cache {
|
||||||
self.update_cache(&old, local_cache, 0)?;
|
self.update_tree_hash_cache(&old, local_cache, 0)?;
|
||||||
} else {
|
} else {
|
||||||
local_cache = Some(self.new_cache()?)
|
local_cache = Some(self.new_tree_hash_cache()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
self.cache = local_cache;
|
self.cache = local_cache;
|
||||||
@ -89,16 +89,16 @@ fn works_when_embedded() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl CachedTreeHashSubTree<InternalCache> for InternalCache {
|
impl CachedTreeHashSubTree<InternalCache> for InternalCache {
|
||||||
fn new_cache(&self) -> Result<TreeHashCache, Error> {
|
fn new_tree_hash_cache(&self) -> Result<TreeHashCache, Error> {
|
||||||
let tree = TreeHashCache::from_leaves_and_subtrees(
|
let tree = TreeHashCache::from_leaves_and_subtrees(
|
||||||
self,
|
self,
|
||||||
vec![self.a.new_cache()?, self.b.new_cache()?],
|
vec![self.a.new_tree_hash_cache()?, self.b.new_tree_hash_cache()?],
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(tree)
|
Ok(tree)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn btree_overlay(&self, chunk_offset: usize) -> Result<BTreeOverlay, Error> {
|
fn tree_hash_cache_overlay(&self, chunk_offset: usize) -> Result<BTreeOverlay, Error> {
|
||||||
let mut lengths = vec![];
|
let mut lengths = vec![];
|
||||||
|
|
||||||
lengths.push(BTreeOverlay::new(&self.a, 0)?.total_nodes());
|
lengths.push(BTreeOverlay::new(&self.a, 0)?.total_nodes());
|
||||||
@ -107,7 +107,7 @@ impl CachedTreeHashSubTree<InternalCache> for InternalCache {
|
|||||||
BTreeOverlay::from_lengths(chunk_offset, lengths)
|
BTreeOverlay::from_lengths(chunk_offset, lengths)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_cache(
|
fn update_tree_hash_cache(
|
||||||
&self,
|
&self,
|
||||||
other: &Self,
|
other: &Self,
|
||||||
cache: &mut TreeHashCache,
|
cache: &mut TreeHashCache,
|
||||||
@ -118,8 +118,8 @@ impl CachedTreeHashSubTree<InternalCache> for InternalCache {
|
|||||||
// Skip past the internal nodes and update any changed leaf nodes.
|
// Skip past the internal nodes and update any changed leaf nodes.
|
||||||
{
|
{
|
||||||
let chunk = offset_handler.first_leaf_node()?;
|
let chunk = offset_handler.first_leaf_node()?;
|
||||||
let chunk = self.a.update_cache(&other.a, cache, chunk)?;
|
let chunk = self.a.update_tree_hash_cache(&other.a, cache, chunk)?;
|
||||||
let _chunk = self.b.update_cache(&other.b, cache, chunk)?;
|
let _chunk = self.b.update_tree_hash_cache(&other.b, cache, chunk)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (&parent, children) in offset_handler.iter_internal_nodes().rev() {
|
for (&parent, children) in offset_handler.iter_internal_nodes().rev() {
|
||||||
@ -170,21 +170,21 @@ impl TreeHash for Inner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl CachedTreeHashSubTree<Inner> for Inner {
|
impl CachedTreeHashSubTree<Inner> for Inner {
|
||||||
fn new_cache(&self) -> Result<TreeHashCache, Error> {
|
fn new_tree_hash_cache(&self) -> Result<TreeHashCache, Error> {
|
||||||
let tree = TreeHashCache::from_leaves_and_subtrees(
|
let tree = TreeHashCache::from_leaves_and_subtrees(
|
||||||
self,
|
self,
|
||||||
vec![
|
vec![
|
||||||
self.a.new_cache()?,
|
self.a.new_tree_hash_cache()?,
|
||||||
self.b.new_cache()?,
|
self.b.new_tree_hash_cache()?,
|
||||||
self.c.new_cache()?,
|
self.c.new_tree_hash_cache()?,
|
||||||
self.d.new_cache()?,
|
self.d.new_tree_hash_cache()?,
|
||||||
],
|
],
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(tree)
|
Ok(tree)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn btree_overlay(&self, chunk_offset: usize) -> Result<BTreeOverlay, Error> {
|
fn tree_hash_cache_overlay(&self, chunk_offset: usize) -> Result<BTreeOverlay, Error> {
|
||||||
let mut lengths = vec![];
|
let mut lengths = vec![];
|
||||||
|
|
||||||
lengths.push(BTreeOverlay::new(&self.a, 0)?.total_nodes());
|
lengths.push(BTreeOverlay::new(&self.a, 0)?.total_nodes());
|
||||||
@ -195,7 +195,7 @@ impl CachedTreeHashSubTree<Inner> for Inner {
|
|||||||
BTreeOverlay::from_lengths(chunk_offset, lengths)
|
BTreeOverlay::from_lengths(chunk_offset, lengths)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_cache(
|
fn update_tree_hash_cache(
|
||||||
&self,
|
&self,
|
||||||
other: &Self,
|
other: &Self,
|
||||||
cache: &mut TreeHashCache,
|
cache: &mut TreeHashCache,
|
||||||
@ -206,10 +206,10 @@ impl CachedTreeHashSubTree<Inner> for Inner {
|
|||||||
// Skip past the internal nodes and update any changed leaf nodes.
|
// Skip past the internal nodes and update any changed leaf nodes.
|
||||||
{
|
{
|
||||||
let chunk = offset_handler.first_leaf_node()?;
|
let chunk = offset_handler.first_leaf_node()?;
|
||||||
let chunk = self.a.update_cache(&other.a, cache, chunk)?;
|
let chunk = self.a.update_tree_hash_cache(&other.a, cache, chunk)?;
|
||||||
let chunk = self.b.update_cache(&other.b, cache, chunk)?;
|
let chunk = self.b.update_tree_hash_cache(&other.b, cache, chunk)?;
|
||||||
let chunk = self.c.update_cache(&other.c, cache, chunk)?;
|
let chunk = self.c.update_tree_hash_cache(&other.c, cache, chunk)?;
|
||||||
let _chunk = self.d.update_cache(&other.d, cache, chunk)?;
|
let _chunk = self.d.update_tree_hash_cache(&other.d, cache, chunk)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (&parent, children) in offset_handler.iter_internal_nodes().rev() {
|
for (&parent, children) in offset_handler.iter_internal_nodes().rev() {
|
||||||
@ -254,20 +254,20 @@ impl TreeHash for Outer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl CachedTreeHashSubTree<Outer> for Outer {
|
impl CachedTreeHashSubTree<Outer> for Outer {
|
||||||
fn new_cache(&self) -> Result<TreeHashCache, Error> {
|
fn new_tree_hash_cache(&self) -> Result<TreeHashCache, Error> {
|
||||||
let tree = TreeHashCache::from_leaves_and_subtrees(
|
let tree = TreeHashCache::from_leaves_and_subtrees(
|
||||||
self,
|
self,
|
||||||
vec![
|
vec![
|
||||||
self.a.new_cache()?,
|
self.a.new_tree_hash_cache()?,
|
||||||
self.b.new_cache()?,
|
self.b.new_tree_hash_cache()?,
|
||||||
self.c.new_cache()?,
|
self.c.new_tree_hash_cache()?,
|
||||||
],
|
],
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(tree)
|
Ok(tree)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn btree_overlay(&self, chunk_offset: usize) -> Result<BTreeOverlay, Error> {
|
fn tree_hash_cache_overlay(&self, chunk_offset: usize) -> Result<BTreeOverlay, Error> {
|
||||||
let mut lengths = vec![];
|
let mut lengths = vec![];
|
||||||
|
|
||||||
lengths.push(BTreeOverlay::new(&self.a, 0)?.total_nodes());
|
lengths.push(BTreeOverlay::new(&self.a, 0)?.total_nodes());
|
||||||
@ -277,7 +277,7 @@ impl CachedTreeHashSubTree<Outer> for Outer {
|
|||||||
BTreeOverlay::from_lengths(chunk_offset, lengths)
|
BTreeOverlay::from_lengths(chunk_offset, lengths)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_cache(
|
fn update_tree_hash_cache(
|
||||||
&self,
|
&self,
|
||||||
other: &Self,
|
other: &Self,
|
||||||
cache: &mut TreeHashCache,
|
cache: &mut TreeHashCache,
|
||||||
@ -288,9 +288,9 @@ impl CachedTreeHashSubTree<Outer> for Outer {
|
|||||||
// Skip past the internal nodes and update any changed leaf nodes.
|
// Skip past the internal nodes and update any changed leaf nodes.
|
||||||
{
|
{
|
||||||
let chunk = offset_handler.first_leaf_node()?;
|
let chunk = offset_handler.first_leaf_node()?;
|
||||||
let chunk = self.a.update_cache(&other.a, cache, chunk)?;
|
let chunk = self.a.update_tree_hash_cache(&other.a, cache, chunk)?;
|
||||||
let chunk = self.b.update_cache(&other.b, cache, chunk)?;
|
let chunk = self.b.update_tree_hash_cache(&other.b, cache, chunk)?;
|
||||||
let _chunk = self.c.update_cache(&other.c, cache, chunk)?;
|
let _chunk = self.c.update_tree_hash_cache(&other.c, cache, chunk)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (&parent, children) in offset_handler.iter_internal_nodes().rev() {
|
for (&parent, children) in offset_handler.iter_internal_nodes().rev() {
|
||||||
@ -341,7 +341,7 @@ fn partial_modification_to_inner_struct() {
|
|||||||
let mut cache_struct = TreeHashCache::new(&original_outer).unwrap();
|
let mut cache_struct = TreeHashCache::new(&original_outer).unwrap();
|
||||||
|
|
||||||
modified_outer
|
modified_outer
|
||||||
.update_cache(&original_outer, &mut cache_struct, 0)
|
.update_tree_hash_cache(&original_outer, &mut cache_struct, 0)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let modified_cache: Vec<u8> = cache_struct.into();
|
let modified_cache: Vec<u8> = cache_struct.into();
|
||||||
@ -395,7 +395,7 @@ fn partial_modification_to_outer() {
|
|||||||
let mut cache_struct = TreeHashCache::new(&original_outer).unwrap();
|
let mut cache_struct = TreeHashCache::new(&original_outer).unwrap();
|
||||||
|
|
||||||
modified_outer
|
modified_outer
|
||||||
.update_cache(&original_outer, &mut cache_struct, 0)
|
.update_tree_hash_cache(&original_outer, &mut cache_struct, 0)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let modified_cache: Vec<u8> = cache_struct.into();
|
let modified_cache: Vec<u8> = cache_struct.into();
|
||||||
@ -481,7 +481,7 @@ fn test_u64_vec_modifications(original: Vec<u64>, modified: Vec<u64>) {
|
|||||||
// Perform a differential hash
|
// Perform a differential hash
|
||||||
let mut cache_struct = TreeHashCache::from_bytes(original_cache.clone(), false).unwrap();
|
let mut cache_struct = TreeHashCache::from_bytes(original_cache.clone(), false).unwrap();
|
||||||
modified
|
modified
|
||||||
.update_cache(&original, &mut cache_struct, 0)
|
.update_tree_hash_cache(&original, &mut cache_struct, 0)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let modified_cache: Vec<u8> = cache_struct.into();
|
let modified_cache: Vec<u8> = cache_struct.into();
|
||||||
|
|
||||||
@ -586,7 +586,9 @@ fn large_vec_of_u64_builds() {
|
|||||||
fn test_inner_vec_modifications(original: Vec<Inner>, modified: Vec<Inner>, reference: Vec<u64>) {
|
fn test_inner_vec_modifications(original: Vec<Inner>, modified: Vec<Inner>, reference: Vec<u64>) {
|
||||||
let mut cache = TreeHashCache::new(&original).unwrap();
|
let mut cache = TreeHashCache::new(&original).unwrap();
|
||||||
|
|
||||||
modified.update_cache(&original, &mut cache, 0).unwrap();
|
modified
|
||||||
|
.update_tree_hash_cache(&original, &mut cache, 0)
|
||||||
|
.unwrap();
|
||||||
let modified_cache: Vec<u8> = cache.into();
|
let modified_cache: Vec<u8> = cache.into();
|
||||||
|
|
||||||
// Build the reference vec.
|
// Build the reference vec.
|
||||||
@ -947,12 +949,12 @@ fn generic_test(index: usize) {
|
|||||||
let mut cache_struct = TreeHashCache::from_bytes(cache.clone(), false).unwrap();
|
let mut cache_struct = TreeHashCache::from_bytes(cache.clone(), false).unwrap();
|
||||||
|
|
||||||
changed_inner
|
changed_inner
|
||||||
.update_cache(&inner, &mut cache_struct, 0)
|
.update_tree_hash_cache(&inner, &mut cache_struct, 0)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// assert_eq!(*cache_struct.hash_count, 3);
|
// assert_eq!(*cache_struct.hash_count, 3);
|
||||||
|
|
||||||
let new_cache: Vec<u8> = cache_struct.into();
|
let new_tree_hash_cache: Vec<u8> = cache_struct.into();
|
||||||
|
|
||||||
let data1 = int_to_bytes32(1);
|
let data1 = int_to_bytes32(1);
|
||||||
let data2 = int_to_bytes32(2);
|
let data2 = int_to_bytes32(2);
|
||||||
@ -965,7 +967,7 @@ fn generic_test(index: usize) {
|
|||||||
|
|
||||||
let expected = merkleize(join(data));
|
let expected = merkleize(join(data));
|
||||||
|
|
||||||
assert_eq!(expected, new_cache);
|
assert_eq!(expected, new_tree_hash_cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -39,9 +39,9 @@ fn should_skip_hashing(field: &syn::Field) -> bool {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Implements `ssz::Encodable` for some `struct`.
|
/// Implements `tree_hash::CachedTreeHashSubTree` for some `struct`.
|
||||||
///
|
///
|
||||||
/// Fields are encoded in the order they are defined.
|
/// Fields are hashed in the order they are defined.
|
||||||
#[proc_macro_derive(CachedTreeHashSubTree, attributes(tree_hash))]
|
#[proc_macro_derive(CachedTreeHashSubTree, attributes(tree_hash))]
|
||||||
pub fn subtree_derive(input: TokenStream) -> TokenStream {
|
pub fn subtree_derive(input: TokenStream) -> TokenStream {
|
||||||
let item = parse_macro_input!(input as DeriveInput);
|
let item = parse_macro_input!(input as DeriveInput);
|
||||||
@ -60,16 +60,12 @@ pub fn subtree_derive(input: TokenStream) -> TokenStream {
|
|||||||
|
|
||||||
let output = quote! {
|
let output = quote! {
|
||||||
impl tree_hash::CachedTreeHashSubTree<#name> for #name {
|
impl tree_hash::CachedTreeHashSubTree<#name> for #name {
|
||||||
fn item_type() -> tree_hash::ItemType {
|
fn new_tree_hash_cache(&self) -> Result<tree_hash::TreeHashCache, tree_hash::Error> {
|
||||||
tree_hash::ItemType::Composite
|
|
||||||
}
|
|
||||||
|
|
||||||
fn new_cache(&self) -> Result<tree_hash::TreeHashCache, tree_hash::Error> {
|
|
||||||
let tree = tree_hash::TreeHashCache::from_leaves_and_subtrees(
|
let tree = tree_hash::TreeHashCache::from_leaves_and_subtrees(
|
||||||
self,
|
self,
|
||||||
vec![
|
vec![
|
||||||
#(
|
#(
|
||||||
self.#idents_a.new_cache()?,
|
self.#idents_a.new_tree_hash_cache()?,
|
||||||
)*
|
)*
|
||||||
],
|
],
|
||||||
)?;
|
)?;
|
||||||
@ -77,7 +73,7 @@ pub fn subtree_derive(input: TokenStream) -> TokenStream {
|
|||||||
Ok(tree)
|
Ok(tree)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn btree_overlay(&self, chunk_offset: usize) -> Result<tree_hash::BTreeOverlay, tree_hash::Error> {
|
fn tree_hash_cache_overlay(&self, chunk_offset: usize) -> Result<tree_hash::BTreeOverlay, tree_hash::Error> {
|
||||||
let mut lengths = vec![];
|
let mut lengths = vec![];
|
||||||
|
|
||||||
#(
|
#(
|
||||||
@ -87,15 +83,7 @@ pub fn subtree_derive(input: TokenStream) -> TokenStream {
|
|||||||
tree_hash::BTreeOverlay::from_lengths(chunk_offset, lengths)
|
tree_hash::BTreeOverlay::from_lengths(chunk_offset, lengths)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn packed_encoding(&self) -> Result<Vec<u8>, tree_hash::Error> {
|
fn update_tree_hash_cache(
|
||||||
Err(tree_hash::Error::ShouldNeverBePacked(Self::item_type()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn packing_factor() -> usize {
|
|
||||||
1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update_cache(
|
|
||||||
&self,
|
&self,
|
||||||
other: &Self,
|
other: &Self,
|
||||||
cache: &mut tree_hash::TreeHashCache,
|
cache: &mut tree_hash::TreeHashCache,
|
||||||
@ -107,7 +95,7 @@ pub fn subtree_derive(input: TokenStream) -> TokenStream {
|
|||||||
{
|
{
|
||||||
let chunk = offset_handler.first_leaf_node()?;
|
let chunk = offset_handler.first_leaf_node()?;
|
||||||
#(
|
#(
|
||||||
let chunk = self.#idents_c.update_cache(&other.#idents_d, cache, chunk)?;
|
let chunk = self.#idents_c.update_tree_hash_cache(&other.#idents_d, cache, chunk)?;
|
||||||
)*
|
)*
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,3 +111,47 @@ pub fn subtree_derive(input: TokenStream) -> TokenStream {
|
|||||||
};
|
};
|
||||||
output.into()
|
output.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Implements `tree_hash::TreeHash` for some `struct`.
|
||||||
|
///
|
||||||
|
/// Fields are hashed in the order they are defined.
|
||||||
|
#[proc_macro_derive(TreeHash, attributes(tree_hash))]
|
||||||
|
pub fn tree_hash_derive(input: TokenStream) -> TokenStream {
|
||||||
|
let item = parse_macro_input!(input as DeriveInput);
|
||||||
|
|
||||||
|
let name = &item.ident;
|
||||||
|
|
||||||
|
let struct_data = match &item.data {
|
||||||
|
syn::Data::Struct(s) => s,
|
||||||
|
_ => panic!("tree_hash_derive only supports structs."),
|
||||||
|
};
|
||||||
|
|
||||||
|
let idents = get_hashable_named_field_idents(&struct_data);
|
||||||
|
|
||||||
|
let output = quote! {
|
||||||
|
impl tree_hash::TreeHash for #name {
|
||||||
|
fn tree_hash_type() -> tree_hash::TreeHashType {
|
||||||
|
tree_hash::TreeHashType::Composite
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
|
||||||
|
unreachable!("Struct should never be packed.")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tree_hash_packing_factor() -> usize {
|
||||||
|
unreachable!("Struct should never be packed.")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tree_hash_root(&self) -> Vec<u8> {
|
||||||
|
let mut leaves = Vec::with_capacity(4 * tree_hash::HASHSIZE);
|
||||||
|
|
||||||
|
#(
|
||||||
|
leaves.append(&mut self.#idents.tree_hash_root());
|
||||||
|
)*
|
||||||
|
|
||||||
|
tree_hash::efficient_merkleize(&leaves)[0..32].to_vec()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
output.into()
|
||||||
|
}
|
||||||
|
@ -1,9 +1,71 @@
|
|||||||
use tree_hash_derive::CachedTreeHashSubTree;
|
use tree_hash::CachedTreeHashSubTree;
|
||||||
|
use tree_hash_derive::{CachedTreeHashSubTree, TreeHash};
|
||||||
|
|
||||||
#[derive(Clone, Debug, CachedTreeHashSubTree)]
|
#[derive(Clone, Debug, TreeHash, CachedTreeHashSubTree)]
|
||||||
pub struct Inner {
|
pub struct Inner {
|
||||||
pub a: u64,
|
pub a: u64,
|
||||||
pub b: u64,
|
pub b: u64,
|
||||||
pub c: u64,
|
pub c: u64,
|
||||||
pub d: u64,
|
pub d: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_standard_and_cached<T>(original: &T, modified: &T)
|
||||||
|
where
|
||||||
|
T: CachedTreeHashSubTree<T>,
|
||||||
|
{
|
||||||
|
let mut cache = original.new_tree_hash_cache().unwrap();
|
||||||
|
|
||||||
|
let standard_root = original.tree_hash_root();
|
||||||
|
let cached_root = cache.root().unwrap().to_vec();
|
||||||
|
assert_eq!(standard_root, cached_root);
|
||||||
|
|
||||||
|
// Test after a modification
|
||||||
|
modified
|
||||||
|
.update_tree_hash_cache(&original, &mut cache, 0)
|
||||||
|
.unwrap();
|
||||||
|
let standard_root = modified.tree_hash_root();
|
||||||
|
let cached_root = cache.root().unwrap().to_vec();
|
||||||
|
assert_eq!(standard_root, cached_root);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn inner_standard_vs_cached() {
|
||||||
|
let original = Inner {
|
||||||
|
a: 1,
|
||||||
|
b: 2,
|
||||||
|
c: 3,
|
||||||
|
d: 4,
|
||||||
|
};
|
||||||
|
let modified = Inner {
|
||||||
|
b: 42,
|
||||||
|
..original.clone()
|
||||||
|
};
|
||||||
|
|
||||||
|
test_standard_and_cached(&original, &modified);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, TreeHash, CachedTreeHashSubTree)]
|
||||||
|
pub struct Uneven {
|
||||||
|
pub a: u64,
|
||||||
|
pub b: u64,
|
||||||
|
pub c: u64,
|
||||||
|
pub d: u64,
|
||||||
|
pub e: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn uneven_standard_vs_cached() {
|
||||||
|
let original = Uneven {
|
||||||
|
a: 1,
|
||||||
|
b: 2,
|
||||||
|
c: 3,
|
||||||
|
d: 4,
|
||||||
|
e: 5,
|
||||||
|
};
|
||||||
|
let modified = Uneven {
|
||||||
|
e: 42,
|
||||||
|
..original.clone()
|
||||||
|
};
|
||||||
|
|
||||||
|
test_standard_and_cached(&original, &modified);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user