A simple blockchain simulation project that demonstrates the basic concepts of blockchain technology including blocks, mining, transactions, and peer-to-peer networking.
- β Block creation with sender, recipient, amount, timestamp, and hash
- π SHA-256 hashing (with anonymized sender/recipient via hashed usernames)
- βοΈ Proof-of-Work mining algorithm with configurable difficulty
- π§± Genesis block generation
- ποΈ LevelDB integration for persistent storage (like Bitcoin's own design)
- π Blockchain validation & integrity check
- π¦ Fully CLI-based interaction β no frontend, no GUI
The blockchain utilizes LevelDB for data persistence, providing a lightweight and efficient key-value storage solutionβsimilar to the mechanism used in Bitcoin's internal architecture.
- Python 3.10
- LevelDB (via
plyvellibrary) - SHA-256 hashing (via Python's
hashlib) - JSON for block serialization
.
βββ block.py # Block class definition
βββ blockchain.py # Blockchain implementation with storage
βββ config.py # Configuration settings for nodes
βββ main.py # CLI interface for interacting with the blockchain
βββ node.py # FastAPI server for P2P communication
βββ requirements.txt # Project dependencies
βββ transaction.py # (Empty file, possibly for future use)
- Clone the repository:
git https://github.com/Finsa-SC/BlockChain
cd BlockChain- Create and activate a virtual environment:
python -m venv .venv
.venv\Scripts\activate # On Windows:
source .venv/bin/activate # On Linux:
- Install the required dependencies:
pip install -r requirements.txt- Make sure you have LevelDB installed on your system, as the project uses PlyveL which requires it:
- On Ubuntu/Debian:
sudo apt-get install libleveldb-dev - On macOS:
brew install leveldb - On Windows: Follow these instructions
- On Ubuntu/Debian:
like blockchain in general, I made this blockchain simulation work in many nodes, but you run it on 1 device using a local host
-
- Set
PORTto the port you want your node to run on (e.g., 8000, 8001, 8002) - Update
PEERSlist with other nodes you want to connect to
- Set
-
if you want to run more than 1 node, then you must also copy the main folder. Because to run it must be different programs, but still connected using the API. the number of folders matches the number of nodes you want to run
-
you must move to the directory to each program through each terminal as well and run the API according to the terminal
uvicorn node:app --port 8000 #in terminal 1
uvicorn node:app --port 8001 #in terminal 2
#and so on according to how many nodes you want-
Make sure at least one node is running (default on port 8000).
-
Run the client interface:
python main.py- Use the interactive menu to:
- Add transactions
- View all blocks in the chain
- Exit the application
The Block class represents a block in the blockchain with:
- Block index
- Timestamp
- Sender (hashed)
- Recipient
- Transaction amount
- Previous block hash
- Nonce for mining
- Current block hash
The Blockchain class manages:
- Creating the genesis block
- Adding new blocks
- Validating the chain
- Persisting blocks to LevelDB
- Implementing consensus rules
A FastAPI server that:
- Exposes endpoints for blockchain operations
- Communicates with peer nodes
- Broadcasts chain updates
- Implements chain synchronization
A command-line interface for:
- Adding transactions to the blockchain
- Viewing the current state of the chain
- Interacting with nodes via API calls
{
"index": 1,
"timestamp": 1715861112.123,
"sender": "hashed_sender",
"recipient": "hashed_recipient",
"amount": 100,
"previous_hash": "0000abcd...",
"nonce": 3123,
"hash": "00003f..."
}
- Proof-of-Work: The block is mined by finding a nonce so that the hash starts with 00 (difficulty = 2).
- LevelDB: Blocks are persisted using LevelDB via plyvel.
- Hashing: SHA-256 used for:
- User identifiers (sender, recipient)
- Block integrity (calculate_hash)
POST /add_transaction: Add a new transaction to the blockchainGET /chain: Get the full blockchain dataPOST /receive_chain: Receive and process a chain from peersGET /ping: Check if a node is activeGET /sync_chain: Synchronize with the longest valid chain in the network
- Hashing of user identities
- Cryptographic linking of blocks
- Proof-of-work consensus mechanism
- Chain validation
- This is a simulation for educational purposes and not suitable for production use
- The mining difficulty can be adjusted in the blockchain.py file
self.difficulty = 2 - Each node maintains its own copy of the blockchain
- To reset the blockchain:
rm -rf db/ - You can test different nodes whether it runs smoothly by turning
API_URL = "http://127.0.0.1:8001"in main.py into the port you want to test
This project is licensed under the MIT License - see the LICENSE file for details.



