#!/usr/bin/env bash # Flush Mempools for All RPC Nodes and Validators # Clears pending transactions from Besu nodes 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 # 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"; } # RPC Nodes RPC_NODES=( "2101:${RPC_CORE_1}:besu-rpc-core-1" "2201:${RPC_PUBLIC_1}:besu-rpc-public-1" ) # Validators (may not have RPC exposed, but check anyway) VALIDATORS=( "1000:${PROXMOX_HOST_ML110}0:besu-validator-1" "1001:${PROXMOX_HOST_ML110}1:besu-validator-2" "1002:${PROXMOX_HOST_ML110}2:besu-validator-3" "1003:${PROXMOX_HOST_ML110}3:besu-validator-4" "1004:${PROXMOX_HOST_ML110}4:besu-validator-5" ) PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}" log_section "Flush Mempools for All Nodes" # Function to check and flush mempool for a node flush_node_mempool() { local vmid=$1 local ip=$2 local name=$3 local rpc_url="http://${ip}:8545" log_info "Checking $name (VMID $vmid, IP $ip)..." # Check if RPC is accessible if ! cast block-number --rpc-url "$rpc_url" >/dev/null 2>&1; then log_warn "$name: RPC not accessible, skipping" return 1 fi # Check if admin API is available ADMIN_AVAILABLE=false if cast rpc admin_peers --rpc-url "$rpc_url" >/dev/null 2>&1; then ADMIN_AVAILABLE=true log_info "$name: Admin API available" else log_warn "$name: Admin API not available" fi # Try to get pending transactions count PENDING_COUNT=0 if [ "$ADMIN_AVAILABLE" = true ]; then # Try Besu-specific admin methods PENDING=$(cast rpc txpool_besuPendingTransactions --rpc-url "$rpc_url" 2>&1 || echo "[]") if echo "$PENDING" | grep -q "\["; then PENDING_COUNT=$(echo "$PENDING" | jq 'length' 2>/dev/null || echo "0") log_info "$name: Found $PENDING_COUNT pending transaction(s)" fi fi # Try to flush mempool using admin API if [ "$ADMIN_AVAILABLE" = true ] && [ "$PENDING_COUNT" -gt 0 ]; then log_info "$name: Attempting to clear mempool..." # Try different Besu admin methods # Method 1: txpool_clearPendingTransactions (if available) if cast rpc txpool_clearPendingTransactions --rpc-url "$rpc_url" 2>&1 | grep -q "true\|success"; then log_success "$name: Mempool cleared successfully" return 0 fi # Method 2: Use debug API to drop transactions # This requires DEBUG API to be enabled log_warn "$name: Standard clear method not available, trying alternative..." fi # If no pending transactions or can't clear, report status if [ "$PENDING_COUNT" -eq 0 ]; then log_success "$name: No pending transactions" else log_warn "$name: $PENDING_COUNT pending transaction(s) found but cannot clear (may require node restart)" fi return 0 } # Flush RPC nodes log_section "Flushing RPC Nodes" for node in "${RPC_NODES[@]}"; do IFS=':' read -r vmid ip name <<< "$node" # Check if we can access via Proxmox if ssh -o ConnectTimeout=5 root@$PROXMOX_HOST "pct status $vmid 2>&1" | grep -q "running"; then # Try direct RPC access flush_node_mempool "$vmid" "$ip" "$name" else log_warn "$name (VMID $vmid): Container not running, skipping" fi echo "" done # Check validators (may not have RPC exposed) log_section "Checking Validators" for validator in "${VALIDATORS[@]}"; do IFS=':' read -r vmid ip name <<< "$validator" log_info "Checking $name (VMID $vmid, IP $ip)..." # Validators typically don't expose RPC, but check anyway if cast block-number --rpc-url "http://${ip}:8545" >/dev/null 2>&1; then flush_node_mempool "$vmid" "$ip" "$name" else log_info "$name: RPC not exposed (normal for validators)" fi echo "" done log_section "Mempool Flush Complete" log_info "Summary:" log_info " • RPC nodes checked and flushed where possible" log_info " • Validators checked (may not expose RPC)" log_info "" log_warn "Note: If transactions persist, may require Besu node restart" log_info "Next: Proceed with deployment using cleared mempool"