Merkle Trees in Blockchain

Blockchain Merkle Trees

You must have heard the word “merkle tree root” in the previous sections of this course. For a minute try to recollect!

Okay, I had first discussed this while explaining the block header where the merkle tree root is stored.

What is a merkle tree?

Also called a hash tree, a merkle tree is a bottom up tree data structure in which every leaf node represents the hash of a block; and every non-leaf node represents the hash of its children.

Merkle trees are binary in nature meaning they require an even number of nodes. In case the number of transactions are odd, the last hash is duplicated once to create an even number.

Let’s consider an example – 

Merkle Tree

If you look at this image, you will notice that H7 is a hash between H5 and H6. H5 in turn is a hash of H1 and H2 which are hashes of Block 1 and Block 2 respectively.

Similarly, H6 is a hash of H3 and H4 which are hashes of Blocks 3 and 4 respectively.

Now you can imagine this process for larger number of blocks, the hashing of blocks and hashing of hashes continues till the point there is only one node present at the top. The single node at the top is called as merkle tree root or just merkle root which is stored in the block header.

This makes blockchain highly secure. How, we will see in the next section.

How Does a Merkle Tree Benefit the Blockchain?

Merkle trees do a couple of things that makes the blockchain a powerful technology to reckon with.

Data Integrity

Data integrity is important. One of the biggest strength of blockchain lies in the fact that it ensures data integrity. Merkle trees play an important role here.

Merkle root contains the summary of all transactions in the nodes that directly or indirectly form it. In our previous example above, the merkle root stores the summary of all the four blocks.

Now imagine there is a bad guy that wants to create havoc in the system by changing one of the transactions in one of the blocks. Once that happens, every node will have to be updated till the merkle root. We are not talking of updating 4 or 5 nodes here but about thousands of nodes. That is impossible practically because it requires massive power. 

Thus data integrity is ensured.

Data Verification and Validation

Data validation is easy using merkle tree and this extends to the blockchain as well. Anyone who is authorized to read data on the network, can validate the data in it. This makes trustless transactions possible on the blockchain.

The ability to prove that a log is complete is important for the blockchain to function properly. In order to prove that a log is consistent, it is important to prove that no previous transactions have been tampered with.

The Block in Blockchain

Now that you know about merkle tree, here’s how the block in a blockchain looks.

Merkle Tree and Block in Bitcoin (from Bitcoin whitepaper)

Merkle trees are a very important component of blockchains and allows them to function by providing the powerful features of immutability and data integrity.

You should understand the role that merkle trees play in distributed networks and their underlying technology of cryptographic hash functions is crucial to grasping the basic concepts within cryptocurrencies as they continue to develop into larger and more complex systems.

Merkle Trees in Blockchain

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top