Lesson 0003: Mining Deep Dive โ€” Consensus, Nonces & Security

โฑ ~20 minutes ๐Ÿ“… 2026-06-10 ๐ŸŽฏ Supplementary โ€ข Mining
Goal: Answer three deep questions about Bitcoin mining: (1) How does the network agree on which block to mine, without a central coordinator? (2) What does "solving the nonce" actually mean โ€” explained so clearly a 10-year-old would get it. (3) What stops miners from cheating, and why can't a rich attacker just take over the network?

Part 1: Who Decides Which Block to Mine?

This is one of the most important questions in Bitcoin. If thousands of miners around the world are all hashing, who tells them which block to work on? Who picks the transactions? Who coordinates the race?

The answer: nobody. There is no coordinator. No vote. No round-robin. No leader election. Every miner independently builds their own candidate block, and the network converges after the fact โ€” by everyone switching to build on whichever valid block wins the race.

The Block Template: Built Locally by Each Miner

Every mining node runs bitcoind (or equivalent software) and maintains its own mempool. When it's time to build a candidate block, the miner (or mining pool operator) does this locally:

  1. Pick the chain tip: Identify the block at the tip of the valid chain with the most accumulated proof-of-work. This is the "previous block" your new block will build on.
  2. Build the coinbase transaction: Create the special first transaction that pays your own address the block subsidy (3.125 BTC) plus all fees from the transactions you include. This is why miners don't need to be told who to pay โ€” they pay themselves.
  3. Select transactions from your mempool: Sort by feerate (highest sat/vB first), pack them in until you hit the 4 million weight unit limit.
  4. Compute the Merkle root โ€” a single 32-byte hash fingerprinting every transaction in the block.
  5. Assemble the 80-byte block header (version, previous block hash, Merkle root, timestamp, difficulty target, nonce = 0) and start hashing.
Every miner builds a different block template:
Miner A in Texas          Miner B in Kazakhstan        Miner C in Japan
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Coinbase โ†’ A's   โ”‚      โ”‚ Coinbase โ†’ B's   โ”‚        โ”‚ Coinbase โ†’ C's   โ”‚
โ”‚   address        โ”‚      โ”‚   address        โ”‚        โ”‚   address        โ”‚
โ”‚                 โ”‚      โ”‚                 โ”‚        โ”‚                 โ”‚
โ”‚ Txs: A's mempool โ”‚      โ”‚ Txs: B's mempool โ”‚        โ”‚ Txs: C's mempool โ”‚
โ”‚ (slightly diff)  โ”‚      โ”‚ (slightly diff)  โ”‚        โ”‚ (slightly diff)  โ”‚
โ”‚                 โ”‚      โ”‚                 โ”‚        โ”‚                 โ”‚
โ”‚ Merkle root:     โ”‚      โ”‚ Merkle root:     โ”‚        โ”‚ Merkle root:     โ”‚
โ”‚  a1b2c3d4...     โ”‚      โ”‚  e5f6g7h8...     โ”‚        โ”‚  i9j0k1l2...     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
      โ”‚                         โ”‚                           โ”‚
      โ–ผ                         โ–ผ                           โ–ผ
  Hash away!                Hash away!                  Hash away!
๐Ÿ”‘ Key insight: Because every miner's coinbase pays a different address, every miner's Merkle root is different. This means every miner is hashing a different block header. They are not racing to solve the same puzzle โ€” they are each solving their own puzzle, and the first one to find a valid solution wins the round.

How the Network Converges: The Emergent Consensus

There is no "agreement" before the block is found. The agreement happens after:

โ› Miner finds valid block
โ†’
๐Ÿ“ก Broadcasts to peers
โ†’
โœ… Every node validates independently
โ†’
๐Ÿ”— All miners switch to building on top of it
โ†’
๐Ÿ”„ Next round begins

The longest valid chain (more precisely: the chain with the most accumulated proof-of-work) is the one everyone follows. If you try to mine on an old block, you're racing not just against one block, but against the entire honest network that's already built on a newer tip. You would need to outrun the combined hashrate of everyone else โ€” which is why it's rational to always build on the tip.

Source: Bitcoin Developer Guide โ€” Block Prototypes; Satoshi Nakamoto โ€” Bitcoin Whitepaper, ยง3โ€“4

๐Ÿง  Quick Check

Why does every miner end up hashing a different block header, even if they all have identical mempools?

Part 2: Solving the Nonce โ€” A 10-Year-Old's Guide

Imagine a Magic Dice Game

Let's play a game. I'm thinking of a giant 256-sided die (well, actually, a number so big it has 78 digits). Your job is to roll the die over and over until you get a number smaller than 5.

That's hard! On average, you'd need to roll about 51 times to get a number under 5 (since only 1, 2, 3, and 4 count out of 256 faces). The more times you roll per second, the faster you'll find a winning roll.

Now imagine I make it harder: you need a number smaller than 1. Now the only winning roll is 0 โ€” and you'll need to roll about 256 times on average. Even harder: what if you needed to roll a number smaller than 0.00000000000000000001? You'd be rolling for a very, very long time.

This is exactly what Bitcoin mining is. But instead of a die, miners use a mathematical function called SHA-256 (applied twice). Instead of rolling, they feed the block header into SHA-256 and get a 256-bit number out. If that number is below the target, they win. If not, they change one number in the header โ€” the nonce โ€” and try again.

What Is SHA-256? The Fingerprint Machine

SHA-256 is a cryptographic hash function. Think of it as a fingerprint machine:

SHA-256 in action (simplified):
SHA256("Hello")   = 185f8db3...a52f6f49  (starts with 1 โ€” not enough zeros)
SHA256("Hello!")  = 336a6101...2b5a67b1  (completely different!)
SHA256("Hello!!") = 0a5f66c1...8829db4e  (still no good)

Bitcoin uses DOUBLE SHA-256:
SHA256(SHA256(block_header)) must be < target

The Nonce: A 4-Billion-Combination Lock

The nonce is a 32-bit number (0 to 4,294,967,295) in the block header. The miner's ASIC tries:

Try nonce = 0:      SHA256ยฒ(header with nonce=0)      โ†’ 7a3f...  Too big โœ—
Try nonce = 1:      SHA256ยฒ(header with nonce=1)      โ†’ e2c1...  Too big โœ—
Try nonce = 2:      SHA256ยฒ(header with nonce=2)      โ†’ 9d4b...  Too big โœ—
...
Try nonce = 847,203,661: SHA256ยฒ(header with nonce=...) โ†’ 0000... Below target โœ“โœ“โœ“
                    โ””โ”€โ”€ BLOCK SOLVED! โ”€โ”€โ”˜

Modern ASICs try hundreds of trillions of nonces per second. But 4 billion nonces isn't enough when the difficulty is high โ€” so miners also tweak the extra nonce in the coinbase transaction, which changes the Merkle root, giving them a fresh 4 billion nonces to try. They repeat this continuously until someone, somewhere, finds a valid hash.

Why "Leading Zeros"?

You may have heard "miners look for a hash starting with many zeros." This is because the target is a very small number. In hexadecimal, small numbers start with zeros. The more leading zeros required, the harder the problem.

Easy target: 0 0 a 3 f 1 b c โ€ฆ โ€ฆ
Hard target: 0 0 0 0 0 0 0 0 0 3 a 1 f

At the current network difficulty (~112 trillion), the target is so small that a valid block hash needs roughly 19 leading zeros in hex. The entire network โ€” around 800 exahashes per second โ€” takes about 10 minutes on average to find one.

Putting It All Together for a 10-Year-Old

Imagine every miner in the world has a magic bingo machine. Each machine shakes a giant bucket of numbered balls and spills them out. If the numbers that come out are all very small numbers (mostly zeros), the miner shouts "BINGO!" and wins a prize โ€” 3.125 brand-new bitcoins plus all the transaction tips.

The machines shake incredibly fast โ€” trillions of times per second. The whole world's machines shake together, but they all have slightly different buckets (because each miner's prize address is different, which changes the balls inside). The first machine to get a "bingo" wins, and everyone immediately resets and starts shaking for the next round.

The game automatically adjusts: if bingos start coming too fast (faster than every 10 minutes), the game makes the target smaller โ€” you need even smaller numbers. If bingos are too slow, it makes the target bigger. This happens every 2,016 rounds (~2 weeks).

That's Bitcoin mining. Trillions of magic bingo shakes per second, worldwide, with a new winner every ~10 minutes.

๐Ÿง  Quick Check

Why do miners also modify the "extra nonce" in the coinbase, instead of just trying all 4 billion nonce values?

Part 3: Why Miners Can't Cheat โ€” The Security Model

Bitcoin's security doesn't come from trust, police, or a central authority. It comes from game theory and economic incentives.

The Core Incentive: Honest Mining Is Profitable

A miner who plays by the rules earns:

Revenue per block = 3.125 BTC (subsidy) + fees from all included transactions

โ‰ˆ $300,000+ per block at current prices

Mining costs real money โ€” electricity, hardware depreciation, cooling, facility costs. A cheater who gets caught (or whose invalid block is rejected by the network) earns zero for all that work. The economic incentive is overwhelmingly to mine honestly.

What Can a Miner Actually Try to Cheat At?

โœ… Things miners CAN do

โŒ Things miners CANNOT do

๐Ÿ”‘ Critical distinction: A 51% attacker can double-spend their own coins (by broadcasting one transaction, mining a secret chain that spends the same coins elsewhere, then revealing the longer secret chain). They cannot steal your coins. Your private key is still your private key. The attack is economically disruptive but not theft of others' funds.

The 51% Attack: Why It's So Hard

A 51% attack (more accurately: a majority-hashrate attack) means an attacker controls more than half of the total network hashrate. With majority hashrate, they can:

  1. Double-spend: Send bitcoin to an exchange, get fiat withdrawn, then mine a longer chain where that transaction never happened and recover the bitcoin.
  2. Censor transactions: Refuse to include certain transactions in any block they mine.
  3. Block other miners: Always build on their own blocks, ignoring (orphaning) blocks from honest miners.

Why No One Has Successfully Done It (Sustainably)

๐Ÿ’ฐ

Astronomical Cost

To control 51% of the network (~400 EH/s), you'd need roughly $20โ€“40 billion in ASIC hardware, plus ongoing electricity costs of ~$10โ€“15 million per day. The attack burns money continuously while the honest chain keeps advancing.

๐Ÿญ

Hardware Scarcity

You can't just "buy" 51% of the hashrate. ASICs are manufactured by a handful of companies (mostly Bitmain and MicroBT) with finite production capacity and long lead times. You'd need to outbid the entire existing mining industry for years of production.

โšก

Energy Constraints

400 EH/s consumes roughly 12โ€“15 gigawatts โ€” comparable to the entire electricity consumption of a mid-sized country. Finding that much power, getting grid connections, and building facilities takes years.

๐Ÿ“‰

Self-Defeating

A successful 51% attack would destroy confidence in Bitcoin, crashing the price. The attacker's hardware (purpose-built for SHA-256 mining) would become near-worthless. The billions spent on the attack would vaporise. You'd spend $30B to maybe steal $5B โ€” and destroy the asset in the process.

๐ŸŒ

Geographic Distribution

Mining is spread across the globe โ€” United States, Kazakhstan, Russia, Canada, and dozens of other countries. No single jurisdiction can compel miners to collude. Pool operators can be pressured, but miners can switch pools in minutes.

๐Ÿ‘๏ธ

Detectable & Responsive

A 51% attack is immediately visible (large reorgs, censored transactions). The community can respond: exchanges can raise confirmation requirements, developers can propose checkpointing or PoW changes, and honest miners have every incentive to coordinate a counter-response. The attacker can't do it secretly.

What About Mining Pools?

Mining pools introduce a subtlety. A single pool controlling >50% of hashrate is a theoretical concern โ€” and it has come close (GHash.io briefly exceeded 50% in 2014). But pool hashrate is not the same as attacker hashrate:

Blockchain Rewrites: The Real Limits of 51%

Even with 51% hashrate, you cannot rewrite arbitrarily far back. The further back you go, the more blocks you need to re-mine, and the honest chain keeps advancing while you try to catch up. The probability of catching up from k blocks behind decreases exponentially with k. This is why 6 confirmations is the standard โ€” after 6 blocks, the probability of a successful reorg by an attacker with even 40% of hashrate is effectively zero.

What 51% Does NOT Enable

A common misconception: "If someone controls 51% of mining, they can steal everyone's bitcoin." This is false.

Source: Satoshi Nakamoto โ€” Bitcoin Whitepaper, ยง11 (Calculations); Bitcoin Developer Guide โ€” Mining; Bitcoin Wiki โ€” Majority Attack; Bitcoin Wiki โ€” Weaknesses

๐Ÿง  Quick Check

If an attacker gains 60% of the network hashrate, which of these can they NOT do?

Key Takeaways

  1. There is no coordinator. Every miner independently builds a candidate block with their own coinbase address. The network converges after a valid block is found โ€” by everyone switching to build on the winning tip.
  2. Mining = guessing. SHA-256 is a one-way fingerprint machine. The only way to find a hash below the target is to try nonces trillions of times per second until you get lucky. The "nonce" is just a counter you keep incrementing.
  3. The coinbase makes every miner's puzzle different. Because each miner pays themselves at a different address, their Merkle roots differ, and they hash different headers. This is what prevents miners from competing on identical work.
  4. Honest mining is the most profitable strategy. Block rewards are worth hundreds of thousands of dollars. Cheating risks earning nothing for enormous electricity expenditure.
  5. A 51% attack is devastatingly expensive and limited in scope. It would cost billions in hardware and millions per day in electricity โ€” and it cannot steal coins, forge signatures, or break consensus rules. It's economically irrational.
  6. The network's distribution is its defence. Thousands of independent miners across dozens of countries, pool-switching flexibility, hardware scarcity, and energy constraints make collusion practically infeasible.
๐Ÿ”— Primary source for deeper reading: Satoshi Nakamoto โ€” Bitcoin Whitepaper โ€” read ยง3 (Timestamp Server), ยง4 (Proof-of-Work), ยง5 (Network), and ยง11 (Calculations) for the original articulation of mining, consensus, and the security model. Also: Bitcoin Developer Guide โ€” Mining for the technical walkthrough; Bitcoin Wiki โ€” Difficulty for the difficulty adjustment algorithm.
๐Ÿ’ฌ Questions? Ask your agent-teacher: "walk me through a specific block's nonce find," "explain the difficulty adjustment in detail," "what's the difference between a soft fork and a hard fork," or "how does Stratum V2 change pool mining?" Every question deepens the model.

Navigate