Week 3
Bitcoin
- Blockchain from zero, week 3 of 12
- From the whitepaper to the mining loop, step by step
Outcomes
What You Will Be Able To Do
- Trace a UTXO transaction
- Describe a block and the chain
- Explain mining and proof of work
- Compute the 21 million supply
- Write a toy miner
Origin
The 2008 Whitepaper
- Published 31 October 2008 by Satoshi Nakamoto
- Nine pages, title: Bitcoin, a peer-to-peer electronic cash system
- Genesis block created on 3 January 2009
- Author's identity is unknown
UTXO model
Coins As Unspent Outputs
- No account balances in the protocol
- A UTXO is a chunk locked to an owner
- Balance equals the sum of UTXOs you can unlock
Inputs and outputs
Anatomy Of A Transaction
- Inputs reference earlier outputs and carry signatures
- Outputs hold an amount and a lock
- Outputs total at most the inputs
- The leftover is the fee
Inputs: UTXO(7 BTC), signed by Alice
Outputs: 5.0 -> Bob
1.999 -> Alice (change)
Fee: 0.001 BTCDouble spend check
Spending An Output Once
- Full nodes track the set of unspent outputs
- Using an output marks it as spent
- A second spend of it is rejected
- Open question: which of two conflicts came first?
Header and body
Anatomy Of A Block
- Header: previous hash, Merkle root, time, target, nonce
- Body: list of transactions
- First transaction is the coinbase
- Target interval is about ten minutes
Header { version, prev_hash, merkle_root,
time, target, nonce }
Body { coinbase, tx1, tx2, ... }Proof of work
Mining: Find A Lucky Nonce
- Change the nonce, hash the header, check the result
- Valid means the hash is below the target
- Finding is slow and random
- Checking takes one hash
Live code
Demo: Toy Miner
- Difficulty equals the count of leading zeros
- Each extra zero multiplies expected work by 16
import hashlib, itertools
def mine(data, difficulty):
prefix = '0' * difficulty
for nonce in itertools.count():
text = f'{data}{nonce}'.encode()
digest = hashlib.sha256(text).hexdigest()
if digest.startswith(prefix):
return nonce, digest
print(mine('block 1', 4))Cost of rewriting
Why Proof Of Work Secures History
- Orders transactions by choosing who writes the next block
- Editing an old block needs redoing its work
- Every later block must be redone as well
- Nodes follow the chain with the most work
Stable rhythm
Difficulty Adjusts Itself
- Every 2,016 blocks nodes compare actual time with two weeks
- Blocks too fast: puzzle gets harder
- Blocks too slow: puzzle gets easier
- Result: one block about every ten minutes
| Parameter | Value |
|---|
| Target block time | About 10 minutes |
| Adjusts every | 2016 blocks |
| Roughly | Two weeks |
Issuance
Block Reward And Halving
- Reward started at 50 BTC per block
- Halves every 210,000 blocks, about four years
- Rewards so far: 50, 25, 12.5, 6.25, 3.125
- Last halving shown here: April 2024
Supply cap
Why 21 Million
- Sum the rewards: a geometric series
- Total approaches 21 million and stays below it
- Smallest unit: one satoshi, a hundred millionth
- Final coins arrive around the year 2140
reward, total = 50, 0
while reward >= 1e-8:
total += 210000 * reward
reward = int(reward * 1e8 / 2) / 1e8
print(total)Roles
Nodes, Miners And Wallets
- Miners propose blocks
- Full nodes validate every rule and accept or reject
- Wallets store keys and sign transactions
- The supply cap is enforced by nodes
Week 3 in five lines
Recap
- UTXOs are coins as unspent outputs
- Blocks link by hashes and hold a Merkle root
- Proof of work picks the next block
- Difficulty keeps blocks near ten minutes
- Halvings lead to a cap of 21 million
Week 4
Next Week: Consensus
- Proof of work versus proof of stake
- Finality, forks and orphaned blocks
- 51 percent attacks and validators
- How Ethereum agrees today
Hands-on
Questions And Exercise
- Write and time mine() at difficulties 1 to 5
- Verify a nonce with one hash
- Build a three-block chain and break it