diff --git a/eth2/utils/tree_hash/src/cached_tree_hash/resize.rs b/eth2/utils/tree_hash/src/cached_tree_hash/resize.rs index 44b3f0ea5..c87746654 100644 --- a/eth2/utils/tree_hash/src/cached_tree_hash/resize.rs +++ b/eth2/utils/tree_hash/src/cached_tree_hash/resize.rs @@ -1,4 +1,5 @@ use super::*; +use std::cmp::min; /// New vec is bigger than old vec. pub fn grow_merkle_cache( @@ -100,8 +101,15 @@ pub fn shrink_merkle_cache( ) }; - to_byte_slice.copy_from_slice(from_byte_slice.get(0..to_byte_slice.len())?); - to_flag_slice.copy_from_slice(from_flag_slice.get(0..to_flag_slice.len())?); + let num_bytes = min(from_byte_slice.len(), to_byte_slice.len()); + let num_flags = min(from_flag_slice.len(), to_flag_slice.len()); + + to_byte_slice + .get_mut(0..num_bytes)? + .copy_from_slice(from_byte_slice.get(0..num_bytes)?); + to_flag_slice + .get_mut(0..num_flags)? + .copy_from_slice(from_flag_slice.get(0..num_flags)?); } Some((bytes, flags)) diff --git a/eth2/utils/tree_hash/tests/tests.rs b/eth2/utils/tree_hash/tests/tests.rs index 726ce5626..27a20cd77 100644 --- a/eth2/utils/tree_hash/tests/tests.rs +++ b/eth2/utils/tree_hash/tests/tests.rs @@ -163,7 +163,7 @@ fn test_vec() { } #[test] -fn test_nested_list() { +fn test_nested_list_of_u64() { let original: Vec> = vec![vec![1]]; let modified = vec![ @@ -175,6 +175,8 @@ fn test_nested_list() { vec![], vec![vec![1, 2], vec![3], vec![4, 5, 6, 7, 8]], vec![], + vec![vec![1], vec![2], vec![3]], + vec![vec![1, 2, 3, 4, 5, 6], vec![1, 2, 3, 4, 5, 6, 7]], ]; test_routine(original, modified);