A simplified blockchain implementation in Python for educational purposes, demonstrating core blockchain concepts including cryptographic hashing, transaction validation, and network communication.
This project implements a basic blockchain system with a client-server architecture where clients can perform deposit and withdrawal operations with a virtual currency called "minicoins". The blockchain maintains transaction integrity through SHA-256 cryptographic hashing, with each block linked to its predecessor.
- Client-Server Architecture: TCP/IP communication between multiple clients and a central server
- Blockchain Implementation: Tamper-evident chain of blocks with cryptographic hashing
- Transaction Types:
- Account registration (name)
- Deposits (add minicoins)
- Withdrawals (remove minicoins)
- Special Block Types:
- Standard transaction blocks
- Account creation blocks (for first deposits with timestamps)
- Concurrency Support: Multi-threaded server handling multiple simultaneous clients
- Graceful Shutdown: Signal handlers for clean termination (SIGINT, SIGTERM, SIGHUP)
- Input Modes: Interactive (stdin) and automated (file-based) client operation
- Maintains the blockchain state
- Handles concurrent client connections using threading
- Validates transactions and blockchain integrity
- Protected shared state with thread locks
- Connects to the server via TCP
- Sends operations (deposit, withdraw, quit)
- Supports both interactive and automated modes
- Stores transaction data (owner, amount, operation)
- Serializable for hash computation
- Linked to previous block via cryptographic hash
- Special block for first client deposit
- Includes account creation timestamp
- Inherits from Block class
- Computes SHA-256 hashes for blocks
- Validates individual blocks and entire blockchain
- Implements tamper-detection mechanism
- Validates transaction rules
- Creates appropriate block types
- Manages client balances
- Prevents overdrafts
- Python 3.11.2 (or compatible version)
- pip (Python package manager)
- Clone the repository:
git clone https://github.com/Andre-Grassi/mini_blockchain.git
cd mini_blockchain- Run the setup script:
chmod +x setup.sh
./setup.shThis will create a virtual environment and install the package in editable mode.
- Activate the virtual environment:
source .venv/bin/activatepython3 src/run_server.py <port>Example:
python3 src/run_server.py 8080The server will display its IP address and start listening for client connections.
python3 src/run_client.py <client_name> <server_ip> <server_port>Example:
python3 src/run_client.py Alice 127.0.0.1 8080Available commands:
deposit <amount>- Deposit minicoinswithdraw <amount>- Withdraw minicoinsq- Quit
python3 src/run_client.py <client_name> <server_ip> <server_port> --input <file>Example:
python3 src/run_client.py Bob 127.0.0.1 8080 --input tests/inputs/correct.txtInput file format (one command per line):
deposit 100
withdraw 50
q
The project includes test scenarios in tests/inputs/:
correct.txt- Valid transaction sequenceincorrect_withdraw.txt- Attempted overdraftincorrect_withdraw2.txt- Withdrawal without depositunknown.txt- Unknown commands
Run automated tests:
cd tests
python3 auto_execution.py <server_ip> <server_port>mini_blockchain/
├── src/
│ ├── run_server.py # Server entry point
│ ├── run_client.py # Client entry point
│ └── models/
│ ├── server.py # Server implementation
│ ├── client.py # Client implementation
│ ├── block.py # Block class
│ ├── acc_creation_block.py # Account creation block
│ ├── hash.py # Hashing utilities
│ ├── transaction_handler.py # Transaction logic
│ ├── operation.py # Operation enum
│ └── network_node.py # Base network class
├── tests/
│ ├── auto_execution.py # Automated test runner
│ ├── inputs/ # Test input files
│ └── logs/ # Test execution logs
├── docs/ # Sphinx documentation
├── site/ # Project website
├── setup.sh # Setup script
├── pyproject.toml # Project configuration
└── README.md # This file
- Client Registration: Client connects and sends name to server
- Operation Request: Client sends deposit/withdraw command
- Validation: Server validates transaction (balance check, amount > 0)
- Block Creation: Server creates appropriate block type
- Hash Computation: Server computes SHA-256 hash (includes previous block's hash)
- Blockchain Validation: Server validates entire chain integrity
- Response: Server sends status back to client
Each block's hash is computed from:
- Block's serialized data (owner, amount, operation)
- Previous block's hash (except genesis block)
This creates a tamper-evident chain where any modification to a block invalidates all subsequent blocks.
- Server spawns a new thread for each client connection
- Shared blockchain state protected by threading locks
- Each client thread runs independently but synchronizes on state modifications
Generate API documentation using Sphinx:
./generate_docs.shView documentation:
open build/html/index.html- Amounts must be positive
- Withdrawals cannot exceed current balance
- Client must register name before transactions
- Each block hash must be valid
- Genesis block validated separately
- Chain validated after each new block
- Invalid operations return error messages
- Overdrafts rejected with balance info
- Corrupted blocks automatically removed
- Graceful shutdown on signals
- Socket timeouts for responsiveness
Follows Google Python Style Guide
Configuration in pylintrc
- Andre Grassi de Jesus
- Ricardo Faria
Educational project for Networks II course (Redes 2)
This is an educational project. Feel free to fork and experiment!
- This is a simplified blockchain for learning purposes
- Not suitable for production use
- No mining or proof-of-work implementation
- Centralized server architecture (not peer-to-peer)
- No persistence layer (blockchain stored in memory)