A comprehensive educational blockchain implementation built in Python, demonstrating core blockchain concepts including mining, proof-of-work, and chain validation.
- Overview
- Features
- Project Structure
- Installation
- Usage
- Key Concepts Demonstrated
- Technical Implementation
- Example Output
- Educational Value
- Contributing
- License
This project implements a simplified blockchain from scratch to demonstrate fundamental blockchain concepts. It includes two implementations:
- Core Implementation: Complete blockchain with mining and validation
- Simple Demo: Interactive demonstration for educational presentations
Perfect for understanding how cryptocurrencies like Bitcoin work under the hood.
- β Block Structure: Index, timestamp, data, previous hash, nonce, and hash
- β Cryptographic Hashing: MD5 implementation for educational purposes
- β Mining Algorithm: Proof-of-Work with adjustable difficulty
- β Chain Validation: Integrity checking and tampering detection
- β Genesis Block: Automatic creation of the first block
- β Live Mining Visualization: Real-time hash generation display
- β Adjustable Difficulty: 1-5+ leading zeros requirement
- β Nonce Iteration: Demonstrates computational work required
- β Performance Metrics: Tracks attempts and mining time
- β Interactive Demo: Menu-driven interface for presentations
- β Tampering Demonstration: Shows how blockchain detects fraud
- β Professional Output: Clean, emoji-free terminal interface
- β Step-by-step Visualization: Perfect for learning and teaching
BlockChain/
βββ SRC/ # Main blockchain implementation
β βββ Block.py # Core block class with mining functionality
β βββ Blockchain.py # Main blockchain class with validation
β βββ live_demo.py # Interactive demonstration script
βββ CONTRIBUTING.md # Contribution guidelines
βββ LICENSE # MIT License
βββ README.md # This documentation
βββ requirements.txt # Python dependencies (none required)
- Python 3.7 or higher
- No external dependencies required (uses built-in libraries)
git clone https://github.com/yourusername/blockchain-implementation.git
cd blockchain-implementation# Interactive live demo
cd SRC
python live_demo.py# Navigate to the SRC directory first
cd SRC
from Blockchain import Blockchain
from Block import Block
# Create a new blockchain
bc = Blockchain()
# Add blocks without mining
bc.add_block("Nirmal sends 10 coins to Shivangi")
# Add blocks with mining (difficulty = leading zeros required)
bc.add_block("Shivangi sends 5 coins to Nirmal", difficulty=3)
# Validate the blockchain
print(f"Blockchain valid: {bc.is_valid()}")
# Print the entire chain
bc.print_chain()For live presentations and demonstrations:
cd SRC
python live_demo.pyMenu Options:
- Add block without mining
- Mine with difficulty 1 (1 leading zero)
- Mine with difficulty 2 (2 leading zeros)
- Mine with difficulty 3 (3 leading zeros)
- Custom difficulty mining
- Display full blockchain
- Demonstrate tampering detection
- Validate blockchain integrity
Each block contains:
- Data: Transaction or information
- Previous Hash: Links to previous block
- Nonce: Number used once (for mining)
- Hash: Unique identifier calculated from all block data
Blocks are cryptographically linked using hashes, creating an immutable chain.
- Miners must find a hash that starts with specific number of zeros
- Requires computational work (trying different nonce values)
- Higher difficulty = more zeros required = more work
- Any change to block data invalidates the hash
- Broken chain is immediately detectable
- Demonstrates blockchain's security properties
- MD5: Used for educational simplicity
- Note: Real blockchains use SHA-256 for security
def mine_block(self, difficulty):
target = "0" * difficulty # e.g., "000" for difficulty 3
while not self.hash.startswith(target):
self.nonce += 1
self.hash = self.calculate_hash()
print(f"Block mined! Hash: {self.hash}")def is_valid(self):
for i in range(1, len(self.chain)):
current = self.chain[i]
previous = self.chain[i-1]
# Verify hash integrity
if current.hash != current.calculate_hash():
return False
# Verify chain linking
if current.previous_hash != previous.hash:
return False
return TrueMINING STARTED
Data: Nirmal sends 50 coins to Shivangi
Target: Hash starting with '000' (3 zeros)
Mining in progress...
Attempt 1: nonce= 1 | hash=d29306c951af3e9ee924896cc41aabcf | match=d29
Attempt 2: nonce= 2 | hash=98ef5ce835b3ebc53206f1b1ac97cd39 | match=98e
...
Attempt 1556: nonce= 1556 | hash=00032c27584e9c0a441c6c31b801a97c | match=000
BLOCK SUCCESSFULLY MINED!
Final hash: 00032c27584e9c0a441c6c31b801a97c
Nonce used: 1556
Total attempts: 1556
============================================================
BLOCKCHAIN (Total Blocks: 3)
============================================================
BLOCK #0:
BLOCK INFO:
Data: Genesis Block
Previous Hash: 0
Nonce: 0
Hash: f865832f11e813271da3b5410d6f2910
BLOCK #1:
BLOCK INFO:
Data: Nirmal sends 50 coins to Shivangi
Previous Hash: f865832f11e813271da3b5410d6f2910
Nonce: 1556
Hash: 00032c27584e9c0a441c6c31b801a97c
============================================================
Blockchain Valid: True
============================================================
This implementation is perfect for:
- Computer Science Students: Understanding blockchain fundamentals
- Presentations: Live mining demonstrations
- Interview Preparation: Showcasing blockchain knowledge
- Teaching: Clear, commented code for instruction
- Portfolio Projects: Demonstrating programming and cryptography skills
- Object-Oriented Programming: Clean class structure and inheritance
- Cryptography: Hash functions and digital signatures concepts
- Algorithms: Proof-of-work mining implementation
- Data Structures: Linked list representation of blockchain
- Software Design: Modular, extensible architecture
- Documentation: Comprehensive code comments and README
Potential improvements for advanced learning:
- Network simulation with multiple nodes
- Transaction pools and block rewards
- Digital signatures for transaction validation
- Merkle trees for efficient verification
- REST API for blockchain interaction
- Web interface for visualization
- Performance optimizations
| Difficulty | Average Attempts | Time (approx.) |
|---|---|---|
| 1 | ~16 | <1 second |
| 2 | ~256 | 1-3 seconds |
| 3 | ~4,096 | 10-30 seconds |
| 4 | ~65,536 | 5-15 minutes |
Note: Times vary based on hardware and random nature of mining
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Nirmal
- GitHub: @nirmal1090
Shivangi
- GitHub: @shivangipatel2508
- Inspired by Bitcoin's blockchain implementation
- Built for educational purposes to understand cryptocurrency fundamentals
- Designed for computer science students and blockchain enthusiasts
β If this project helped you understand blockchain technology, please give it a star!