From 0287876987e798c8a640985e68a8d9bfcc5f1035 Mon Sep 17 00:00:00 2001 From: Konstantinos Chalkias Date: Wed, 8 Feb 2017 15:21:40 +0000 Subject: [PATCH] minor changes in for/while loops. Sanity check for even number of nodes (in buildMerkleTree) has now been moved before entering the loop. --- core/src/main/kotlin/net/corda/core/crypto/MerkleTree.kt | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/core/src/main/kotlin/net/corda/core/crypto/MerkleTree.kt b/core/src/main/kotlin/net/corda/core/crypto/MerkleTree.kt index 817ef515e6..85e6911f8d 100644 --- a/core/src/main/kotlin/net/corda/core/crypto/MerkleTree.kt +++ b/core/src/main/kotlin/net/corda/core/crypto/MerkleTree.kt @@ -32,9 +32,8 @@ sealed class MerkleTree(val hash: SecureHash) { var n = allLeavesHashes.size if (isPow2(n)) return allLeavesHashes val paddedHashes = ArrayList(allLeavesHashes) - while (!isPow2(n)) { + while (!isPow2(n++)) { paddedHashes.add(SecureHash.zeroHash) - n++ } return paddedHashes } @@ -51,16 +50,14 @@ sealed class MerkleTree(val hash: SecureHash) { return lastNodesList[0] //Root reached. } else { val newLevelHashes: MutableList = ArrayList() - var i = 0 val n = lastNodesList.size - while (i < n) { + require((n and 1) == 0) { "Sanity check: number of nodes should be even." } + for (i in 0..n-2 step 2) { val left = lastNodesList[i] - require(i+1 <= n-1) { "Sanity check: number of nodes should be even." } val right = lastNodesList[i+1] val newHash = left.hash.hashConcat(right.hash) val combined = Node(newHash, left, right) newLevelHashes.add(combined) - i += 2 } return buildMerkleTree(newLevelHashes) }