Skip to content

Commit 4d35ecb

Browse files
Refactor RavenChain module and enhance requirements
Add module-level docstring to `__init__.py` and define public API. Update `requirements.txt` to include Uvicorn and SQLAlchemy. Introduce new scripts for database checks and block inspections, and improve block validation logic in `block.py`.
1 parent 43518d1 commit 4d35ecb

14 files changed

+395
-187
lines changed

Dockerfile

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
# Use Python 3.13 slim image as base
2-
FROM python:3.13-slim
1+
FROM python:3.13-slim AS base
32

43
# Set environment variables
54
ENV PYTHONDONTWRITEBYTECODE=1 \
6-
PYTHONUNBUFFERED=1 \
7-
POETRY_VERSION=1.7.1
5+
PYTHONUNBUFFERED=1
86

97
# Set working directory
108
WORKDIR /app
@@ -14,6 +12,7 @@ RUN apt-get update \
1412
&& apt-get install -y --no-install-recommends \
1513
gcc \
1614
python3-dev \
15+
curl \
1716
&& rm -rf /var/lib/apt/lists/*
1817

1918
# Copy only requirements first to leverage Docker cache
@@ -28,8 +27,11 @@ COPY . .
2827
# Install the package in editable mode
2928
RUN pip install -e .
3029

31-
# Expose port for API (if implementing REST API)
30+
# API stage
31+
FROM base AS api
3232
EXPOSE 8000
33+
CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"]
3334

34-
# Command to run the application
35+
# Core stage
36+
FROM base AS core
3537
CMD ["python", "main.py"]

README.md

+28-26
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ A modern, Python-based blockchain implementation focusing on simplicity, securit
1313
- ✅ Persistent wallet storage
1414
- ✅ Chain validation and integrity checking
1515
- ✅ Mining rewards system
16+
- ✅ FastAPI-based REST API
17+
- ✅ Database persistence with PostgreSQL
18+
- ✅ Docker containerization
1619

1720
### Planned Features
1821
- 🔄 Peer-to-peer networking
19-
- 🌐 FastAPI-based REST API
2022
- 📊 Block explorer web interface
2123
- 📜 Smart contract support
22-
- 💾 Database persistence
2324
- 🔍 UTXO model implementation
2425
- 🔒 Enhanced security features
2526
- ⚡ Performance optimizations
@@ -28,9 +29,9 @@ A modern, Python-based blockchain implementation focusing on simplicity, securit
2829

2930
- **Language**: Python 3.13+
3031
- **Cryptography**: `ecdsa`, `hashlib`
31-
- **Storage**: PostgreSQL for block explorer and indexing
32+
- **Storage**: PostgreSQL for blockchain persistence
3233
- **Backend**: FastAPI for REST API endpoints
33-
- **Frontend**: React 19 with TypeScript
34+
- **Frontend**: React 19 with TypeScript (planned)
3435
- **Future Stack**:
3536
- WebSocket for real-time updates
3637
- Redis for caching
@@ -49,20 +50,21 @@ ravenchain/
4950
├── api/ # FastAPI backend
5051
│ ├── __init__.py
5152
│ ├── main.py # API entry point
52-
│ ├── routes/ # API routes
53-
└── models/ # Pydantic models
54-
├── frontend/ # React 19 frontend
55-
│ ├── src/
56-
── public/
57-
── package.json
58-
│ └── tsconfig.json
53+
│ ├── routes/ # API route handlers
54+
│ ├── block_routes.py
55+
│ │ ├── mining_routes.py
56+
├── transaction_routes.py
57+
│ └── wallet_routes.py
58+
── database/ # Database models and config
59+
└── models.py
5960
├── config/ # Configuration
60-
│ └── settings.py # Global settings
61-
├── tests/ # Test suite
62-
├── utils/ # Utility functions
63-
├── docker-compose.yml # Docker configuration
64-
└── README.md # Documentation
65-
```
61+
│ ├── settings.py # App settings
62+
│ └── logging.py # Logging setup
63+
├── scripts/ # Utility scripts
64+
├── tests/ # Test suite
65+
├── Dockerfile # Docker build file
66+
├── docker-compose.yml # Docker services config
67+
└── requirements.txt # Python dependencies
6668
6769
## 🚀 Quick Start
6870
@@ -130,17 +132,17 @@ docker-compose run --rm ravenchain pytest --cov=ravenchain
130132
- [x] Wallet management system
131133
- [x] Command-line interface
132134
- [x] Transaction handling
133-
- [ ] Unit test coverage
134-
- [ ] Documentation improvements
135+
- [x] Unit test coverage
136+
- [x] Documentation improvements
135137

136138
### Phase 2: Data Persistence & API (Next)
137-
- [ ] Implement PostgreSQL for blockchain storage
138-
- [ ] Design and implement FastAPI REST API
139-
- [ ] Block endpoints
140-
- [ ] Transaction endpoints
141-
- [ ] Wallet endpoints
142-
- [ ] Mining endpoints
143-
- [ ] API documentation with Swagger/OpenAPI
139+
- [x] Implement PostgreSQL for blockchain storage
140+
- [x] Design and implement FastAPI REST API
141+
- [x] Block endpoints
142+
- [x] Transaction endpoints
143+
- [x] Wallet endpoints
144+
- [x] Mining endpoints
145+
- [x] API documentation with Swagger/OpenAPI
144146
- [ ] Request rate limiting
145147
- [ ] API authentication system
146148

docker-compose.yml

+53-85
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
services:
22
ravenchain:
3-
build: .
3+
build:
4+
context: .
5+
target: core
46
volumes:
57
- .:/app
68
- blockchain_data:/app/data
@@ -18,92 +20,58 @@ services:
1820
stdin_open: true # Keep STDIN open
1921
restart: unless-stopped
2022

21-
# FastAPI Backend (Uncomment when ready)
22-
# api:
23-
# build:
24-
# context: .
25-
# dockerfile: Dockerfile
26-
# target: api # Using multi-stage build for API
27-
# volumes:
28-
# - .:/app
29-
# - logs:/app/logs
30-
# ports:
31-
# - "8000:8000"
32-
# environment:
33-
# - ENVIRONMENT=development
34-
# - DEBUG=1
35-
# - DATABASE_URL=postgresql://ravenchain:ravenchain@db:5432/ravenchain
36-
# - RAVENCHAIN_LOGS_DIR=/app/logs
37-
# - LOG_LEVEL=DEBUG
38-
# - LOG_JSON=1
39-
# - CORS_ORIGINS=http://localhost:3000 # Allow frontend in dev
40-
# - MAX_WORKERS=4 # Uvicorn workers
41-
# command: uvicorn api.main:app --host 0.0.0.0 --port 8000 --reload --workers 4
42-
# healthcheck:
43-
# test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
44-
# interval: 30s
45-
# timeout: 10s
46-
# retries: 3
47-
# depends_on:
48-
# db:
49-
# condition: service_healthy
50-
# ravenchain:
51-
# condition: service_started
52-
53-
# React 19 Frontend (Uncomment when ready)
54-
# frontend:
55-
# build:
56-
# context: ./frontend
57-
# dockerfile: Dockerfile.frontend
58-
# target: development # Using multi-stage build for frontend
59-
# volumes:
60-
# - ./frontend:/app
61-
# - /app/node_modules
62-
# ports:
63-
# - "3000:3000"
64-
# environment:
65-
# - NODE_ENV=development
66-
# - VITE_API_URL=http://localhost:8000 # Using Vite for React 19
67-
# - WATCHPACK_POLLING=true # Better hot reload in Docker
68-
# - VITE_BLOCKCHAIN_WS=ws://localhost:8000/ws # WebSocket for real-time updates
69-
# - VITE_ENABLE_DEVTOOLS=true # React DevTools
70-
# command: npm run dev
71-
# healthcheck:
72-
# test: ["CMD", "curl", "-f", "http://localhost:3000"]
73-
# interval: 30s
74-
# timeout: 10s
75-
# retries: 3
76-
# depends_on:
77-
# api:
78-
# condition: service_healthy
23+
api:
24+
build:
25+
context: .
26+
dockerfile: Dockerfile
27+
target: api
28+
volumes:
29+
- .:/app
30+
- logs:/app/logs
31+
ports:
32+
- "8000:8000"
33+
environment:
34+
- ENVIRONMENT=development
35+
- DEBUG=1
36+
- RAVENCHAIN_DB_HOST=db
37+
- RAVENCHAIN_DB_PORT=5432
38+
- RAVENCHAIN_DB_NAME=ravenchain
39+
- RAVENCHAIN_DB_USER=postgres
40+
- RAVENCHAIN_DB_PASS=admin
41+
- RAVENCHAIN_LOGS_DIR=/app/logs
42+
- LOG_LEVEL=DEBUG
43+
- LOG_JSON=1
44+
- CORS_ORIGINS=http://localhost:3000
45+
- MAX_WORKERS=4
46+
command: uvicorn api.main:app --host 0.0.0.0 --port 8000 --reload --workers 4
47+
healthcheck:
48+
test: ["CMD", "curl", "-f", "http://localhost:8000/api/health"]
49+
interval: 30s
50+
timeout: 10s
51+
retries: 3
52+
depends_on:
53+
db:
54+
condition: service_healthy
55+
ravenchain:
56+
condition: service_started
7957

80-
# PostgreSQL 17 Database (Uncomment when ready)
81-
# db:
82-
# image: postgres:17-alpine
83-
# volumes:
84-
# - postgres_data:/var/lib/postgresql/data/
85-
# - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro # Initial schema
86-
# environment:
87-
# - POSTGRES_USER=ravenchain
88-
# - POSTGRES_PASSWORD=ravenchain
89-
# - POSTGRES_DB=ravenchain
90-
# - PGDATA=/var/lib/postgresql/data/pgdata # Custom data directory
91-
# ports:
92-
# - "5432:5432"
93-
# healthcheck:
94-
# test: ["CMD-SHELL", "pg_isready -U ravenchain"]
95-
# interval: 5s
96-
# timeout: 5s
97-
# retries: 5
98-
# command: >
99-
# postgres
100-
# -c shared_buffers=256MB
101-
# -c max_connections=200
102-
# -c effective_cache_size=1GB
103-
# -c work_mem=16MB
104-
# shm_size: '256mb' # Shared memory size for better performance
58+
db:
59+
image: postgres:17-alpine
60+
volumes:
61+
- postgres_data:/var/lib/postgresql/data
62+
environment:
63+
- POSTGRES_USER=postgres
64+
- POSTGRES_PASSWORD=admin
65+
- POSTGRES_DB=ravenchain
66+
ports:
67+
- "5432:5432"
68+
healthcheck:
69+
test: ["CMD-SHELL", "pg_isready -U postgres"]
70+
interval: 10s
71+
timeout: 5s
72+
retries: 5
10573

10674
volumes:
10775
blockchain_data:
10876
logs:
109-
# postgres_data: # Uncomment when adding database
77+
postgres_data:

ravenchain/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1+
"""RavenChain Module."""
12

3+
from .blockchain import Blockchain
4+
from .block import Block
5+
from .transaction import Transaction
6+
from .wallet import Wallet
7+
8+
__all__ = ["Blockchain", "Block", "Transaction", "Wallet"]

ravenchain/block.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ def __init__(self, index, timestamp=None, data=None, previous_hash=None):
99
"""Initialize a block with its attributes"""
1010
if index is None or index < 0:
1111
raise ValueError("Block index must be a non-negative integer")
12-
if not isinstance(data, list) or not all(isinstance(tx, Transaction) for tx in data):
12+
if data is not None and (
13+
not isinstance(data, list) or not all(isinstance(tx, Transaction) for tx in data)
14+
):
1315
raise ValueError("Block data must be a list of Transaction objects")
1416

1517
self.index = index

0 commit comments

Comments
 (0)