# ThirdWeb RPC Nodes - Complete Next Steps

## Overview
This document lists all next steps to complete the ThirdWeb RPC node setup, from deployment to integration.

---

## Phase 1: Deploy Containers

### Step 1.1: Run the Setup Script
```bash
cd /home/intlc/projects/proxmox
./scripts/setup-thirdweb-rpc-nodes.sh
```

**Expected outcome:**
- Creates 3 LXC containers (VMIDs 2400-2402)
- Installs Besu RPC software
- Configures static IPs (192.168.11.240-242)
- Sets up systemd services

**Troubleshooting:**
- If containers fail to create, check storage: `ssh root@192.168.11.10 'pvesm status'`
- Verify template exists: `ssh root@192.168.11.10 'pvesm list local'`
- Check SSH access: `ssh root@192.168.11.10 'echo OK'`

---

## Phase 2: Verify Deployment

### Step 2.1: Check Container Status
```bash
# List all ThirdWeb containers
ssh root@192.168.11.10 "pct list | grep -E '240[0-2]'"

# Check individual container status
ssh root@192.168.11.10 "pct status 2400"
ssh root@192.168.11.10 "pct status 2401"
ssh root@192.168.11.10 "pct status 2402"
```

**Expected output:**
```
    2400  2400  thirdweb-rpc-1  running
    2401  2401  thirdweb-rpc-2  running
    2402  2402  thirdweb-rpc-3  running
```

### Step 2.2: Verify IP Addresses
```bash
# Check IP configuration for each container
ssh root@192.168.11.10 "pct exec 2400 -- hostname -I"
ssh root@192.168.11.10 "pct exec 2401 -- hostname -I"
ssh root@192.168.11.10 "pct exec 2402 -- hostname -I"
```

**Expected output:**
- Container 2400: `192.168.11.240`
- Container 2401: `192.168.11.241`
- Container 2402: `192.168.11.242`

### Step 2.3: Test Network Connectivity
```bash
# Ping each container
ping -c 3 192.168.11.240
ping -c 3 192.168.11.241
ping -c 3 192.168.11.242

# Test port accessibility
nc -zv 192.168.11.240 8545  # HTTP RPC
nc -zv 192.168.11.240 8546  # WebSocket RPC
nc -zv 192.168.11.240 9545  # Metrics
```

---

## Phase 3: Configure Besu Services

### Step 3.1: Verify Besu Installation
```bash
# Check Besu version on each container
ssh root@192.168.11.10 "pct exec 2400 -- /opt/besu/bin/besu --version"
ssh root@192.168.11.10 "pct exec 2401 -- /opt/besu/bin/besu --version"
ssh root@192.168.11.10 "pct exec 2402 -- /opt/besu/bin/besu --version"
```

### Step 3.2: Verify Configuration Files
```bash
# Check config file exists and is correct
ssh root@192.168.11.10 "pct exec 2400 -- cat /etc/besu/config-rpc-thirdweb.toml"
```

**Verify key settings:**
- `network-id=138`
- `rpc-http-enabled=true`
- `rpc-http-port=8545`
- `rpc-ws-enabled=true`
- `rpc-ws-port=8546`
- `rpc-http-api=["ETH","NET","WEB3","DEBUG","TRACE"]`

### Step 3.3: Check Genesis and Permissions Files
```bash
# Verify genesis file exists
ssh root@192.168.11.10 "pct exec 2400 -- ls -la /genesis/genesis.json"

# Verify static nodes file exists
ssh root@192.168.11.10 "pct exec 2400 -- ls -la /genesis/static-nodes.json"

# Verify permissions file exists
ssh root@192.168.11.10 "pct exec 2400 -- ls -la /permissions/permissions-nodes.toml"
```

**If files are missing:**
- Copy from existing RPC nodes or source project
- See `smom-dbis-138/genesis/` and `smom-dbis-138/permissions/` directories

---

## Phase 4: Start and Monitor Services

### Step 4.1: Start Besu Services
```bash
# Start services on all containers
ssh root@192.168.11.10 "pct exec 2400 -- systemctl start besu-rpc.service"
ssh root@192.168.11.10 "pct exec 2401 -- systemctl start besu-rpc.service"
ssh root@192.168.11.10 "pct exec 2402 -- systemctl start besu-rpc.service"

# Enable auto-start on boot
ssh root@192.168.11.10 "pct exec 2400 -- systemctl enable besu-rpc.service"
ssh root@192.168.11.10 "pct exec 2401 -- systemctl enable besu-rpc.service"
ssh root@192.168.11.10 "pct exec 2402 -- systemctl enable besu-rpc.service"
```

### Step 4.2: Check Service Status
```bash
# Check if services are running
ssh root@192.168.11.10 "pct exec 2400 -- systemctl status besu-rpc.service"
ssh root@192.168.11.10 "pct exec 2401 -- systemctl status besu-rpc.service"
ssh root@192.168.11.10 "pct exec 2402 -- systemctl status besu-rpc.service"
```

**Expected status:** `Active: active (running)`

### Step 4.3: Monitor Service Logs
```bash
# View recent logs
ssh root@192.168.11.10 "pct exec 2400 -- journalctl -u besu-rpc.service -n 100"

# Follow logs in real-time (Ctrl+C to exit)
ssh root@192.168.11.10 "pct exec 2400 -- journalctl -u besu-rpc.service -f"
```

**Look for:**
- `Besu is listening on` messages
- `P2P started` message
- Any error messages

---

## Phase 5: Test RPC Endpoints

### Step 5.1: Test HTTP RPC Endpoints
```bash
# Test each RPC endpoint
curl -X POST http://192.168.11.240:8545 \
  -H 'Content-Type: application/json' \
  --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'

curl -X POST http://192.168.11.241:8545 \
  -H 'Content-Type: application/json' \
  --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'

curl -X POST http://192.168.11.242:8545 \
  -H 'Content-Type: application/json' \
  --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'
```

**Expected response:**
```json
{"jsonrpc":"2.0","id":1,"result":"0x..."}
```

### Step 5.2: Test WebSocket Endpoints
```bash
# Install wscat if needed: npm install -g wscat

# Test WebSocket connection
wscat -c ws://192.168.11.240:8546

# Then send: {"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}
```

### Step 5.3: Test Additional RPC Methods
```bash
# Get chain ID
curl -X POST http://192.168.11.240:8545 \
  -H 'Content-Type: application/json' \
  --data '{"jsonrpc":"2.0","method":"eth_chainId","params":[],"id":1}'

# Get network ID
curl -X POST http://192.168.11.240:8545 \
  -H 'Content-Type: application/json' \
  --data '{"jsonrpc":"2.0","method":"net_version","params":[],"id":1}'

# Get client version
curl -X POST http://192.168.11.240:8545 \
  -H 'Content-Type: application/json' \
  --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}'
```

### Step 5.4: Check Metrics Endpoints
```bash
# Check metrics (Prometheus format)
curl http://192.168.11.240:9545/metrics | head -20
```

---

## Phase 6: ThirdWeb Integration

### Step 6.1: Configure ThirdWeb SDK

**JavaScript/TypeScript:**
```javascript
import { ThirdwebSDK } from "@thirdweb-dev/sdk";

// HTTP RPC endpoint
const sdk = new ThirdwebSDK("http://192.168.11.240:8545", {
  supportedChains: [138], // Your ChainID
});

// Or with WebSocket for subscriptions
const sdk = new ThirdwebSDK("ws://192.168.11.240:8546", {
  supportedChains: [138],
});
```

### Step 6.2: Set Environment Variables
```bash
# Add to your .env file
echo "THIRDWEB_RPC_URL=http://192.168.11.240:8545" >> .env
echo "THIRDWEB_RPC_WS_URL=ws://192.168.11.240:8546" >> .env
echo "THIRDWEB_CHAIN_ID=138" >> .env
```

### Step 6.3: Configure ThirdWeb Dashboard

1. Go to ThirdWeb Dashboard → Settings → Networks
2. Click "Add Custom Network"
3. Enter:
   - **Network Name**: ChainID 138 (Custom)
   - **RPC URL**: `http://192.168.11.240:8545`
   - **Chain ID**: `138`
   - **Currency Symbol**: Your token symbol
   - **Block Explorer**: (Optional) Your explorer URL

### Step 6.4: Test ThirdWeb Connection
```javascript
// Test connection
const provider = await sdk.getProvider();
const network = await provider.getNetwork();
console.log("Connected to:", network.chainId);
```

---

## Phase 7: Production Configuration

### Step 7.1: Set Up Load Balancing (Optional)

**Nginx Configuration:**
```nginx
upstream thirdweb_rpc {
    least_conn;
    server 192.168.11.240:8545;
    server 192.168.11.241:8545;
    server 192.168.11.242:8545;
}

server {
    listen 80;
    server_name rpc.thirdweb.yourdomain.com;

    location / {
        proxy_pass http://thirdweb_rpc;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }
}
```

### Step 7.2: Configure Cloudflare Tunnel (Optional)

**Add to cloudflared config:**
```yaml
ingress:
  - hostname: rpc-thirdweb.d-bis.org
    service: http://192.168.11.240:8545
  - hostname: rpc-thirdweb-2.d-bis.org
    service: http://192.168.11.241:8545
  - hostname: rpc-thirdweb-3.d-bis.org
    service: http://192.168.11.242:8545
```

### Step 7.3: Set Up Monitoring

**Monitor metrics:**
```bash
# Set up Prometheus scraping
# Add to prometheus.yml:
scrape_configs:
  - job_name: 'thirdweb-rpc'
    static_configs:
      - targets:
        - '192.168.11.240:9545'
        - '192.168.11.241:9545'
        - '192.168.11.242:9545'
```

---

## Phase 8: Documentation and Maintenance

### Step 8.1: Update Documentation
- [ ] Update infrastructure documentation with new IPs
- [ ] Document ThirdWeb RPC endpoints
- [ ] Add monitoring dashboards
- [ ] Document load balancing setup (if applicable)

### Step 8.2: Create Backup Procedures
```bash
# Backup Besu data directories
ssh root@192.168.11.10 "pct exec 2400 -- tar -czf /tmp/besu-backup-$(date +%Y%m%d).tar.gz /data/besu"

# Backup configuration files
ssh root@192.168.11.10 "pct exec 2400 -- tar -czf /tmp/besu-config-$(date +%Y%m%d).tar.gz /etc/besu"
```

### Step 8.3: Set Up Health Checks

**Create health check script:**
```bash
#!/bin/bash
# health-check-thirdweb-rpc.sh

for ip in 192.168.11.240 192.168.11.241 192.168.11.242; do
    if curl -s -X POST http://${ip}:8545 \
        -H 'Content-Type: application/json' \
        --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' \
        | grep -q "result"; then
        echo "✓ ${ip}:8545 is healthy"
    else
        echo "✗ ${ip}:8545 is down"
    fi
done
```

---

## Troubleshooting Checklist

If containers fail to start:
- [ ] Check storage availability: `pvesm status`
- [ ] Verify template exists: `pvesm list local`
- [ ] Check container logs: `pct config <VMID>`

If Besu services fail:
- [ ] Check service logs: `journalctl -u besu-rpc.service -f`
- [ ] Verify config file syntax: `besu --config-file=/etc/besu/config-rpc-thirdweb.toml validate`
- [ ] Check disk space: `df -h`
- [ ] Verify network connectivity to validators/sentries

If RPC endpoints don't respond:
- [ ] Verify firewall rules: `iptables -L -n | grep 8545`
- [ ] Check Besu is listening: `netstat -tlnp | grep 8545`
- [ ] Verify chain sync: Check logs for sync progress
- [ ] Test connectivity: `ping` and `nc` tests

---

## Quick Reference Commands

```bash
# Status check
ssh root@192.168.11.10 "pct list | grep 240"

# Restart all services
for vmid in 2400 2401 2402; do
    ssh root@192.168.11.10 "pct exec $vmid -- systemctl restart besu-rpc.service"
done

# View all logs
for vmid in 2400 2401 2402; do
    echo "=== Container $vmid ==="
    ssh root@192.168.11.10 "pct exec $vmid -- journalctl -u besu-rpc.service -n 20"
done

# Test all endpoints
for ip in 240 241 242; do
    curl -X POST http://192.168.11.${ip}:8545 \
        -H 'Content-Type: application/json' \
        --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'
done
```

---

## Completion Checklist

- [ ] All containers created and running
- [ ] IP addresses configured correctly
- [ ] Besu services started and enabled
- [ ] RPC endpoints responding
- [ ] ThirdWeb SDK configured
- [ ] Load balancing configured (if needed)
- [ ] Monitoring set up (if needed)
- [ ] Documentation updated
- [ ] Health checks implemented
