Sub topic 1
What a Blockchain Ledger Does
Prerequisites
This chapter assumes you know the following:
- What a cryptocurrency is, at the level of "digital money that is not issued by a bank".
- What a cryptographic hash function is, and the idea of a public/private key pair, at a high level.
- How to read short Python snippets.
You do not need to know anything about Bitcoin or Ethereum internals yet. Later chapters build on this one.
The problem a ledger solves
A ledger is a record of who owns what and of how ownership changed. A bank keeps one. When you pay someone, the bank subtracts from your balance and adds to theirs. Everyone trusts the bank to keep the record correct and to refuse impossible changes.
Digital money has a specific difficulty. Digital data can be copied for free. If a coin is just a file, you could send the same file to two people. This is the double-spend problem. A ledger prevents it by recording each transfer in one agreed order, so the second attempt to spend the same coin is visibly invalid.
A blockchain is a ledger that solves this without a single trusted keeper. Many computers, called nodes, each hold a copy of the ledger. They follow shared rules to decide which new entries are valid and in what order they count.
The ledger as a chain of blocks
A blockchain groups transactions into blocks. Each block contains:
- A list of transactions.
- A timestamp and other metadata.
- The hash of the previous block, called the parent hash.
A hash is a fixed-size fingerprint of some data. Changing even one bit of the input produces a completely different fingerprint. Because each block includes the hash of the block before it, the blocks form a chain. The hash of a block depends on everything inside it, including the parent hash, so it depends indirectly on the entire history before it.
The first block has no parent. It is called the genesis block and is written into the software itself.
Why tampering is detectable
Suppose someone edits a transaction in block 1. The hash of block 1 changes. Block 2 stores the old hash of block 1, so block 2 no longer matches. To hide the edit, the attacker must recompute block 2, which changes its hash, which breaks block 3, and so on up to the newest block. On a network where creating a valid block is expensive (see the chapter on consensus), redoing all those blocks is impractical.
Here is a small, runnable demonstration of that chain-breaking effect.
import hashlib
import json
def block_hash(block):
data = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(data).hexdigest()
def make_chain(tx_lists):
chain = []
parent = "0" * 64
for height, txs in enumerate(tx_lists):
block = {"height": height, "parent": parent, "txs": txs}
chain.append(block)
parent = block_hash(block)
return chain
def is_valid(chain):
for i in range(1, len(chain)):
if chain[i]["parent"] != block_hash(chain[i - 1]):
return False
return True
chain = make_chain([
["genesis"],
["alice->bob:5"],
["bob->carol:2"],
["carol->dave:1"],
])
print("valid before edit:", is_valid(chain))
chain[1]["txs"] = ["alice->mallory:5"]
print("valid after edit: ", is_valid(chain))The first check prints True. After the edit, block 2's stored parent hash no longer matches the recomputed hash of block 1, so the second check prints False.
Notice what this code does not do. It has no rule about who may add a block or how much work it takes. Anyone can rebuild the whole chain with consistent hashes. Hash links make tampering detectable. Making tampering costly is the job of the consensus mechanism, which the chapter after next covers.
What the ledger records
Blockchains differ in what a transaction looks like and what state the nodes track.
- Bitcoin records transfers of unspent outputs. The ledger's state is the set of coins that can still be spent.
- Ethereum records transactions that change account balances and contract storage. The ledger's state is a table of accounts.
Both designs get their own chapters. For now, the shared idea matters: the chain is a history of changes, and the state is the result of replaying that history from the genesis block. Any node can verify the current state for itself by replaying every transaction from the start and checking each one against the rules.
What nodes check
A node does not trust a block because it arrived from the network. It checks the block against the rules. Typical checks include:
- The block's format is valid and its parent hash points to a block the node already knows.
- Each transaction carries a valid digital signature from the owner of the funds being spent.
- Each transaction spends funds that exist and have not already been spent.
- The block satisfies the consensus rules, such as the proof-of-work target in Bitcoin.
- The block creator's reward is no more than the protocol allows.
If any check fails, the node rejects the block, no matter who sent it. This is why the rules, not the participants, are the source of trust. A miner or validator cannot create coins out of thin air, because every honest node would reject the block.
Ordering, forks, and finality
All nodes must agree on one order of transactions. Two valid blocks can appear at nearly the same time, each pointing to the same parent. This creates a temporary fork. Different nodes may see different tips of the chain for a short time.
The protocol includes a fork-choice rule to pick one branch. In Bitcoin, nodes follow the valid chain with the most accumulated proof of work. The losing branch is abandoned, and its transactions return to the pool of unconfirmed transactions unless they are already in the winning branch.
This has a practical consequence. A transaction in the newest block can still be reversed if a competing branch wins. Each additional block built on top makes reversal less likely. This is why services wait for several confirmations, meaning blocks stacked on top of the one containing your transaction. This is probabilistic finality: the chance of reversal shrinks with depth but never reaches exactly zero in proof-of-work systems. Some proof-of-stake systems add an explicit finalization step, which a later chapter covers.
Public ledger, pseudonymous users
Most major blockchains are public. Anyone can download the ledger and read every transaction. Participants appear as addresses, which are strings derived from public keys, rather than as names. This is pseudonymity, not anonymity. If an address is ever linked to a real person, for example through an exchange account, the whole history of that address becomes linked to them. The next chapter explains addresses and keys in detail.
Costs and limits of the design
Replicating the ledger on thousands of nodes has real costs.
| Property | Benefit | Cost |
|---|---|---|
| Every node stores and checks everything | No single point of failure; anyone can verify | Limited throughput and growing storage |
| Append-only history | Hard to rewrite; auditable | Mistakes cannot be edited away |
| Open participation | No permission needed to use or run a node | Must defend against attackers who join freely |
| Public data | Transparency | Weak privacy for users |
These costs recur in every later chapter. A blockchain is slower and more expensive than a normal database for most jobs. It makes sense only when you need many parties who do not trust each other to share one record without a central operator.
Common errors to avoid
- "The blockchain stores coins." It stores records of transfers. Ownership is shown by the ability to produce a valid signature, not by a file on your computer.
- "Immutable means impossible to change." A chain can be rewritten if an attacker controls enough mining power or stake. Immutable here means changing it is costly and detectable.
- "A confirmed transaction is final." Confirmation depth sets the risk level. One confirmation is weaker than six.
- "Hash links alone secure the chain." As the Python example shows, anyone can recompute hashes. Security also needs a cost to create valid blocks.
- "Public means anonymous." Public ledgers are traceable by design.
Summary
A blockchain ledger is a shared, append-only record of transactions, grouped into blocks that are linked by hashes. Every node validates every block against the same rules, so no single party decides what is true. Hash links reveal tampering, a consensus mechanism makes rewriting expensive, and a fork-choice rule settles temporary disagreements. The cost is lower throughput, weaker privacy, and slower change than a central database.
Exercise
In the Python example, you edit the transactions in block 1 and also recompute and overwrite the parent hash stored in block 2 so that block 2 matches. Does is_valid return True or False, and what does this show about hash links?
Show answer
It still returns False, because block 2's changed contents give it a new hash, and block 3 still stores the old hash of block 2. The attacker must repair every later block as well. In this toy code, though, nothing stops them from doing that, so is_valid would return True once all later blocks are repaired. This shows that hash links only make tampering detectable and force the attacker to redo work. Making that work expensive is the job of the consensus mechanism.
Exercise
A merchant accepts a payment after one confirmation. Later, a competing branch with more accumulated proof of work overtakes the chain containing that payment. What happened to the transaction, and what could the merchant have done to lower the risk?
Show answer
The block containing the payment became part of an abandoned branch, so the payment is no longer confirmed. If the sender did not also include the transaction in the winning branch, it returns to the unconfirmed pool and may never be confirmed. The merchant could have waited for more confirmations before delivering goods, since each extra block makes a reversal less likely.