#!/usr/bin/env bash
# =============================================================================
#  XDC xone client fragment  —  sourced by one.sh
#  Binary:   xone  (ETH2030 / go-ethereum-derived XDC execution client)
#  Modes:    snap (default) · full · archive · validator
#  Networks: apothem · mainnet (xdc-mainnet) · devnet (xdc-devnet)
#  Status:   Experimental — research client. Do NOT use for production/real funds.
#            Build: https://github.com/XDCIndia/xOneGo
#
#  xone ships BUILT-IN genesis + bootnodes for apothem / xdc-mainnet / xdc-devnet,
#  so no --bootnodes is required for peering. Override with XDC_BOOTNODES if needed.
#
#  Node types (--mode):
#    snap       -syncmode snap                 fast initial sync (default)
#    full       -syncmode full                 full block execution from genesis
#    archive    -syncmode full -gcmode archive full node, no state pruning
#    validator  -syncmode full -mine …         XDPoS V2 block producer (needs keystore)
#
#  Validator mode requires a funded/authorised signer account:
#    XDC_ETHERBASE        signer address (0x…) for --miner.etherbase (required)
#    XDC_MINER_PASSWORD   path to keystore password file (required to unlock)
#    XDC_UNLOCK           address to unlock (defaults to XDC_ETHERBASE)
#  ONE_SH_VERSION: 1.0.0
# =============================================================================

# Map the installer's network name → xone's -network value.
_xone_network(){
  case "${1:-apothem}" in
    mainnet) echo "xdc-mainnet" ;;
    devnet)  echo "xdc-devnet" ;;
    *)       echo "apothem" ;;
  esac
}

# Hook: no snapshot published for xone yet.
client_snapshot_name(){
  echo ""
}

# Hook: sync time estimate (no snapshot → sync from genesis).
client_sync_estimate(){
  local _mode="${2:-snap}"
  case "$_mode" in
    snap)    echo "hours–days (snap sync from genesis; no snapshot published)" ;;
    *)       echo "days (full execution sync from genesis; no snapshot published)" ;;
  esac
}

# Hook: pre-flight requirements. Only validator mode has extra needs.
client_requirements(){
  if [ "${NODETYPE:-}" = "validator" ]; then
    if [ -z "${XDC_ETHERBASE:-}" ]; then
      err "xone validator mode requires a signer address."
      err "  Set XDC_ETHERBASE=0x… (and XDC_MINER_PASSWORD=/path/to/pwfile to unlock the keystore)."
      return 1
    fi
    if [ -n "${XDC_MINER_PASSWORD:-}" ] && [ ! -f "${XDC_MINER_PASSWORD}" ]; then
      err "XDC_MINER_PASSWORD points to a missing file: ${XDC_MINER_PASSWORD}"
      return 1
    fi
  fi
  return 0
}

# Hook: launch arguments. Signature: client_start_args <network> <mode>
client_start_args(){
  local _net="${1:-apothem}" _mode="${2:-snap}"
  local _datadir="${DATADIR:-$DIR/data}"
  local _xnet; _xnet=$(_xone_network "$_net")

  # Mode → sync/prune/miner flags.
  local _mode_args=""
  case "$_mode" in
    snap)      _mode_args="-syncmode snap" ;;
    full)      _mode_args="-syncmode full" ;;
    archive)   _mode_args="-syncmode full -gcmode archive" ;;
    validator)
      _mode_args="-syncmode full -mine -miner.etherbase \"${XDC_ETHERBASE}\""
      [ -n "${XDC_MINER_PASSWORD:-}" ] && _mode_args="$_mode_args -password \"${XDC_MINER_PASSWORD}\" -unlock \"${XDC_UNLOCK:-$XDC_ETHERBASE}\""
      ;;
    *)         _mode_args="-syncmode snap" ;;
  esac

  # Optional operator-supplied bootnodes (xone has sane built-ins otherwise).
  local _boot_args=""
  [ -n "${XDC_BOOTNODES:-}" ] && _boot_args="-bootnodes \"${XDC_BOOTNODES}\""

  printf '%s' "\
-datadir \"$_datadir\" \
-network \"$_xnet\" \
-http -http.addr 127.0.0.1 -http.port \"${RPC_PORT:-8615}\" \
-http.api eth,net,web3,debug,txpool,admin \
-ws -ws.addr 127.0.0.1 -ws.port \"${WS_PORT:-8616}\" -ws.api eth,net,web3 \
-authrpc.addr 127.0.0.1 -authrpc.port \"${AUTH_PORT:-8617}\" \
-port \"${P2P_PORT:-30356}\" \
-maxpeers ${MAXPEERS:-50} \
$_boot_args \
$_mode_args \
-verbosity ${VERBOSITY:-3}"
}

# Hook: RPC port for health-check.
client_rpc_port(){ echo "${RPC_PORT:-8615}"; }

# Hook: generic SIGTERM stop.
client_stop(){ return 0; }
