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

Anatomy of a Block

A block is just a Python dictionary

We have Python. We have hashing. Today we build the first real unit of the blockchain — the block itself.

Lecture 75 min  ·  09:00–10:15
Lab 60 min  ·  Write make_block()
Module M3  ·  Blockchain Basics
>> Today's roadmap

From hash to block

  • What's in a block? The fields every block must have.
  • The block as a Python dict — make_block().
  • Computing the block's hash — hash_block().
  • The genesis block — the very first block.
  • Lab — create your first block.
>> 01 / 03 · Real world first

What does a real Polkadot block look like?

Before we write any code, let's look at the real thing. Here is what you see when you open Subscan (Polkadot's block explorer) and click on any block.

POLKADOT BLOCK FIELDS
number: 22,847,391
hash: 0x1a3f...
parent_hash: 0x9c82...
state_root: 0xef79...
extrinsics_root: 0x5fee...
timestamp: 2026-06-12 09:00:00
OUR TOY BLOCK FIELDS
index: 1
timestamp: "2026-06-12 09:00:00"
data: "Alice → Bob: 10 DOT"
previous_hash: "0000...0000"
hash: (calculated from all the above)
>> 02 / 03 · Building the block

make_block() — the blueprint

block.py
import hashlib, time def hash_block(block): content = ( str(block["index"]) + block["timestamp"] + str(block["data"]) + block["previous_hash"] ) return hashlib.sha256(content.encode()).hexdigest() def make_block(index, data, previous_hash): block = { "index": index, "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"), "data": data, "previous_hash": previous_hash, "hash": "", # fill in next } block["hash"] = hash_block(block) return block
>> Inside hash_block()

How we fingerprint a block

To hash a block, we concatenate all its fields into one long string, then run SHA-256 on it.

hash_breakdown.py
block = { "index": 1, "timestamp": "2026-06-12 09:00:00", "data": "Alice sent 10 DOT to Bob", "previous_hash": "0000000000000000", } # The string we hash: content = "1" + "2026-06-12 09:00:00" + "Alice sent 10 DOT to Bob" + "0000000000000000" block["hash"] = hashlib.sha256(content.encode()).hexdigest() print(block["hash"]) # a 64-character hex fingerprint
>> 03 / 03 · The first block

The genesis block — where every chain begins

The very first block is special: it has no previous block. So we invent a previous_hash of all zeros — a known "nothing" value.

genesis.py
genesis = make_block( index = 0, data = "Genesis Block", previous_hash = "0" * 64, # no previous block ) print(genesis) # { # "index": 0, # "timestamp": "2026-06-12 09:00:00", # "data": "Genesis Block", # "previous_hash": "0000...0000", # "hash": "a3f7..." # }
POLKADOT'S GENESIS

Polkadot's genesis block was created on 26 May 2020. Its hash and initial state were set by the network founders.

IMMUTABLE START

The genesis block can never be changed. Every subsequent block's chain of hashes ultimately links back to it.

>> Putting it together

Create and print your first block

first_block.py
import hashlib, time # ... (paste hash_block and make_block here) ... # Create the genesis block genesis = make_block(0, "Genesis Block", "0" * 64) print("Index :", genesis["index"]) print("Timestamp :", genesis["timestamp"]) print("Data :", genesis["data"]) print("Prev hash :", genesis["previous_hash"][:16], "...") print("Hash :", genesis["hash"])
>> 11:00 – 12:00 · Lab

Today's lab — Your First Block

CORE

make_block() from scratch

Write hash_block() and make_block() yourself (no copy-paste). Create the genesis block and one more block after it. Print all fields.

STRETCH ★

Multiple transactions

Change the data field to be a list of transactions [{"from":"Alice","to":"Bob","amount":10}] and update hash_block to handle it.

>> End of Day 23

A block is done

✓ A block is a Python dict: index, timestamp, data, previous_hash, hash.
✓ hash_block() fingerprints all fields together — any change breaks it.
✓ The genesis block starts the chain with a previous_hash of all zeros.
01 / 9
←/→ navigate   F fullscreen