shuijian-xu / bitcoin

0 stars 0 forks source link

How to construct the Merkle tree? #224

Open shuijian-xu opened 4 years ago

shuijian-xu commented 4 years ago

To construct the Merkle tree, we follow this algorithm:

  1. Hash all the items of the ordered list with the provided hash function.

  2. If there is exactly 1 hash, we are done.

  3. Otherwise, if there is an odd number of hashes, we duplicate the last hash in the list and add it to the end so that we have an even number of hashes.

  4. We pair the hashes in order and hash the concatenation to get the parent level, which should have half the number of hashes.

  5. Go to #2.