<p>The <code>Merkle root</code> is an important part of how blocks in the Bitcoin blockchain are linked together securely. Each new block contains a hash of the <strong>previous block's header</strong>. This connects the blocks in a chain, because if anything changes in a previous block, the hash will change too.</p>
<p>Specifically, each block header contains a <code>Merkle root</code> hash representing all the transactions in that block. If even a single transaction in the block were <strong>modified</strong>, it would cause the <strong>Merkle root to change</strong>. This would make the block's overall <strong>header hash</strong> change too.</p>
<p>So the <code>Merkle root</code> allows any changes to previous transactions to be detected. If a transaction were deleted from a previous block, the Merkle root in the header of all following blocks would change. This would break the chain of hashes connecting the blocks.</p>
<p>You might wonder why we don't simply <strong>hash all transactions</strong> together to create a <code>transactions hash</code> field in the block header, instead of hashing them in <strong>pairs</strong> to create a <code>Merkle root</code>.<br />
While hashing all transactions together would ensure data <strong>integrity</strong>, it would <strong>not be efficient</strong> for <strong>verification</strong>. For instance, if a node wants to verify that a transaction is contained in a block (which has, let's say, 100 transactions), it would need to download and hash all 99 other transactions hashes. If the resulting hash matches the expected hash, then the transaction is confirmed to be in the block.</p>
<p>However, with a Merkle tree, the process is much more efficient. The node only needs to obtain a few <code>Merkle proofs</code>. With a simple mathematical algorithm, it can confirm that the transaction is indeed included in the block. This is because Merkle trees allow for the proof of data belonging to a set without needing to store the entire set.</p>
<p>For instance, to prove that a specific transaction 'a' is part of a Merkle tree, everyone in the network will be aware of the hash function used by all Merkle trees. The hash of 'a' and its corresponding hash are hashed together, moving up the tree until the <strong>root hash</strong>, which is <strong>publicly known</strong>, is obtained. By comparing the obtained <strong>Merkle root</strong> and the <strong>Merkle root</strong> already available within the block header, the presence of transaction 'a' in this block can be verified. From this example, it is clear that in order to verify the presence of 'a', 'a' does not have to be revealed, nor do the other transactions have to be revealed; only some of their hashes are sufficient. This makes <strong>Merkle proof</strong> an efficient and simple method of verifying inclusivity.</p>
<p>The <strong>mathematical</strong> process of verifying a Merkle proof involves hashing the value in question with the hashes provided in the proof, iteratively, until you arrive at a computed Merkle root. If this computed root matches the known Merkle root of the block, the proof is valid, and the transaction is confirmed to be part of the block</p>
<p>When you broadcast a transaction to the Bitcoin network, your wallet can download the new block headers and check if the transaction it created for you is included in the new block or not. This is made possible by the Merkle Tree structure.</p>

CC BY-SA 4.0
2023-10-05T21:20:15.480Z
2023-10-05T21:20:15.480Z
mlibre
<p>I'm currently learning about the Bitcoin blockchain.<br />
I've come across the concept of <code>Merkle trees</code> in the context of <code>Bitcoin</code>. I understand that Merkle trees are used to verify the integrity of the data, but I'm struggling to understand why this is necessary when we can query the database directly. Can anyone shed some light on the practical use of Merkle trees in the Bitcoin blockchain? Why can't we simply query the database for the transaction data?</p>

CC BY-SA 4.0
2023-10-06T01:30:28.450Z
See [How is blockchain data stored](https://bitcoin.stackexchange.com/q/98788/13866)
RedGrittyBrick
<bitcoin-core><bitcoind><bitcoin-cli><merkle-tree><database>
Understanding Bitcoin - Usage of Merkle Trees
mlibre
# Understanding Bitcoin - Usage of Merkle Trees

(Asked by: username_0 on 2023-10-05)

I'm currently learning about the Bitcoin blockchain.  
I've come across the concept of `Merkle trees` in the context of `Bitcoin`. I understand that Merkle trees are used to verify the integrity of the data, but I'm struggling to understand why this is necessary when we can query the database directly. Can anyone shed some light on the practical use of Merkle trees in the Bitcoin blockchain? Why can't we simply query the database for the transaction data?

**Tags:** bitcoin-core, bitcoind, bitcoin-cli, merkle-tree, database

> **Comments:**
> **username_1**: See [How is blockchain data stored](https://bitcoin.stackexchange.com/q/98788/13866)  

---

## 1 answers

### Answer by username_0 on 2023-10-05. Score: 2

The `Merkle root` is an important part of how blocks in the Bitcoin blockchain are linked together securely. Each new block contains a hash of the **previous block's header**. This connects the blocks in a chain, because if anything changes in a previous block, the hash will change too.

Specifically, each block header contains a `Merkle root` hash representing all the transactions in that block. If even a single transaction in the block were **modified**, it would cause the **Merkle root to change**. This would make the block's overall **header hash** change too.

So the `Merkle root` allows any changes to previous transactions to be detected. If a transaction were deleted from a previous block, the Merkle root in the header of all following blocks would change. This would break the chain of hashes connecting the blocks.

You might wonder why we don't simply **hash all transactions** together to create a `transactions hash` field in the block header, instead of hashing them in **pairs** to create a `Merkle root`.  
While hashing all transactions together would ensure data **integrity**, it would **not be efficient** for **verification**. For instance, if a node wants to verify that a transaction is contained in a block (which has, let's say, 100 transactions), it would need to download and hash all 99 other transactions hashes. If the resulting hash matches the expected hash, then the transaction is confirmed to be in the block.

However, with a Merkle tree, the process is much more efficient. The node only needs to obtain a few `Merkle proofs`. With a simple mathematical algorithm, it can confirm that the transaction is indeed included in the block. This is because Merkle trees allow for the proof of data belonging to a set without needing to store the entire set.

For instance, to prove that a specific transaction 'a' is part of a Merkle tree, everyone in the network will be aware of the hash function used by all Merkle trees. The hash of 'a' and its corresponding hash are hashed together, moving up the tree until the **root hash**, which is **publicly known**, is obtained. By comparing the obtained **Merkle root** and the **Merkle root** already available within the block header, the presence of transaction 'a' in this block can be verified. From this example, it is clear that in order to verify the presence of 'a', 'a' does not have to be revealed, nor do the other transactions have to be revealed; only some of their hashes are sufficient. This makes **Merkle proof** an efficient and simple method of verifying inclusivity.

The **mathematical** process of verifying a Merkle proof involves hashing the value in question with the hashes provided in the proof, iteratively, until you arrive at a computed Merkle root. If this computed root matches the known Merkle root of the block, the proof is valid, and the transaction is confirmed to be part of the block

When you broadcast a transaction to the Bitcoin network, your wallet can download the new block headers and check if the transaction it created for you is included in the new block or not. This is made possible by the Merkle Tree structure.


---

