Fix tree hash bug

This commit is contained in:
Paul Hauner 2019-04-22 16:58:40 +10:00
parent 7c64a5a21b
commit 2f69185ccb
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
2 changed files with 13 additions and 3 deletions

View File

@ -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))

View File

@ -163,7 +163,7 @@ fn test_vec() {
}
#[test]
fn test_nested_list() {
fn test_nested_list_of_u64() {
let original: Vec<Vec<u64>> = 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);