Skip to content
KALANBLON
Show contents

Chapter 1 of 6 · 20 min

Forks, reorganisations and confirmations

By the end of this chapter, you'll be able to

  • Explain how a fork appears and how it is resolved
  • Apply the rule of the chain that required the most work
  • Describe a reorganisation and what happens to the transactions of abandoned blocks
  • Compute the probability of a successful double spend with the formula from the Bitcoin white paper

The fork and its resolution

A fork appears when two valid blocks have the same parent. Each node keeps mining on the first one it received. As soon as one branch gets one more block, it becomes the "heaviest": all honest nodes switch to it. That is a reorganisation (reorg): the blocks of the abandoned branch become orphans, and their transactions that are not in the winning branch go back to the waiting queue.

The orphan block's transactions are not lost: they will be included in a later block, if they are still valid.

Step 5 of 5
Text description of the animation
  1. Everyone agrees up to block 100.
  2. Two valid blocks at the same height: each node keeps the one it saw first.
  3. A miner working on 101-N finds the next block: this branch now has more work.
  4. A single rule: follow the chain that required the most work in total.
  5. The orphan block's transactions are not lost: they will be included in a later block, if they are still valid.
def work(chain):
    """Total work: each block counts 16^difficulty attempts on average."""
    return sum(16 ** b["difficulty"] for b in chain)

def best(chains):
    return max(chains, key=work)

common = [{"id": f"b{i}", "difficulty": 4, "tx": []} for i in range(1, 101)]
dakar = common + [{"id": "101-D", "difficulty": 4, "tx": ["Awa -> Kofi 5000", "Mei -> Yao 200"]}]
nairobi = common + [{"id": "101-N", "difficulty": 4, "tx": ["Mei -> Yao 200"]}]

print("tie:", work(dakar) == work(nairobi), "→ each keeps the first block received")

nairobi = nairobi + [{"id": "102", "difficulty": 4, "tx": ["Fatou -> Awa 900"]}]
winner = best([dakar, nairobi])
print("chain kept:", " → ".join(b["id"] for b in winner[-3:]))

in_winner = {tx for b in winner for tx in b["tx"]}
orphans = [tx for tx in dakar[-1]["tx"] if tx not in in_winner]
print("back to the waiting queue:", orphans)
tie: True → each keeps the first block received
chain kept: b100 → 101-N → 102
back to the waiting queue: ['Awa -> Kofi 5000']

The transaction from Awa to Kofi had one confirmation on the Dakar branch… then it disappeared from the chain kept. It will probably be included later, but if Awa has meanwhile spent the same funds differently, Kofi will never be paid. Hence caution with weakly confirmed payments.

The probability of a double spend

A fraudster controlling a share q of the computing power pays a merchant, waits for the merchant to deliver after z confirmations, then tries to publish a heavier competing chain in which the payment does not exist. The Bitcoin white paper (2008) gives the probability of success: while the honest chain finds z blocks, the fraudster finds a random number of them (Poisson distribution with parameter λ = z·q/p), and must then close the gap.

import math

def double_spend_probability(q, z):
    p = 1 - q
    lam = z * q / p
    total = 1.0
    for k in range(z + 1):
        poisson = math.exp(-lam) * lam ** k / math.factorial(k)
        total -= poisson * (1 - (q / p) ** (z - k))
    return total

for q in (0.10, 0.30):
    line = "  ".join(f"z={z}: {double_spend_probability(q, z):.7f}" for z in (0, 1, 2, 5, 10))
    print(f"q = {q:.0%} → {line}")

# How many confirmations to go below 0.1%?
for q in (0.10, 0.15, 0.25, 0.30):
    z = next(z for z in range(200) if double_spend_probability(q, z) < 0.001)
    print(f"fraudster at {q:.0%}: {z} confirmations")
q = 10% → z=0: 1.0000000  z=1: 0.2045873  z=2: 0.0509779  z=5: 0.0009137  z=10: 0.0000012
q = 30% → z=0: 1.0000000  z=1: 0.6277491  z=2: 0.4457171  z=5: 0.1773523  z=10: 0.0416605
fraudster at 10%: 5 confirmations
fraudster at 15%: 8 confirmations
fraudster at 25%: 15 confirmations
fraudster at 30%: 24 confirmations

With 10% of the power, 5 confirmations make the attack very unlikely (less than one chance in a thousand); with 30%, 24 are needed. For a coffee, many merchants accept a higher risk, with a single confirmation; for a car or a large transfer, they wait longer. And beyond 50%, the probability tends to 1: that is the 51% attack.

Mini-lab: simulating the racePython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: the careful merchant

A merchant wants a simple rule: depending on the amount of a sale, how many confirmations should be awaited so that the double-spend probability, against a fraudster estimated at 10% of the network's power, stays below a threshold that depends on the amount? Write confirmations_needed(amount) with the thresholds: under 10,000 F CFA → 1%; under 1,000,000 → 0.1%; above → 0.0001%. Display the result for 2,500, 150,000 and 8,000,000 F CFA.

Hint 1
Reuse double_spend_probability(q, z) and look for the smallest z below the threshold with next(z for z in range(100) if ...).
Hint 2
Careful: with z = 0, the formula gives a probability of 1 (no confirmation, no protection).
Show the solution and common mistakes
import math

def double_spend_probability(q, z):
    p = 1 - q
    lam = z * q / p
    total = 1.0
    for k in range(z + 1):
        total -= math.exp(-lam) * lam ** k / math.factorial(k) * (1 - (q / p) ** (z - k))
    return total

def confirmations_needed(amount, q=0.10):
    threshold = 0.01 if amount < 10_000 else 0.001 if amount < 1_000_000 else 0.000001
    return next(z for z in range(100) if double_spend_probability(q, z) < threshold)

for amount in (2_500, 150_000, 8_000_000):
    z = confirmations_needed(amount)
    print(f"{amount:>9} F CFA → {z} confirmations (about {z * 10} minutes on Bitcoin)")
     2500 F CFA → 4 confirmations (about 40 minutes on Bitcoin)
   150000 F CFA → 5 confirmations (about 50 minutes on Bitcoin)
  8000000 F CFA → 11 confirmations (about 110 minutes on Bitcoin)

The rule grows slowly with the stakes: each extra confirmation divides the success probability of a 10% fraudster by about 3 to 4. The merchant must also reassess q: an overly optimistic estimate of the attacker's share skews the whole computation.

Common mistakes:

  • Using the simplified probability (q/p)^z: it forgets that the fraudster mines in secret during the wait, and underestimates the risk.
  • Forgetting that the formula assumes q < p: beyond 50%, no number of confirmations protects.
  • Counting confirmations from the next block instead of the block containing the transaction: you wait one block too many, or one too few depending on the convention; it must be set clearly.

Summary

  1. 1A fork appears when two valid blocks have the same parent; it is resolved when one branch receives more work.
  2. 2Nodes follow the chain with the highest cumulative work, not the one with the most blocks.
  3. 3During a reorganisation, the transactions of orphan blocks go back to the waiting queue.
  4. 4The double-spend probability drops fast with confirmations, as long as the fraudster has less than half of the power.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.