A blockchain is made up of blocks.
But, it is what is inside these blocks that form the foundation of the entire technology behind blockchain.
We know that when a block is being generated by a miner, the details of a large set of up to 2020 transactions chosen by that miner, along with their Coinbase address ( the wallet address of the miner who has mined the block, so that their reward for mining the block may be paid there.) are collectively used to create a single block. But how are all these transactions put into a single block? And how are they found later?
This is where the concept of Merkle trees comes into action.
To understand Merkle trees, let us first look at the concept of Hashing.
Hashing and hash values:
The concept of hashing basically involves taking a long word of length L, and representing it using a shorter word of Length S, (where L>S). The value stored into the S length word is termed as the hash value for the original word. Basically, it’s like creating a nickname for a person!
There exist several algorithms that are used to convert letters or numbers into hash values of fixed length. These hash values can be combined and run through the same algorithm again to get another hash value.
For example, if we have two names: Thomas And Benjamin. We can use a hash algorithm to create a hash value for both these values- Tom and Ben. Now, we combine these two values together to get TomBen. Passing this value through a hash value could give us a new hash value, that can represent both – TB.
A Merkle Tree is essentially a hash tree which uses the hash values of the various child transactions and combines them to give the parent hash value.
This process is repeated multiple times until a single hash value that can represent the entire tree at the root node is generated.
The root node element formed by combining the hash values of all the child node hash values is called the Merkle Root.
The Merkle root can be used along with some values to generate the entire tree as and when necessary. This allows us to identify every transaction stored on the single block and to verify if any transaction is a part of the block with a certain Merkle root.
For example, let us consider a Merkle tree of colours.
We can say that if there are 4 leaf nodes- made up of the colours: Red, White, Yellow, and Blue.
Let us assume the hash algorithm gets hashes by mixing the colours up! So, we’d get Pink and Green as the next level nodes.
The Merkle root would be the combination of Pink and Green- Which gives us brown!
So, if we were asked to verify the existence of green in the system, and we were told about the existence of Blue, pink and Yellow- then all we need is the Merkle root- which is brown.
So how would we go about solving this?
We know that brown is made using Blue and Pink. We also know that to make Blue, we need to use Green Alongside Yellow (which we already know exists).
Thus, by looking back, we are able to acknowledge the existence of Green in the system.
In a similar manner, it is possible for us to check the existence of any transaction within a block using the Merkle root, and the details of a few other child nodes already in the system.
Let’s look at another case!
If we suddenly decide to change the colour from red to Purple.
This would change the colour of the node above since the hashing algorithm is mixing the two colours. So, this would also change the Merkle root! Thus, any passer-by would notice that the root value (or in this case, colour) has changed!
Thus, the Merkle root allows us to identify any changes that are attempted on any transaction on the block.
Now, if some attacker tries to change the values of any transaction, the hash value will also change. Thus, the of hashes of the nodes above will also change and a different, incorrect root will be produced- thus telling the external viewers of the blockchain, that somebody has attacked it.
And That’s how Merkle Trees work!
Blockchain 101 is a series of posts that aim to make the concepts of blockchain easy and understandable.
Stay tuned for more!