# Besu Node Optimization Summary

**Date**: 2026-01-04  
**Status**: ✅ **Optimization Complete**

---

## Overview

All Validator and Sentry nodes have been optimized to address warnings and improve performance. The optimizations target:

1. **CORS Configuration** - Fixed excessive CORS rejection errors
2. **JVM Settings** - Optimized to reduce thread blocking warnings
3. **Vert.x Thread Pool** - Tuned to prevent thread blocking
4. **Resource Limits** - Increased for better performance
5. **Performance Tuning** - Added various optimizations

---

## Optimizations Applied

### 1. CORS Configuration Fix

**Before**: 
- Validators: No CORS configuration (causing rejections)
- Sentries: Wildcard CORS (`["*"]`) causing security errors

**After**:
- Validators: Restricted to localhost and internal network (`192.168.11.0/24`)
- Sentries: Restricted to specific origins instead of wildcard

**Impact**: Eliminated hundreds of CORS rejection errors per node

### 2. JVM Settings Optimization

**Before**:
```ini
BESU_OPTS=-Xmx4g -Xms4g
JAVA_OPTS=-XX:+UseG1GC -XX:MaxGCPauseMillis=200
```

**After**:
```ini
BESU_OPTS=-Xmx6g -Xms6g
JAVA_OPTS=-XX:+UseG1GC -XX:MaxGCPauseMillis=100 -XX:G1HeapRegionSize=16m 
          -XX:+ParallelRefProcEnabled -XX:InitiatingHeapOccupancyPercent=45 
          -XX:ConcGCThreads=4 -XX:ParallelGCThreads=4 
          -XX:+UseStringDeduplication
          -Dvertx.eventLoopPoolSize=4 -Dvertx.workerPoolSize=20 
          -Dvertx.blockedThreadCheckInterval=5000
```

**Impact**: 
- Increased heap from 4GB to 6GB for better performance
- Reduced GC pause time from 200ms to 100ms
- Optimized GC threads and settings
- Added Vert.x thread pool optimizations

### 3. Vert.x Thread Pool Optimization

**Settings Added**:
- `vertx.eventLoopPoolSize=4` - Optimized event loop threads
- `vertx.workerPoolSize=20` - Increased worker threads
- `vertx.blockedThreadCheckInterval=5000` - Reduced blocking check frequency

**Impact**: Significantly reduced thread blocking warnings

### 4. Resource Limits Increase

**Before**:
- `LimitNOFILE=65536`
- `LimitNPROC=32768`

**After**:
- `LimitNOFILE=131072` (doubled)
- `LimitNPROC=65536` (doubled)
- `CPUQuota=400%`
- `MemoryMax=8G`
- `MemoryHigh=7G`

**Impact**: Better handling of high load scenarios

### 5. Performance Tuning Parameters

**Added to Configuration**:
- `fast-sync-min-peers=2` - Faster sync with minimum peers
- `tx-pool-max-size=8192` - Optimized transaction pool
- `tx-pool-price-bump=10` - Transaction pool price bump
- `tx-pool-retention-hours=6` - Transaction retention
- `max-remote-initiated-connections=10` - Connection limits
- `pruning-blocks-retained=1024` - Pruning settings

---

## Results

### Error Reduction

**Before Optimization**:
- Validator 1000: 300+ CORS errors in last 100 log lines
- Validator 1001: Thread blocking warnings and exceptions
- Sentries: Similar issues across all nodes

**After Optimization**:
- **Error reduction: ~95%** (from 300+ to 5-6 errors per node)
- Remaining errors are mostly startup-related or non-critical
- CORS errors eliminated
- Thread blocking warnings significantly reduced

### Service Status

All services are **active and running**:
- ✅ Validators (1000-1004): All active
- ✅ Sentries (1500-1503): All active

---

## Configuration Files Updated

### Validators
- `/etc/besu/config-validator.toml` - Optimized configuration
- `/etc/systemd/system/besu-validator.service` - Optimized service file

### Sentries
- `/etc/besu/config-sentry.toml` - Optimized configuration
- `/etc/systemd/system/besu-sentry.service` - Optimized service file

### Backups
All original configurations were backed up with timestamp:
- `config-validator.toml.backup.YYYYMMDD_HHMMSS`
- `config-sentry.toml.backup.YYYYMMDD_HHMMSS`

---

## Monitoring Recommendations

1. **Monitor Logs**: Check logs periodically for any new issues
   ```bash
   ./scripts/check-validator-sentry-logs.sh 100
   ```

2. **Monitor Performance**: Watch for thread blocking warnings
   ```bash
   journalctl -u besu-validator.service | grep -i "blocked"
   ```

3. **Monitor Memory**: Ensure nodes have sufficient memory
   ```bash
   systemctl status besu-validator.service | grep Memory
   ```

4. **Monitor CORS**: Verify CORS errors are eliminated
   ```bash
   journalctl -u besu-validator.service | grep -i "CORS"
   ```

---

## Scripts Created

1. **`scripts/optimize-besu-nodes.sh`** - Main optimization script
   - Applies all optimizations to validators and sentries
   - Creates backups before changes
   - Restarts services with new configuration

2. **`scripts/check-validator-sentry-logs.sh`** - Log checking script
   - Checks all validator and sentry logs
   - Identifies errors and warnings
   - Provides summary report

---

## Next Steps

1. ✅ **Completed**: All optimizations applied
2. ✅ **Completed**: Services restarted and verified
3. ⏳ **Ongoing**: Monitor logs for continued improvement
4. ⏳ **Optional**: Fine-tune JVM settings based on actual workload

---

## Notes

- The optimizations are conservative and should work well for most workloads
- If nodes have less than 8GB RAM, consider reducing `MemoryMax` and `MemoryHigh`
- Thread blocking warnings may still occur under extreme load but should be significantly reduced
- CORS errors should be completely eliminated with the new configuration

---

**Last Updated**: 2026-01-04  
**Status**: ✅ All optimizations successfully applied
