DAY 26 / 40
>> Module 03 / Day 26 / Lecture

Proof of Work

How Bitcoin mines — and why it's expensive

Our chain is tamper-detectable. But detecting tampering isn't enough — we need it to be too costly to tamper. Today: Proof of Work.

Lecture75 min
Lab60 min · Mine your own blocks
ModuleM3 · Blockchain Basics
DAY 26 / 40
>> Today's roadmap

What we'll cover

  1. The problem: who gets to add the next block?
  2. The mining race — find a lucky number (nonce)
  3. Difficulty — what "starting with zeros" means
  4. The mining loop in Python
  5. Why this makes tampering expensive
  6. Lab — mine 3 blocks, adjust difficulty
DAY 26 / 40
>> 01 / 04 · The problem

In a decentralised network — who adds the next block?

Nobody owns the blockchain. There's no boss. Thousands of computers all want to add the next block. If it's free to add a block, anyone can spam the chain with fake transactions.

The solution: Make adding a block cost something — specifically, computation time. Make the computer do lots of useless work (find a special number) before it earns the right to add a block. This is Proof of Work.
DAY 26 / 40
>> 02 / 04 · The key idea

Mining = finding the right nonce

A nonce is just a number we add to the block. We keep changing it until the block's hash starts with enough zeros — that's the "difficulty target."

mining_concept.py
import hashlib def hash_with_nonce(data, nonce): content = data + str(nonce) return hashlib.sha256(content.encode()).hexdigest() # Try different nonces: print(hash_with_nonce("block data", 0)) # → 5d7b9a... (doesn't start with 0) print(hash_with_nonce("block data", 1)) # → a3c21f... (doesn't start with 0) print(hash_with_nonce("block data", 42)) # → 0c74bd... (starts with 0 — with difficulty=1 this is valid!)
DAY 26 / 40
>> 03 / 04 · The code

The miner — a loop that keeps guessing

miner.py
def mine_block(index, data, previous_hash, difficulty=3): target = "0" * difficulty # e.g. "000" for difficulty 3 nonce = 0 while True: block = { "index": index, "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"), "data": data, "previous_hash": previous_hash, "nonce": nonce, "hash": "", } block["hash"] = hash_block(block) if block["hash"].startswith(target): print(f"Mined! nonce={nonce}, hash={block['hash'][:16]}...") return block nonce += 1 # Mine a block: b = mine_block(1, "Alice → Bob: 10 DOT", "0" * 64, difficulty=3)
DAY 26 / 40
>> 04 / 04 · Difficulty matters

More zeros = exponentially more guesses

DIFFICULTY 1
Target: "0..."

Takes 1–16 guesses on average. Instant.

DIFFICULTY 4
Target: "0000..."

Takes ~65,536 guesses on average. A few seconds.

DIFFICULTY 6
Target: "000000..."

Takes ~16 million guesses. Minutes on a laptop.

DAY 26 / 40
>> The tamper cost

Now tampering is expensive, not just detectable

If you tamper with block 5 in a 1000-block chain, you have to:
  1. Recalculate block 5's hash to meet the difficulty target — costs lots of work.
  2. Recalculate block 6, 7, 8... all the way to block 1000 — each costs work.
  3. Do all this faster than the whole honest network is adding new blocks.
With 51%+ of the network's computing power, you could theoretically do it — this is the famous 51% attack. Bitcoin's enormous mining network makes this practically impossible.
DAY 26 / 40
>> Updated validator

Add the nonce check to validation

validate_pow.py
def is_valid_chain(chain, difficulty=3): target = "0" * difficulty for i, block in enumerate(chain): # Check 1: hash integrity if block["hash"] != hash_block(block): return False, f"Block {i}: hash invalid" # Check 2: proof of work if not block["hash"].startswith(target): return False, f"Block {i}: insufficient proof of work" # Check 3: chain link if i > 0 and block["previous_hash"] != chain[i-1]["hash"]: return False, f"Block {i}: broken link" return True, "Chain is valid"
DAY 26 / 40
>> 11:00 – 12:00 · Lab

Today's lab — Mine Your Own Blocks

CORE
Mining loop
  • ›Implement mine_block() with configurable difficulty.
  • ›Mine a chain of 3 blocks with difficulty=3.
  • ›Print the nonce and hash for each mined block.
STRETCH ★
Difficulty benchmark

Mine the same block at difficulty 1, 2, 3, and 4. Record how many nonce attempts each took. Print a simple table comparing them.

DAY 26 / 40
>> End of Day 26

Mining is just a guessing game

  • ✓Mining = loop through nonces until the block hash starts with the required zeros.
  • ✓Higher difficulty = more zeros required = exponentially more guesses needed.
  • ✓Tampering is now expensive: you'd have to re-mine every subsequent block to fix the chain.
01 / 10
←/→ navigate   F fullscreen