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

Validating the Chain

Catching tamperers — automatically

We can build a chain and tamper with it. Today we write the code that catches the tampering — is_valid_chain().

Lecture75 min
Lab60 min · Tamper and detect
ModuleM3 · Blockchain Basics
DAY 25 / 40
>> Today's roadmap

Detecting a broken chain

  1. What does "valid" mean for a chain?
  2. Check 1 — recalculate each block's hash
  3. Check 2 — verify each link (previous_hash)
  4. is_valid_chain() — the full function
  5. Lab — tamper with a block and prove validation fails
DAY 25 / 40
>> 01 / 03 · The rules

A chain is valid if two conditions hold

CHECK 1: HASH INTEGRITY

Every block's stored hash must equal what we get when we recalculate it right now. If even one character of the block was changed after it was created, the hash won't match.

CHECK 2: CHAIN LINK INTEGRITY

Every block's previous_hash must equal the actual hash of the block that came before it. This proves the blocks are in the correct order and none have been inserted or swapped.

DAY 25 / 40
>> 02 / 03 · The function

is_valid_chain() — the full validator

validate.py
def is_valid_chain(chain): for i in range(len(chain)): block = chain[i] # Check 1: stored hash must match recalculated hash if block["hash"] != hash_block(block): print(f"Block {i}: hash is invalid!") return False # Check 2: previous_hash must match prior block's hash if i > 0: # skip genesis block if block["previous_hash"] != chain[i - 1]["hash"]: print(f"Block {i}: link is broken!") return False return True # all checks passed chain = build_chain() print(is_valid_chain(chain)) # True
DAY 25 / 40
>> 03 / 03 · The tamper test

Prove it catches cheating

tamper_demo.py
chain = build_chain() print("Before tamper:", is_valid_chain(chain)) # True # Secretly change block 1's data chain[1]["data"] = "Alice sends 9999 DOT to Bob" print("After tamper:", is_valid_chain(chain)) # Block 1: hash is invalid! # False # What about recalculating the hash too? chain[1]["hash"] = hash_block(chain[1]) # update block 1's hash print("After hash fix:", is_valid_chain(chain)) # Block 2: link is broken! # False — block 2 still stores the OLD hash of block 1
DAY 25 / 40
>> Real world anchor

What Polkadot validators actually check

OUR CHAIN

We check: (1) hash integrity, (2) chain link. Runs in milliseconds on a laptop. Great for understanding the concept.

POLKADOT VALIDATORS

Also check: digital signatures on every extrinsic (transaction), account balances, runtime logic, state transitions. The concept is the same; the implementation is vastly more complex.

The key insight: Polkadot validators are doing is_valid_chain() — just with hundreds of additional rules layered on top. You now understand the foundation.
DAY 25 / 40
>> Progress check

What you've built across Days 22–25

D22 HASHING
hash_block()

SHA-256 fingerprint of any block's fields.

D23–24 THE CHAIN
make_block(), add_block()

Creates and links blocks.

D25 VALIDATION
is_valid_chain()

Verifies every hash and link.

DAY 25 / 40
>> 11:00 – 12:00 · Lab

Today's lab — Tamper and Detect

CORE
Validation in action
  • ›Build a chain of 4 blocks. Run is_valid_chain() → True.
  • ›Tamper with block 2's data. Run again → False.
  • ›Tamper with block 3's previous_hash. Run → False.
  • ›Print which block fails and why.
STRETCH ★
Full report

Instead of returning True/False, return a list of error messages. For each invalid block, include: block index, which check failed, and the two hash values that don't match.

DAY 25 / 40
>> End of Day 25

The chain is tamper-proof

  • ✓Check 1: every block's stored hash must equal its recalculated hash.
  • ✓Check 2: every block's previous_hash must match the prior block's actual hash.
  • ✓Fixing one block just breaks the next — you can't silently rewrite history.
01 / 9
←/→ navigate   F fullscreen