Show contents
Chapter 1 of 6 · 20 min
Throughput limits and layer-2 solutions
By the end of this chapter, you'll be able to
- Compute a blockchain's maximum throughput from block size and transaction size
- Explain the trade-off between decentralisation, security and scaling
- Simulate a payment channel with signed, numbered states
- Compare the per-transaction cost of a rollup and its two families: optimistic and validity-proof
Measuring the limit
A blockchain's throughput is easy to compute: number of transactions per block, divided by the interval between two blocks.
def throughput(bytes_per_block, bytes_per_tx, seconds_per_block):
return bytes_per_block / bytes_per_tx / seconds_per_block
print(f"Bitcoin-like chain : {throughput(1_500_000, 250, 600):>6.1f} tx/s")
print(f"blocks 10 times bigger: {throughput(15_000_000, 250, 600):>6.1f} tx/s")
print(f"blocks every 12 s : {throughput(1_500_000, 250, 12):>6.1f} tx/s")
# What growth costs the nodes: storage per year
for size in (1_500_000, 15_000_000, 150_000_000):
per_year = size * (365 * 24 * 3600 / 600) / 1e9
print(f"blocks of {size / 1e6:>5.1f} MB every 10 min → {per_year:>6.0f} GB more per year")
Bitcoin-like chain : 10.0 tx/s
blocks 10 times bigger: 100.0 tx/s
blocks every 12 s : 500.0 tx/s
blocks of 1.5 MB every 10 min → 79 GB more per year
blocks of 15.0 MB every 10 min → 788 GB more per year
blocks of 150.0 MB every 10 min → 7884 GB more per year
Multiplying the block size by 100 gives about 1,000 transactions per second, still far from large payment networks, and each node must then store and check nearly 8 TB more per year, with the matching bandwidth. That is the often-quoted trilemma: it is hard to have decentralisation (many modest nodes), security and high throughput all on the same layer.
The payment channel
Awa sells meals to Kofi every day. Recording each 1,500 F CFA payment on the chain would be expensive and slow. They open a channel:
- opening (on the chain): each deposits a sum into a shared address that requires both signatures to be spent;
- payments (off the chain): for each meal, they both sign a new numbered state of the channel: "version 37: Awa 131,500, Kofi 68,500";
- closing (on the chain): one of them publishes the latest signed state; after a dispute period, the split is applied.
60 payments, 2 transactions on the chain.
Text description of the animation
- The funds are locked: neither can withdraw them alone.
- Each meal produces a new state signed by both, without touching the chain.
- Either of them can close with the latest signed state.
- If the published state is outdated, the other party presents a more recent version and the cheater is penalised.
- 60 payments, 2 transactions on the chain.
import hashlib
import json
E = 65537
def wallet(p, q): return {"pub": p * q, "priv": pow(E, -1, (p - 1) * (q - 1))}
def h(o): return int(hashlib.sha256(json.dumps(o, sort_keys=True).encode()).hexdigest(), 16)
def sign(w, state): return pow(h(state), w["priv"], w["pub"])
def verify(pub, state, sig): return pow(sig, E, pub) == h(state)
awa, kofi = wallet(2**521 - 1, 2**607 - 1), wallet(2**127 - 1, 2**521 - 1)
state = {"version": 0, "awa": 100_000, "kofi": 100_000}
history = []
for meal in range(60): # 60 meals at 1,500 F CFA
state = {"version": state["version"] + 1, "awa": state["awa"] + 1_500, "kofi": state["kofi"] - 1_500}
history.append((state, sign(awa, state), sign(kofi, state)))
last, sig_a, sig_k = history[-1]
print("latest state:", last)
print("signed by both:", verify(awa["pub"], last, sig_a) and verify(kofi["pub"], last, sig_k))
print("on-chain transactions: 2 (opening and closing) for", len(history), "payments")
latest state: {'version': 60, 'awa': 190000, 'kofi': 10000}
signed by both: True
on-chain transactions: 2 (opening and closing) for 60 payments
Payments are instant and almost free: exchanging two signatures is enough. Bitcoin's Lightning network links such channels together so that Awa can pay someone with whom Awa has no direct channel, going through intermediaries who cannot divert the funds.
Rollups
A channel suits two parties who exchange often. For an application open to everyone, a rollup is used: an operator executes thousands of transactions off the main chain, then publishes on it, in one go, the compressed data of these transactions and the fingerprint of the new state. Two families:
- optimistic: the batch is assumed correct; during a dispute window (often 7 days), anyone can publish a fraud proof that cancels an incorrect batch;
- validity-proof (ZK-rollups): each batch comes with a cryptographic proof, checked by the main chain, that all transactions are correct; no waiting, but an expensive proof computation.
Mini-lab: a rollup's cost per transactionPython 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Your turn
Worked exercise
Worked exercise: the channel's referee
Write settle(publications, awa_pub, kofi_pub, total_deposit) that plays the role of the chain at closing time. publications is the list, in order, of the states published during the period, each with its two signatures and the name of who published it. The function ignores badly signed states, keeps the highest version, and if the first publication was outdated, gives the whole deposit to the other party. Test: an honest closing, a challenged cheating attempt, and a challenge with a forged signature.
Hint 1
valid = [p for p in publications if verify(awa_pub, p["state"], p["sig_awa"]) and verify(kofi_pub, p["state"], p["sig_kofi"])].Hint 2
max(valid, key=lambda p: p["state"]["version"]). If it is more recent than the first valid publication, whoever published first cheated.Show the solution and common mistakes
import hashlib
import json
E = 65537
def wallet(p, q): return {"pub": p * q, "priv": pow(E, -1, (p - 1) * (q - 1))}
def h(o): return int(hashlib.sha256(json.dumps(o, sort_keys=True).encode()).hexdigest(), 16)
def sign(w, state): return pow(h(state), w["priv"], w["pub"])
def verify(pub, state, sig): return pow(sig, E, pub) == h(state)
def settle(publications, awa_pub, kofi_pub, total_deposit):
valid = [p for p in publications
if verify(awa_pub, p["state"], p["sig_awa"]) and verify(kofi_pub, p["state"], p["sig_kofi"])]
if not valid:
return "no valid state"
first = valid[0]
kept = max(valid, key=lambda p: p["state"]["version"])
if kept["state"]["version"] > first["state"]["version"]:
wronged = "kofi" if first["by"] == "awa" else "awa"
return f"cheating by {first['by']}: {wronged} receives {total_deposit}"
s = kept["state"]
return f"version {s['version']}: awa {s['awa']}, kofi {s['kofi']}"
awa, kofi = wallet(2**521 - 1, 2**607 - 1), wallet(2**127 - 1, 2**521 - 1)
def publication(state, by, sig_awa=None):
return {"state": state, "by": by, "sig_awa": sig_awa or sign(awa, state), "sig_kofi": sign(kofi, state)}
v1 = {"version": 1, "awa": 101_500, "kofi": 98_500}
v60 = {"version": 60, "awa": 190_000, "kofi": 10_000}
v61_forged = {"version": 61, "awa": 0, "kofi": 200_000}
print(settle([publication(v60, "kofi")], awa["pub"], kofi["pub"], 200_000))
print(settle([publication(v1, "kofi"), publication(v60, "awa")], awa["pub"], kofi["pub"], 200_000))
print(settle([publication(v60, "awa"), publication(v61_forged, "kofi", sig_awa=12345)], awa["pub"], kofi["pub"], 200_000))
version 60: awa 190000, kofi 10000
cheating by kofi: awa receives 200000
version 60: awa 190000, kofi 10000
The forged version 61, made by Kofi without Awa's signature, is simply ignored: only states signed by both count. And the attempt to close with version 1 costs Kofi the whole share.
Common mistakes:
- Keeping the last published state instead of the highest version: a cheater would simply publish last.
- Accepting a state signed by only one of the two parties: each could award themselves the whole channel.
- Forgetting the period: a referee who settles immediately leaves no chance for a challenge.
Summary
- 1A chain's throughput is limited by block size and interval; increasing it makes every node heavier.
- 2A payment channel only uses the chain for opening and closing; payments are signed, numbered states.
- 3A published outdated state is challenged during a period and penalised; the chain must be watched, or the watching delegated.
- 4A rollup groups thousands of transactions into one publication; optimistic with fraud proofs, or with validity proofs.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.