Merkle Tree in Blockchain
Merkle Tree in Blockchain
• The majority of hash tree implementations are binary (each node has
two child nodes), but they can also have many more child nodes.
Need of Merkel Tree
• Merkle trees, also known as Binary hash trees, are a prevalent sort of
data structure in computer science.
• In bitcoin and other cryptocurrencies, they're used to encrypt
blockchain data more efficiently and securely.
• It enables quick and secure content verification across big datasets
and verifies the consistency and content of the data.
• As mentioned earlier, each block is supposed to hold a certain number of transactions.
• Now the question arises, how to store these transactions within a block?
• One approach can be to form a hash pointer-based linked list of transactions and store
this complete linked list in a block.
• However, when we put this approach into perspective, it does not seem practical to
store a huge list of hundreds of transactions.
• What if there is a need to find whether a particular transaction belongs to a block?
Then we will have to traverse the blocks one by one and within each block traverse the
linked list of transactions.
• This is a huge overhead and can reduce the efficiency of the blockchain.
• Now, this is where the Merkle tree comes into the picture.
• Merkle tree is a per-block tree of all the transactions that are included in the block.
• It allows us to have a hash/digest of all transactions and provides proof of membership
in a time-efficient manner.
• what is this block header?
• A block is composed of a header and a body. The block header
contains Merkel root, Timestamp, Block Version number (indicates
which set of block validation rules to follow), Difficulty Target, Nonce,
and Previous Hash. On the other hand, the block body contains all
confirmed transactions within the block.
• Block header: The header data contains metadata of the block, i.e
information about the block itself. The contents of the block header
include-
• Hash of the previous block header
• Hash of the current block
• Timestamp
• Cryptographic nonce
• Merkle root
What Is a Merkle Root?
• A hash function maps any type of arbitrary data of any length to a fixed-
size output. It is commonly used in cryptography since it is a cryptographic
function.
• They are efficient and are well-known for one property: they are
irreversible. It's a one-way function that's only meant to work in one
direction.
• Some of the Hash families available are Message Direct (MD), Secure Hash
Function (SHF), and RIPE Message Direct (RIPEMD).
• Now, take an example, if you use the SHA256 hash algorithm and pass
101Blockchains as input, you will get the following output
fbffd63a60374a31aa9811cbc80b577e23925a5874e86a17f712bab874f33ac9
Working of Merkle Trees
• Hash A
• Hash B
• Hash C
• Hash D
• The hashes are paired together, resulting in:
• Hash AB
• and
• Hash CD
• And therefore, your Merkle Root is formed by combining these two
hashes: Hash ABCD.
Benefits of Merkle Tree in Blockchain