The Block Tree Structure
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.
- Active Chain: The path from the current tip back to Genesis with the most cumulative work.
- Side Branches: Valid paths that have less cumulative work than the active chain.
- Orphaned Headers: Headers that are valid but whose parents haven't been downloaded yet.
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.
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: