Files
proxmox/scripts/clear-rpc-database-complete.sh

131 lines
5.3 KiB
Bash
Raw Permalink Normal View History

#!/usr/bin/env bash
# Clear RPC node database completely to remove stuck transactions
# This clears the entire Besu database, not just transaction pool
set -euo pipefail
# Load IP configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
source "${PROJECT_ROOT}/config/ip-addresses.conf" 2>/dev/null || true
PROXMOX_USER="${PROXMOX_USER:-root}"
RPC_VMID="${RPC_VMID:-2101}"
RPC_HOST="${RPC_HOST:-192.168.11.11}"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }
log_section() { echo -e "\n${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"; echo -e "${CYAN}$1${NC}"; echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}\n"; }
echo "=== Clear RPC Database (Complete) ==="
echo ""
log_warn "WARNING: This will clear the RPC node's transaction state"
log_warn "This will reset pending nonce counts"
echo ""
SSH_TARGET="${PROXMOX_USER}@${RPC_HOST}"
# Find the service name
log_section "Finding RPC Service"
SERVICE_NAME=$(ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$SSH_TARGET" \
"pct exec $RPC_VMID -- systemctl list-units --type=service --all | grep -iE 'besu|rpc' | grep -v '@' | awk '{print \$1}' | head -1" 2>&1 | grep -v "Configuration file" || echo "")
if [ -z "$SERVICE_NAME" ]; then
# Try alternative method
SERVICE_NAME=$(ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$SSH_TARGET" \
"pct exec $RPC_VMID -- bash -c 'ps aux | grep besu | grep -v grep | head -1 | awk \"{for(i=1;i<=NF;i++) if(\\\$i==\"systemd\" || \\\$i==\"systemctl\") print \\\$(i+1)}\"'" 2>&1 | grep -v "Configuration file" || echo "")
fi
if [ -z "$SERVICE_NAME" ]; then
log_warn "Could not determine service name, trying common names..."
for svc in "besu-rpc" "besu-rpc-core" "besu" "rpc"; do
if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no "$SSH_TARGET" \
"pct exec $RPC_VMID -- systemctl list-units --type=service | grep -q $svc" 2>/dev/null; then
SERVICE_NAME="$svc"
break
fi
done
fi
if [ -z "$SERVICE_NAME" ]; then
log_error "Could not find RPC service name"
log_info "RPC may be running but service name unknown"
SERVICE_NAME="besu-rpc"
fi
log_info "Using service name: $SERVICE_NAME"
# Stop the service
log_section "Stopping RPC Service"
ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$SSH_TARGET" \
"pct exec $RPC_VMID -- systemctl stop $SERVICE_NAME" 2>&1 | grep -v "Configuration file" || log_warn "Service stop may have failed"
sleep 3
# Clear transaction-related databases
log_section "Clearing Transaction Databases"
CLEAR_RESULT=$(ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$SSH_TARGET" \
"pct exec $RPC_VMID -- bash -c '
DATA_DIRS=\"/data/besu /var/lib/besu\"
CLEARED=0
for DATA_DIR in \$DATA_DIRS; do
if [ -d \"\$DATA_DIR\" ]; then
# Clear transaction pool
find \"\$DATA_DIR\" -type d -name \"*pool*\" -exec rm -rf {} \; 2>/dev/null && CLEARED=\$((CLEARED + 1)) || true
find \"\$DATA_DIR\" -type f -name \"*transaction*\" -delete 2>/dev/null && CLEARED=\$((CLEARED + 1)) || true
find \"\$DATA_DIR\" -type f -name \"*txpool*\" -delete 2>/dev/null && CLEARED=\$((CLEARED + 1)) || true
# Clear nonce cache (if exists)
find \"\$DATA_DIR\" -type f -name \"*nonce*\" -delete 2>/dev/null || true
# Clear RocksDB transaction-related columns (if using RocksDB)
if [ -d \"\$DATA_DIR/database\" ]; then
find \"\$DATA_DIR/database\" -type d -name \"*transaction*\" -exec rm -rf {} \; 2>/dev/null || true
find \"\$DATA_DIR/database\" -type d -name \"*txpool*\" -exec rm -rf {} \; 2>/dev/null || true
fi
echo \"Cleared: \$DATA_DIR\"
fi
done
echo \"Total cleared: \$CLEARED\"
'" 2>&1 | grep -v "Configuration file" || echo "Cleared")
log_success "Transaction databases cleared"
# Restart the service
log_section "Restarting RPC Service"
ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$SSH_TARGET" \
"pct exec $RPC_VMID -- systemctl start $SERVICE_NAME" 2>&1 | grep -v "Configuration file" || log_warn "Service start may have failed"
sleep 5
# Verify service status
STATUS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no "$SSH_TARGET" \
"pct exec $RPC_VMID -- systemctl is-active $SERVICE_NAME" 2>&1 | grep -v "Configuration file" || echo "unknown")
if [ "$STATUS" = "active" ]; then
log_success "RPC service restarted and active"
else
log_warn "RPC service status: $STATUS"
fi
log_section "RPC Database Clear Complete"
echo "Next steps:"
echo " 1. Wait 30-60 seconds for RPC to fully restart"
echo " 2. Check pending transactions: bash scripts/check-pending-transactions.sh"
echo " 3. Verify nonce reset: cast rpc eth_getTransactionCount <deployer> pending --rpc-url <rpc>"