A simple blockchain implementation in Python to showcase fundamental blockchain concepts and security properties. This is a proof-of-concept implementation for educational purposes and experimentation with blockchain mechanics:
- Not production-ready: Use only for learning about blockchain mechanics
- Consensus model: Simple proof-of-work (not Byzantine Fault Tolerant)
- Network: Single-node (no peer-to-peer networking)
- Cryptography: Well-tested libraries (cryptography, hashlib)
- Performance: Optimized for clarity, not high throughput
- Block Structure: Immutable blocks with merkle root validation
- Transaction Management: Signed transactions with cryptographic verification
- Blockchain Validation: Chain integrity verification with proof-of-work consensus
- Wallet System: Elliptic curve cryptography (SECP256K1) for secure transaction signing
- Mempool: Transaction pool with fee-based ordering
- Node Implementation: Full node with mining capabilities and balance tracking
- FastAPI Server: REST API for transaction submission and blockchain queries
- Digital Signatures: All transactions must be cryptographically signed by the sender
- Merkle Tree Verification: Transaction integrity via merkle root hashing
- Proof of Work: Adjustable difficulty consensus mechanism
- Balance Validation: Double-spending prevention through account balance tracking
- Signature Verification: Invalid signatures are rejected at both transaction and API levels
- Chain Integrity: Tampering detection through linked block hashing
blockchain/
├── account_balance.py # Account balance tracking
├── app.py # FastAPI application
├── block.py # Block definition and operations
├── blockchain.py # Blockchain management
├── mempool.py # Transaction mempool with fee ordering
├── node.py # Node operations and mining
├── transaction.py # Transaction definition and signing
├── wallet.py # Wallet and key management
└── tests/ # Comprehensive test suite
├── conftest.py # Pytest fixtures
├── unit/ # Unit tests
│ ├── test_app.py # API endpoint tests
│ ├── test_block.py # Block tests (unit + property-based)
│ ├── test_blockchain.py # Blockchain tests (unit + property-based)
│ ├── test_mempool.py # Mempool tests
│ ├── test_node.py # Node tests (including fraud prevention)
│ ├── test_transaction.py # Transaction tests (unit + property-based)
│ └── test_wallet.py # Wallet and signing tests
└── integration/ # Integration tests
└── test_app.py # Full application tests
This project uses Dev Containers for a consistent, reproducible development environment.
Dev Containers provide a containerized development environment with all dependencies pre-configured:
-
Install prerequisites:
-
Open in container:
- Clone the repository
- Open in VS Code
- Click the "Reopen in Container" prompt, or use the command palette (
Ctrl+Shift+P) and select "Dev Containers: Reopen in Container"
-
Ready to go:
- All dependencies are automatically installed
- Python environment is pre-configured
- Start developing immediately
For more details, see the devcontainer configuration.
python server.pyThe API will be available at http://localhost:8000
Generate a wallet file:
python client.py --wallet-file wallet.pem init-walletRequest the miner to give you an allowance:
python client.py --wallet-file wallet.pem topup-walletRetrieve and display the current wallet balance:
python client.py --wallet-file wallet.pem show-walletCreate and submit a transaction:
python client.py --wallet-file wallet.pem send --recipient 0x1234 --amount 3 --fee 1Retrieve and display the chain:
python client.py blockspytestpytest blockchain/tests/unit/Security tests, marked with @pytest.mark.security, verify critical blockchain properties including:
- Transaction signature validation and tampering detection
- Prevention of double-spending and insufficient balance scenarios
- Chain integrity and tamper detection
- Wallet cryptographic operations and authentication
pytest blockchain/tests/ -m securityBlocks are immutable structures containing:
- Transaction list with merkle root
- Nonce (proof-of-work solution)
- Previous block hash (linking)
- Difficulty level
- Timestamp
Transactions are digitally signed transfers:
- Sender public key
- Recipient address
- Amount and fee
- ECDSA signature (SECP256K1 curve)
Wallets manage cryptographic keys:
- Private key for signing transactions
- Public key derivation (X962 format)
- Address calculation (SHA256 of public key)
Memory pool maintains pending transactions:
- Fee-based ordering (highest fee first)
- Stable sorting for equal fees
- O(1) lookup and removal by hash
Mining (proof-of-work):
- Configurable difficulty
- Nonce search to find valid block hash
- Block reward + transaction fees
- Transaction selection from mempool
This project follows PEP 8 guidelines with:
- Maximum line length: 128 characters
- Type hints for all functions
- Comprehensive docstrings (PEP 257)
- Clear variable and function names
This project uses pre-commit to automatically enforce code quality checks before each commit:
- Linting: Ruff checks code style and catches common issues
- Type Checking: Pyright validates type hints and catches type errors
- Formatting: Code is automatically formatted to meet standards
Pre-commit hooks are configured in the Dev Container and run automatically when you commit. To manually run all checks without committing:
pre-commit run --all-filesThe .pre-commit-config.yaml file defines all checks and their configuration. Pre-commit ensures code quality is maintained consistently across the codebase.
See LICENSE file for details.