Files
proxmox/scripts/monitoring/monitor-transaction-pool.sh

101 lines
3.2 KiB
Bash
Raw Permalink Normal View History

#!/usr/bin/env bash
# Transaction Pool Monitor
# Monitors transaction pool for stuck transactions
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
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
# Load environment
if [ -f "$PROJECT_ROOT/smom-dbis-138/.env" ]; then
set +e
source "$PROJECT_ROOT/smom-dbis-138/.env" 2>/dev/null || true
set -e
fi
RPC_URL="${RPC_URL_138:-http://${RPC_CORE_1}:8545}"
PRIVATE_KEY="${PRIVATE_KEY:-}"
CHECK_INTERVAL=60 # Check every 60 seconds
STUCK_THRESHOLD=300 # Consider stuck if pending for 5 minutes
CLEANUP_SCRIPT="${SCRIPT_DIR}/cleanup-stuck-transactions.sh"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
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"; }
check_transaction_pool() {
if [ -z "$PRIVATE_KEY" ]; then
log_warn "PRIVATE_KEY not set, skipping transaction pool check"
return 0
fi
local deployer=$(cast wallet address "$PRIVATE_KEY" 2>/dev/null || echo "")
if [ -z "$deployer" ]; then
log_warn "Cannot derive deployer address"
return 0
fi
local latest_hex=$(cast rpc eth_getTransactionCount "$deployer" latest --rpc-url "$RPC_URL" 2>/dev/null)
local pending_hex=$(cast rpc eth_getTransactionCount "$deployer" pending --rpc-url "$RPC_URL" 2>/dev/null)
local latest_clean=$(echo "$latest_hex" | tr -d '"')
local pending_clean=$(echo "$pending_hex" | tr -d '"')
local latest_dec=$(python3 -c "print(int('$latest_clean', 16))" 2>/dev/null || echo "0")
local pending_dec=$(python3 -c "print(int('$pending_clean', 16))" 2>/dev/null || echo "0")
local pending_count=$((pending_dec - latest_dec))
log_info "Latest nonce: $latest_dec"
log_info "Pending nonce: $pending_dec"
log_info "Pending transactions: $pending_count"
if [ "$pending_count" -gt 0 ]; then
log_warn "Found $pending_count pending transaction(s)"
# Check if transactions are stuck (pending for too long)
# This is a simplified check - in production, track transaction age
if [ "$pending_count" -gt 5 ]; then
log_error "High number of pending transactions: $pending_count"
log_warn "Transactions may be stuck. Consider cleanup."
if [ -f "$CLEANUP_SCRIPT" ]; then
log_info "Running cleanup script..."
bash "$CLEANUP_SCRIPT"
fi
fi
else
log_success "No pending transactions"
fi
}
monitor_continuously() {
log_info "Starting transaction pool monitor..."
log_info "RPC URL: $RPC_URL"
log_info "Check interval: ${CHECK_INTERVAL}s"
echo ""
while true; do
check_transaction_pool
sleep "$CHECK_INTERVAL"
done
}
if [ "${1:-}" = "--once" ]; then
check_transaction_pool
else
monitor_continuously
fi