#!/usr/bin/env bash
# Check Besu transaction pool and logs for transaction rejection reasons
# Usage: ./check-besu-transaction-pool.sh

set -uo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_PROJECT="/home/intlc/projects/smom-dbis-138"

source "$SOURCE_PROJECT/.env" 2>/dev/null || true

RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
DEPLOYER=$(cast wallet address --private-key "$PRIVATE_KEY" 2>/dev/null || echo "")
BESU_HOST="192.168.11.250"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
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}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
log_detail() { echo -e "${CYAN}[DETAIL]${NC} $1"; }

echo "========================================="
echo "Besu Transaction Pool & Log Analysis"
echo "========================================="
echo ""

# Check transaction pool status
log_info "Checking transaction pool status..."
TXPOOL_STATUS=$(curl -s -X POST -H "Content-Type: application/json" \
    --data '{"jsonrpc":"2.0","method":"txpool_status","params":[],"id":1}' \
    "$RPC_URL" 2>/dev/null)

if [ -n "$TXPOOL_STATUS" ]; then
    PENDING=$(echo "$TXPOOL_STATUS" | jq -r '.result.pending // 0' 2>/dev/null || echo "0")
    QUEUED=$(echo "$TXPOOL_STATUS" | jq -r '.result.queued // 0' 2>/dev/null || echo "0")
    
    log_info "Transaction Pool Status:"
    log_detail "  Pending: $PENDING"
    log_detail "  Queued: $QUEUED"
    
    if [ "$PENDING" != "0" ] || [ "$QUEUED" != "0" ]; then
        log_warn "⚠ Transactions in pool: $PENDING pending, $QUEUED queued"
    else
        log_success "✓ Transaction pool is empty"
    fi
else
    log_warn "⚠ Could not query transaction pool status"
fi

echo ""

# Check transaction pool content
log_info "Checking transaction pool content..."
TXPOOL_CONTENT=$(curl -s -X POST -H "Content-Type: application/json" \
    --data '{"jsonrpc":"2.0","method":"txpool_content","params":[],"id":1}' \
    "$RPC_URL" 2>/dev/null)

if [ -n "$TXPOOL_CONTENT" ] && [ "$TXPOOL_CONTENT" != "null" ]; then
    if echo "$TXPOOL_CONTENT" | jq -e '.result.pending' >/dev/null 2>&1; then
        PENDING_TXS=$(echo "$TXPOOL_CONTENT" | jq -r '.result.pending | length' 2>/dev/null || echo "0")
        log_info "Found $PENDING_TXS pending transaction(s)"
        
        if [ "$PENDING_TXS" -gt 0 ] && [ -n "$DEPLOYER" ]; then
            log_info "Checking for transactions from deployer ($DEPLOYER)..."
            DEPLOYER_TXS=$(echo "$TXPOOL_CONTENT" | jq -r ".result.pending.\"$DEPLOYER\" // {}" 2>/dev/null)
            if [ -n "$DEPLOYER_TXS" ] && [ "$DEPLOYER_TXS" != "{}" ] && [ "$DEPLOYER_TXS" != "null" ]; then
                TX_COUNT=$(echo "$DEPLOYER_TXS" | jq 'length' 2>/dev/null || echo "0")
                log_warn "⚠ Found $TX_COUNT transaction(s) from deployer in pool"
                
                # Show transaction details
                echo "$DEPLOYER_TXS" | jq -r 'to_entries[] | "  Nonce: \(.key), GasPrice: \(.value.gasPrice), Hash: \(.value.hash)"' 2>/dev/null | head -10
            else
                log_info "No transactions from deployer in pending pool"
            fi
        fi
    fi
else
    log_warn "⚠ Could not retrieve transaction pool content"
fi

echo ""

# Check transaction pool inspect (more detailed)
log_info "Checking transaction pool inspection..."
TXPOOL_INSPECT=$(curl -s -X POST -H "Content-Type: application/json" \
    --data '{"jsonrpc":"2.0","method":"txpool_inspect","params":[],"id":1}' \
    "$RPC_URL" 2>/dev/null)

if [ -n "$TXPOOL_INSPECT" ] && echo "$TXPOOL_INSPECT" | jq -e '.result' >/dev/null 2>&1; then
    log_info "Transaction pool inspection:"
    echo "$TXPOOL_INSPECT" | jq -r '.result.pending // {}' 2>/dev/null | head -30
else
    log_warn "⚠ Could not inspect transaction pool"
fi

echo ""

# Try to access Besu logs via SSH
log_info "Attempting to access Besu logs on $BESU_HOST..."
BESU_LOGS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no "$BESU_HOST" \
    "journalctl -u 'besu*' --no-pager -n 200 2>/dev/null || \
     journalctl -u 'hyperledger*' --no-pager -n 200 2>/dev/null || \
     find /var/log -name '*besu*' -type f 2>/dev/null | head -1 | xargs tail -100 2>/dev/null || \
     echo 'Could not access logs'" 2>&1)

if [ -n "$BESU_LOGS" ] && ! echo "$BESU_LOGS" | grep -q "Could not access logs"; then
    log_success "✓ Retrieved Besu logs"
    echo ""
    log_info "Searching for transaction-related errors..."
    
    # Look for transaction rejection reasons
    echo "$BESU_LOGS" | grep -iE "transaction|reject|invalid|revert|underpriced|nonce|gas" | tail -20
    
    echo ""
    log_info "Searching for mempool-related messages..."
    echo "$BESU_LOGS" | grep -iE "mempool|txpool|pool" | tail -10
else
    log_warn "⚠ Could not access Besu logs directly"
    log_info "Logs may be on a different machine or require different access"
fi

echo ""

# Summary
echo "========================================="
echo "Summary"
echo "========================================="
echo ""

if [ -n "$TXPOOL_STATUS" ]; then
    PENDING=$(echo "$TXPOOL_STATUS" | jq -r '.result.pending // 0' 2>/dev/null || echo "0")
    if [ "$PENDING" != "0" ]; then
        log_warn "⚠️ Transaction pool has $PENDING pending transaction(s)"
        echo ""
        echo "Recommendations:"
        echo "  1. Check transaction details above for rejection reasons"
        echo "  2. If transactions are invalid, they may need to be cleared"
        echo "  3. Check Besu validator logs for specific rejection reasons"
        echo "  4. Consider restarting Besu to clear stuck transactions"
    else
        log_success "✅ Transaction pool appears clear"
        echo ""
        echo "If configuration still fails, the issue may be:"
        echo "  1. Transaction validation rules"
        echo "  2. Gas price limits"
        echo "  3. Account permissioning"
    fi
else
    log_warn "⚠️ Could not determine transaction pool status"
fi

echo ""

