Week 4
Consensus
- Blockchain from zero, week 4 of 12
- How strangers agree on one history without a boss
Outcomes
What You Will Be Able To Do
- Define consensus and why it is hard
- Compare proof of work and proof of stake
- Explain forks and the longest-chain rule
- Distinguish kinds of finality
- Describe a 51 percent attack
Why it is hard
The Agreement Problem
- Messages arrive in different orders
- Nodes crash or go offline
- Some participants lie to gain advantage
- Goal: one order of blocks for everyone
Framework
Four Questions Every Protocol Answers
- Who may propose the next block?
- How do others verify it?
- What happens when two valid blocks collide?
- What does cheating cost?
Bitcoin's answer
Proof Of Work In One Slide
- Miners race to find a valid nonce
- Winner proposes the block and earns the reward
- Everyone verifies with one hash
- Security: hardware and energy spent
Locked capital
Proof Of Stake
- Validators deposit coins as stake
- Proposer is chosen randomly, weighted by stake
- Other validators vote on the block
- Rule-breakers are slashed
| Work | Stake |
|---|
| Resource | Energy | Locked coins |
| Cheat cost | Wasted energy | Slashing |
| Example | Bitcoin | Ethereum |
Real numbers
Ethereum'S Proof Of Stake
- The Merge: September 2022
- A validator deposits 32 ETH
- Slot: 12 seconds, one proposer
- Epoch: 32 slots, committees attest
slot = 12 seconds
epoch = 32 slots
= 384 seconds
= 6.4 minutesTemporary splits
Forks: Two Blocks At Once
- Two miners find valid blocks together
- Parts of the network see different blocks first
- Both branches are valid for a moment
- The next block breaks the tie
A1 - A2 - A3
\
B2 - B3 - B4 <- heavier, winsFork choice
The Longest-Chain Rule
- Nodes follow the valid chain with most work
- Orphaned blocks drop out of the main chain
- Confirmations count blocks built on top of yours
- More confirmations mean lower reversal risk
Demo
Fork Choice In Python
- Chains are lists with a work value per block
- Pick the chain with the highest total work
def choose(chains):
return max(chains, key=lambda c: sum(b['work'] for b in c))
base = [{'n': 1, 'work': 1}]
A = base + [{'n': i, 'work': 1} for i in range(2, 7)]
B = base + [{'n': i, 'work': 1} for i in range(2, 5)]
print(len(choose([A, B])))Rule changes
Soft And Hard Forks
- Soft fork: tighter rules, old nodes still accept blocks
- Hard fork: changed rules, nodes must upgrade
- A refusal to upgrade can split the network for good
- Example: Ethereum and Ethereum Classic, 2016
| Type | Old nodes |
|---|
| Soft fork | Still accept blocks |
| Hard fork | Reject new blocks |
How final is final
Finality
- Bitcoin: probabilistic, risk falls with each block
- Many services wait around six confirmations for large sums
- Ethereum: finalised after two thirds vote across two epochs
- Reverting finalised blocks risks slashing a third of stake
Threat model
The 51 Percent Attack
- A majority builds a private chain and publishes it
- Can reverse its own recent payments
- Can censor chosen transactions
- Cannot spend coins without the owner's signature
- Cannot mint beyond the supply rule
Demo
Stake-Weighted Selection In Python
- Pick proposers in proportion to stake
- Large samples approach the stake shares
import random
from collections import Counter
stake = {'Ana': 32, 'Ben': 64, 'Cem': 160}
names = list(stake)
weights = list(stake.values())
picks = random.choices(names, weights, k=10000)
print(Counter(picks))Judging a network
How Decentralised Is It?
- How many independent block producers exist?
- Can ordinary hardware run a full node?
- How many separate software clients are in use?
- What if a big staking provider fails?
Week 4 in five lines
Recap
- Consensus is agreement on one order of blocks
- Work uses energy, stake uses locked capital
- Forks resolve to the heaviest chain
- Finality is probabilistic or economic
- A majority attacker can rewrite recent history only
Week 5
Next Week: Ethereum
- Accounts, gas and the EVM
- The world computer idea
- Smart contracts at a high level
- Sending a transaction on a testnet
Hands-on
Questions And Exercise
- Build the fork choice function and switch chains
- Run stake-weighted selection 10,000 times
- Write a short attacker analysis