Chain Selection & Reorgs

Lesson, slides, and applied problem sets.

View Slides

Lesson

Chain Selection & Reorgs

Why this module exists

Networks see forks. Nodes must resolve them deterministically using the "most work" rule and safely roll state backward and forward.


1) Competing branches

Two valid blocks can share the same parent. Eventually, one branch gains more work and becomes the main chain.


2) Reorg mechanics

Reorg = undo old branch, apply new branch:

  • Undo removes outputs created on the old branch
  • Undo restores spent outputs
  • Apply new branch forward in order

3) Undo logs

To rollback efficiently, store what was spent:

(outpoint, original_output)

This enables deterministic state reversal.


4) Consensus safety

A buggy reorg can cause:

  • Phantom UTXOs
  • Double spends
  • Permanent divergence

What you will build

  1. Most-work chain selection
  2. Fork point detection
  3. UTXO undo/redo across reorgs

Key takeaways

  • Reorgs are normal in distributed networks.
  • Undo logs are essential for correctness.
  • Chain selection must be deterministic.

Module Items