This repository is a compact Python playground that I originally built three years ago to introduce students to the core ideas that underpin blockchains: blocks, chain linkage, and consensus via proof-of-work. The code is intentionally small so learners can read every line during a single lecture or lab session.
block.py # Block data model and hashing
blockchain.py # Global chain list plus simple consensus function
main.py # Demo script creating and validating a handful of blocks
-
Blocks (
block.Block): each block stores the creator hash, the previous block hash (or a genesis marker), a timestamp, and arbitrary payload data. A SHA-256 digest of those fields plus a random nonce becomes the block hash. -
Chain (
blockchain.chain): an in-memory list that starts with a hard-coded genesis hash ("0"). New blocks always point to the previous block's hash, enforcing immutability. -
Consensus (
blockchain.consensus): mimics proof-of-work by requiring the block hash to start with five zeros. Formally,$hash(block)_{0..4} = 0$ . A block is appended to the chain only when it satisfies this rule; otherwise a new block is attempted.
Because Block mixes in a random nonce, repeated attempts eventually stumble upon a hash that passes the difficulty target, illustrating why mining consumes many tries.
- Ensure Python 3.8+ is installed. (A virtual environment is optional but recommended.)
- From the repository root, run:
python main.pyYou will see log lines each time a block satisfies consensus, followed by a printout of the in-memory chain. Expect multiple hashing attempts per block—the randomness makes runtime vary from seconds to minutes.
- Adjust difficulty: change the
"00000"prefix inblockchain.consensusto show how harder targets slow down mining. - Visualize links: have students print
block.previous_blockfor each block to highlight the tamper-evident chain. - Data integrity exercise: modify
block.block_dataafter mining to demonstrate how the hash no longer meets consensus. - Persistence extension: challenge students to write the chain to disk or expose it via a small API.
- No networking or peer-to-peer coordination; everything runs in a single process.
- Blocks are stored only in memory, so the chain disappears when the script ends.
- The proof-of-work target is fixed and does not retarget based on time.
Use these constraints as prompts for class discussions about what real-world blockchain systems must handle beyond this minimal prototype.