merkle tree
Each block in the blockchain contains all the transactions generated in that block and is represented by a Merkle tree.
A Merkle tree (also called a hash tree) is a binary tree consisting of a root node, a set of intermediate nodes, and a set of leaf nodes. The bottommost leaf node contains the stored data or its hash value, each intermediate node is the hash value of the content of its two child nodes, and the root node is also composed of the hash value of the content of its two child nodes. The characteristic of Merkle tree is that any changes in the underlying data will be passed to its parent node all the way to the root of the tree.
Applications in Blockchain
In the Bitcoin network, Merkle trees are used to summarize all transactions in a block, generate digital fingerprints of the entire transaction set, and provide an efficient way to verify whether a block has a transaction . Generating a complete Merkle tree requires recursively hashing pairs of hash nodes, and inserting the newly generated hash nodes into the Merkle tree until there is only one hash node left, which is Merkle tree roots.
How to verify transactions
In order to prove the existence of a specific transaction in a block, a node only needs to calculate log2(N) 32-byte hash values to form an authentication path or Merkle path from a specific transaction to the root of the tree. As the number of transactions increases dramatically, this amount of computation becomes extremely important, since the logarithm of the base 2 transaction number grows much more slowly relative to the increase in the number of transactions. This allows Bitcoin nodes to efficiently generate a path of 10 or 12 hashes (320-384 bytes) to prove that there are thousands of transactions in a megabyte-sized block the existence of a transaction.
For example, we want to verify that transaction D3 is in the block, we need a merkle path to prove that D3 is in the block. The path is represented by pink blocks, H2, H8, H13; after we receive the merkle path, we generate H9 based on our existing H3, then generate H12 from H9 and H8, and finally generate root; then with the merkel in the block root comparison, if they are consistent, it means that the transaction is indeed in the block.
data .