diff --git a/eth2/utils/ssz/src/cached_tree_hash.rs b/eth2/utils/ssz/src/cached_tree_hash.rs index 0889718a2..9960d1f6a 100644 --- a/eth2/utils/ssz/src/cached_tree_hash.rs +++ b/eth2/utils/ssz/src/cached_tree_hash.rs @@ -195,15 +195,18 @@ impl TreeHashCache { Ok(()) } - pub fn chunk_equals(&mut self, chunk: usize, other: &[u8]) -> Result { + pub fn get_chunk(&self, chunk: usize) -> Result<&[u8], Error> { let start = chunk * BYTES_PER_CHUNK; let end = start + BYTES_PER_CHUNK; Ok(self .cache .get(start..end) - .ok_or_else(|| Error::NoModifiedFieldForChunk(chunk))? - == other) + .ok_or_else(|| Error::NoModifiedFieldForChunk(chunk))?) + } + + pub fn chunk_equals(&mut self, chunk: usize, other: &[u8]) -> Result { + Ok(self.get_chunk(chunk)? == other) } pub fn changed(&self, chunk: usize) -> Result { @@ -218,15 +221,11 @@ impl TreeHashCache { } pub fn hash_children(&self, children: (&usize, &usize)) -> Result, Error> { - let start = children.0 * BYTES_PER_CHUNK; - let end = start + BYTES_PER_CHUNK * 2; + let mut child_bytes = Vec::with_capacity(BYTES_PER_CHUNK * 2); + child_bytes.append(&mut self.get_chunk(*children.0)?.to_vec()); + child_bytes.append(&mut self.get_chunk(*children.1)?.to_vec()); - let children = &self - .cache - .get(start..end) - .ok_or_else(|| Error::NoChildrenForHashing((*children.0, *children.1)))?; - - Ok(hash(children)) + Ok(hash(&child_bytes)) } pub fn into_merkle_tree(self) -> Vec { diff --git a/eth2/utils/ssz/src/cached_tree_hash/impls.rs b/eth2/utils/ssz/src/cached_tree_hash/impls.rs index 58343de3a..c6cd05cd9 100644 --- a/eth2/utils/ssz/src/cached_tree_hash/impls.rs +++ b/eth2/utils/ssz/src/cached_tree_hash/impls.rs @@ -133,14 +133,13 @@ where // The item existed in the previous list and exsits in the current list. (Some(old), Some(new)) => { new.cached_hash_tree_root(old, cache, *start_chunk)?; - }, + } // The item didn't exist in the old list and doesn't exist in the new list, // nothing to do. - (None, None) => {}, - _ => panic!("variable sized lists not implemented") + (None, None) => {} + _ => panic!("variable sized lists not implemented"), }; } - // this thing } }