#!/bin/bash
# XDC fleet parity check: for each fleet node, compare its block at the node's
# CURRENT synced height against the canonical public RPC — hash + stateRoot.
# A match = the client validated that block identically (no bypass). Works
# mid-sync (compares at the node's own height, not tip).
SSHK="-p 12141 -i /root/.ssh/id_ed25519 -o ConnectTimeout=8 -o StrictHostKeyChecking=no"
MAIN_RPC="https://rpc.xdcrpc.com"
APO_RPC="https://erpc.apothem.network"
LOG=/mnt/data/snapshots/parity/parity-$(date -u +%Y%m%d-%H%M%S 2>/dev/null || echo run).log
mkdir -p /mnt/data/snapshots/parity 2>/dev/null

rpc(){ curl -s -X POST "$1" -H 'Content-Type: application/json' -d "$2" --max-time 10 2>/dev/null; }
# node RPC over ssh to <host>:<port>
nrpc(){ ssh -n $SSHK root@"$1" "curl -s -X POST http://localhost:$2 -H 'Content-Type: application/json' -d '$3' --max-time 6 2>/dev/null"; }
hexfield(){ echo "$1" | grep -oE "\"$2\":\"0x[0-9a-f]+\"" | head -1 | sed -E "s/.*:\"//;s/\"//"; }

# node spec: name host port network
NODES="
geth-mainnet 167.235.13.113 9745 main
nethermind-mainnet 167.235.13.113 10045 main
nethermind-apothem 167.235.13.113 10095 apo
geth-apothem 167.235.13.113 9795 apo
xone-mainnet 167.235.13.113 10245 main
reth-apothem 167.235.13.113 10345 apo
besu-apothem 167.235.13.113 9995 apo
erigon-apothem 65.21.71.4 9895 apo
"
echo "=== XDC fleet parity check $(date -u 2>/dev/null) ===" | tee -a "$LOG"
printf "%-16s %-10s %-8s %-10s %s\n" NODE HEIGHT PEERS PARITY DETAIL | tee -a "$LOG"
echo "$NODES" | while read name host port net; do
  [ -z "$name" ] && continue
  bnresp=$(nrpc "$host" "$port" '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}')
  bn=$(echo "$bnresp" | grep -oE '0x[0-9a-f]+' | head -1)
  [ -z "$bn" ] && { printf "%-16s %-10s %-8s %-10s %s\n" "$name" "DOWN" "-" "SKIP" "no RPC" | tee -a "$LOG"; continue; }
  dec=$((16#${bn#0x}))
  pc=$(nrpc "$host" "$port" '{"jsonrpc":"2.0","method":"net_peerCount","params":[],"id":1}' | grep -oE '0x[0-9a-f]+' | head -1)
  peers=$([ -n "$pc" ] && echo $((16#${pc#0x})) || echo -)
  [ "$dec" -lt 1 ] && { printf "%-16s %-10s %-8s %-10s %s\n" "$name" "$dec" "$peers" "SKIP" "at genesis" | tee -a "$LOG"; continue; }
  # compare block at the node's height vs canonical
  canon=$([ "$net" = main ] && echo "$MAIN_RPC" || echo "$APO_RPC")
  q="{\"jsonrpc\":\"2.0\",\"method\":\"eth_getBlockByNumber\",\"params\":[\"$bn\",false],\"id\":1}"
  nblk=$(nrpc "$host" "$port" "$q"); cblk=$(rpc "$canon" "$q")
  nh=$(hexfield "$nblk" hash); ch=$(hexfield "$cblk" hash)
  ns=$(hexfield "$nblk" stateRoot); cs=$(hexfield "$cblk" stateRoot)
  if [ -z "$ch" ]; then verdict="NO-CANON"; detail="canonical RPC no block@$dec"
  elif [ "$nh" = "$ch" ] && [ "$ns" = "$cs" ]; then verdict="MATCH"; detail="hash+stateRoot == canonical @ $dec"
  elif [ "$nh" = "$ch" ]; then verdict="HASH-ONLY"; detail="hash match, stateRoot differs @ $dec (POSSIBLE BYPASS)"
  else verdict="MISMATCH"; detail="node $nh vs canon $ch @ $dec"
  fi
  printf "%-16s %-10s %-8s %-10s %s\n" "$name" "$dec" "$peers" "$verdict" "$detail" | tee -a "$LOG"
done
echo "=== log: $LOG ===" | tee -a "$LOG"
