TL;DR
A blockchain is a list of blocks in which each block stores the hash of the block before it. A hash is a short fingerprint computed from the contents, so changing an old record changes its fingerprint, which breaks the link stored in the next block. A change therefore has to be repeated on every block after it, and many computers keep copies that they check against each other. The structure solves the problem of keeping one shared record of events among parties who do not trust a single keeper of that record.
Builds on: Digital Signature, Distributed Consensus, Proof of Work · Leads to: Bitcoin: A peer-to-peer electronic cash system, State Machine Replication
Where it's used
Cryptocurrency ledgers. Bitcoin keeps a public record of every transfer of its currency as a chain of blocks. Anyone can download the chain from a peer-to-peer network and check every signature and every link, so no bank has to be trusted to keep the books. The design is described in the Bitcoin paper.
Shared records between companies. Several firms that must agree on one history, such as who shipped which goods, can run a permissioned ledger. Its writers are known members, each entry is signed by the party that made it, and each member holds a copy. No single firm can rewrite the history without the others seeing the change.
Proof that a document existed. A person can publish the hash of a document inside a block. The block's place in the chain fixes the earliest time the document could have existed, and anyone can check that claim without trusting the publisher.
How it works
This walkthrough uses a toy hash so that every number can be checked by hand. The toy hash of a block is , where keeps only the remainder after dividing by 10 (see Modular Arithmetic). Each block stores three things: its data, the previous block's hash, and its own hash. The first block has no predecessor, so its previous hash is 0.
-
The first block. Its data is 4, standing for a small payment. Its hash is . Stored: data 4, previous 0, hash 4. Check: the hash depends only on the block's own contents.
-
The second block. Its data is 7. Its previous hash is the first block's hash, 4, which is copied into the block. Its hash is . Stored: data 7, previous 4, hash 5. Check: this block now depends on the first one, because the stored previous value is the first block's hash.
-
The third block. Its data is 2, its previous hash is 5, and its hash is . Stored: data 2, previous 5, hash 2. Check: each previous field equals the hash of the block before it. Here 4 matches 4, and 5 matches 5.
-
Verify the chain. Anyone can recompute every hash from the stored data and the stored previous hash, then compare each previous field with the block before it. All three hashes recompute correctly and all links match, so the chain passes. Check: verification needs no secret, only the stored contents.
-
Tamper with the first block. An attacker changes its data from 4 to 6. Its hash becomes . If the attacker also updates the stored hash to 6, the first block looks consistent with itself. The second block still says its previous hash is 4, and , so the link breaks. Check: the damage shows up at the next block, not at the changed one.
-
Repair the links. To hide the change, the attacker must rewrite the second block: its previous hash becomes 6, and its hash becomes . Now the third block's previous field, 5, no longer matches 9, so the third block must be rewritten too: its previous hash becomes 9 and its hash becomes . Check: all three blocks changed, including the one the attacker first touched.
-
Count the cost. The pattern is general. Changing block forces rewriting block and every block after it. The newest block is the cheapest to change, because nothing comes after it. Check: the chain resists change in proportion to how many blocks sit on top of the record.
flowchart LR B0["Block 0: data 4, previous 0, hash 4"] --> B1["Block 1: data 7, previous 4, hash 5"] B1 --> B2["Block 2: data 2, previous 5, hash 2"] B2 --> B3["Next block: previous 2"]
The toy leaves out two things that make the structure useful in practice. The first is cost: real blocks require real computing work, as in Proof of Work, so rewriting a block costs something every time. The second is agreement: the attacker's rewritten chain is internally consistent, so a single copy cannot tell the difference. The check that decides is that the other copies disagree with the rewrite, and the rule for choosing among chains is the subject of the next sections.
The math
Let block hold data , a previous hash and its own hash . A hash function gives . The chain is valid when every recomputes from its contents and for every block after the first. By convention the first block has .
Step 2 in symbols: the toy hash
The toy function is . In the walkthrough, , then , then . A real chain uses a cryptographic hash, where a one-character change in the data gives an output that cannot be predicted. The toy only imitates the dependence on the inputs.
Step 4 in symbols: verification
A verifier starts at the first block and checks . For each later block it checks and . The check uses only public data, so anyone can run it.
Step 6 in symbols: the cascade
Suppose the chain has blocks to , and block is altered. Its hash changes, so block must have its previous field updated to the new . That changes , which forces a change to block , and so on. Altering block therefore forces rewriting blocks. In the walkthrough, and , which gives three rewritten blocks.
Catching up in a race
Let the attacker produce a share of new blocks and the honest network a share . Use a simplified model in which each new block goes to the attacker with probability , independently of the others, and ignore timing. Suppose the attacker starts blocks behind. The probability that the attacker ever draws level is
Each block moves the race one step: the attacker gains one step with probability and the honest network with probability , so the gap is a Random Walk that drifts toward the honest side. From a deficit of one block, the attacker ever draws level with probability . To close a deficit of , it must do this times in a row, and the chances multiply, which gives . This is the standard gambler's-ruin result for a random walk that moves up with probability and down with probability . If the attacker eventually catches up with certainty, which is why the design assumes that no attacker holds a majority of the work.
Worked examples
Use the three-block chain from the walkthrough, with hashes 4, 5 and 2.
- An attacker changes the data of block 2 from 2 to 5.
- Its new hash is .
- A verifier who recomputes block 2 gets 5, but the honest copy stores 2. The stored value and the recomputed value differ, so the copy that was changed fails its own check.
- The attacker can avoid that failure by also writing 5 into the stored hash. Then block 2 looks consistent with itself and nothing after it needs repair, because there is no block 3.
- Check: the honest copies still hold 2, so comparing copies exposes the rewrite.
Interpretation: a change to the newest block costs one rewrite, which is why the newest records are the easiest to alter and why the protocol waits for more blocks before trusting a record.
Alice sends Bob coins in a block, then secretly builds a rival chain that sends the same coins back to her. Bob waits until his payment is five blocks deep. The attacker holds of the new blocks, so and .
- Compute the ratio .
- Raise it to the power : .
- Check: and , and is about 0.0145.
- Compare with one block of waiting: . The attacker catches up 43 percent of the time, which is why one confirmation is weak.
Interpretation: at five confirmations, the rival chain succeeds about 1.4 percent of the time in this model. Each extra block of depth multiplies the chance by .
A shop accepts a payment after six confirmations. Suppose an attacker holds of the new blocks, so and .
- The chance of catching up from six blocks behind is .
- Check: .
- Numerically, , which is about 1.9 in a million.
- Try it: the shop cuts its wait to two confirmations and the attacker's share stays at 10 percent. What is the chance of catching up from two blocks behind? Answer: , about 1.2 percent.
Interpretation: a small change in waiting time changes the risk by orders of magnitude, so a shop chooses its wait according to how much it stands to lose.
Common mistakes and tradeoffs
It is tempting to think that a blockchain makes its records true. In fact it makes them hard to change quietly and lets everyone see the same order of entries. Whether an entry is correct is a separate question, answered by the rules that define valid entries.
It is tempting to think that the hash chain alone makes history immutable. In fact the links only make changes visible to someone who compares copies. Without agreement on which chain counts, an attacker can present a rewritten copy. The consensus rule is what makes the link useful, as explored in Distributed Consensus.
It is tempting to think that a rewrite is impossible. In fact it is only expensive. The cost grows with the number of blocks after the change, and with the amount of work each block required, which is why the security argument depends on the attacker holding a minority of the work.
It is tempting to think that a payment is final when it is first recorded. In fact a payment becomes safer as more blocks are added on top of it, and the risk falls sharply, as Example 3 shows.
| Need | Better choice | Why |
|---|---|---|
| Many parties with no common operator | A public chain with Proof of Work or proof of stake | Anyone can join and verify, with no trusted keeper |
| Known members who share one record | A permissioned ledger with Byzantine fault tolerant voting | Faster finality, and no computing race |
| One organisation that trusts itself | A database with an audit log | Simpler, faster, and corrections are allowed |
When to use it. Use a blockchain when several parties must share one append-only record and none of them can serve as the trusted keeper.
When not to. Do not use it when one trusted database already does the job. Chains are slower, use more storage, and make corrections awkward, because the design is built to resist edits.
History
In 1991, Stuart Haber and W. Scott Stornetta proposed a scheme for time-stamping digital documents by linking each certificate to the one before it with a hash, which is the same basic structure. Ralph Merkle's hash tree of 1979 gave the chain a compact way to commit to many records, as described in Merkle Tree.
In October 2008, a paper under the name Satoshi Nakamoto described a payment system that uses a chain of blocks, Proof of Work and a peer-to-peer network to order transactions without a central keeper. The network began running in January 2009. The idea caught on because it made a shared ledger possible among strangers, and later systems extended the chain to run small programs, starting in 2015 with Ethereum.
Recap
- A block stores its data, the hash of the previous block, and its own hash.
- Altering a block breaks the next link, so a change must be repeated on every later block.
- Copies held by many computers, and a rule for choosing among chains, make the change visible.
- Proof of work makes rewriting expensive, and the chance of a successful rewrite falls with each extra block.
- Read next: Bitcoin: A peer-to-peer electronic cash system for the original design, and State Machine Replication for the general pattern of a replicated log.
Check yourself
QuestionRecall: what does each block store, and what does the stored previous hash let a verifier check?Show answer
Each block stores its data, the hash of the previous block, and its own hash. A verifier recomputes the block's hash from its data and previous hash, and checks that the stored previous hash equals the hash of the block before it. A mismatch shows that a record was altered.
QuestionComputation: in the walkthrough, the third block has hash 2. A fourth block arrives with data 9 and previous hash 2. What is its hash?Show answer
Using the toy hash, . The fourth block's hash is 3.
QuestionWhy: why does rewriting an old block not make the chain secure by itself?Show answer
Rewriting a block is possible, but it breaks the links after it, so the attacker must rewrite every later block. The honest copies still hold the original hashes, so a single rewritten copy is detected only when it is compared with them. Security therefore depends on the rule for choosing among chains and on the attacker lacking most of the computing work, not on the links alone.
QuestionApplied: a shop accepts a payment after one confirmation, and an attacker holds 10 percent of the new blocks. Using the catch-up model, what is the chance the attacker catches up from one block behind, and why is that worse than waiting six blocks?Show answer
The ratio is , so the chance is , about 11 percent. Waiting six blocks gives in a million. Each extra confirmation divides the chance by nine in this model, so the shop's wait sets its risk.