A simple, educational blockchain implementation in Python that demonstrates the core concepts of blockchain technology.
This project helps you understand:
- How blocks are structured and linked together
- Cryptographic hashing and chain integrity
- Proof of Work consensus mechanism
- Transaction management
- Chain validation
blockchain-edu/
├── blockchain.py # Core Blockchain class
├── block.py # Block data structure
├── transaction.py # Transaction handling
├── demo.py # Interactive demonstration
├── tests.py # Unit tests
└── README.md # Documentation
# Run the interactive demo
python demo.py
# Run tests
python tests.py
Each block contains:
- Index: Position in the chain
- Timestamp: When the block was created
- Transactions: List of transactions in this block
- Previous Hash: Hash of the previous block (creates the "chain")
- Nonce: Number used in mining (Proof of Work)
- Hash: This block's unique identifier
We use SHA-256 to create a unique fingerprint for each block. Any change to the block data completely changes the hash, making tampering obvious.
Mining requires finding a hash that starts with a certain number of zeros. This makes it computationally expensive to add blocks, securing the chain against attacks.
The blockchain validates itself by:
- Checking that each block's hash is correct
- Verifying the chain of previous hashes is intact
- Ensuring Proof of Work requirements are met
- ✅ Block creation and hashing
- ✅ Proof of Work mining with adjustable difficulty
- ✅ Transaction handling
- ✅ Chain validation and integrity checks
- ✅ Detailed logging and visualization
- ✅ Comprehensive test suite
- Python 3.7 or higher
- No external dependencies (uses only standard library)
from blockchain import Blockchain
# Create a new blockchain
blockchain = Blockchain(difficulty=4)
# Add transactions
blockchain.add_transaction("Alice", "Bob", 50)
blockchain.add_transaction("Bob", "Charlie", 25)
# Mine a new block
blockchain.mine_pending_transactions("Miner1")
# Validate the entire chain
is_valid = blockchain.is_chain_valid()
print(f"Blockchain valid: {is_valid}")
Proof of Work makes it expensive to modify past blocks. If someone tries to change an old block, they'd need to re-mine that block AND all subsequent blocks, which requires enormous computational power.
Every blockchain starts with a "genesis block" - the first block that has no previous block to reference.
The difficulty parameter controls how many leading zeros are required in the hash. Higher difficulty = more secure but slower mining.
The project includes comprehensive tests covering:
- Block creation and hashing
- Chain validation
- Mining functionality
- Transaction handling
- Tampering detection
This is an educational implementation. It intentionally omits:
- Network/peer-to-peer communication
- Merkle trees
- Digital signatures
- UTXO model
- Consensus mechanisms beyond Proof of Work
- Persistent storage
These features are important for production blockchains but are excluded here to keep the core concepts clear and understandable.
MIT License - Feel free to use this for learning and teaching!
This is an educational project. Feel free to:
- Add more detailed comments
- Improve the documentation
- Add visualization features
- Create additional examples