Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

When not to use a blockchain

After this lesson: I can say when a blockchain is the wrong tool and name a simpler one.

60 min lesson2 commandsBlockchain lab: a toy chain in Python to read, break, mine and sign

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

WordWhat it means
Throughputhow many updates a system handles each second
Energy costthe electricity spent on mining
Append-only loga list you may add to but never edit
Trustrelying on somebody to act honestly
Erasureremoving 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

Command
docker exec jupyter python3 steps/mine.py

Shown 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

Command
docker exec jupyter wc -l chain.py ledger.py lamport.py

Shown 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

NeedFits bestWhy
A shop's stock levels, many updates a secondA databaseOne owner, fast, easy to correct
A company's audit log, to prove it was not editedA signed append-only logOne writer, hashes make edits visible
Records that must be erasable on requestA databaseErasure is simple
Many organisations sharing one list, none in chargeA blockchain may fitNo single owner to trust

The cells students fill in are left blank in this preview.

A tamper-evident log with no mining

Code: python
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.

About this lab and its source: Blockchain lab: a toy chain in Python to read, break, mine and sign

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.