Skip to content

bbcoder-gh/blockchain-example

Repository files navigation

Educational Blockchain Implementation

A simple, educational blockchain implementation in Python that demonstrates the core concepts of blockchain technology.

🎓 Learning Objectives

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

🏗️ Project Structure

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

🚀 Quick Start

# Run the interactive demo
python demo.py

# Run tests
python tests.py

📚 Core Concepts

1. Blocks

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

2. Hashing

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.

3. Proof of Work

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.

4. Chain Validation

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

💡 Key Features

  • ✅ 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

🔧 Requirements

  • Python 3.7 or higher
  • No external dependencies (uses only standard library)

📖 Usage Examples

Creating a blockchain

from blockchain import Blockchain

# Create a new blockchain
blockchain = Blockchain(difficulty=4)

Adding transactions and mining

# Add transactions
blockchain.add_transaction("Alice", "Bob", 50)
blockchain.add_transaction("Bob", "Charlie", 25)

# Mine a new block
blockchain.mine_pending_transactions("Miner1")

Checking validity

# Validate the entire chain
is_valid = blockchain.is_chain_valid()
print(f"Blockchain valid: {is_valid}")

🎯 Educational Notes

Why Proof of Work?

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.

The Genesis Block

Every blockchain starts with a "genesis block" - the first block that has no previous block to reference.

Difficulty Adjustment

The difficulty parameter controls how many leading zeros are required in the hash. Higher difficulty = more secure but slower mining.

🧪 Testing

The project includes comprehensive tests covering:

  • Block creation and hashing
  • Chain validation
  • Mining functionality
  • Transaction handling
  • Tampering detection

🚧 Limitations (By Design)

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.

📝 License

MIT License - Feel free to use this for learning and teaching!

🤝 Contributing

This is an educational project. Feel free to:

  • Add more detailed comments
  • Improve the documentation
  • Add visualization features
  • Create additional examples

📚 Further Reading

About

A blockchain example for educational purposes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages