Source: docs/lotus-v2/header-history-tree.md

Header History Merkle Tree (HEADER_HISTORY)

Status: implemented in lotusd (src/lotus_v2/header_history.{h,cpp}) and specified in P02.


1. Why: Trustless Historical Finality in Kilobytes

In Practice

When you open a wallet on your phone or browser, or verify an older payment or cross-chain proof, how does your app know that a block from six months ago really happened on the canonical Lotus blockchain—without downloading tens of gigabytes of history?

In legacy blockchains, each block header only points to the immediately previous block (prev_block_hash). Proving that Block #100,000 is part of the chain at Block #1,000,000 requires downloading all 900,000 headers in between (~72–120 MB of linear chain links).

With the Header History Merkle Tree, every Lotus V2 block header cryptographically seals a compact fingerprint (history_root) of every block hash from Genesis up to its parent. Any device only needs the latest 134-byte header (confirmed by Avalanche/PoW) plus a ~1 KB cryptographic proof (HeaderInclusionProof) to verify any historical block in less than a millisecond.

Under the Hood

Linear hash chains ($H_{n} \to H_{n-1} \to \dots \to H_0$) require $O(N)$ bandwidth and $O(N)$ verification work to prove historical ancestry across $N$ blocks. FlyClient / MMR constructions solve this with logarithmic peaks, but naive MMR bagging either changes tree topology across heights or requires separate peak-bagging proofs.

Lotus V2 embeds a canonical Binary Carry-Chain Mountain Frontier (HeaderHistoryFrontier) directly into MonetaryState::history_root (committed inside the 134-byte header's hashStateRoot):

  • Consensus state footprint: Fixed $64 \times 32\text{ B} + 8\text{ B} = 2{,}056\text{ bytes}$ in memory (popcount(H) * 32 bytes on disk).
  • Per-block update cost: Amortized $O(1)$—specifically $\text{ctz}(H)$ domain-separated BLAKE3 tagged hashes ("Lotus/v2/header/history") when appending block $H-1$ (1 leaf hash on odd heights, and $1 + \text{ctz}(H)$ hashes on even heights).
  • Proof size & verification: Strictly $\lceil \log_2 H \rceil$ 32-byte siblings ($\le 20$ hashes for $10^6$ blocks, $\le 640\text{ B}$), verified in one bottom-up pass with canonical null-right-sibling enforcement.

2. HOW: The Binary Carry-Chain Frontier

Every block at height $H \ge 1$ commits to the ordered sequence of all $H$ prior canonical block hashes:

$$\big(0,\, \text{BlockID}_0\big),\; \big(1,\, \text{BlockID}_1\big),\; \dots,\; \big(H-1,\, \text{BlockID}_{H-1}\big)$$

How Binary Carry Addition Builds the Tree

Think of the block count $H$ written in binary. Each set bit $k$ where ((H >> k) & 1) == 1 represents one complete, immutable binary subtree of exactly $2^k$ headers (peaks[k]):

flowchart TD subgraph Peaks["Active Frontier at count = 6 (binary 110₂: peaks[2] and peaks[1])"] P2["peaks[2]: Level 2 Subtree Root<br/>(Heights 0..3, 4 leaves)"] P1["peaks[1]: Level 1 Subtree Root<br/>(Heights 4..5, 2 leaves)"] end subgraph RootFold["HeaderHistoryRoot(count = 6)"] R1["acc₁ = InternalHash(level=1, peaks[1], NULL_HASH)"] R2["history_root = InternalHash(level=2, peaks[2], acc₁)"] end P1 --> R1 P2 --> R2 R1 --> R2

When block $H$ is appended (AppendHeaderToFrontier):

  1. Compute carry = HeaderLeafHash(ChainID, H, BlockID_H).
  2. Walk bit positions $k = 0, 1, \dots, 63$:
  3. Bit $k$ is 0: Slot $k$ is empty! Place peaks[k] = carry, increment count = H + 1, and stop immediately.
  4. Bit $k$ is 1: Slot $k$ already holds a complete $2^k$-leaf subtree peaks[k]. Merge it with our carry to form a complete $2^{k+1}$-leaf subtree (carry = HeaderInternalHash(ChainID, k, peaks[k], carry)), clear peaks[k] = 0x00^32, and carry into level $k + 1$.

Because half of all integers have bit $0 = 0$, 50% of blocks require zero internal node hashes, 25% require 1 internal hash, 12.5% require 2, and the average number of internal hashes per block as $H \to \infty$ is less than 1.


3. WHAT: Exact Cryptographic Specification

3.1 Domain-Separated Leaf and Internal Hashes

All hashes use unkeyed 32-byte BLAKE3 (C08, C26) with domain tag "Lotus/v2/header/history" (H_header_history(x) = BLAKE3("Lotus/v2/header/history" || 0x00 || x) in src/lotus_v2/tagged_hash.cpp):

NULL_HASH = 0x00^32

HeaderLeafHash(ChainID, height: u64, block_hash: h32) -> h32:
  H_header_history(ChainID || 0x00 || u64_le(height) || block_hash)

HeaderInternalHash(ChainID, level: u8, left: h32, right: h32) -> h32:
  H_header_history(ChainID || 0x01 || u8(level) || left || right)

3.2 Computing HeaderHistoryRoot from HeaderHistoryFrontier

Given frontier with count committed prior headers:

3.3 Canonical Inclusion Proofs (HeaderInclusionProof)

A proof that block_hash was the canonical block at leaf_height inside a chain of total_count committed headers (leaf_height < total_count) is serialized as:

HeaderInclusionProof =
    u64_le(leaf_height)
 || u64_le(total_count)
 || block_hash:h32
 || u8(num_siblings)
 || siblings: h32[num_siblings]

Verification Algorithm (VerifyHeaderInclusionProof)

  1. Bounds check: Require total_count > 0 and leaf_height < total_count.
  2. Exact depth check: Let tree depth $D$ be: $$D = \begin{cases} \text{ctz}(\text{total\_count}) & \text{if } (\text{total\_count} \mathbin{\&} (\text{total\_count} - 1)) = 0 \\ 64 - \text{clz}(\text{total\_count}) & \text{otherwise} \end{cases}$$ Require siblings.size() == D.
  3. Bottom-up fold with canonical null-sibling check: Initialize cur = HeaderLeafHash(ChainID, leaf_height, block_hash). For $k = 0, 1, \dots, D - 1$:
  4. Let idx = leaf_height >> k.
  5. If (idx & 1) == 0 (left child):
  6. Let right_subtree_start = (idx + 1) << k.
  7. Canonical null-sibling rule: If right_subtree_start >= total_count (no leaves exist in the right child subtree at level $k$), require siblings[k] == NULL_HASH.
  8. Set cur = HeaderInternalHash(ChainID, k, cur, siblings[k]).
  9. Else ((idx & 1) == 1, right child):
  10. Set cur = HeaderInternalHash(ChainID, k, siblings[k], cur).
  11. Root comparison: Return cur == expected_root.

3.4 Node Persistence & RPC Interface


References

  1. [Merkle 1987] Ralph C. Merkle, "A Digital Signature Based on a Conventional Encryption Function", in Advances in Cryptology — CRYPTO '87, Lecture Notes in Computer Science, vol. 293, pp. 369–378, Springer, 1988.
  2. [Crosby & Wallach 2009] Scott A. Crosby and Dan S. Wallach, "Efficient Data Structures for Tamper-Evident Logging", in Proceedings of the 18th USENIX Security Symposium, pp. 317–334, August 2009.
  3. [Todd 2012/2016] Peter Todd, "Merkle Mountain Ranges", OpenTimestamps / Bitcoin Core developer notes, 2012–2016.
  4. [Bünz, Kiffer, Luu, & Zamani 2020] Benedikt Bünz, Lucianna Kiffer, Loi Luu, and Mahdi Zamani, "FlyClient: Super-Light Clients for Cryptocurrencies", in 2020 IEEE Symposium on Security and Privacy (SP), pp. 928–946, May 2020.
  5. [O'Connor, Aumasson, Neves, & Wilcox-O'Hearn 2020] Jack O'Connor, Jean-Philippe Aumasson, Samuel Neves, and Zooko Wilcox-O'Hearn, "BLAKE3: One Function, Fast Everywhere", January 2020 (https://github.com/BLAKE3-team/BLAKE3-specs).
  6. [Nakamoto 2008] Satoshi Nakamoto, "Bitcoin: A Peer-to-Peer Electronic Cash System", October 2008 (https://bitcoin.org/bitcoin.pdf).