diff --git a/deploy/night/README.md b/deploy/night/README.md new file mode 100644 index 0000000..ed73676 --- /dev/null +++ b/deploy/night/README.md @@ -0,0 +1,18 @@ +# Night 1 private testnet (sim-testnet) + +A 3-node **testnet-only** fixture on poweredge-r350. It is reachable on the LAN only, and uses the Docker project `wikideck-night`. + +| node | URL | +|---|---| +| node-a | http://192.168.1.32:3101 | +| node-b | http://192.168.1.32:3102 | +| node-c | http://192.168.1.32:3103 | + +- `compose.lan.yaml` overrides only the published ports (bound to `192.168.1.32`, never `0.0.0.0`) and `PUBLIC_ORIGIN`. It changes nothing in consensus or the image. +- `mine.ts` is a multi-worker native RandomX miner. It runs inside the `wikideck:testnet` image, which ships the native engine, so the host needs no cmake. + - It mines to 4 disposable wallets (alice, bob, carol, dave) and writes chain-bound encrypted browser backups to `$STATE_DIR/wallets/`. + - It never changes consensus code. Speed comes from the protocol's own testnet parameters: the initial target accepts 1/16 of hashes, and each 10-block retarget at most quadruples the difficulty. + - Each template costs several Wikipedia API calls, and Wikipedia rate-limits our egress IP. The miner therefore paces templates (`MIN_GAP`, default 45 s) and backs off exponentially on failure. +- `testnet.sh` has `up`, `mine N [workers]`, `status` and `down`. Run it from the repo root on a checkout of the integration branch. `up` rebuilds the image from that checkout; the ledgers persist in the named volumes. + +Never point this at mainnet or the live `wikideck` namespace. diff --git a/deploy/night/compose.lan.yaml b/deploy/night/compose.lan.yaml new file mode 100644 index 0000000..973bd1d --- /dev/null +++ b/deploy/night/compose.lan.yaml @@ -0,0 +1,24 @@ +# sim-testnet override: publish the fixture on r350's LAN address only (not 0.0.0.0). +# Game-mode env (modes team, #6) is inert until night/modes/hosting merges. +services: + node-a: + ports: !override ["192.168.1.32:3101:3001"] + environment: + PUBLIC_ORIGIN: http://192.168.1.32:3101 + WIKIDECK_MODES: classic,trending + WIKIDECK_GAME_OPERATOR: Testnet A + WIKIDECK_GAME_PEERS: http://192.168.1.32:3102,http://192.168.1.32:3103 + node-b: + ports: !override ["192.168.1.32:3102:3001"] + environment: + PUBLIC_ORIGIN: http://192.168.1.32:3102 + WIKIDECK_MODES: draft + WIKIDECK_GAME_OPERATOR: Testnet B + WIKIDECK_GAME_PEERS: http://192.168.1.32:3101,http://192.168.1.32:3103 + node-c: + ports: !override ["192.168.1.32:3103:3001"] + environment: + PUBLIC_ORIGIN: http://192.168.1.32:3103 + WIKIDECK_MODES: classic,trending,draft + WIKIDECK_GAME_OPERATOR: Testnet C + WIKIDECK_GAME_PEERS: http://192.168.1.32:3101,http://192.168.1.32:3102 diff --git a/deploy/night/mine.ts b/deploy/night/mine.ts new file mode 100644 index 0000000..96f9e4e --- /dev/null +++ b/deploy/night/mine.ts @@ -0,0 +1,99 @@ +// sim-testnet: multi-worker native RandomX miner for the PRIVATE wikideck-night testnet. +// Runs inside the wikideck:testnet image (which ships the native engine). Testnet only. +// Usage: node --experimental-strip-types /night/mine.ts +import { mkdirSync, readFileSync, writeFileSync, existsSync, appendFileSync } from 'node:fs'; +import { randomInt, webcrypto } from 'node:crypto'; +import { RandomX } from '/app/server/testnet/randomx.ts'; +import { CHAIN_ID, sha256, miningBlob, mintId, blockSchema, address } from '/app/server/testnet/consensus.ts'; +import { blockClaimMessage, NETWORK, type MiningTemplate, type TestnetBlock } from '/app/shared/testnet.ts'; +import { encryptWallet } from '/app/shared/wallet-backup.ts'; + +if (!NETWORK.includes('testnet')) throw new Error('testnet only'); +const blocks = Number(process.argv[2] ?? 45), workers = Number(process.argv[3] ?? 4); +if (!(workers >= 1 && workers <= 8)) throw new Error('1..8 workers'); +const NODES = ['http://node-a:3001', 'http://node-b:3001', 'http://node-c:3001']; +const WALLET_NAMES = ['alice', 'bob', 'carol', 'dave']; +const PASSWORD = 'wikideck-night-testnet'; +const STATE = process.env.STATE_DIR ?? '/state'; +const dir = `${STATE}/wallets`; +mkdirSync(dir, { recursive: true }); +const subtle = webcrypto.subtle; + +type W = { name: string; address: string; publicKey: string; privateKey: CryptoKey }; +async function loadOrCreate(name: string): Promise { + const keyFile = `${dir}/${name}.jwk.json`; + let jwk: JsonWebKey; + if (existsSync(keyFile)) jwk = JSON.parse(readFileSync(keyFile, 'utf8')); + else { + const kp = await subtle.generateKey({ name: 'ECDSA', namedCurve: 'P-256' }, true, ['sign', 'verify']) as CryptoKeyPair; + jwk = await subtle.exportKey('jwk', kp.privateKey); + writeFileSync(keyFile, JSON.stringify(jwk), { mode: 0o600 }); + } + const privateKey = await subtle.importKey('jwk', jwk, { name: 'ECDSA', namedCurve: 'P-256' }, true, ['sign']); + const pubJwk = { kty: jwk.kty, crv: jwk.crv, x: jwk.x, y: jwk.y }; + const pub = await subtle.importKey('jwk', pubJwk, { name: 'ECDSA', namedCurve: 'P-256' }, true, ['verify']); + const publicKey = Buffer.from(await subtle.exportKey('spki', pub)).toString('base64'); + const w = { name, address: address(publicKey), publicKey, privateKey }; + const backupFile = `${dir}/${name}.backup.json`; + if (!existsSync(backupFile)) writeFileSync(backupFile, JSON.stringify(await encryptWallet(privateKey, publicKey, PASSWORD), null, 2)); + return w; +} +const wallets = await Promise.all(WALLET_NAMES.map(loadOrCreate)); +writeFileSync(`${dir}/addresses.json`, JSON.stringify(Object.fromEntries(wallets.map(w => [w.name, w.address])), null, 2)); +console.log(`chain ${CHAIN_ID}; wallets: ${wallets.map(w => `${w.name}=${w.address.slice(0, 12)}`).join(' ')}`); + +async function api(node: string, path: string, body?: unknown) { + const r = await fetch(node + path, { method: body ? 'POST' : 'GET', redirect: 'error', signal: AbortSignal.timeout(60000), + headers: body ? { 'Content-Type': 'application/json' } : {}, body: body ? JSON.stringify(body) : undefined }); + const data = await r.json(); + if (!r.ok) throw new Error(data.error ?? `HTTP ${r.status}`); + return data; +} +const engines = Array.from({ length: workers }, () => new RandomX()); +const close = () => { for (const e of engines) e.close(); }; +process.once('SIGTERM', () => { close(); process.exit(0); }); + +let accepted = 0, round = 0, fails = 0; +// Each template costs several Wikipedia API calls, and Wikipedia rate-limits this egress IP: +// pace templates (>= MIN_GAP s apart) and back off exponentially on failure. +const MIN_GAP = Number(process.env.MIN_GAP ?? 45), sleep = (s: number) => new Promise(r => setTimeout(r, s * 1000)); +let lastTemplate = 0; +try { + while (accepted < blocks) { + const wait = lastTemplate + MIN_GAP * 1000 - Date.now(); if (wait > 0) await sleep(wait / 1000); + lastTemplate = Date.now(); + const node = NODES[round % NODES.length], w = wallets[accepted % wallets.length]; round++; + let template: MiningTemplate; + try { template = await api(node, '/api/mining/template', { owner: w.address, publicKey: w.publicKey }); fails = 0; } + catch (e) { fails++; const b = Math.min(60 * 2 ** (fails - 1), 600); console.error(`template ${node}: ${(e as Error).message} (fail ${fails}, backoff ${b}s)`); await sleep(b); continue; } + const h = template.header; if (h.chainId !== CHAIN_ID) throw new Error('wrong chain'); + const started = Date.now(); let attempts = 0; let found: { hash: string; nonce: number } | null = null; + let base = randomInt(0, 2 ** 40); const BATCH = 64; + while (!found) { + const results = await Promise.all(engines.map((e, i) => { + const hdr = { ...h, nonce: base + i * BATCH }; + return e.mine(h.key, miningBlob(hdr), h.target, 32, BATCH); + })); + attempts += BATCH * workers; base += BATCH * workers; + found = results.find(Boolean) ?? null; + if (!found && Date.now() / 1000 - h.createdAt > 600) break; // stale template + } + if (!found) continue; + h.nonce = found.nonce; + const hash = sha256(miningBlob(h)); + const signature = Buffer.from(await subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, w.privateKey, + new TextEncoder().encode(blockClaimMessage(hash)))).toString('base64'); + const block: TestnetBlock = { ...h, hash, tokenId: mintId(h), powHash: found.hash, signature, transactions: template.transactions }; + blockSchema.parse(block); + try { + const res = await api(node, '/api/blocks', block); + if (!res.accepted) { console.error(`not accepted: ${JSON.stringify(res).slice(0, 200)}`); continue; } + accepted++; + const secs = (Date.now() - started) / 1000; + const line = `#${h.height} ${w.name} "${h.article.title}" token=${block.tokenId} via=${node} ${attempts}H ${(attempts / secs).toFixed(0)}H/s`; + console.log(`[${accepted}/${blocks}] ${line}`); + appendFileSync(`${STATE}/mined.log`, `${new Date().toISOString()} ${line}\n`); + } catch (e) { console.error(`submit: ${(e as Error).message}`); } + } +} finally { close(); } +console.log('done'); diff --git a/deploy/night/testnet.sh b/deploy/night/testnet.sh new file mode 100755 index 0000000..e392f4e --- /dev/null +++ b/deploy/night/testnet.sh @@ -0,0 +1,32 @@ +#!/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 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 ;; + 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,9p "$0"; exit 1 ;; +esac