Is a blockchain the right tool?
A blockchain is a shared, tamper-evident list kept by many computers that do not trust each other. For most storage problems it is the wrong tool, because it is slow, costly and hard to erase from.
You will weigh its costs, then choose between a database, a signed log and a chain from a list of needs.
By the end of this lesson
Note
I can say when a blockchain is the wrong tool and name a simpler one.
Key words
| Word | What it means |
|---|---|
| Throughput | how many updates a system handles each second |
| Energy cost | the electricity spent on mining |
| Append-only log | a list you may add to but never edit |
| Trust | relying on somebody to act honestly |
| Erasure | removing data so it can no longer be found |
The cells students fill in are left blank in this preview.
A chain costs work, time and copies
Everything the last five lessons built has a price. Mining spends electricity on trying nonces, and it must stay expensive or the chain is easy to rewrite. Every participant keeps a full copy of the list, and every block waits for others to agree.
A database with one owner does the same job with none of those costs.
See the work one small block costs
docker exec jupyter python3 steps/mine.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
The code is tiny, the work is not
The Run printed the tries needed for one block at four difficulties. At difficulty 4 the lab tried 109,028 nonces, for one short payment. A real chain sets its difficulty far higher.
The code is tiny. The next Run counts its lines. The cost is not the code. The cost is all that repeated trying and copying.
Count the lines of the whole toy
docker exec jupyter wc -l chain.py ledger.py lamport.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A public history cannot be erased
A chain is tamper-evident because every copy holds the same history. That is exactly what makes erasure hard. Removing a block from every copy would break every link after it.
Personal data often has to be erasable, because people may ask for it to be removed. A list that nobody can edit is a poor place to put it.
Worked example: which tool fits which need
| Need | Fits best | Why |
|---|---|---|
| A shop's stock levels, many updates a second | A database | One owner, fast, easy to correct |
| A company's audit log, to prove it was not edited | A signed append-only log | One writer, hashes make edits visible |
| Records that must be erasable on request | A database | Erasure is simple |
| Many organisations sharing one list, none in charge | A blockchain may fit | No single owner to trust |
The cells students fill in are left blank in this preview.
A tamper-evident log with no mining
import hashlib
def fingerprint(previous, record):
return hashlib.sha256((previous + "|" + record).encode()).hexdigest()
log = []
previous = "0" * 64
for record in ["opened", "item 5 shipped", "closed"]:
previous = fingerprint(previous, record)
log.append([record, previous])
print("entries:", len(log))
print("last fingerprint starts:", log[-1][1][:8])
# One company can run this log with no mining at all.
# Change "item 5 shipped" and see the last fingerprint change.
A common mistake
Warning
Many people think a blockchain is a better database. It is not. It is a slower, costlier database that suits one narrow case: many writers who do not trust each other and have no owner. If one party can be trusted to run the list, a database with a signed log does the job.