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

The Complete Picture

Eight days of building — all connected

Today we pause building and zoom out. We'll walk through the complete tiny blockchain end-to-end, connect every piece to a real Polkadot concept, and clear up misconceptions before the mini-project.

Lecture 75 min
Lab 60 min · Run the full chain
M3 · Blockchain Basics
>> The journey

Eight days of building — one system

D22
HASHING
hashlib.sha256() — the tamper-proof fingerprint
D23
ANATOMY
make_block() — index, timestamp, data, hashes
D24
CHAIN
add_block() — linking via previous_hash
D25
VALIDATION
is_valid_chain() — detecting tampered blocks
D26
PROOF OF WORK
mine_block() — making tampering expensive
D27
PROOF OF STAKE
weighted_choice() — Polkadot's consensus model
D28
TRANSACTIONS
{from, to, amount} packed into block data
>> 01 / 03 · Full code walkthrough

Part 1 — the block factory

blockchain.py — Part 1
import hashlib, time, random # ── HASHING ────────────────────────────────────────── def hash_block(block): content = (str(block["index"]) + block["timestamp"] + str(block["transactions"]) + block["previous_hash"] + str(block["nonce"])) return hashlib.sha256(content.encode()).hexdigest() # ── BLOCK CREATION ─────────────────────────────────── def make_block(index, transactions, previous_hash, nonce=0): block = { "index": index, "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"), "transactions": transactions, "previous_hash": previous_hash, "nonce": nonce, "hash": "", } block["hash"] = hash_block(block) return block
>> 02 / 03 · Full code walkthrough

Part 2 — mining and growing the chain

blockchain.py — Part 2
# ── MINING ─────────────────────────────────────────── def mine_block(index, transactions, previous_hash, difficulty=3): target = "0" * difficulty nonce = 0 while True: block = make_block(index, transactions, previous_hash, nonce) if block["hash"].startswith(target): return block nonce += 1 # ── CHAIN OPERATIONS ────────────────────────────────── def add_block(chain, transactions, difficulty=3): chain.append(mine_block(len(chain), transactions, chain[-1]["hash"], difficulty)) def is_valid_chain(chain): for i, b in enumerate(chain): if b["hash"] != hash_block(b): return False if i > 0 and b["previous_hash"] != chain[i-1]["hash"]: return False return True
>> 03 / 03 · Full code walkthrough

Part 3 — building and running the chain

blockchain.py — Part 3
# ── START THE CHAIN ─────────────────────────────────── chain = [make_block(0, [], "0" * 64)] # genesis # Add blocks with transactions add_block(chain, [{"from":"Alice","to":"Bob","amount":10}]) add_block(chain, [{"from":"Bob","to":"Carol","amount":4}, {"from":"Alice","to":"Dave","amount":2}]) # View the chain for b in chain: print(f"Block {b['index']}: {len(b['transactions'])} txns | " f"hash={b['hash'][:12]}...") # Validate print("Valid:", is_valid_chain(chain)) # True
>> Connecting to Polkadot

Our tiny chain vs the real Polkadot

Feature Our Tiny Blockchain Polkadot Relay Chain
Block structure index, timestamp, transactions,
prev_hash, nonce, hash
number, parent_hash, state_root,
extrinsics_root, timestamp
Consensus Proof of Work (nonce guessing) NPoS — 300 validators elect block authors
Validation hash integrity + chain links signatures + state transitions + runtime logic
Persistence Python list in memory RocksDB database on every validator node
>> Clearing the air

Five things people get wrong about blockchain

  1. "Blockchain is the same as Bitcoin" — Bitcoin is one blockchain. There are thousands. Polkadot is another.
  2. "Blockchain is unhackable" — The chain is tamper-evident, not unhackable. Smart contracts and wallets are frequently exploited.
  3. "All blockchains use PoW" — Polkadot, Ethereum 2.0, Cardano all use Proof of Stake variants.
  4. "Transactions are anonymous" — They're pseudonymous. All transactions are public; the address isn't linked to a name by default.
  5. "Blockchain solves everything" — It solves the trust/immutability problem. For most databases a regular SQL db is faster and cheaper.
>> 11:00 – 12:00 · Lab

Today's lab — Run the Full Chain

CORE
End-to-end run
Assemble all the functions from Days 22–28 into one file. Build a chain of 5 blocks, each with 2–3 transactions. Validate the chain. Print a summary: total blocks, total transactions, final hash.
★ STRETCH
Save to JSON
After building the chain, save it to chain.json using the json module. Then load it back and validate the loaded chain. Prove persistence works.
>> End of Day 29

The system is complete

  • ✓ All pieces assembled: hash, block, chain, validation, mining, transactions.
  • ✓ Our chain mirrors real Polkadot structure — same concepts, production complexity.
  • ✓ Misconceptions cleared: pseudonymous not anonymous, not unhackable, PoW is just one option.
01 / 9
←/→ navigate   F fullscreen