DAY 24 / 40
>> Module 03 / Day 24 / Lecture

Building the Chain

Linking blocks — and breaking them

We have a block. Today we chain them together — and then deliberately tamper with one to see the chain shatter.

Lecture 75 min
Lab 60 min  ·  Build a 3-block chain
Module M3  ·  Blockchain Basics
>> Today's roadmap

From block to blockchain

  • Review: block structure recap
  • The linking rule: previous_hash
  • build_chain() — a list of linked blocks
  • Tamper demo — change one block, watch the chain break
  • Lab — build a 3-block chain manually
>> 01 / 03 · The key rule

Each block remembers the one before it

Every block stores the hash of the block before it in a field called previous_hash. This single rule creates the unbreakable link.

Block 0
index: 0
hash: a3f7...
prev: 0000...
← points to ←
Block 1
index: 1
hash: 9c82...
prev: a3f7...
← points to ←
Block 2
index: 2
hash: ef79...
prev: 9c82...
>> 02 / 03 · The code

Building the chain in Python

chain.py
def build_chain(): chain = [] # Block 0 — genesis genesis = make_block(0, "Genesis Block", "0" * 64) chain.append(genesis) # Block 1 — points to genesis block1 = make_block(1, "Alice sends 10 DOT to Bob", previous_hash=chain[-1]["hash"]) chain.append(block1) # Block 2 — points to block 1 block2 = make_block(2, "Bob sends 4 DOT to Carol", previous_hash=chain[-1]["hash"]) chain.append(block2) return chain chain = build_chain() for b in chain: print(b["index"], b["hash"][:12], b["previous_hash"][:12])
>> 03 / 03 · The tamper test

What happens if we cheat?

tamper.py
chain = build_chain() # Secretly change Block 1's data chain[1]["data"] = "Alice sends 1000 DOT to Bob" # TAMPER! # Now check: does Block 1's hash still match? print("Block 1 hash (stored) :", chain[1]["hash"][:20]) print("Block 1 hash (recalc) :", hash_block(chain[1])[:20]) # These two are different! Block 1 has been tampered with. # And Block 2's link is now broken too: print("Block 2 expects prev :", chain[2]["previous_hash"][:20]) print("Block 1 hash now is :", chain[1]["hash"][:20]) # Also different — the chain is invalid.
>> Real world anchor

This is exactly how Polkadot works

POLKADOT RELAY CHAIN

Every Polkadot block contains the hash of its parent block in the parent_hash field. This is visible on every block on Subscan. The chain of 22+ million blocks all link back to block 0.

VALIDATORS

In Polkadot, validators don't just trust the previous hash — they run complex state transition checks. But the core principle is the same: verify the link, detect any tampering.

Go to subscan.io right now → click any block → look at the "Parent Hash" field. That value matches the "Hash" of the block numbered one less. Same chain, production scale.

>> Clean code

A cleaner way — add_block()

add_block.py
def add_block(chain, data): previous_hash = chain[-1]["hash"] index = len(chain) new_block = make_block(index, data, previous_hash) chain.append(new_block) return new_block # Usage: chain = [make_block(0, "Genesis Block", "0" * 64)] add_block(chain, "Alice → Bob: 10 DOT") add_block(chain, "Bob → Carol: 4 DOT") add_block(chain, "Carol → Alice: 2 DOT") print(f"Chain length: {len(chain)} blocks")
>> 11:00 – 12:00 · Lab

Today's lab — Build a 3-Block Chain

CORE

Linked chain

Write add_block() and use it to build a chain of at least 3 blocks (genesis + 2 transactions). Print the index, hash (first 16 chars), and previous_hash (first 16 chars) of each block.

STRETCH ★

Tamper and detect

After building the chain, manually change the data in block 1. Print the stored hash vs the recalculated hash and show they differ. Extra: add an is_tampered(block) function.

>> End of Day 24

The chain is alive

✓ Blocks link via previous_hash — each block stores the hash of the one before it.
✓ add_block() makes it easy to grow the chain one block at a time.
✓ Tampering breaks the chain — stored hash no longer matches the recalculated hash.
01 / 9
←/→ navigate   F fullscreen