TeachMeBitcoin

Blockchain Reorganizations (Reorgs): The Anchor Guide to Chain Swaps

From TeachMeBitcoin, the free encyclopedia Reading time: 5 min

Blockchain Reorganizations (Reorgs): The Anchor Guide to Chain Swaps

IMPORTANT

Executive Summary: A Blockchain Reorganization (Reorg) is the process by which a Bitcoin node switches its current "Active" chain tip to a newly discovered branch that contains more cumulative work. Reorgs occur naturally due to network latency (1-block reorgs) or can be the result of a deliberate attack. During a reorg, blocks on the "Minority" branch are disconnected, and their transactions are returned to the mempool, while the "Majority" branch becomes the definitive history. This mechanism ensures that the entire global network eventually converges on a single version of the truth.


🔍 Why This Module Matters

Reorgs are the "Erasers" of the blockchain. They allow the network to correct mistakes and resolve conflicts without central intervention. However, for users and exchanges, a reorg is a moment of risk: a transaction that looked "Confirmed" can suddenly disappear or be reversed. This module will deconstruct the "Disconnect/Connect" logic of a reorg, explain why "6 Confirmations" is the industry standard, and detail the technical steps a node takes to "Rewrite" its local database.


🏛️ The Anatomy of a Chain Swap

A reorg is not a "Deletion"; it is a "Switch."

1. The Fork Event

Two miners find Block 800,001 at the same time. The network is split.

2. The Heavier Branch Discovery

A third miner finds Block 800,002 on top of Branch A. When a node on Branch B receives this new block, it realizes its current history is "Lighter" than the incoming news.

3. The Technical Step-by-Step

To align with the truth, the node performs a 4-step sequence:

  1. Find Common Ancestor: The node looks back to find the last block both branches share (Block 800,000).

  2. Disconnect (Rollback): The node "Undoes" the transactions in its local Block 800,001B. These coins are returned to the "Unspent" state.

  3. Connect (Apply): The node applies the transactions from Block 800,001A and then Block 800,002A.

  4. Update Tip: The node marks 800,002A as the new "Active Tip."

graph TD
 A[Block 800,000] --> B[Block 800,001A]
 B --> C[Block 800,002A]
 A --> D[Block 800,001B]

 C -->|Discover New Tip| E{Is Work Higher?}
 E -- YES --> F[Reorg Triggered]
 F --> G[Disconnect B]
 G --> H[Connect A & C]
 H --> I[New Active Tip: C]

 style C fill:#9f9,stroke:#333,stroke-width:2px
 style D fill:#f66,stroke:#333,stroke-width:2px

⚙️ What Happens to Your Money During a Reorg?

Scenario Result Status
Transaction is in BOTH branches No change. Confirmed
Transaction is ONLY in the minority branch Returned to Mempool. Pending
Transaction is ONLY in the majority branch Added to Ledger. Confirmed

The Double-Spend Risk: If an attacker sends coins to an exchange on the minority branch, but excludes that transaction from the majority branch they are privately mining, the exchange loses money when the reorg happens. This is why you must wait for confirmations.


🛠️ The Confirmation Margin: Why 6 Blocks?

The probability of a reorg drops exponentially as more blocks are added to a chain.


🛡️ Database Implementation: ConnectBlock vs. DisconnectBlock

In the Bitcoin Core source code (src/validation.cpp), the reorg logic is handled by two primary functions:

This "Transactional" database approach ensures that even if a computer crashes in the middle of a reorg, the blockchain data remains consistent.


🎯 Learning Objectives for this Module

By the end of this module, you will be able to:

  1. Define a Blockchain Reorganization and identify its primary cause.

  2. Explain the "Disconnect/Connect" sequence of a chain swap.

  3. Trace the path of a transaction that is "Orphaned" during a fork.

  4. Justify the use of confirmation depths (e.g., 6 blocks) for high-value payments.

  5. Understand the role of the "Common Ancestor" in resolving chain divergences.


🗺️ Module Roadmap: What's Next?

Now that we've seen the "Eraser" in action, we will look at the rules that govern it:

  1. BIP 34 (Coinbase Height): Why the block height is now mandatory in the coinbase.

  2. The Most Work Rule: A deep dive into the math of nChainWork vs. nHeight.

  3. Natural Forks & Latency: Why small reorgs are a sign of a healthy network.

  4. Python UTXO Reorg Simulator: Writing a script to "Undo" and "Redo" transaction blocks.


🎓 Summary

Reorgs are the "Self-Correction" mechanism of Bitcoin. They ensure that no matter how messy the network gets, it always returns to a single, shared reality. By mastering the mechanics of reorgs, you are understanding the dynamic, living nature of the global ledger and why "Confirmation" is the ultimate measure of finality in the digital age.

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