The Fork in the Road: Understanding `getchaintips`
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
Contains(&block_index): The "Active Chain" is the "Main Highway" of Bitcoin history. If a block is not part of that highway, it's a "Side Road" that led to a dead end. The node keeps these side roads in its library just in case the main highway turns out to be wrong. It is the "Redundancy of Truth."
The "Valid-Fork" and the "Invalid"
When you run getchaintips, the bridge tells you the "Status" of each branch.
-
active: The main highway. -
valid-fork: A side road that is perfectly valid, but just shorter than the main highway. -
invalid: A side road that broke the rules of Bitcoin (e.g., someone tried to print money). This command is the "Vigilance of the Auditor." It allows you to see the "Conflicts" that were resolved by the network's consensus. It is the "Graveyard of Failed Ideas."
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: