# Quick Fixes and Immediate Actions

## Critical Fixes (Do First)

### 1. Fix Genesis ExtraData Generation

**File**: `scripts/generate-genesis.sh`

**Issue**: Script doesn't generate proper QBFT extraData

**Fix**: Create a proper genesis generation script that uses Besu's operator tool:

```bash
#!/bin/bash
# Generate proper QBFT extraData using Besu operator

besu operator generate-blockchain-config \
  --config-file=config/genesis-template.json \
  --to=keys/validators \
  --private-key-file-name=key.priv

# Extract extraData from generated genesis
# Update config/genesis.json with proper extraData
```

### 2. Pin Image Versions

**Files**: 
- `k8s/base/validators/statefulset.yaml`
- `k8s/base/sentries/statefulset.yaml`
- `k8s/base/rpc/statefulset.yaml`
- `k8s/blockscout/deployment.yaml`
- `monitoring/k8s/prometheus.yaml`
- `helm/besu-network/values.yaml`

**Fix**: Replace `:latest` with specific versions:

```yaml
image: hyperledger/besu:23.10.0
image: blockscout/blockscout:v5.1.5
image: prom/prometheus:v2.45.0
image: busybox:1.36
```

### 3. Remove Hardcoded Secrets

**File**: `k8s/blockscout/deployment.yaml`

**Fix**: Remove hardcoded secrets, use Kubernetes Secrets:

```yaml
# Remove this:
stringData:
  secret_key_base: "change-me-in-production"
  postgres_password: "change-me-in-production"

# Replace with:
# Generate secrets using:
# kubectl create secret generic blockscout-secrets \
#   --from-literal=secret_key_base=$(openssl rand -hex 32) \
#   --from-literal=postgres_password=$(openssl rand -base64 32)
```

### 4. Fix Health Checks

**Files**: All StatefulSet files

**Issue**: Besu may not have `/liveness` and `/readiness` endpoints

**Fix**: Use metrics endpoint or implement custom health checks:

```yaml
livenessProbe:
  httpGet:
    path: /metrics
    port: metrics
  initialDelaySeconds: 120
  periodSeconds: 30

readinessProbe:
  httpGet:
    path: /metrics
    port: metrics
  initialDelaySeconds: 60
  periodSeconds: 10
```

### 5. Complete Application Gateway

**File**: `terraform/modules/networking/main.tf`

**Fix**: Complete Application Gateway configuration with backend pools, listeners, and rules.

## High Priority Fixes

### 6. Add Resource Limits to Init Containers

**Files**: All StatefulSet files

**Fix**: Add resource limits to init containers:

```yaml
initContainers:
  - name: config-init
    resources:
      requests:
        cpu: "10m"
        memory: "32Mi"
      limits:
        cpu: "100m"
        memory: "64Mi"
```

### 7. Configure Terraform Backend

**File**: `terraform/main.tf`

**Fix**: Uncomment and configure backend:

```hcl
backend "azurerm" {
  resource_group_name  = "tfstate-rg"
  storage_account_name = "tfstate${random_id.storage.hex}"
  container_name       = "tfstate"
  key                  = "defi-oracle-mainnet.terraform.tfstate"
}
```

### 8. Add Network Policies

**File**: Create `k8s/network-policies/`

**Fix**: Implement Kubernetes Network Policies for pod-to-pod communication.

### 9. Implement RBAC

**File**: Create `k8s/rbac/`

**Fix**: Create RBAC resources for service accounts with least privilege.

### 10. Add HPA for RPC Nodes

**File**: Create `k8s/base/rpc/hpa.yaml`

**Fix**: Add HorizontalPodAutoscaler for RPC nodes based on CPU/memory usage.

## Medium Priority Fixes

### 11. Improve Smart Contract Security

**Files**: `contracts/oracle/Proxy.sol`, `contracts/oracle/Aggregator.sol`

**Fix**: Use OpenZeppelin Contracts for proxy pattern and access control.

### 12. Add Comprehensive Tests

**Files**: `test/*.t.sol`

**Fix**: Add more test cases, fuzz tests, and integration tests.

### 13. Improve Oracle Publisher

**File**: `services/oracle-publisher/oracle_publisher.py`

**Fix**: Add retry logic, circuit breaker, and better error handling.

### 14. Complete Monitoring

**Files**: `monitoring/*`

**Fix**: Deploy Grafana, configure Alertmanager with real notification channels.

### 15. Add Documentation

**Files**: Create missing documentation files

**Fix**: Create CONTRIBUTING.md, CHANGELOG.md, architecture diagrams.

## Security Fixes

### 16. Implement CORS Properly

**File**: `config/rpc/besu-config.toml`

**Fix**: Replace `["*"]` with specific origins:

```toml
rpc-http-cors-origins=["https://yourdomain.com", "https://app.yourdomain.com"]
```

### 17. Add IP Allowlisting

**File**: `k8s/gateway/nginx-config.yaml`

**Fix**: Add IP allowlisting for admin operations:

```nginx
location /admin {
    allow 10.0.0.0/16;  # Internal only
    deny all;
}
```

### 18. Implement Secrets Rotation

**Files**: Create rotation scripts

**Fix**: Implement automated secrets rotation using Azure Key Vault.

### 19. Add Pod Security Standards

**File**: Create `k8s/psp/`

**Fix**: Implement Pod Security Standards for all namespaces.

### 20. Add Network Policies

**File**: Create `k8s/network-policies/`

**Fix**: Implement Kubernetes Network Policies to restrict pod-to-pod communication.

## Operational Fixes

### 21. Add Backup Procedures

**Files**: Create `scripts/backup/`

**Fix**: Implement automated backup procedures for chaindata.

### 22. Create Disaster Recovery Runbooks

**Files**: Create `runbooks/disaster-recovery.md`

**Fix**: Document disaster recovery procedures and test them regularly.

### 23. Add Troubleshooting Guide

**Files**: Create `docs/TROUBLESHOOTING.md`

**Fix**: Document common issues and solutions.

### 24. Implement Logging Best Practices

**Files**: All application files

**Fix**: Implement structured logging with correlation IDs.

### 25. Add Performance Monitoring

**Files**: `monitoring/grafana/dashboards/`

**Fix**: Add performance dashboards and set up alerts for performance degradation.

## Quick Implementation Guide

### Step 1: Critical Fixes (Day 1)
1. Fix genesis extraData generation
2. Pin all image versions
3. Remove hardcoded secrets

### Step 2: High Priority Fixes (Week 1)
1. Complete Application Gateway
2. Fix health checks
3. Add resource limits
4. Configure Terraform backend

### Step 3: Security Fixes (Week 2)
1. Implement CORS properly
2. Add IP allowlisting
3. Implement RBAC
4. Add Network Policies

### Step 4: Operational Fixes (Week 3-4)
1. Complete monitoring
2. Add backup procedures
3. Create runbooks
4. Improve documentation

## Testing After Fixes

After implementing fixes, test:

1. **Genesis Generation**: Verify extraData is properly generated
2. **Deployment**: Deploy to test environment
3. **Health Checks**: Verify all health checks work
4. **Monitoring**: Verify metrics are collected
5. **Security**: Run security scans
6. **Performance**: Run load tests
7. **Disaster Recovery**: Test backup and restore procedures

## Validation Checklist

- [ ] Genesis extraData is properly generated
- [ ] All image versions are pinned
- [ ] No hardcoded secrets
- [ ] Health checks work correctly
- [ ] Application Gateway is configured
- [ ] Resource limits are set
- [ ] Terraform backend is configured
- [ ] Security configurations are implemented
- [ ] Monitoring is working
- [ ] Backup procedures are implemented
- [ ] Runbooks are created
- [ ] Documentation is complete

