From ec43a4085c8f8917f9e5c3b010797460947c11a4 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 22 Apr 2019 21:31:39 +1000 Subject: [PATCH] Introduce failing test --- .../tree_hash/src/cached_tree_hash/impls.rs | 4 ++ .../src/cached_tree_hash/impls/vec.rs | 11 +++-- eth2/utils/tree_hash/tests/tests.rs | 43 +++++++++++++++++++ eth2/utils/tree_hash_derive/src/lib.rs | 4 ++ 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/eth2/utils/tree_hash/src/cached_tree_hash/impls.rs b/eth2/utils/tree_hash/src/cached_tree_hash/impls.rs index ece025ccd..64fab5cf8 100644 --- a/eth2/utils/tree_hash/src/cached_tree_hash/impls.rs +++ b/eth2/utils/tree_hash/src/cached_tree_hash/impls.rs @@ -19,9 +19,13 @@ impl CachedTreeHashSubTree for u64 { let leaf = merkleize(self.to_le_bytes().to_vec()); cache.maybe_update_chunk(cache.chunk_index, &leaf)?; + dbg!(cache.overlay_index); + cache.chunk_index += 1; cache.overlay_index += 1; + dbg!(cache.overlay_index); + Ok(()) } } diff --git a/eth2/utils/tree_hash/src/cached_tree_hash/impls/vec.rs b/eth2/utils/tree_hash/src/cached_tree_hash/impls/vec.rs index d8d503af9..43c9ce7e3 100644 --- a/eth2/utils/tree_hash/src/cached_tree_hash/impls/vec.rs +++ b/eth2/utils/tree_hash/src/cached_tree_hash/impls/vec.rs @@ -63,6 +63,11 @@ where let new_overlay = BTreeOverlay::new(self, cache.chunk_index)?; let old_overlay = cache.get_overlay(cache.overlay_index, cache.chunk_index)?; + dbg!(cache.overlay_index); + + // dbg!(&new_overlay); + // dbg!(&old_overlay); + // If the merkle tree required to represent the new list is of a different size to the one // required for the previous list, then update our cache. // @@ -106,7 +111,6 @@ where TreeHashType::Container | TreeHashType::List | TreeHashType::Vector => { let mut local_overlay_index = cache.overlay_index; - for i in 0..new_overlay.num_leaf_nodes() { cache.overlay_index = local_overlay_index; @@ -122,7 +126,6 @@ where (Some(_old), Some(new)) => { cache.chunk_index = new.start; - self[i].update_tree_hash_cache(cache)?; local_overlay_index += 1; @@ -131,7 +134,7 @@ where // // Viz., the list has been shortened. (Some(old), None) => { - if new_overlay.num_items == 0 { + if new_overlay.num_items == 0 { // In this case, the list has been made empty and we should make // this node padding. cache.maybe_update_chunk(new_overlay.root(), &[0; HASHSIZE])?; @@ -188,6 +191,8 @@ where cache.chunk_index = new_overlay.next_node(); + dbg!(&cache.overlay_index); + Ok(()) } } diff --git a/eth2/utils/tree_hash/tests/tests.rs b/eth2/utils/tree_hash/tests/tests.rs index 27a20cd77..d4897d2c0 100644 --- a/eth2/utils/tree_hash/tests/tests.rs +++ b/eth2/utils/tree_hash/tests/tests.rs @@ -177,6 +177,47 @@ fn test_nested_list_of_u64() { vec![], vec![vec![1], vec![2], vec![3]], vec![vec![1, 2, 3, 4, 5, 6], vec![1, 2, 3, 4, 5, 6, 7]], + vec![vec![], vec![], vec![]], + vec![vec![0, 0, 0], vec![0], vec![0]], + ]; + + test_routine(original, modified); +} + +#[test] +fn test_list_of_struct_with_vec() { + let a = StructWithVec { + a: 42, + b: Inner { + a: 12, + b: 13, + c: 14, + d: 15, + }, + c: vec![1, 2, 3, 4, 5], + }; + let b = StructWithVec { + c: vec![], + ..a.clone() + }; + let c = StructWithVec { + b: Inner { + a: 99, + b: 100, + c: 101, + d: 102, + }, + ..a.clone() + }; + let d = StructWithVec { a: 0, ..a.clone() }; + + let original: Vec = vec![a.clone(), c.clone()]; + + let modified = vec![ + vec![a.clone(), c.clone()], + // vec![a.clone(), b.clone(), c.clone(), d.clone()], + // vec![b.clone(), a.clone(), c.clone(), d.clone()], + vec![], ]; test_routine(original, modified); @@ -255,6 +296,8 @@ impl CachedTreeHashSubTree for Inner { self.c.update_tree_hash_cache(cache)?; self.d.update_tree_hash_cache(cache)?; + dbg!(cache.overlay_index); + // Iterate through the internal nodes, updating them if their children have changed. cache.update_internal_nodes(&overlay)?; diff --git a/eth2/utils/tree_hash_derive/src/lib.rs b/eth2/utils/tree_hash_derive/src/lib.rs index d0a9e7a4e..6160913ee 100644 --- a/eth2/utils/tree_hash_derive/src/lib.rs +++ b/eth2/utils/tree_hash_derive/src/lib.rs @@ -85,6 +85,8 @@ pub fn subtree_derive(input: TokenStream) -> TokenStream { fn update_tree_hash_cache(&self, cache: &mut TreeHashCache) -> Result<(), Error> { let overlay = BTreeOverlay::new(self, cache.chunk_index)?; + println!("start derive - cache.overlay_index: {}", cache.overlay_index); + // Skip the chunk index to the first leaf node of this struct. cache.chunk_index = overlay.first_leaf_node(); // Skip the overlay index to the first leaf node of this struct. @@ -95,6 +97,8 @@ pub fn subtree_derive(input: TokenStream) -> TokenStream { self.#idents_c.update_tree_hash_cache(cache)?; )* + println!("end derive - cache.overlay_index: {}", cache.overlay_index); + // Iterate through the internal nodes, updating them if their children have changed. cache.update_internal_nodes(&overlay)?;