A functional blockchain implementation built from scratch in Python, demonstrating core distributed systems concepts including transaction broadcasting, block validation, consensus mechanisms, and chain conflict resolution.
Light Coin is an educational blockchain project that implements the fundamental principles of cryptocurrency systems. Built entirely in Python with a Flask REST API, it provides a hands-on exploration of how blockchains work under the hood.
- Proof of Work Consensus: Mining algorithm with adjustable difficulty
- Transaction Broadcasting: Peer-to-peer transaction propagation across nodes
- Wallet System: Public/private key cryptography for secure transactions
- Chain Conflict Resolution: Longest chain rule for consensus across the network
- REST API: Full HTTP interface for node communication and blockchain interaction
- Multi-Node Support: Run multiple nodes simultaneously to simulate a distributed network
light_coin/
├── blockchain.py # Core blockchain logic and chain management
├── block.py # Block structure and validation
├── transaction.py # Transaction creation and verification
├── wallet.py # Wallet management and cryptographic signing
├── node.py # Flask REST API for node communication
├── CLI_node.py # Command-line interface for node operations
└── Utilities/ # Helper functions and utilities
- Python 3.7+
- pip (Python package manager)
- Clone the repository:
git clone https://github.com/Aditya1001001/light_coin.git
cd light_coin- Install dependencies:
pip install -r requirements.txtStart a blockchain node on port 5001:
python node.py -p 5001Start additional nodes on different ports to simulate a network:
python node.py -p 5002
python node.py -p 5003Interact with your node using the command-line interface:
python CLI_node.pyGET /chain- Retrieve the full blockchainGET /mine- Mine a new blockPOST /transaction- Create a new transactionGET /transactions- View pending transactions
POST /node/register- Register a new node in the networkGET /node/resolve- Resolve chain conflicts using consensus algorithmGET /nodes- List all registered nodes
POST /wallet/create- Generate a new walletGET /wallet/balance- Check wallet balance
When a user creates a transaction, it's signed with their private key and broadcast to all nodes in the network:
# Example transaction structure
{
"sender": "public_key_here",
"recipient": "recipient_public_key",
"amount": 10,
"signature": "transaction_signature"
}Nodes compete to mine new blocks by solving a proof-of-work puzzle:
- Collect pending transactions
- Find a nonce that produces a hash with required leading zeros
- Broadcast the new block to the network
When conflicts arise (multiple valid chains), nodes use the longest chain rule:
- Query all nodes for their chains
- Compare chain lengths and validity
- Adopt the longest valid chain
Each block and the entire chain undergoes validation:
- Hash integrity checks
- Proof of work verification
- Transaction signature validation
- Previous hash linking
- Hashing: SHA-256 for block hashing and proof of work
- Digital Signatures: RSA for transaction signing and verification
- Public/Private Keys: Secure wallet management
This project demonstrates understanding of:
- Distributed Systems: Peer-to-peer networking and consensus
- Cryptography: Hash functions, digital signatures, public-key infrastructure
- Data Structures: Linked list implementation via block hashing
- API Design: RESTful architecture for blockchain interaction
- Conflict Resolution: Byzantine fault tolerance principles
Run multiple nodes locally to simulate a distributed network:
# Terminal 1
python node.py -p 5001
# Terminal 2
python node.py -p 5002
# Terminal 3 - Register nodes with each other
curl -X POST http://localhost:5001/node/register -H "Content-Type: application/json" -d '{"nodes": ["http://localhost:5002"]}'Create transactions, mine blocks on different nodes, and observe consensus in action.
- Language: Python 3
- Web Framework: Flask
- Cryptography: hashlib (SHA-256), Crypto (RSA)
- Data Persistence: File-based storage (JSON)
Built while learning from:
- Bitcoin whitepaper by Satoshi Nakamoto
- "Mastering Bitcoin" by Andreas Antonopoulos
- Various blockchain development tutorials and documentation
Aditya Singh
- GitHub: @Aditya1001001
- LinkedIn: aditya1001001
This project is open source and available for educational purposes.
Note: This is an educational project and should not be used for production cryptocurrency applications. It's designed to demonstrate blockchain concepts and is not optimized for security or scalability at an enterprise level.