TeachMeBitcoin

The Block Tree Structure

From TeachMeBitcoin, the free encyclopedia ⏱️ 2 min read

The Block Tree: Managing Competing Chain Realities

A Bitcoin node does not just store a single linear list of blocks. Instead, it maintains a Block Tree (stored in the BlockIndex database). This tree contains every valid block header the node has ever seen, including those on branches that are not currently the "active" chain.

Maintaining this tree is what allows a node to perform a Chain Reorganization instantly when a heavier branch is discovered.


🌳 1. Anatomy of the Block Tree

Every block in the tree (except the Genesis block) points to a hashPrevBlock.

                           THE BLOCK TREE STRUCTURE

                                     ┌── [ Block 840,002 (Active Tip) ]
                                     │
              ┌── [ Block 840,001A ] ┴── [ Block 840,002 (Side Branch) ]
              │
    [ 840,000 ] ── [ Block 840,001B (Side Branch) ]

🗄️ 2. The BlockIndex Database

In Bitcoin Core, the leveldb database stores the BlockIndex. Each entry contains: 1. Block Header: The 80-byte header. 2. Height: The block's distance from Genesis. 3. nChainWork: Total work accumulated from Genesis to this block. 4. Status: Is the data for this block on disk? Is it validated?


⚡ 3. The setBlockIndexCandidates Set

To efficiently find the best chain, Bitcoin Core maintains a memory-resident set called setBlockIndexCandidates. * Whenever a new block header arrives, it is added to the tree. * If the block is valid and could potentially be part of the best chain, it is added to this "candidates" set. * The node then compares the nChainWork of the best candidate against the current chainActive.Tip().


🔄 4. Transitioning Tips

If a candidate has more work, the node triggers the reorganization logic.

Because the node has already stored the headers and block data for the side branch in its Block Tree, it doesn't need to re-download anything. It simply "unwinds" the active chain back to the common ancestor and "winds up" the new branch.

This architecture ensures that Bitcoin is Eventual Consistent. Even if nodes see blocks in a different order, they will all eventually build the same tree and select the same heaviest path.

☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!