Merkle tree construction: single leaf is automatically promoted to root.
See original GitHub issueI would like to draw your attention to how Merkle tree is constructed when from a list of SecureHash
-es in the case when this list contains a single element. In such a case any leaf automatically becomes the Merkle root.
Step by step:
-
getMerkleTree
(here) receives a list ofSecureHash
-es. -
padWithZeroes
(here) ensures that the list contains such a number of elements which is a power of 2. Importantly 1 is a power of 2, i.e., 2^0 = 1. That is, no padding occurs. -
buildMerkleTree
(here) is executed with a single element list which means, that the only leaf automatically becomes the root of the Merkle tree, i.e.,
return if (lastNodesList.size == 1) {
lastNodesList[0] // Root reached.
}
This behaviour clearly contradicts the documentation, in particular this image, see Attachment, Notary, TimeWindow groups.
A technically simple fix can align the code with the documentation and the actual behaviour: Change this conditional
if (isPow2(n)) return allLeavesHashes
to
if (isPow2(n) && n != 1) return allLeavesHashes
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Automatically created Jira issue: CORDA-4019
Fixed by #6895