Search

Search papers and lessons

Blockchain

Level 3 · 13 min read

A blockchain is a list of blocks in which each block stores the hash of the one before, so changing an old record breaks every link after it.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Security & cryptography
  • Distributed systems
  • Calculus
  • Networking
  • ReadReady to readNot yet

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 (data+2×previous hash) mod 10(\text{data} + 2 \times \text{previous hash}) \bmod 10, where  mod 10\bmod 10 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.

  1. The first block. Its data is 4, standing for a small payment. Its hash is (4+2×0) mod 10=4(4 + 2 \times 0) \bmod 10 = 4. Stored: data 4, previous 0, hash 4. Check: the hash depends only on the block's own contents.

  2. 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 (7+2×4) mod 10=15 mod 10=5(7 + 2 \times 4) \bmod 10 = 15 \bmod 10 = 5. 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.

  3. The third block. Its data is 2, its previous hash is 5, and its hash is (2+2×5) mod 10=12 mod 10=2(2 + 2 \times 5) \bmod 10 = 12 \bmod 10 = 2. 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.

  4. 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.

  5. Tamper with the first block. An attacker changes its data from 4 to 6. Its hash becomes (6+0) mod 10=6(6 + 0) \bmod 10 = 6. 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 4≠64 \neq 6, so the link breaks. Check: the damage shows up at the next block, not at the changed one.

  6. Repair the links. To hide the change, the attacker must rewrite the second block: its previous hash becomes 6, and its hash becomes (7+2×6) mod 10=9(7 + 2 \times 6) \bmod 10 = 9. 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 (2+2×9) mod 10=0(2 + 2 \times 9) \bmod 10 = 0. Check: all three blocks changed, including the one the attacker first touched.

  7. Count the cost. The pattern is general. Changing block kk forces rewriting block kk 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

Definition: Blockchain

Let block ii hold data did_i, a previous hash pip_i and its own hash hih_i. A hash function HH gives hi=H(di,pi)h_i = H(d_i, p_i). The chain is valid when every hih_i recomputes from its contents and pi=hi−1p_i = h_{i-1} for every block after the first. By convention the first block has p0=0p_0 = 0.

Step 2 in symbols: the toy hash

The toy function is H(d,p)=(d+2p) mod 10H(d, p) = (d + 2p) \bmod 10. In the walkthrough, h0=4h_0 = 4, then h1=(7+2⋅4) mod 10=5h_1 = (7 + 2 \cdot 4) \bmod 10 = 5, then h2=(2+2⋅5) mod 10=2h_2 = (2 + 2 \cdot 5) \bmod 10 = 2. 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 H(d0,0)=h0H(d_0, 0) = h_0. For each later block ii it checks H(di,pi)=hiH(d_i, p_i) = h_i and pi=hi−1p_i = h_{i-1}. The check uses only public data, so anyone can run it.

Step 6 in symbols: the cascade

Suppose the chain has blocks 00 to nn, and block kk is altered. Its hash hkh_k changes, so block k+1k+1 must have its previous field updated to the new hkh_k. That changes hk+1h_{k+1}, which forces a change to block k+2k+2, and so on. Altering block kk therefore forces rewriting n−k+1n - k + 1 blocks. In the walkthrough, n=2n = 2 and k=0k = 0, which gives three rewritten blocks.

Catching up in a race

Let the attacker produce a share qq of new blocks and the honest network a share p=1−qp = 1 - q. Use a simplified model in which each new block goes to the attacker with probability qq, independently of the others, and ignore timing. Suppose the attacker starts zz blocks behind. The probability that the attacker ever draws level is

P=(qp)z,q<p.P = \left(\frac{q}{p}\right)^{z}, \qquad q \lt p.

Each block moves the race one step: the attacker gains one step with probability qq and the honest network with probability pp, 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 q/pq/p. To close a deficit of zz, it must do this zz times in a row, and the chances multiply, which gives (q/p)z(q/p)^z. This is the standard gambler's-ruin result for a random walk that moves up with probability qq and down with probability pp. If q≥pq \ge p the attacker eventually catches up with certainty, which is why the design assumes that no attacker holds a majority of the work.

Worked examples

Example
1. Changing the newest block (tiny)

Use the three-block chain from the walkthrough, with hashes 4, 5 and 2.

  1. An attacker changes the data of block 2 from 2 to 5.
  2. Its new hash is (5+2×5) mod 10=15 mod 10=5(5 + 2 \times 5) \bmod 10 = 15 \bmod 10 = 5.
  3. 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.
  4. 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.
  5. 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.

Example
2. A double spend with a thirty percent attacker (standard)

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 q=0.3q = 0.3 of the new blocks, so p=0.7p = 0.7 and z=5z = 5.

  1. Compute the ratio q/p=0.3/0.7=3/7≈0.4286q / p = 0.3 / 0.7 = 3/7 \approx 0.4286.
  2. Raise it to the power z=5z = 5: (3/7)5=243/16807≈0.0145(3/7)^5 = 243 / 16807 \approx 0.0145.
  3. Check: 35=2433^5 = 243 and 75=168077^5 = 16807, and 243/16807243 / 16807 is about 0.0145.
  4. Compare with one block of waiting: (3/7)1≈0.43(3/7)^1 \approx 0.43. 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 q/pq / p.

Example
3. A shop with a ten percent attacker (realistic)

A shop accepts a payment after six confirmations. Suppose an attacker holds q=0.1q = 0.1 of the new blocks, so p=0.9p = 0.9 and q/p=1/9q / p = 1/9.

  1. The chance of catching up from six blocks behind is (1/9)6=1/531441(1/9)^6 = 1/531441.
  2. Check: 96=5314419^6 = 531441.
  3. Numerically, 1/531441≈0.00000191/531441 \approx 0.0000019, which is about 1.9 in a million.
  4. 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: (1/9)2=1/81≈0.0123(1/9)^2 = 1/81 \approx 0.0123, 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.

NeedBetter choiceWhy
Many parties with no common operatorA public chain with Proof of Work or proof of stakeAnyone can join and verify, with no trusted keeper
Known members who share one recordA permissioned ledger with Byzantine fault tolerant votingFaster finality, and no computing race
One organisation that trusts itselfA database with an audit logSimpler, 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, (9+2×2) mod 10=(9+4) mod 10=13 mod 10=3(9 + 2 \times 2) \bmod 10 = (9 + 4) \bmod 10 = 13 \bmod 10 = 3. 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 q/p=1/9q / p = 1/9, so the chance is 1/9≈0.1111/9 \approx 0.111, about 11 percent. Waiting six blocks gives (1/9)6≈1.9(1/9)^6 \approx 1.9 in a million. Each extra confirmation divides the chance by nine in this model, so the shop's wait sets its risk.

Sources