Files
wikideck/deploy/night/testnet.sh
T

50 lines
2.9 KiB
Bash
Executable File

#!/bin/sh
# sim-testnet: private 3-node testnet fixture for WikiDeck Night 1 (poweredge-r350, LAN only).
# Usage (from the repo root):
# deploy/night/testnet.sh up build the image from this checkout and (re)start the 3 nodes
# deploy/night/testnet.sh rolling build, then restart node-a, b, c one at a time (waits for /healthz)
# deploy/night/testnet.sh busy print live matches + open lobbies across the 3 nodes
# deploy/night/testnet.sh mine N [W] mine N real RandomX blocks with W workers (default 4, max 8)
# deploy/night/testnet.sh status tip/height of each node
# deploy/night/testnet.sh down stop the nodes (volumes, i.e. the ledgers, are kept)
# STATE_DIR (default ~/.local/share/wikideck-night) holds the disposable testnet wallets and mined.log.
# It is outside the repo on purpose: never commit wallets.
set -eu
P=wikideck-night
LAN_IP=${LAN_IP:-192.168.1.32}
STATE_DIR=${STATE_DIR:-$HOME/.local/share/wikideck-night}
DC="docker compose -p $P -f compose.testnet.yaml -f deploy/night/compose.lan.yaml"
case "${1:-}" in
up) $DC build node-a && $DC up -d --no-build ;;
down) $DC down ;;
rolling)
# Never take the whole testnet dark: one node at a time, each must pass /healthz before the next.
$DC build node-a
for n in a b c; do
port=$(( 3100 + $(printf %d "'$n") - 96 ))
$DC up -d --no-build --no-deps node-$n
i=0
until node -e "fetch('http://$LAN_IP:$port/healthz',{signal:AbortSignal.timeout(3000)}).then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"; do
i=$((i+1)); [ $i -ge 60 ] && { echo "node-$n not healthy after 120 s"; exit 1; }; sleep 2
done
echo "node-$n healthy"
done ;;
busy)
# Sum of liveMatches + openLobbies over all hosted modes on all nodes (unreachable node counts 0).
node -e "Promise.all([3101,3102,3103].map(p=>fetch('http://$LAN_IP:'+p+'/api/game/modes',{signal:AbortSignal.timeout(5000)}).then(r=>r.json()).then(m=>m.modes.reduce((t,x)=>t+(x.stats?.liveMatches??0)+(x.stats?.openLobbies??0),0)).catch(()=>0))).then(v=>console.log(v.reduce((a,b)=>a+b,0)))" ;;
mine)
mkdir -p "$STATE_DIR"
W=${3:-4}
docker rm -f $P-miner >/dev/null 2>&1 || true
docker run -d --name $P-miner --network ${P}_default --cpus "$W" --memory 1600m \
--user "$(id -u):$(id -g)" -e WIKIDECK_NETWORK=testnet -e WIKIDECK_GENESIS_TIME=1792411200 \
-e MIN_GAP="${MIN_GAP:-45}" -v "$STATE_DIR":/state -v "$PWD/deploy/night":/night:ro \
wikideck:testnet node --experimental-strip-types /night/mine.ts "$2" "$W"
echo "follow with: docker logs -f $P-miner" ;;
status)
for port in 3101 3102 3103; do
docker run --rm --network host wikideck:testnet node -e "fetch('http://$LAN_IP:$port/p2p/status').then(r=>r.json()).then(s=>console.log($port,'blocks',s.size,'tip',s.tip.slice(0,16))).catch(e=>console.log($port,'DOWN',e.message))"
done ;;
*) sed -n 2,11p "$0"; exit 1 ;;
esac