From e6c33e1b60e560fb7539c0965f441dccb6af277d Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 15 Apr 2019 11:44:44 +1000 Subject: [PATCH] Remove child_nodes method from CachedTreeHash --- eth2/utils/tree_hash/src/impls.rs | 14 ++--------- eth2/utils/tree_hash/src/lib.rs | 2 -- eth2/utils/tree_hash/tests/tests.rs | 37 ++++++----------------------- 3 files changed, 9 insertions(+), 44 deletions(-) diff --git a/eth2/utils/tree_hash/src/impls.rs b/eth2/utils/tree_hash/src/impls.rs index 9149cf8aa..4349d73d8 100644 --- a/eth2/utils/tree_hash/src/impls.rs +++ b/eth2/utils/tree_hash/src/impls.rs @@ -14,18 +14,14 @@ impl CachedTreeHash for u64 { )?) } - fn btree_overlay(&self, _chunk_offset: usize) -> Result { - Err(Error::ShouldNotProduceBTreeOverlay) + fn btree_overlay(&self, chunk_offset: usize) -> Result { + BTreeOverlay::from_lengths(chunk_offset, vec![1]) } fn num_bytes(&self) -> usize { 8 } - fn num_child_nodes(&self) -> usize { - 0 - } - fn packed_encoding(&self) -> Vec { ssz_encode(self) } @@ -72,7 +68,6 @@ where } fn btree_overlay(&self, chunk_offset: usize) -> Result { - // let lengths = match T::item_type() { ItemType::Basic => vec![1; self.len() / T::packing_factor()], ItemType::Composite | ItemType::List => { @@ -89,11 +84,6 @@ where BTreeOverlay::from_lengths(chunk_offset, lengths) } - fn num_child_nodes(&self) -> usize { - // TODO - 42 - } - fn num_bytes(&self) -> usize { self.iter().fold(0, |acc, item| acc + item.num_bytes()) } diff --git a/eth2/utils/tree_hash/src/lib.rs b/eth2/utils/tree_hash/src/lib.rs index e356210a4..4e1cff0e8 100644 --- a/eth2/utils/tree_hash/src/lib.rs +++ b/eth2/utils/tree_hash/src/lib.rs @@ -47,8 +47,6 @@ pub trait CachedTreeHash: Debug { fn btree_overlay(&self, chunk_offset: usize) -> Result; - fn num_child_nodes(&self) -> usize; - fn packed_encoding(&self) -> Vec; fn packing_factor() -> usize; diff --git a/eth2/utils/tree_hash/tests/tests.rs b/eth2/utils/tree_hash/tests/tests.rs index af4204cc2..a315feeed 100644 --- a/eth2/utils/tree_hash/tests/tests.rs +++ b/eth2/utils/tree_hash/tests/tests.rs @@ -47,26 +47,14 @@ impl CachedTreeHash for Inner { fn btree_overlay(&self, chunk_offset: usize) -> Result { let mut lengths = vec![]; - lengths.push(self.a.num_child_nodes() + 1); - lengths.push(self.b.num_child_nodes() + 1); - lengths.push(self.c.num_child_nodes() + 1); - lengths.push(self.d.num_child_nodes() + 1); + lengths.push(BTreeOverlay::new(&self.a, 0)?.total_nodes()); + lengths.push(BTreeOverlay::new(&self.b, 0)?.total_nodes()); + lengths.push(BTreeOverlay::new(&self.c, 0)?.total_nodes()); + lengths.push(BTreeOverlay::new(&self.d, 0)?.total_nodes()); BTreeOverlay::from_lengths(chunk_offset, lengths) } - fn num_child_nodes(&self) -> usize { - let mut children = 0; - let leaves = 4; - - children += self.a.num_child_nodes(); - children += self.b.num_child_nodes(); - children += self.c.num_child_nodes(); - children += self.d.num_child_nodes(); - - num_nodes(leaves) + children - 1 - } - fn packed_encoding(&self) -> Vec { panic!("Struct should never be packed") } @@ -135,23 +123,12 @@ impl CachedTreeHash for Outer { bytes } - fn num_child_nodes(&self) -> usize { - let mut children = 0; - let leaves = 3; - - children += self.a.num_child_nodes(); - children += self.b.num_child_nodes(); - children += self.c.num_child_nodes(); - - num_nodes(leaves) + children - 1 - } - fn btree_overlay(&self, chunk_offset: usize) -> Result { let mut lengths = vec![]; - lengths.push(self.a.num_child_nodes() + 1); - lengths.push(self.b.num_child_nodes() + 1); - lengths.push(self.c.num_child_nodes() + 1); + lengths.push(BTreeOverlay::new(&self.a, 0)?.total_nodes()); + lengths.push(BTreeOverlay::new(&self.b, 0)?.total_nodes()); + lengths.push(BTreeOverlay::new(&self.c, 0)?.total_nodes()); BTreeOverlay::from_lengths(chunk_offset, lengths) }