默克尔树
高效验证
默克尔树 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Problem Merkle Trees Solve
A block may contain thousands of transactions. How can a lightweight device verify that one specific transaction is included without downloading them all?
The answer is the Merkle tree — a structure that lets you prove membership with just a handful of hashes.
What Is a Merkle Tree?
A Merkle tree (or hash tree) is a binary tree where:
- Each leaf is the hash of one transaction
- Each internal node is the hash of its two children combined
- The single top node is the Merkle root
Building the Tree
Construction works bottom-up. Hash each transaction to form the leaves, then repeatedly hash pairs together until only one hash remains.
Leaves: H(tx1) H(tx2) H(tx3) H(tx4)
Level 1: H(H(tx1)+H(tx2)) H(H(tx3)+H(tx4))
Root: H(level1a + level1b)The Merkle Root
The Merkle root is a single hash that represents every transaction in the block. It is stored in the block header.
If even one transaction changes, the root changes — so the root acts as a compact fingerprint of the entire transaction set.
Handling an Odd Number of Leaves
If a level has an odd number of nodes, the last node is usually duplicated so it can be paired.
This keeps the tree balanced and ensures every node has a sibling to hash with.
Leaves: H(tx1) H(tx2) H(tx3)
-> duplicate last: H(tx3) H(tx3)
Level 1: H(tx1+tx2) H(tx3+tx3)Merkle Proofs
A Merkle proof is the small set of sibling hashes needed to recompute the root from a single leaf.
To prove tx2 is included, you only need the siblings along its path — not the other transactions themselves.
Prove tx2 is in the block:
provide H(tx1) and H(tx3+tx4)
recompute: H( H(tx1)+H(tx2) )
then: H( that + H(tx3+tx4) )
compare to stored Merkle rootWhy Proofs Are Efficient
For N transactions, a Merkle proof needs only about log2(N) hashes.
For one million transactions, that is roughly 20 hashes instead of a million — a massive saving for light clients and mobile wallets.
Light Clients (SPV)
Simplified Payment Verification clients download only block headers, not full blocks.
Using a Merkle proof from a full node, an SPV client can confirm a transaction is in a block without trusting that node blindly.
Tamper Detection
Because every transaction feeds into the Merkle root, altering any transaction produces a different root.
The header's root would no longer match the recomputed root, so the tampering is immediately exposed.
Merkle Trees Beyond Transactions
Merkle trees are used far beyond transaction lists. Ethereum uses a variant called a Merkle Patricia Trie to commit to the entire world state and to receipts.
Git, IPFS, and many databases also rely on Merkle structures for integrity.
Putting It Together
The Merkle tree turns a large set of transactions into a single root hash while still allowing compact, verifiable membership proofs.
It is one of the most elegant data structures behind scalable, trustless verification.
Quick Check
Check your grasp of Merkle proofs.
Recap: Merkle Trees
You learned that:
- A Merkle tree hashes transactions in pairs up to a single root
- The Merkle root is a fingerprint stored in the header
- A Merkle proof needs only ~log2(N) hashes
- This enables efficient light clients and tamper detection
Next we dig into the hashing that powers all of this.
常见问题解答
「默克尔树」课时是免费的吗?
是的 — 「默克尔树」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
「默克尔树」这节课中我会学到什么?
高效验证 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Web3 & DApp Development Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「默克尔树」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?
能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。