How a Blockchain Works
Understand hash functions, how blocks link together into a chain, and why changing one old record would break every block that comes after it.
Welcome back!
Step 1 of 12Learn
Fingerprints for data: hash functions
The core building block of a blockchain is the cryptographic hash function. It takes any input, a word, a file or a whole block of transactions, and produces a fixed-length output called a hash: a kind of digital fingerprint.
Bitcoin uses a function called SHA-256, which always produces 256 bits, usually written as 64 hexadecimal characters, whether the input is one letter or a whole book.
Good hash functions have four properties that make blockchains possible:
- Deterministic: the same input always gives the same hash.
- Fast to compute in one direction.
- One-way: given a hash, there's no practical way to work out the input, other than guessing inputs and checking.
- Avalanche effect: change one tiny detail of the input, say, a single comma, and the hash changes completely and unpredictably.
Because of these properties, a hash works like a tamper seal. If you have the hash of a document, anyone can check whether the document has changed by hashing it again. If even one character changed, the hashes won't match.
You use hashes daily without knowing it. Websites store hashes of passwords instead of the passwords themselves. Software downloads publish hashes so you can check files weren't altered. Blockchains apply the same idea to an entire history of transactions.
Step 2 of 12Practice · Select all that apply
Which are properties of a good cryptographic hash function? Select all that apply.
Select all that apply.
Step 3 of 12Compare
One capital letter, a completely different fingerprint
| Aspect | Input: hello | Input: Hello |
|---|---|---|
| SHA-256 hash (first half) | 2cf24dba5fb0a30e26e83b2ac5b9e29e | 185f8db32271fe25f561a6fc938b2e26 |
| SHA-256 hash (second half) | 1b161e5c1fa7425e73043362938b9824 | 4306ec304eda518007d1764826381969 |
| Length | 64 hex characters | 64 hex characters |
| Same input again? | Exactly the same hash | Exactly the same hash |
Step 4 of 12Learn
Blocks chained by hashes
A blockchain groups transactions into blocks. Each block contains:
- A list of transactions, such as "address A sends 0.5 coins to address B"
- A timestamp
- The hash of the previous block
- Other data used by the consensus method, such as a number called a nonce in proof of work
That third item is the clever part. Because every block includes the previous block's hash, the blocks form a chain, each one locked to the one before it, all the way back to the first block, called the genesis block.
Now imagine someone tries to change an old transaction, say, in block 100, to give themselves more coins. Changing block 100's contents changes its hash. But block 101 contains the old hash of block 100, so it no longer matches. To hide the change, the attacker must recalculate block 101, which changes its hash, which breaks block 102, and so on, all the way to the newest block.
On its own, recalculating hashes is quick for a computer. What makes it practically impossible is the consensus method, which you'll meet in module 2: on Bitcoin, each block requires an enormous amount of computing work, and the attacker would have to redo all of it faster than the rest of the network keeps adding new blocks. On top of that, thousands of independent computers hold copies of the real chain and would simply reject the altered one.
Step 5 of 12Practice · Put in order
Put the steps for adding a new block to a blockchain in order.
Drag the items, or use the arrow buttons, to put them in order.
- The network checks that the block follows the rules
- Users broadcast transactions to the network
- Nodes add the valid block to their copy of the chain
- The candidate block includes the hash of the latest block in the chain
- A block producer collects pending transactions into a candidate block
Step 6 of 12Diagram
A chain of blocks, each pointing back
Each block carries the fingerprint of the one before it. Tap a block to see what it holds.
Tap a shape with to see more.
- Genesis block
The first block. It has no previous block to point to.
- Block 99
Transactions + timestamp + hash of block 98.
- Block 100
Transactions + timestamp + hash of block 99.
- Block 101
Transactions + timestamp + hash of block 100.
- Next block
Will include the hash of block 101 once it's produced.
Step 7 of 12Learn
A ledger kept by everyone
The final ingredient is distribution. A blockchain isn't stored on one server. It's copied across many computers called nodes, run by individuals, companies, universities and hobbyists around the world. Bitcoin has many thousands of reachable nodes.
Each full node keeps a complete copy of the blockchain and independently checks every transaction and block against the rules: Are the signatures valid? Does the sender actually have these coins? Has this coin already been spent? A node that receives an invalid block simply refuses it. No one has to trust any single node, because each one verifies everything itself.
This design has trade-offs:
- Transparency. On public blockchains like Bitcoin and Ethereum, every transaction is visible to anyone, forever. Addresses aren't names, so it's pseudonymous, not anonymous: once an address is linked to a person, for example through an exchange account, their history can often be traced.
- Permanence. Data written to the chain is effectively permanent. Mistakes can't be erased; they can only be offset by new transactions.
- Cost and speed. Keeping thousands of copies in sync is slower and more expensive than a single database. Bitcoin adds a block about every 10 minutes.
That's why "put it on a blockchain" isn't automatically better. Blockchains shine when many parties who don't trust each other need a shared record no one controls. When one trusted organization runs the system anyway, a normal database is usually faster and cheaper.
Step 8 of 12Practice · Sort into groups
For each situation, decide whether a public blockchain is a natural fit or whether an ordinary database is likely better.
Tap an item, then tap its group (or drag it there).
Step 9 of 12Note
Step 10 of 12Flashcards
Blockchain vocabulary
Tap a card to flip it.
Step 11 of 12Practice · True or false
Changing a transaction in an old block would only change that one block's hash, leaving later blocks valid.
Step 12 of 12Practice · Choose one
Why does each block include the hash of the previous block?
Lesson complete!