Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 41 ยท Consensus and Networks

Longest Chain Rule, Forks and Reorgs

See how proof-of-work chains settle disagreement: competing blocks, stale blocks, reorganizations, confirmations and 51 percent attack odds, simulated in Python.

The Double-Spend Problem and Byzantine Faults ended with a question: how do thousands of nodes that hear about events at different times still end up with one history? A proof-of-work chain answers with a delightfully blunt rule: follow the chain with the most accumulated work, and when in doubt, keep mining on the one you saw first. Disagreements are allowed to happen. They are resolved automatically as soon as one side gets ahead.

This lesson explains that rule and its consequences. You will simulate a small network, see forks and discarded blocks appear because of message delay, learn what a reorganization is, understand why exchanges wait for confirmations, and calculate how likely an attacker with a given share of mining power is to rewrite recent history.

The rule in one paragraph

Every node keeps the blocks it has seen as a tree. Each block points to its parent by hash, and different blocks may share a parent. The node considers the best chain to be the path from the genesis block with the most total work. In a chain with fixed difficulty that is simply the longest chain (the name "longest chain rule" is shorthand for "most cumulative work"). A miner always builds on the tip of its best chain. If two chains have equal work, a node keeps whichever it saw first.

That is the whole rule. No voting messages, no leader, no coordination. The only communication is announcing new blocks.

Why forks happen

Blocks do not spread instantly. Suppose miner A finds block 100 and announces it. Before the news reaches miner B, B finds a different block 100 on the same parent. Now the network has two valid blocks at the same height, a fork. Miners hear one or the other first and build on it. Both branches grow for a moment until one miner finds block 101. That block makes one branch longer; everyone who learns of it switches to it. The losing block 100 becomes a stale block (often called an orphan; strictly, an orphan is a block whose parent is unknown, but the two words are used loosely).

flowchart LR
    G["99"] --> A1["100a"]
    G --> B1["100b"]
    A1 --> A2["101"]
    A2 --> A3["102"]
    B1 -. "stale" .-> X["abandoned"]

Two blocks at height 100 compete. The branch that gets the next block first wins; the other block is stale and its transactions return to the mempool.

The chance of a fork depends on the propagation delay compared with the block interval. If news takes 10 seconds to cross the network and blocks come every 600 seconds, forks are rare. If blocks come every 12 seconds and news takes 6, they are common. This is one of the reasons chains cannot just shorten their block time to be faster.

Simulating a network

The simulation below makes three miners with 50 percent, 30 percent and 20 percent of the hash power. Blocks are found as a random process (mean 600 seconds). When a miner finds a block it builds on the best chain it knows at that time: its own blocks are known instantly, other miners' blocks only after a propagation delay. We use a seeded random generator so every run prints the same numbers.

Python How the delay creates stale blocks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output is:

TEXT
delay(s)  blocks found  on main chain  stale  stale rate
       0          1000           1000      0       0.0%
      10          1000            992      8       0.8%
      60          1000            941     59       5.9%
     180          1000            850    150      15.0%

With zero delay there are no stale blocks, because everybody always sees the latest block before mining. As the delay grows relative to the 600-second interval, the stale rate climbs. A rough rule of thumb is that the chance a competing block appears is about delay / interval for a network of many equal miners. The figures above come out lower (15 percent at a 180-second delay, where the rule says about 26 percent) because a miner never forks against itself, and with only three miners and one seed the numbers are a single sample, not an average.

There is also a subtle fairness effect. A miner with a larger share of the power wins more of the near-ties, since it builds on its own blocks instantly while others wait for news. Large miners therefore suffer fewer stale blocks. This is one reason fast propagation is treated as a security issue and not just a convenience.

Watching a fork resolve

Let's print the first blocks of a high-delay run and see which ones end up stale.

Python Which blocks lost the race?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output is:

TEXT
 id  height  miner  time(s)  parent  status
  1       1      B      585       0  main
  2       2      C     1537       1  main
  3       3      C     2345       2  main
  4       3      A     2362       2  STALE
  5       4      B     4085       3  main
  6       5      A     5472       5  main
  7       6      A     5852       6  main
  8       7      B     6323       7  STALE
  9       7      A     6330       7  main
 10       7      C     6527       7  STALE
 11       8      A     7398       9  main
 12       9      A     8355      11  main
 13      10      A     8932      12  main
 14      10      C     8933      12  STALE
 15      11      A     9074      13  main
 16      12      C    11498      15  main
 17      13      C    11703      16  main
 18      14      B    12168      17  main

Block 4 (miner A, time 2,362) is the first fork. Miner C found block 3 just 17 seconds earlier, but with a 240-second delay A had not heard of it, so A built a second block on top of block 2. Block 5 then extended block 3's branch, which made it the longer one and left block 4 stale. A messier case is height 7: blocks 8, 9 and 10 all have parent 7, found by three different miners who each had not heard of the others. Block 11 chose to build on block 9, so blocks 8 and 10 were dropped. Blocks 13 and 14 were found one second apart, and again one of them lost. The stale blocks cost their miners the reward: all that work was wasted.

Reorganizations and confirmations

When a node learns about a longer branch than the one it was following, it performs a reorganization (reorg): it unwinds the blocks on its old tip back to the fork point, applies the blocks of the new branch, and returns transactions from abandoned blocks to the mempool (unless they are now invalid or already included on the new branch). A reorg of one block happens now and then in a healthy network. Deep reorgs are warning signs.

This is why a payment's safety grows with time. A transaction in the newest block is exposed to any competing block that arrives at once. Each block built on top of it is another block an attacker would have to outpace. The number of blocks on top, counting the one holding the transaction, is the number of confirmations. A shop selling coffee may accept zero or one confirmation, while an exchange crediting a large deposit may wait for a dozen or more. Chains that offer finality (see Proof of Stake) replace this probabilistic safety with a point at which a block can no longer be reverted.

How likely is an attack?

Imagine an attacker with a fraction q of the total hash power, and the honest network has p = 1 - q. The attacker pays a merchant, waits for z confirmations, then tries to publish a secret chain, mined in private, that replaces the block holding the payment. Each new block belongs to the attacker with probability q, otherwise to the honest side.

If the attacker is z blocks behind, a classic gambler's-ruin argument shows they ever catch up with probability (q / p) ** z when q < p, and with certainty when q >= p. Accounting for the progress the attacker makes while the honest chain finds its z blocks gives a more exact formula from the original Bitcoin paper:

P = 1 - sum for k = 0..z of ( Poisson(k; lambda) * (1 - (q/p) ** (z - k)) ) with lambda = z * q / p.

The simulation below uses that formula and checks it with a random experiment.

Python Probability that an attacker rewrites history

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output is:

TEXT
attacker share q, confirmations z, formula, simulation
q=0.10  z=1  formula= 0.2046  simulation= 0.1972
q=0.10  z=3  formula= 0.0132  simulation= 0.0165
q=0.10  z=6  formula= 0.0002  simulation= 0.0005
q=0.30  z=1  formula= 0.6277  simulation= 0.6015
q=0.30  z=3  formula= 0.3246  simulation= 0.3287
q=0.30  z=6  formula= 0.1321  simulation= 0.1511
q=0.45  z=1  formula= 0.9198  simulation= 0.9003
q=0.45  z=3  formula= 0.8444  simulation= 0.8141
q=0.45  z=6  formula= 0.7661  simulation= 0.7280
q=0.51 any z: formula = 1.0

The two columns agree to within a few percentage points. The formula uses a Poisson approximation for the attacker's progress and the experiment uses 10,000 random trials, so small gaps are expected. Read the table by rows: an attacker with 10 percent of the power has a 20 percent chance after one confirmation but only about 0.02 percent after six. With 30 percent, six confirmations still leave roughly a 13 percent chance. With 45 percent, even six confirmations leave a chance above 70 percent.

The takeaways are the same ones Bitcoin users rely on. With a modest share of the power, each extra confirmation cuts the success chance by a large factor. As q approaches one half, the chance of success stays substantial even after many confirmations, and at 51 percent the attacker eventually wins whatever the number of confirmations. That is the origin of the phrase 51 percent attack: with a majority of the mining power, an attacker can reorganize the chain at will, double-spend and censor transactions (though not forge signatures or print unlimited coins, since honest nodes still validate every rule).

The cost of reaching a majority is the defence. On a large proof-of-work chain it means buying or renting an enormous share of the world's mining equipment, which is why attacks succeed mostly against small chains whose power can be rented for a few hours.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which chain does a proof-of-work node treat as the best one?

    Nodes follow the valid chain that represents the most total work, and keep the first-seen one on a tie.
  2. 2. Why do forks occur even when every miner is honest?

    Propagation takes time, so two valid blocks can compete at the same height until one branch grows longer.
  3. 3. What happens to the transactions in a stale block?

    During a reorg nodes put transactions from abandoned blocks back into the pool so they can be included again.
  4. 4. An attacker controls 30 percent of the hash power. What does the gambler's-ruin result say about catching up from z blocks behind?

    With q < p, the catch-up chance is (q/p)^z, which shrinks exponentially with the number of blocks the attacker is behind.
  5. 5. What can a 51 percent attacker NOT do?

    Nodes still reject blocks with invalid signatures, so majority hash power gives control over ordering, not over other people's keys.

Summary

  • Nodes follow the valid chain with the most accumulated work, and mining on the first-seen tip breaks ties. No voting is needed.
  • Propagation delay makes honest miners occasionally produce competing blocks; the losing blocks become stale and their transactions return to the mempool.
  • A reorganization is a node switching to a better branch; confirmations count how many blocks sit on top of a transaction.
  • An attacker with share q < 0.5 succeeds with a probability that shrinks exponentially in the number of confirmations; with q > 0.5 they eventually always succeed.
  • Short block intervals and slow propagation increase forks and weaken security, so chains tune them together.

Next: Peer-to-Peer Networking and Gossip, the network layer that spreads blocks and transactions in the first place.