Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions besu/.env.database
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# PostgreSQL Database Configuration for Besu Contracts

# Database Connection
DB_HOST=localhost
DB_PORT=5432
DB_NAME=besu_contracts
DB_USER=besu_user
DB_PASSWORD=besu_password

# Full Connection URL
DATABASE_URL=postgresql://besu_user:besu_password@localhost:5432/besu_contracts

# PgAdmin Web Interface
PGADMIN_URL=http://localhost:8080
PGADMIN_EMAIL=admin@besu.local
PGADMIN_PASSWORD=admin123

# Besu Network Configuration
BESU_RPC_URL=http://localhost:8545
BESU_NETWORK_ID=1337

# Database Schemas
CONTRACTS_SCHEMA=contracts
TRANSACTIONS_SCHEMA=transactions
EVENTS_SCHEMA=events
302 changes: 302 additions & 0 deletions besu/ARCHITECTURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
# Architecture Documentation

## Overview

This project implements a complete blockchain development environment using Hyperledger Besu with dual API implementations (Go and Node.js) and PostgreSQL integration.

## System Architecture

### High-Level Components

```
┌─────────────────────────────────────────────────────────────────┐
│ External Access Layer │
├─────────────────────────────────────────────────────────────────┤
│ Go REST API │ Node.js API + Swagger UI │
│ (Port 8080) │ (Port 3001) │
│ │ │
│ • Clean Arch │ • Express.js │
│ • Gin Framework │ • Swagger Documentation │
│ • Direct DB/BC │ • Web3.js Integration │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ Infrastructure Layer │
├─────────────────────────────────────────────────────────────────┤
│ Hyperledger Besu Network │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Node 0 │ │ Node 1 │ │ Node 2 │ │ Node 3 │ │
│ │:8545 │ │:8546 │ │:8547 │ │:8548 │ │
│ └─────────┘ └─────────┘ └─────────┘ └─────────┘ │
│ QBFT Consensus Mechanism │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ Data Layer │
├─────────────────────────────────────────────────────────────────┤
│ PostgreSQL Database (Port 5432) │
│ • Contract data persistence │
│ • Transaction history │
│ • Synchronization tracking │
│ • Health monitoring │
└─────────────────────────────────────────────────────────────────┘
```

## Detailed Component Analysis

### 1. Hyperledger Besu Network

**Purpose**: Private blockchain network with enterprise-grade features

**Configuration**:
- **Consensus**: QBFT (Quantum Byzantine Fault Tolerance)
- **Nodes**: 4 validator nodes in private network
- **Network ID**: 1337
- **Genesis Block**: Pre-funded accounts with test ETH

**Key Features**:
- Immediate finality through QBFT consensus
- Enterprise privacy features
- EVM compatibility for Ethereum smart contracts
- JSON-RPC API endpoints for each node

**Network Topology**:
```
Node 0 (Bootnode) ←→ Node 1
↕ ↕
Node 3 ←————————————→ Node 2
```

### 2. Smart Contract Layer

**Contract**: SimpleStorage.sol
- Stores a single integer value
- Provides get/set functionality
- Emits events for value changes
- Deployed address: `0x42699A7612A82f1d9C36148af9C77354759b210b`

### 3. Go API Implementation

**Architecture Pattern**: Clean Architecture

```
┌─────────────────┐
│ HTTP Layer │ ← Gin framework, routes, middleware
├─────────────────┤
│ Handler Layer │ ← Request/response handling
├─────────────────┤
│ Business Logic │ ← Core application logic
├─────────────────┤
│ Infrastructure │ ← Database, blockchain clients
└─────────────────┘
```

**Package Structure**:
- `internal/handlers/`: HTTP request handlers
- `internal/database/`: PostgreSQL operations
- `internal/blockchain/`: Ethereum client wrapper
- `internal/models/`: Data structures

**Key Design Decisions**:
- Dependency injection for testability
- Error handling with proper HTTP status codes
- Environment-based configuration
- Clean, comment-free code structure

### 4. Node.js API Implementation

**Architecture**: Express.js with service layer

```
┌─────────────────┐
│ Routes │ ← Express routes definition
├─────────────────┤
│ Services │ ← Business logic layer
├─────────────────┤
│ Data Access │ ← Database and blockchain
└─────────────────┘
```

**Features**:
- Swagger UI for API documentation
- Web3.js for blockchain interaction
- Sequelize ORM for database operations
- Comprehensive error handling

### 5. Database Design

**PostgreSQL Schema**:

```sql
-- Contract values table
CREATE TABLE contract_values (
id SERIAL PRIMARY KEY,
value INTEGER NOT NULL,
tx_hash VARCHAR(66),
block_number BIGINT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- System metadata
CREATE TABLE system_metadata (
key VARCHAR(255) PRIMARY KEY,
value TEXT,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```

## Data Flow Patterns

### 1. SET Operation Flow

```
Client Request → API Validation → Blockchain Transaction → Database Update → Response
↓ ↓ ↓ ↓ ↓
JSON Validate Smart Contract Record TX Success/Error
Payload Input Method Call in DB Message
```

### 2. GET Operation Flow

```
Client Request → Cache Check → Blockchain Query → Response
↓ ↓ ↓ ↓
Request Check DB Call Contract Return Value
Received First get() Method + Metadata
```

### 3. SYNC Operation Flow

```
Client Request → Blockchain Query → Database Compare → Update DB → Response
↓ ↓ ↓ ↓ ↓
Manual Get Current Check Sync Status
Trigger Contract Value Differences Data Report
```

## Security Considerations

### 1. Private Key Management
- Environment variable storage
- No hardcoded credentials
- Separate keys for different operations

### 2. Input Validation
- JSON schema validation
- Type checking
- Range validation for numeric inputs

### 3. Network Security
- Private blockchain network
- Docker network isolation
- No external blockchain exposure

### 4. Database Security
- Parameterized queries
- Connection pooling
- Health check monitoring

## Scalability Design

### 1. Horizontal Scaling
- Stateless API design
- Database connection pooling
- Load balancer ready

### 2. Performance Optimization
- Async/await patterns
- Connection reuse
- Efficient database queries

### 3. Monitoring
- Health check endpoints
- System status monitoring
- Error logging and tracking

## Integration Patterns

### 1. API-to-Blockchain
- JSON-RPC calls to Besu nodes
- Transaction signing and submission
- Event monitoring capabilities

### 2. API-to-Database
- Connection pooling
- Transaction management
- Data synchronization

### 3. Cross-API Communication
- Shared database for state
- Common data models
- Consistent response formats

## Deployment Strategy

### 1. Development Environment
- Docker Compose for local development
- Hot reload for both APIs
- Integrated logging

### 2. Infrastructure as Code
- Docker configurations
- Environment variable management
- Service dependencies

### 3. Testing Strategy
- Unit tests for business logic
- Integration tests for API endpoints
- End-to-end blockchain interaction tests

## Technology Choices Rationale

### 1. Hyperledger Besu
- Enterprise-grade blockchain
- QBFT consensus for finality
- EVM compatibility
- Strong security model

### 2. Dual API Implementation
- **Go**: Performance, clean architecture, strong typing
- **Node.js**: Rapid development, rich ecosystem, Swagger integration

### 3. PostgreSQL
- ACID compliance
- Rich data types
- Excellent performance
- Strong ecosystem support

### 4. Docker
- Environment consistency
- Easy deployment
- Service isolation
- Development/production parity

## Future Enhancement Opportunities

### 1. Advanced Features
- Multi-contract support
- Event listening and webhooks
- Advanced querying capabilities
- Real-time updates via WebSockets

### 2. Security Enhancements
- JWT authentication
- Rate limiting
- API key management
- Audit logging

### 3. Monitoring and Observability
- Metrics collection
- Distributed tracing
- Performance monitoring
- Alert management

### 4. Scalability Improvements
- Microservices architecture
- Message queues
- Caching layers
- Read replicas

---

This architecture provides a solid foundation for blockchain application development with clear separation of concerns, robust error handling, and scalable design patterns.
Loading