diff --git a/docs/night2/TESTNET.md b/docs/night2/TESTNET.md new file mode 100644 index 0000000..c7d312d --- /dev/null +++ b/docs/night2/TESTNET.md @@ -0,0 +1,57 @@ +# Night 2 private testnet: how to test (testnet-ops, #63) + +**Testnet only, LAN only, on poweredge-r350.** It is never mainnet and never the live `wikideck` namespace. + +## Nodes +| node | URL | game modes (env from Night 1 #22) | +|---|---|---| +| node-a | http://192.168.1.32:3101 | classic, trending | +| node-b | http://192.168.1.32:3102 | draft | +| node-c | http://192.168.1.32:3103 | classic, draft, trending | + +- **Autodeploy:** the nodes follow `night/2026-11-17`. A loop on r350 (`deploy/night/autodeploy.sh`, in a dedicated detached worktree) polls the branch every 3 min. + - On a new head it rebuilds the image and restarts node-a, then b, then c, each after the previous one passes `/healthz`. + - While any match or lobby is live it defers, for up to 15 min. + - Lobbies are in memory, so a deploy that does go through ends games in progress. After night-lead merges, wait about 5 min, or check that all three nodes report the new head, before you start an end-to-end run. +- Ledgers persist in the Docker volumes across deploys. + +## Test wallets +Four disposable wallets: **alice**, **bob**, **carol** and **dave**. + +| wallet | address | cards (confirmed) | +|---|---|---| +| alice | `ff9273368c225698d8ae34430800cfe30b10a1c3d8c21091d42960493db556df` | see #63 | +| bob | `80f17f10ca782efd16783d10d71ce43b2993aadccda36dbd0364dee1921cbc22` | see #63 | +| carol | `f2d908633c4313bd77e86650c13d11ac3e7e656223ebe881cc34ffba3f3e0524` | leftovers | +| dave | `02bb97f202e1616ab97b45e8938e384dcbf622a0b44a7f4c1589f44236d3ccef` | leftovers | + +- alice and bob were consolidated to 40 cards each with on-chain NFT transfers from carol and dave, so **production `deckSize` 40 works on the testnet** and no lower-deckSize test mode is needed. +- **The encrypted browser backups are in a comment on #63**, not in git. + - Backup password: `wikideck-night-testnet`. It is **public and testnet-only**: never reuse it, and never put a real secret next to it. +- Re-check counts at any time: `GET http://192.168.1.32:3101/api/decks/
?offset=0&limit=24` (pages of 24, follow `next`). + +### Load a wallet in the browser +Use a separate browser profile, or a private window, per player, because a browser holds one wallet per origin. +1. Open a node URL, then **Open wallet**. +2. **Recover from a local backup**: choose the backup JSON file (save it from the #63 comment as `alice.backup.json`), and enter the backup password above. +3. Set a new local encryption password (at least 12 characters, anything testnet-only), then confirm it. +4. The wallet dialog should show the address above and its confirmed card count. + +### eom.dev login (Dex) +- There is no new Dex config tonight. The existing `wikideck` client already allows the 3101–3103 callbacks. +- Log in with **Log in with eom.dev**, then unlock the wallet and press **Link wallet …** to link it to the account. +- PvP seats are keyed to the account, and ownership is checked on the server against the account's linked wallets. So for a two-player run you need **two eom.dev accounts**, one with alice linked and one with bob. Which accounts to use is night-lead's call (see #53/#63). Never use a real person's account without their OK. + +## Mining on the testnet +- Mining isn't needed for tests. Only run a miner if night-lead asks for more blocks, or to confirm transfers. +- `deploy/night/testnet.sh mine N [workers]` runs the native multi-worker miner in a container named `wikideck-night-miner` (at most 4 workers on r350). + - Every template costs Wikipedia requests on the node, so it is paced by `MIN_GAP`. + - **testnet-ops stops any miner by 01:55** (`docker rm -f wikideck-night-miner`). The nodes stay up. +- **Difficulty (for mobile mining, #59):** at block ~90, an accepted block took between about 2 K and 130 K hashes. That is up to ~8 min at the 4-worker r350 rate of ~250 H/s. One light-mode thread on the emulator is far slower, so expect a long wait per block, with high variance. + +## Card identity pre-bake (engineer WS E) +- `server/game/identity.ts` is a persistent per-token store (`node:sqlite`). It freezes each card's off-chain identity at first sight and never rewrites it. The identity covers P31 classes, categories, country, dates, coordinates and an outlinks hash. +- `scripts/night2/prebake-identity.ts --node --store [--cards ...]` reads the canonical chain from one node and freezes what the cached card files know. + - It **never calls Wikimedia itself**. + - Live facts arrive only through the wikimedia team's client on its `background` lane (#62), once that client has merged. + - Cards that are still unknown stay unfrozen and are retried on a later run. diff --git a/scripts/night2/prebake-identity.ts b/scripts/night2/prebake-identity.ts new file mode 100644 index 0000000..f7ab0c8 --- /dev/null +++ b/scripts/night2/prebake-identity.ts @@ -0,0 +1,56 @@ +// testnet-ops (engineer WS E): pre-bake frozen card identities for every card on a chain. +// +// node --experimental-strip-types scripts/night2/prebake-identity.ts --node http://192.168.1.32:3101 \ +// --store ./identity.sqlite [--cards docs/game/card-sample.json] [--batch 50] +// +// - Reads the canonical chain from ONE node (GET /api/summary, then /api/canonical pages), so every +// card is built from its mint block, never from client JSON. +// - Facts come ONLY from cached card files (`--cards`, repeatable). This script never calls +// Wikipedia or Wikidata itself. Live facts must go through the wikimedia team's process-wide client +// on its `background` lane (server/wikimedia, #62); that IdentitySource plugs into `prebake()` in +// server/game/identity.ts once it has merged. Until then, unknown cards are left unfrozen and are +// picked up by a later run. +// - Idempotent and freeze-at-first-sight: already frozen tokens are skipped and never rewritten. +import { readFileSync } from 'node:fs'; +import { parseArgs } from 'node:util'; +import { IdentityStore, cachedSource, prebake, type ChainArticle } from '../../server/game/identity.ts'; + +const { values } = parseArgs({ options: { + node: { type: 'string', default: 'http://192.168.1.32:3101' }, + store: { type: 'string', default: 'identity.sqlite' }, + cards: { type: 'string', multiple: true, default: ['docs/game/card-sample.json'] }, + batch: { type: 'string', default: '50' }, +} }); +const nodeUrl = new URL(values.node!); +if (!['http:', 'https:'].includes(nodeUrl.protocol)) throw new Error('--node must be an http(s) WikiDeck node URL'); +if (/wikipedia\.org|wikidata\.org|wikimedia\.org/i.test(nodeUrl.hostname)) throw new Error('--node must be a WikiDeck node, not Wikimedia'); + +async function get(path: string): Promise { + const r = await fetch(new URL(path, nodeUrl), { signal: AbortSignal.timeout(30000), redirect: 'error' }); + if (!r.ok) throw new Error(`${path}: HTTP ${r.status}`); + return await r.json() as T; +} + +type Block = { tokenId: string; article: { pageId: number; title: string; revisionId?: number } }; +async function chainArticles(): Promise { + const { tip } = await get<{ tip: string }>('/api/summary'); + const out: ChainArticle[] = []; + for (let offset: number | null = 0; offset !== null;) { + const page: { blocks: Block[]; next: number | null } = await get(`/api/canonical?tip=${tip}&offset=${offset}&limit=16`); + for (const b of page.blocks) if (b.article && b.tokenId) + out.push({ tokenId: b.tokenId, pageId: b.article.pageId, title: b.article.title, revisionId: b.article.revisionId ?? null }); + offset = page.next; + } + return out; +} + +const cards = values.cards!.flatMap(f => { + const d = JSON.parse(readFileSync(f, 'utf8')); + return Array.isArray(d) ? d : d.cards ?? []; +}); +const articles = await chainArticles(); +const store = new IdentityStore(values.store!); +try { + const report = await prebake(store, cachedSource(cards), articles, { batchSize: Number(values.batch) }); + console.log(JSON.stringify({ node: nodeUrl.origin, chainCards: articles.length, cachedCards: cards.length, ...report, stored: store.count() })); +} finally { store.close(); } diff --git a/server/game/identity.ts b/server/game/identity.ts new file mode 100644 index 0000000..3d099fb --- /dev/null +++ b/server/game/identity.ts @@ -0,0 +1,168 @@ +// Card identity (engineer WS E, data review §3): the off-chain, mutable facts about a card's article +// (P31 classes, a few Wikidata claims, categories, outlinks) frozen ONCE per tokenId, the first time +// the card is seen, so that a later Wikipedia/Wikidata edit can never retype a card someone owns. +// +// - On-chain data (article, statsSnapshot) stays the source of truth for everything it covers. +// - Identities are never fetched at match time. They are pre-baked in the background +// (scripts/night2/prebake-identity.ts), from cached data or through an injected IdentitySource that +// must use the process-wide Wikimedia client on its `background` lane. This file performs no I/O +// besides the sqlite store and never calls fetch itself. +// - First write wins: `freeze` never overwrites an existing record (data §3 "freeze at first sight"). +import { DatabaseSync } from 'node:sqlite'; +import { createHash } from 'node:crypto'; + +export const IDENTITY_VERSION = 1; + +export type WikidataClass = { id: string; label?: string }; +export type IdentityDates = { birth?: string; death?: string; inception?: string; pointInTime?: string; start?: string }; +export type CardIdentity = { + version: typeof IDENTITY_VERSION; + tokenId: string; + pageId: number; + title: string; + /** article.revisionId from the mint block. */ + mintRevisionId: number | null; + /** The revision the off-chain facts were read from (may differ from the mint revision). */ + fetchedRevisionId: number | null; + wikidataId: string | null; + wikidataRev: number | null; + instanceOf: WikidataClass[]; + categories: string[]; + /** P17 (country) or P27 (citizenship) Q-ids. */ + country: string[]; + dates: IdentityDates; + /** P625 or prop=coordinates, [lat, lon]. */ + coords: [number, number] | null; + /** Main-namespace outlink titles, sorted, deduplicated. Empty when unknown. */ + outlinks: string[]; + /** sha256 of the sorted outlinks, so match records can log a short fingerprint. */ + outlinksHash: string; + /** Where the facts came from: an offline cache (e.g. card-sample.json) or a live background fetch. */ + source: 'cache' | 'wikimedia'; + frozenAt: string; +}; + +/** What a source knows about an article; any field may be missing. */ +export type IdentityFacts = Partial>; +/** The minimum the chain tells us about a card (built from its mint block, never from client JSON). */ +export type ChainArticle = { tokenId: string; pageId: number; title: string; revisionId?: number | null }; + +/** + * A source of identity facts. A live implementation must route every request through the shared + * Wikimedia client on the background lane (never the mint or interactive lanes) and batch where it + * can. Returning `undefined` for a page means "unknown for now": nothing is frozen and it is retried + * on a later run. + */ +export interface IdentitySource { + readonly kind: CardIdentity['source']; + lookup(articles: ChainArticle[]): Promise>; +} + +export function outlinksHash(outlinks: readonly string[]): string { + return createHash('sha256').update(JSON.stringify(outlinks)).digest('hex'); +} + +const uniqSorted = (xs: readonly string[] | undefined) => [...new Set((xs ?? []).filter(x => typeof x === 'string'))].sort(); + +/** Pure: build a frozen identity from chain data plus whatever facts a source returned. */ +export function buildIdentity(article: ChainArticle, facts: IdentityFacts, source: CardIdentity['source'], now: Date): CardIdentity { + const outlinks = uniqSorted(facts.outlinks); + const coords = facts.coords && facts.coords.length === 2 && facts.coords.every(Number.isFinite) ? [facts.coords[0], facts.coords[1]] as [number, number] : null; + return { + version: IDENTITY_VERSION, + tokenId: article.tokenId, + pageId: article.pageId, + title: article.title, + mintRevisionId: article.revisionId ?? null, + fetchedRevisionId: facts.fetchedRevisionId ?? null, + wikidataId: facts.wikidataId && /^Q\d+$/.test(facts.wikidataId) ? facts.wikidataId : null, + wikidataRev: facts.wikidataRev ?? null, + instanceOf: (facts.instanceOf ?? []).filter(c => /^Q\d+$/.test(c.id)).map(c => c.label ? { id: c.id, label: c.label } : { id: c.id }), + categories: uniqSorted(facts.categories), + country: uniqSorted(facts.country).filter(q => /^Q\d+$/.test(q)), + dates: { ...(facts.dates ?? {}) }, + coords, + outlinks, + outlinksHash: outlinksHash(outlinks), + source, + frozenAt: now.toISOString(), + }; +} + +/** Persistent per-token identity store. Records are immutable once written. */ +export class IdentityStore { + readonly #db: DatabaseSync; + constructor(file: string) { + this.#db = new DatabaseSync(file); + this.#db.exec(`CREATE TABLE IF NOT EXISTS card_identity ( + token_id TEXT PRIMARY KEY, page_id INTEGER NOT NULL, version INTEGER NOT NULL, body TEXT NOT NULL)`); + } + get(tokenId: string): CardIdentity | undefined { + const row = this.#db.prepare('SELECT body FROM card_identity WHERE token_id = ?').get(tokenId) as { body: string } | undefined; + return row ? JSON.parse(row.body) as CardIdentity : undefined; + } + has(tokenId: string): boolean { return this.get(tokenId) !== undefined; } + /** Freeze at first sight: returns the stored record, which is the earlier one if it already existed. */ + freeze(identity: CardIdentity): CardIdentity { + this.#db.prepare('INSERT OR IGNORE INTO card_identity (token_id, page_id, version, body) VALUES (?, ?, ?, ?)') + .run(identity.tokenId, identity.pageId, identity.version, JSON.stringify(identity)); + return this.get(identity.tokenId)!; + } + /** Freeze several in one transaction (one fsync per batch, not per card). */ + freezeMany(identities: CardIdentity[]): void { + this.#db.exec('BEGIN'); + try { for (const i of identities) this.freeze(i); this.#db.exec('COMMIT'); } + catch (e) { this.#db.exec('ROLLBACK'); throw e; } + } + count(): number { return (this.#db.prepare('SELECT COUNT(*) AS n FROM card_identity').get() as { n: number }).n; } + all(): CardIdentity[] { + return (this.#db.prepare('SELECT body FROM card_identity ORDER BY token_id').all() as { body: string }[]).map(r => JSON.parse(r.body)); + } + close() { this.#db.close(); } +} + +export type PrebakeReport = { seen: number; alreadyFrozen: number; frozen: number; unknown: number }; + +/** + * Freeze identities for every card not yet in the store, asking the source in batches. The caller + * controls pacing (batch size, and the source's client controls request rate). + */ +export async function prebake(store: IdentityStore, source: IdentitySource, articles: ChainArticle[], + opts: { batchSize?: number; now?: () => Date } = {}): Promise { + const batchSize = Math.max(1, Math.min(50, opts.batchSize ?? 50)), now = opts.now ?? (() => new Date()); + const seenTokens = new Set(), todo: ChainArticle[] = []; + let alreadyFrozen = 0; + for (const a of articles) { + if (seenTokens.has(a.tokenId)) continue; + seenTokens.add(a.tokenId); + if (store.has(a.tokenId)) alreadyFrozen++; else todo.push(a); + } + let frozen = 0, unknown = 0; + for (let i = 0; i < todo.length; i += batchSize) { + const batch = todo.slice(i, i + batchSize), facts = await source.lookup(batch), ready = []; + for (const a of batch) { + const f = facts.get(a.pageId); + if (!f) { unknown++; continue; } + ready.push(buildIdentity(a, f, source.kind, now())); + } + store.freezeMany(ready); frozen += ready.length; + } + return { seen: seenTokens.size, alreadyFrozen, frozen, unknown }; +} + +/** Offline source: facts from an already-cached card file such as docs/game/card-sample.json. */ +export function cachedSource(cards: readonly { pageId: number; wikidataId?: string | null; revisionId?: number | null; + instanceOf?: WikidataClass[]; categories?: string[] }[]): IdentitySource { + const byPage = new Map(cards.map(c => [c.pageId, c])); + return { + kind: 'cache', + async lookup(articles) { + return new Map(articles.map(a => { + const c = byPage.get(a.pageId); + return [a.pageId, c ? { fetchedRevisionId: c.revisionId ?? null, wikidataId: c.wikidataId ?? null, + instanceOf: c.instanceOf ?? [], categories: c.categories ?? [] } : undefined]; + })); + }, + }; +} diff --git a/tests/testnet-ops-identity.test.ts b/tests/testnet-ops-identity.test.ts new file mode 100644 index 0000000..dd7ae8f --- /dev/null +++ b/tests/testnet-ops-identity.test.ts @@ -0,0 +1,79 @@ +// testnet-ops: card identity freeze-at-first-sight and pre-bake. Zero network calls. +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { mkdtempSync, readFileSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { IdentityStore, buildIdentity, cachedSource, outlinksHash, prebake, + type ChainArticle, type IdentitySource } from '../server/game/identity.ts'; + +const sample = JSON.parse(readFileSync(new URL('../docs/game/card-sample.json', import.meta.url), 'utf8')).cards as any[]; +const now = () => new Date('2026-11-17T22:00:00Z'); +function tmpStore() { + const dir = mkdtempSync(join(tmpdir(), 'identity-')); + return { store: new IdentityStore(join(dir, 'identity.sqlite')), done: () => rmSync(dir, { recursive: true, force: true }) }; +} +const art = (c: any, i: number): ChainArticle => ({ tokenId: i.toString(16).padStart(64, '0'), pageId: c.pageId, title: c.title, revisionId: c.revisionId }); + +test('no fetch: pre-bake from the cached sample freezes every sample card', async () => { + const realFetch = globalThis.fetch; + globalThis.fetch = (() => { throw new Error('network call in test'); }) as typeof fetch; + const { store, done } = tmpStore(); + try { + const articles = sample.map(art); + const r = await prebake(store, cachedSource(sample), articles, { now }); + assert.deepEqual(r, { seen: sample.length, alreadyFrozen: 0, frozen: sample.length, unknown: 0 }); + const salah = store.get(articles[sample.findIndex(c => c.title === 'Salahuddin of Aceh')].tokenId)!; + assert.equal(salah.wikidataId, 'Q4264920'); + assert.deepEqual(salah.instanceOf, [{ id: 'Q5', label: 'human' }]); + assert.equal(salah.source, 'cache'); + assert.ok(salah.categories.includes('Sultans of Aceh')); + } finally { globalThis.fetch = realFetch; store.close(); done(); } +}); + +test('freeze at first sight: a later, different profile never overwrites the stored one', async () => { + const { store, done } = tmpStore(); + try { + const a = art(sample[0], 1); + const first = store.freeze(buildIdentity(a, { instanceOf: [{ id: 'Q5' }] }, 'cache', now())); + const second = store.freeze(buildIdentity(a, { instanceOf: [{ id: 'Q515' }] }, 'wikimedia', new Date())); + assert.deepEqual(second, first); + assert.equal(store.get(a.tokenId)!.instanceOf[0].id, 'Q5'); + const again = await prebake(store, cachedSource(sample), [a, a], { now }); + assert.deepEqual(again, { seen: 1, alreadyFrozen: 1, frozen: 0, unknown: 0 }); + } finally { store.close(); done(); } +}); + +test('unknown pages are not frozen, so a later run can retry them', async () => { + const { store, done } = tmpStore(); + try { + const stranger: ChainArticle = { tokenId: 'f'.repeat(64), pageId: 999999999, title: 'Not in sample', revisionId: 1 }; + const r = await prebake(store, cachedSource(sample), [stranger], { now }); + assert.deepEqual(r, { seen: 1, alreadyFrozen: 0, frozen: 0, unknown: 1 }); + assert.equal(store.has(stranger.tokenId), false); + } finally { store.close(); done(); } +}); + +test('sources are asked in bounded batches and identities persist across reopen', async () => { + const dir = mkdtempSync(join(tmpdir(), 'identity-')); + try { + const calls: number[] = []; + const source: IdentitySource = { kind: 'wikimedia', async lookup(batch) { + calls.push(batch.length); + return new Map(batch.map(a => [a.pageId, { outlinks: ['B', 'A', 'A'], coords: [1, 2] as [number, number], country: ['Q30', 'bad'] }])); + } }; + let store = new IdentityStore(join(dir, 'id.sqlite')); + await prebake(store, source, sample.slice(0, 12).map(art), { batchSize: 5, now }); + assert.deepEqual(calls, [5, 5, 2]); + store.close(); + store = new IdentityStore(join(dir, 'id.sqlite')); + const one = store.get(art(sample[3], 3).tokenId)!; + assert.deepEqual(one.outlinks, ['A', 'B']); + assert.equal(one.outlinksHash, outlinksHash(['A', 'B'])); + assert.deepEqual(one.country, ['Q30']); + assert.deepEqual(one.coords, [1, 2]); + assert.equal(one.mintRevisionId, sample[3].revisionId); + assert.equal(store.count(), 12); + store.close(); + } finally { rmSync(dir, { recursive: true, force: true }); } +});