# Besu Deployment Fixes Applied

This document describes all the fixes applied to resolve deployment issues with Besu containers.

## Issues Fixed

### 1. IP Address Configuration ✅

**Problem**: Containers were using DHCP and getting assigned IP addresses from `10.3.1.X/24` range instead of the intended `192.168.11.X/24` range.

**Solution**:
- Created `scripts/fix-container-ips.sh` to configure static IP addresses on existing containers
- Updated `config/network.conf` to use `192.168.11.X` subnet instead of `10.3.1.X`
- Updated `scripts/deployment/deploy-besu-nodes.sh` to use static IP configuration instead of DHCP

**IP Address Mapping**:
- Validators: `192.168.11.100-104` (VMID 1000-1004)
- Sentries: `192.168.11.150-153` (VMID 1500-1503)
- RPC Nodes: `192.168.11.250-252` (VMID 2500-2502)

**Usage**:
```bash
# Fix IP addresses on existing containers
./scripts/fix-container-ips.sh
```

### 2. Service Management ✅

**Problem**: Besu services may not be enabled or running on containers.

**Solution**:
- Created `scripts/fix-besu-services.sh` to:
  - Check service status on all containers
  - Enable services if not enabled
  - Start services if not running
  - Validate service health (checking ports)
  - Verify configuration files exist before starting services

**Usage**:
```bash
# Fix and start all Besu services
./scripts/fix-besu-services.sh
```

### 3. Configuration Validation ✅

**Problem**: Need to ensure all configuration files are correctly placed and formatted.

**Solution**:
- Created `scripts/validate-besu-config.sh` to validate:
  - `genesis.json` exists and is valid JSON
  - Configuration files (config-validator.toml, config-sentry.toml, config-rpc-*.toml) exist
  - Validator keys exist and are properly formatted
  - Key file permissions are secure (600 or 400)
  - Address files contain valid Ethereum addresses
  - Directory ownership is correct (besu:besu)
  - `nodekey` files are correct size (32 bytes)
  - `permissions-nodes.toml` and `static-nodes.json` exist

**Usage**:
```bash
# Validate all Besu configurations
./scripts/validate-besu-config.sh
```

### 4. Master Fix Script ✅

**Problem**: Multiple fixes need to be applied in the correct order.

**Solution**:
- Created `scripts/fix-all-besu.sh` as a master script that:
  1. Fixes container IP addresses
  2. Copies missing configuration files (if source project available)
  3. Validates configuration files
  4. Fixes and starts services

**Usage**:
```bash
# Run all fixes in sequence
./scripts/fix-all-besu.sh
```

## Files Modified

### Configuration Files
- `config/network.conf` - Updated subnet from `10.3.1.X` to `192.168.11.X`

### Deployment Scripts
- `scripts/deployment/deploy-besu-nodes.sh` - Updated to use static IP configuration instead of DHCP

### New Scripts Created
- `scripts/fix-container-ips.sh` - Fixes IP addresses on existing containers
- `scripts/fix-besu-services.sh` - Ensures services are enabled and running
- `scripts/validate-besu-config.sh` - Validates all Besu configuration files
- `scripts/fix-all-besu.sh` - Master script to run all fixes

## Configuration File Locations

All Besu configuration files should be in the following locations:

### On Containers
- **genesis.json**: `/etc/besu/genesis.json`
- **Config files**: `/etc/besu/config-validator.toml`, `/etc/besu/config-sentry.toml`, `/etc/besu/config-rpc-*.toml`
- **Permissions**: `/etc/besu/permissions-nodes.toml`, `/etc/besu/permissions-accounts.toml`
- **Static nodes**: `/etc/besu/static-nodes.json`
- **Validator keys**: `/keys/validators/validator-*/` (validators only)
- **Nodekey**: `/data/besu/nodekey` (auto-generated if not provided)

## Next Steps

After running the fix scripts:

1. **Verify IP addresses**:
   ```bash
   pct exec <vmid> -- ip addr show eth0
   ```

2. **Check service status**:
   ```bash
   pct exec <vmid> -- systemctl status besu-validator
   pct exec <vmid> -- systemctl status besu-sentry
   pct exec <vmid> -- systemctl status besu-rpc
   ```

3. **View service logs**:
   ```bash
   pct exec <vmid> -- journalctl -u besu-validator -f
   ```

4. **Test connectivity**:
   ```bash
   pct exec <vmid> -- ping -c 3 192.168.11.1
   ```

5. **Update static-nodes.json** with actual enodes if needed:
   ```bash
   ./scripts/network/bootstrap-network.sh
   ```

## Troubleshooting

### IP Address Not Changed
If IP address didn't change after running `fix-container-ips.sh`:
1. Ensure container is stopped: `pct stop <vmid>`
2. Manually update: `pct set <vmid> --net0 "bridge=vmbr0,name=eth0,ip=192.168.11.X/24,gw=192.168.11.1,type=veth"`
3. Start container: `pct start <vmid>`

### Service Won't Start
1. Check logs: `pct exec <vmid> -- journalctl -u besu-<type> -n 50`
2. Verify configuration files exist
3. Check file permissions and ownership
4. Ensure genesis.json is valid JSON

### Configuration Validation Errors
1. Run `copy-besu-config-with-nodes.sh` to copy missing files
2. Check source project directory structure
3. Verify key files exist and are readable

## Network Configuration

### Gateway
- **IP**: `192.168.11.1`
- **Subnet**: `192.168.11.0/24`
- **Bridge**: `vmbr0` (default)

### DNS Servers
- Primary: `8.8.8.8`
- Secondary: `8.8.4.4`

## Service Configuration

### Besu Services
- **besu-validator**: Validator nodes (VMID 1000-1004)
- **besu-sentry**: Sentry nodes (VMID 1500-1503)
- **besu-rpc**: RPC nodes (VMID 2500-2502)

All services are:
- Enabled to start on boot
- Configured to restart on failure
- Running as `besu:besu` user
- Using appropriate memory limits (configured in service files)

