From 4e0d92a6ec7920d1347f92dda91ba4fa97740ba0 Mon Sep 17 00:00:00 2001 From: Sergey Vasilyev Date: Tue, 28 Apr 2020 01:12:21 +0300 Subject: [PATCH] Merkle tree fails to compute the root when there's no need in dummy nodes - fix --- crypto-primitives/src/merkle_tree/mod.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/crypto-primitives/src/merkle_tree/mod.rs b/crypto-primitives/src/merkle_tree/mod.rs index 366bd1a..1ea81b2 100644 --- a/crypto-primitives/src/merkle_tree/mod.rs +++ b/crypto-primitives/src/merkle_tree/mod.rs @@ -161,14 +161,16 @@ impl MerkleHashTree

{ let mut cur_height = tree_height; let mut padding_tree = Vec::new(); let mut cur_hash = tree[0].clone(); - while cur_height < (Self::HEIGHT - 1) as usize { - cur_hash = hash_inner_node::(¶meters, &cur_hash, &empty_hash, &mut buffer)?; - padding_tree.push((cur_hash.clone(), empty_hash.clone())); - cur_height += 1; - } - - let root_hash = hash_inner_node::(¶meters, &cur_hash, &empty_hash, &mut buffer)?; - + let root_hash = if cur_height < Self::HEIGHT as usize { + while cur_height < (Self::HEIGHT - 1) as usize { + cur_hash = hash_inner_node::(¶meters, &cur_hash, &empty_hash, &mut buffer)?; + padding_tree.push((cur_hash.clone(), empty_hash.clone())); + cur_height += 1; + } + hash_inner_node::(¶meters, &cur_hash, &empty_hash, &mut buffer)? + } else { + cur_hash + }; end_timer!(new_time); Ok(MerkleHashTree {