#!/usr/bin/env bash # Cleanup Stuck Transactions # Detects and cleans up stuck transactions from mempool 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:-}" STUCK_THRESHOLD=300 # 5 minutes # 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_and_cleanup() { if [ -z "$PRIVATE_KEY" ]; then log_error "PRIVATE_KEY not set" return 1 fi local deployer=$(cast wallet address "$PRIVATE_KEY" 2>/dev/null || echo "") if [ -z "$deployer" ]; then log_error "Cannot derive deployer address" return 1 fi log_info "Checking for stuck transactions..." 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" -eq 0 ]; then log_success "No stuck transactions found" return 0 fi log_warn "Found $pending_count pending transaction(s)" log_info "Note: This script can detect stuck transactions but cannot automatically clear them." log_info "Options:" log_info " 1. Wait for transactions to confirm (if blocks are being produced)" log_info " 2. Use nonce skip to deploy new transactions" log_info " 3. Clear transaction pool database on all nodes (requires restart)" return 0 } main() { log_info "Stuck Transaction Cleanup" echo "" check_and_cleanup } main "$@"