From d0e5e2716270b623e98db7f553d309da359e85c3 Mon Sep 17 00:00:00 2001 From: Ismail Khoffi Date: Sat, 9 Nov 2019 13:26:02 +0100 Subject: [PATCH] use Vec> with inner recursion method --- tendermint/src/merkle.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tendermint/src/merkle.rs b/tendermint/src/merkle.rs index 3492642c3..accbc1cae 100644 --- a/tendermint/src/merkle.rs +++ b/tendermint/src/merkle.rs @@ -10,14 +10,19 @@ pub type Hash = [u8; HASH_SIZE]; /// Compute a simple Merkle root from the arbitrary byte arrays pub fn simple_hash_from_byte_slices(byte_slices: Vec>) -> Hash { + simple_hash_from_byte_slices_inner(byte_slices.as_slice()) +} + +// recurse into subtrees +fn simple_hash_from_byte_slices_inner(byte_slices: &[Vec]) -> Hash { let length = byte_slices.len(); match length { 0 => [0; HASH_SIZE], - 1 => leaf_hash(byte_slices[0].to_vec()), + 1 => leaf_hash(byte_slices[0].as_slice()), _ => { let k = get_split_point(length); - let left = simple_hash_from_byte_slices(byte_slices[0..k].to_vec()); - let right = simple_hash_from_byte_slices(byte_slices[k..].to_vec()); + let left = simple_hash_from_byte_slices_inner(&byte_slices[..k]); + let right = simple_hash_from_byte_slices_inner(&byte_slices[k..]); inner_hash(&left, &right) } } @@ -34,11 +39,11 @@ fn get_split_point(length: usize) -> usize { } // tmhash(0x00 || leaf) -fn leaf_hash(bytes: Vec) -> Hash { +fn leaf_hash(bytes: &[u8]) -> Hash { // make a new array starting with 0 and copy in the bytes let mut leaf_bytes = Vec::with_capacity(bytes.len() + 1); leaf_bytes.push(0x00); - leaf_bytes.extend_from_slice(&bytes); + leaf_bytes.extend_from_slice(bytes); // hash it ! let digest = Sha256::digest(&leaf_bytes);