TeachMeBitcoin

The Fork in the Road: Understanding `getchaintips`

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

10. The Fork in the Road: Understanding getchaintips

Bitcoin is a decentralized system, which means there is no "Central Authority" to decide which version of history is correct. Sometimes, two miners find a block at the same time, and the network "Splits." For a few minutes, there are two versions of reality. Eventually, one version grows longer, and the other one is "Abandoned." The command getchaintips allows us to see these "Orphaned Branches" of history. It is the "Map of Parallel Universes."

Analyzing the "Fork Finder" Code

In the source code (src/rpc/blockchain.cpp), the node iterates through its entire "Index" to find every block that acts as a "Tip"—even the ones that aren't part of the main chain.

/**
 * This function maps out every 'Tip' the node knows about.
 * It is the "Cartographer of History."
 */
static RPCMethod getchaintips()
{
    // ... (Arguments omitted)
    [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
{
    ChainstateManager& chainman = EnsureAnyChainman(request.context);
    LOCK(cs_main);

    // We look for every block that has no "Children."
    // These are the 'Tips' of the various branches.
    std::set<const CBlockIndex*, CompareBlocksByHeight> setTips;
    for (const auto& [_, block_index] : chainman.BlockIndex()) {
        if (!chainman.ActiveChain().Contains(&block_index)) {
            // This is a branch!
            setTips.insert(&block_index);
        }
    }

    // (Formatting logic omitted)
    return results;
}

Explaining the Branches to a Non-Coder

The "Valid-Fork" and the "Invalid"

When you run getchaintips, the bridge tells you the "Status" of each branch.


☕ 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!