Files
proxmox/scripts/deployment/preflight-chain138-deploy.sh
defiQUG de1a274f6a fix(ops): Proxmox SSH user for pool clear; optional preflight RPC override
- clear-all-transaction-pools: use PROXMOX_SSH_USER (never root@pam for SSH);
  align R630 host with ip-addresses PROXMOX_R630_01; document post-clear RPC delay
- preflight-chain138-deploy: CHAIN138_PREFLIGHT_RPC_URL for nonce/RPC checks when
  Core 2101 is restarting (e.g. after pool clear) but public RPC is up

Made-with: Cursor
2026-04-01 11:46:20 -07:00

114 lines
4.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Pre-flight checks before Chain 138 deployment: correct RPC, dotenv, nonce (no stuck txs).
# Optionally run gas/cost estimate for deployment cost.
#
# Usage: ./scripts/deployment/preflight-chain138-deploy.sh [--cost]
# --cost Also run cost estimate (smom-dbis-138/scripts/deployment/calculate-costs-consolidated.sh).
# Optional env: CHAIN138_PREFLIGHT_RPC_URL=https://... Same chainId 138; use when Core RPC (2101) is down briefly
# (e.g. after tx-pool clear / RocksDB WAL) and public RPC (e.g. 192.168.11.221:8545) is up. Deploy still use Core when healthy.
#
# Requires: run from repo root. Uses smom-dbis-138/.env only (PRIVATE_KEY, RPC_URL_138).
# See: docs/03-deployment/DEPLOYMENT_ORDER_OF_OPERATIONS.md § Deployment safety
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
SMOM="${PROJECT_ROOT}/smom-dbis-138"
RUN_COST=""
for a in "$@"; do [[ "$a" == "--cost" ]] && RUN_COST=1; done
echo "=== Chain 138 deployment pre-flight ==="
echo ""
# 1) Dotenv: must exist and be the canonical one
if [[ ! -f "$SMOM/.env" ]]; then
echo "FAIL: $SMOM/.env not found. Use smom-dbis-138/.env only for deployment secrets." >&2
exit 1
fi
echo "OK Dotenv: $SMOM/.env exists (canonical for Chain 138 deploy)."
# 2) Env check script (keys only, no values)
if [[ -f "$SMOM/scripts/deployment/check-env-required.sh" ]]; then
echo ""
bash "$SMOM/scripts/deployment/check-env-required.sh" || { echo "FAIL: env check failed. Fix MISS entries in $SMOM/.env." >&2; exit 1; }
else
echo "WARN: $SMOM/scripts/deployment/check-env-required.sh not found; skipping env key check." >&2
fi
# 3) Load env for RPC and nonce checks (no secrets printed)
[[ -f "${PROJECT_ROOT}/config/ip-addresses.conf" ]] && source "${PROJECT_ROOT}/config/ip-addresses.conf" 2>/dev/null || true
if [[ -f "$SMOM/scripts/lib/deployment/dotenv.sh" ]]; then
# shellcheck disable=SC1090
source "$SMOM/scripts/lib/deployment/dotenv.sh"
load_deployment_env --repo-root "$SMOM"
else
local_had_nounset=0
if [[ $- == *u* ]]; then
local_had_nounset=1
set +u
fi
set -a
# shellcheck disable=SC1090
source "$SMOM/.env"
set +a
(( local_had_nounset )) && set -u
fi
RPC="${RPC_URL_138:-http://192.168.11.211:8545}"
# Optional: same chain, different endpoint (e.g. public RPC 2201) when Core 2101 is restarting after pool maintenance.
if [[ -n "${CHAIN138_PREFLIGHT_RPC_URL:-}" ]]; then
RPC="$CHAIN138_PREFLIGHT_RPC_URL"
echo "NOTE: Using CHAIN138_PREFLIGHT_RPC_URL for this preflight only: $RPC" >&2
fi
if [[ -z "${PRIVATE_KEY:-}" ]]; then
echo "FAIL: PRIVATE_KEY not set in $SMOM/.env." >&2
exit 1
fi
# 4) RPC: must be Core (chainId 138 = 0x8a)
echo ""
chain_id_hex=$(curl -s -m 10 -X POST "$RPC" -H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"eth_chainId","params":[],"id":1}' 2>/dev/null | sed -n 's/.*"result":"\([^"]*\)".*/\1/p') || true
if [[ "$chain_id_hex" != "0x8a" ]]; then
if [[ -n "$chain_id_hex" ]]; then
echo "FAIL: RPC returned chainId $chain_id_hex (expected 0x8a for Chain 138). Use Core RPC only (RPC_URL_138 = VMID 2101, e.g. http://192.168.11.211:8545)." >&2
else
echo "FAIL: RPC unreachable: $RPC. Fix Core RPC or network." >&2
fi
echo " See docs/04-configuration/RPC_ENDPOINTS_MASTER.md." >&2
exit 1
fi
echo "OK RPC (Core): $RPC (chainId 138)."
# 5) Nonce: warn if pending > latest (stuck txs)
DEPLOYER=$(cast wallet address --private-key "$PRIVATE_KEY" 2>/dev/null) || { echo "FAIL: cast wallet address failed. Check PRIVATE_KEY in .env." >&2; exit 1; }
NONCE_PENDING=$(cast nonce "$DEPLOYER" --rpc-url "$RPC" --block pending 2>/dev/null) || true
NONCE_LATEST=$(cast nonce "$DEPLOYER" --rpc-url "$RPC" --block latest 2>/dev/null) || true
# Normalize to decimal (cast may return hex 0xN or decimal N)
[[ -z "${NONCE_LATEST//[0-9a-fA-Fx]/}" && -n "$NONCE_LATEST" ]] || NONCE_LATEST=0
[[ -z "${NONCE_PENDING//[0-9a-fA-Fx]/}" && -n "$NONCE_PENDING" ]] || NONCE_PENDING="$NONCE_LATEST"
NONCE_PENDING=$((NONCE_PENDING))
NONCE_LATEST=$((NONCE_LATEST))
echo " Deployer: $DEPLOYER"
echo " Nonce (pending): $NONCE_PENDING (latest: $NONCE_LATEST)"
if [[ $NONCE_PENDING -gt $NONCE_LATEST ]]; then
echo ""
echo "WARN: Pending nonce ($NONCE_PENDING) > latest ($NONCE_LATEST) — possible stuck transaction(s)." >&2
echo " Do NOT deploy until resolved: run ./scripts/clear-all-transaction-pools.sh then wait ~60s." >&2
echo " See docs/03-deployment/DEPLOYMENT_ORDER_OF_OPERATIONS.md § Do not deploy when stuck." >&2
exit 1
fi
echo "OK No stuck transactions (nonce consistent)."
# 6) Optional: cost estimate
if [[ -n "$RUN_COST" ]] && [[ -f "$SMOM/scripts/deployment/calculate-costs-consolidated.sh" ]]; then
echo ""
echo "=== Cost estimate (--cost) ==="
(cd "$SMOM" && bash scripts/deployment/calculate-costs-consolidated.sh) || true
fi
echo ""
echo "Pre-flight passed. Proceed with deployment using scripts that source $SMOM/.env and use RPC_URL_138 (Core only)."