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.
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.
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 blockTo hash a block, we concatenate all its fields into one long string, then run SHA-256 on it.
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 fingerprintThe very first block is special: it has no previous block. So we invent a previous_hash of all zeros — a known "nothing" value.
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 block was created on 26 May 2020. Its hash and initial state were set by the network founders.
The genesis block can never be changed. Every subsequent block's chain of hashes ultimately links back to it.
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"])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.
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.