Course 5.2Lesson 2 of 3
A blockchain is a ledger that nobody can change in secret. This lesson opens one block: its data, its time-stamp, its own hash and the hash of the block before. Then it shows why changing one old record breaks the chain.
Try the code and the simulations beside the lesson.
Imagine a class diary where each page ends with a code worked out from everything on that page. The next page starts by copying that code. If someone rubs out a word on page 3, its code no longer fits the one copied on page 4. Anyone can see that page 3 was changed.
A blockchain works like that diary. In its basic form, a blockchain is a digital ledger: a time-stamped series of records that cannot be altered.
The records are grouped into blocks. Each block holds four things.
The first block in a chain has no block before it. It is often called the genesis block.
A hash is a short value, like a fingerprint, that a hashing algorithm works out from data.
So the hash of a block is a fingerprint of all its contents, including the hash of the previous block.
Suppose someone changes the data in block 2, for example to make a payment bigger. Block 2 now has a different hash. But block 3 still stores the old hash of block 2. The two no longer match, so the chain is broken at that point. Every computer that checks the chain sees it.
To hide the change, the person would have to work out a new hash for block 2. Then they would have to change the stored previous hash in block 3, which changes block 3's own hash. And so on, for every later block. The older the block, the more blocks must change.
The program uses hashlib, which comes with Python, to work out SHA-256 hashes. SHA-256 is one widely used hashing algorithm. The program keeps only the first characters of each hash, as the pictures do, so it fits on the screen. First it hashes two records that differ by one digit. Then it builds a chain of three blocks, each storing the hash of the block before. To keep it short, it leaves out the time-stamp.
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.