question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Merkle tree construction: single leaf is automatically promoted to root.

See original GitHub issue

I 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:

  1. getMerkleTree (here) receives a list of SecureHash-es.

  2. 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.

  3. 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
r3jirabotcommented, Sep 1, 2020

Automatically created Jira issue: CORDA-4019

0reactions
mvdboscommented, Apr 13, 2021

Fixed by #6895

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Ultimate Merkle Tree Guide in Solidity
At the root of the Merkle tree is the root hash. It's created by hashing all its original values as leaf nodes. Now...
Read more >
Merkle Tree in Blockchain: What is it and How does it work
Merkle trees are made by hashing pairs of nodes repeatedly until only one hash remains; this hash is known as the Merkle Root...
Read more >
Can we place the WOTS scheme into a Merkle tree structure?
Yes, any one time scheme (such as lamport, WOTS) can be turned many time scheme by placing the public keys into a merkle...
Read more >
Merkle Tree - ThornyDev
Merkle trees are typically implemented as binary trees where each non-leaf node is a hash of the two nodes below it. The leaves...
Read more >
Merkle Tree Construction and Proof-of-Inclusion - derp turkey
This type of tree uses hashes for the leaves of the tree. Interior nodes are constructed by concatenating the hashes of the children...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found