From 52df0f01201af784d4f333c92d02205173afe138 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 00:21:08 -0500 Subject: [PATCH 1/5] host-pvp: per-seat time bank (bankSeconds), clock in the seat view; tested on engine-wip --- server/game/duel-binding.ts | 2 ++ server/game/engine-port.ts | 2 ++ server/game/host.ts | 23 +++++++++++++++++++---- tests/host-pvp-host.test.ts | 25 +++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/server/game/duel-binding.ts b/server/game/duel-binding.ts index 07c4348..13993d4 100644 --- a/server/game/duel-binding.ts +++ b/server/game/duel-binding.ts @@ -37,6 +37,8 @@ export function duelBinding(port: AnyPort, sources: DuelCardSources = {}): AnyPo ...port, fromChain: (_rules, card) => toDuelCard(card, sources), copyLimit: rules => (rules as { maxCopiesPerPage?: number }).maxCopiesPerPage ?? 4, + // BRIEF "Clock": 60 s per turn plus a 5-minute bank, both ruleset data. + bankSeconds: rules => (rules as { bankSeconds?: number }).bankSeconds ?? 0, }; } diff --git a/server/game/engine-port.ts b/server/game/engine-port.ts index 8d0cd53..53c7a06 100644 --- a/server/game/engine-port.ts +++ b/server/game/engine-port.ts @@ -32,6 +32,8 @@ export interface EnginePort= 0) void this.takePick(l, l.draft.order[l.draft.turn]!, idx).catch(() => { l.phase = 'expired'; }); } else if (l.phase === 'playing' && l.state !== undefined && now - (l.turnStartedAt ?? l.lastMove) > this.turnMs(l)) { const waiting = ([0, 1] as Seat[]).filter(s => engine.legalActions(l.state, s).length > 0); - if (engine.timeoutAction) { + // Past the turn timer, a lone waiting seat spends its bank before it times out. + const bank = waiting.length === 1 && l.bank ? l.bank[waiting[0]!] : 0; + if (now - (l.turnStartedAt ?? l.lastMove) <= this.turnMs(l) + bank) { /* still on the bank */ } + else if (engine.timeoutAction) { // The engine decides what a timeout means (auto-commit, AFK forfeit); it is logged and replayable. - for (const s of waiting) if (l.phase === 'playing') this.apply(l, s, engine.timeoutAction(s)); + for (const s of waiting) if (l.phase === 'playing') { if (l.bank) l.bank[s] = 0; this.apply(l, s, engine.timeoutAction(s)); } l.turnStartedAt = now; } else if (waiting.length === 1) this.finish(l, waiting[0] === 0 ? 1 : 0, 'timeout'); else this.finish(l, null, 'abandoned'); @@ -437,6 +443,8 @@ export class ModeHost { l.state = engine.createMatch(l.rules, mapped, l.seed); l.phase = 'playing'; l.startedAt = l.lastMove = l.turnStartedAt = this.now(); + const bankSeconds = engine.bankSeconds?.(l.rules); + if (bankSeconds !== undefined && bankSeconds > 0) l.bank = [bankSeconds * 1000, bankSeconds * 1000]; l.turn = engine.turnOf?.(l.state); delete l.draft; this.bump(l); @@ -471,7 +479,14 @@ export class ModeHost { l.lastMove = this.now(); this.bump(l); const turn = engine.turnOf?.(l.state); - if (turn !== l.turn) { l.turn = turn; l.turnStartedAt = l.lastMove; } + if (turn !== l.turn) { + // The seat that ended the turn pays any time over the turn timer from its bank. + if (l.bank && l.turnStartedAt !== undefined) { + const over = l.lastMove - l.turnStartedAt - this.turnMs(l); + if (over > 0) l.bank[seat] = Math.max(0, l.bank[seat] - over); + } + l.turn = turn; l.turnStartedAt = l.lastMove; + } if (engine.isOver(l.state)) { const r = engine.result(l.state); this.finish(l, r.winner, reason ?? r.reason ?? 'complete'); } else if (l.actions.length >= 2000) this.finish(l, null, 'action-limit'); } diff --git a/tests/host-pvp-host.test.ts b/tests/host-pvp-host.test.ts index 795648a..1d5b957 100644 --- a/tests/host-pvp-host.test.ts +++ b/tests/host-pvp-host.test.ts @@ -342,3 +342,28 @@ test('host-pvp: seeded shuffle is deterministic and a permutation', () => { assert.notDeepEqual(seededShuffle(xs, 's'), seededShuffle(xs, 't')); assert.deepEqual([...seededShuffle(xs, 's')].sort((a, b) => a - b), xs); }); + +test('host-pvp: a slow seat spends its time bank before it times out', async () => { + let now = 1_000_000; + // Stub turns are simultaneous; a 10 s turn plus a 30 s bank, timeouts from the engine. + const timed: EnginePort = { ...duelStub, turnSeconds: () => 10, bankSeconds: () => 30, turnOf: (s: any) => s.turn, + timeoutAction: () => ({ type: 'play', card: 0 }) }; + const own = fakeOwnership(); + const host = new ModeHost({ identity, modes: [duelMode()], engines: { duel: timed }, ownership: own.port, + store: new MatchStore(null), now: () => now }); + const a = await host.createLobby('duel-classic', { idToken: 'id-alice', deck: deckOf('alice') }); + const b = await host.join(a.lobbyId, { idToken: 'id-bob', deck: deckOf('bob') }); + await host.act(a.lobbyId, a.token, (host.view(a.lobbyId, a.token) as any).legalActions[0]); // alice moves, bob stalls + const clock = (host.view(a.lobbyId, b.token) as any).clock; + assert.deepEqual(clock.bankMs, [30_000, 30_000]); + assert.equal(clock.turnMs, 15_000, 'turn timer includes 5 s network grace'); + now += 15_000 + 20_000; // past the turn timer, inside bob's bank + host.sweep(); + assert.equal((host.view(a.lobbyId, b.token) as any).state.turn, 0, 'bob is still on his bank'); + now += 11_000; // bank exhausted + host.sweep(); + const v = host.view(a.lobbyId, b.token) as any; + assert.equal(v.state.turn, 1, 'timeout applied'); + assert.equal(v.clock.bankMs[1], 0); + assert.equal(v.clock.bankMs[0], 30_000); +}); From 56f1446d8f4f71fdd3c631801c543dbee224aecf Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 00:33:04 -0500 Subject: [PATCH 2/5] host-pvp: mount GET /collection in gameRouter with the toDuelCard hook; one identity store (ownership's proposal) --- server/game/index.ts | 18 +- server/testnet/http.ts | 383 ++++++++++++++++----------------- tests/host-pvp-binding.test.ts | 21 ++ 3 files changed, 225 insertions(+), 197 deletions(-) diff --git a/server/game/index.ts b/server/game/index.ts index 63749d7..7d667df 100644 --- a/server/game/index.ts +++ b/server/game/index.ts @@ -4,7 +4,7 @@ */ import { join } from 'node:path'; import { existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs'; -import type express from 'express'; +import express from 'express'; import { loadModes, hostIdentity, gamePeers } from './config.ts'; import { ModeDiscovery } from './discovery.ts'; import { ModeHost } from './host.ts'; @@ -13,7 +13,8 @@ import { MatchStore } from './store.ts'; import type { EnginePort, OwnershipPort } from './engine-port.ts'; import type { EngineId } from '../../shared/game/modes.ts'; import { TrendCache } from './trends.ts'; -import { loadDuelEngine } from './duel-binding.ts'; +import { loadDuelEngine, toDuelCard } from './duel-binding.ts'; +import { collectionRouter } from './collection.ts'; import { IdentityStore } from './identity.ts'; import { collection, resolveDeck, verifyAccount, type OwnershipDeps } from './ownership.ts'; @@ -32,9 +33,10 @@ export async function gameRouter(options: { dataDir: string | null; enrichment?: const env = options.env ?? process.env; const engine = options.engine ?? (await import('./engine-binding.ts')).engineBinding; const engines: Partial>> = { editwar: engine, ...options.engines }; + // Frozen card identities (testnet-ops #68) give duel cards their P31 typing. Opened once, here, + // and shared by the duel engine binding and the collection API's card faces. + const identity = options.dataDir ? new IdentityStore(join(options.dataDir, 'card-identity.sqlite')) : undefined; if (!engines.duel) { - // Frozen card identities (testnet-ops #68) give duel cards their P31 typing; opened read-mostly. - const identity = options.dataDir ? new IdentityStore(join(options.dataDir, 'card-identity.sqlite')) : undefined; const duel = await loadDuelEngine({ identity }); if (duel) engines.duel = duel; } @@ -67,7 +69,13 @@ export async function gameRouter(options: { dataDir: string | null; enrichment?: if (options.dataDir) persistLive(host, join(options.dataDir, 'game'), options.handleSignals !== false); const timer = setInterval(() => host.sweep(), 5_000); timer.unref(); - return createGameRouter(host, discovery); + const game = createGameRouter(host, discovery); + if (!options.ownership) return game; + // GET /api/game/collection (#57), with each card's DuelCard face from the same toDuelCard the host uses. + const router = express.Router(); + router.use('/collection', collectionRouter({ ...options.ownership, duelCard: c => toDuelCard(c, { identity }) })); + router.use(game); + return router; } /** The async port the host calls, over ownership.ts's pure functions (#57). */ diff --git a/server/testnet/http.ts b/server/testnet/http.ts index 9053218..c6dabed 100644 --- a/server/testnet/http.ts +++ b/server/testnet/http.ts @@ -1,192 +1,191 @@ -import express from 'express'; -import helmet from 'helmet'; -import rateLimit from 'express-rate-limit'; -import { resolve } from 'node:path'; -import { fileURLToPath } from 'node:url'; -import { mkdir, open, unlink } from 'node:fs/promises'; -import { z } from 'zod'; -import { NETWORK } from '../../shared/testnet.ts'; -import { CHAIN_ID, blockSchema, tradeSchema, transferSchema, transactionSchema } from './consensus.ts'; -import { RandomX } from './randomx.ts'; -import { Ledger } from './ledger.ts'; -import { PeerNetwork } from './peers.ts'; -import { wikipediaEnrichment } from '../enrichment.ts'; -import { snapshotStats } from '../../shared/enrichment.ts'; -import { wikipediaArticles } from '../wiki.ts'; -import { IS_MAINNET, PROFILE } from '../../shared/network.ts'; -import { assertLaunchAllowed, launchManifest } from '../launch.ts'; -import { resourceConfig, RpcBudget, RpcError } from '../resources.ts'; -import { trustedProxies, operationsPort, startOperations } from '../operations.ts'; -import { accountVerifier, createAccountRouter } from '../account/router.ts'; -import { AccountStore } from '../account/store.ts'; -import { DexKeys, oidcConfig } from '../account/oidc.ts'; -import { collectionRouter } from '../game/collection.ts'; - -const root = fileURLToPath(new URL('../../', import.meta.url)); -assertLaunchAllowed(); -const limits = resourceConfig(); -const proxyTrust=trustedProxies(); -const opsPort=operationsPort(); -const apiBudget = new RpcBudget(limits); -const peerBudget = new RpcBudget({ ...limits, requestsPerSecond: 40, writesPerSecond: 10, active: 16 }); -const dataDir = process.env.WIKIDECK_DATA_DIR ?? resolve(root,'data'); -await mkdir(dataDir,{recursive:true}); -// Never permit two processes to rewrite the same ledger. A stale lock requires operator review. -const lockPath = resolve(dataDir,`${PROFILE}-node.lock`); -const lock = await open(lockPath,'wx',0o600).catch(()=>{throw new Error('Data directory locked. Stop its other node; remove stale testnet-node.lock only after verifying no node is running.');}); -await lock.writeFile(String(process.pid)); -let engine: RandomX; -try { engine = new RandomX(); } -catch(e) { await lock.close();await unlink(lockPath);throw e; } -let ledger: Ledger; -try { ledger = await Ledger.open(dataDir,engine); } -catch(e) {engine.close();await lock.close();await unlink(lockPath);throw e;} -let peers: PeerNetwork; -try { peers = new PeerNetwork({ ledger, urls:(process.env.WIKIDECK_PEERS ?? '').split(',').map(s=>s.trim()).filter(Boolean), - allowPrivate:process.env.WIKIDECK_ALLOW_PRIVATE_PEERS === '1' }); } -catch(e) {ledger.close();engine.close();await lock.close();await unlink(lockPath);throw e;} -const app = express(); -app.disable('x-powered-by'); -if(proxyTrust.length)app.set('trust proxy',proxyTrust); -app.use(helmet({contentSecurityPolicy:{directives:{defaultSrc:["'self'"],scriptSrc:["'self'"], - imgSrc:["'self'",'data:','https://upload.wikimedia.org','https://thumb.wikimedia.org'],styleSrc:["'self'","'unsafe-inline'"], - connectSrc:["'self'"],upgradeInsecureRequests:null}},crossOriginEmbedderPolicy:false, - strictTransportSecurity:process.env.NODE_ENV==='production'})); -app.get('/healthz',(_req,res)=>{res.setHeader('Cache-Control','no-store');res.json({status:'ok',network:NETWORK});}); -// Admission comes before body parsing and signature/schema work. Disable compressed request inflation. -app.use('/api',apiBudget.middleware()); -app.use('/p2p',peerBudget.middleware()); -app.use(['/api','/p2p'],rateLimit({windowMs:60000,limit:300,standardHeaders:'draft-8',legacyHeaders:false})); -app.use(['/api','/p2p'],(req,res,next)=>{ - res.setHeader('Cache-Control','no-store'); - if(req.method==='POST') { - if(!req.is('application/json')){res.status(415).json({error:'JSON required'});return;} - const origin=req.get('origin'); - const allowed=new Set(['http://127.0.0.1:5173','http://localhost:5173','http://127.0.0.1:3001','http://localhost:3001',process.env.PUBLIC_ORIGIN]); - if(origin&&!allowed.has(origin)){res.status(403).json({error:'Origin not allowed'});return;} - } - next(); -}); -app.use(['/api','/p2p'],express.json({limit:'68kb',strict:true,inflate:false})); -app.get('/api/state',(_req,res)=>res.json(ledger.legacySnapshot(limits.legacyBlocks))); -app.get('/api/summary',(_req,res)=>res.json(ledger.summary())); -app.get('/api/canonical',(req,res)=>{ - const q=z.object({tip:z.string().regex(/^[a-f0-9]{64}$/),offset:z.string().regex(/^\d{1,12}$/).optional(), - limit:z.string().regex(/^\d{1,2}$/).optional()}).strict().parse(req.query); - res.json(ledger.canonicalPage(q.tip,Number(q.offset??0),Number(q.limit??16))); -}); -app.get('/api/network',(_req,res)=>res.json(launchManifest())); -app.get('/api/chain',(_req,res)=>res.json(ledger.legacySnapshot(limits.legacyBlocks))); -app.get('/api/peers',(_req,res)=>res.json({peers:peers.status()})); -app.get('/api/decks/:address', rateLimit({windowMs:60000,limit:60,standardHeaders:'draft-8',legacyHeaders:false}), (req,res)=>{ - if(typeof req.params.address!=='string'||!/^[a-f0-9]{64}$/.test(req.params.address)){ - res.status(400).json({error:'Invalid wallet address'});return; - } - const offset=Number(req.query.offset??0),limit=Number(req.query.limit??24); - if(!Number.isSafeInteger(offset)||offset<0||!Number.isSafeInteger(limit)||limit<1||limit>24){ - res.status(400).json({error:'Invalid deck page'});return; - } - res.setHeader('Cache-Control','no-store'); - if(req.query.tip!==undefined && req.query.tip!==ledger.summary().tip){ - res.status(409).json({error:'Canonical tip changed; restart deck pagination.'});return; - } - res.json(ledger.deck(req.params.address,offset,limit)); -}); -// Optional live data is intentionally not used by genesis, block validation or NFT ownership. -app.get('/api/wikipedia/:pageId', rateLimit({windowMs:60000,limit:60,standardHeaders:'draft-8',legacyHeaders:false}), async(req,res)=>{ - if(typeof req.params.pageId!=='string'||!/^[1-9]\d{0,9}$/.test(req.params.pageId)){res.status(400).json({error:'Invalid Wikipedia page ID'});return;} - const pageId=Number(req.params.pageId); - try {res.json(await wikipediaEnrichment.get(pageId));} - catch {res.status(503).json({error:'Wikipedia enrichment is temporarily unavailable. NFT data is unaffected.'});} -}); -app.get('/p2p/status',(_req,res)=>res.json(ledger.peerStatus())); -app.get('/p2p/blocks',(req,res)=>{ - const offset=Number(req.query.offset??0); - if(!Number.isSafeInteger(offset)||offset<0){res.status(400).json({error:'Invalid offset'});return;} - const blocks=ledger.inventory(offset,32); - res.json({blocks,next:offset+blocks.lengthres.json({transactions:ledger.pendingTransactions()})); -app.get('/api/nfts/:id', (req,res)=>{ - if(typeof req.params.id!=='string'||!/^[a-f0-9]{64}$/.test(req.params.id)){ - res.status(400).json({error:'Invalid NFT identifier'});return; - } - const nft=ledger.nft(req.params.id); - if(!nft){res.status(404).json({error:'NFT not found on the canonical chain'});return;} - const block=nft.block; - res.json({network:NETWORK,chainId:CHAIN_ID,tokenId:block.tokenId,owner:nft.owner,sequence:nft.sequence,blockHash:block.hash, - confirmations:nft.confirmations,article:block.article,statsSnapshot:block.statsSnapshot, - stateRoot:ledger.block(ledger.summary().tip)?.stateRoot}); -}); -const templateLimit=rateLimit({windowMs:60000,limit:20,standardHeaders:'draft-8',legacyHeaders:false}); -app.post('/api/mining/template', templateLimit, async (req,res)=>{ - const body=z.object({owner:z.string().regex(/^[a-f0-9]{64}$/),publicKey:z.string().max(256)}).strict().parse(req.body); - try { - const article=await wikipediaArticles.random(); - const metrics=await wikipediaEnrichment.getFresh(article.pageId); - res.json(ledger.template(body.owner,body.publicKey,article,snapshotStats(metrics))); - } catch { - res.status(503).json({error:'Could not prepare a Wikipedia article and current statistics. Retry shortly.'}); - } -}); -const workLimit=rateLimit({windowMs:60000,limit:30,standardHeaders:'draft-8',legacyHeaders:false}); -app.post(['/api/blocks','/p2p/block'],workLimit,async(req,res)=>{ - const block=blockSchema.parse(req.body); - const result=await ledger.submit(block); - res.status(result.accepted?201:200).json(result); - if(result.accepted)void peers.broadcastBlock(block).catch(()=>{}); -}); -app.post('/api/transfer',workLimit,async(req,res)=>{ - const tx=transferSchema.parse(req.body); - const id=await ledger.submitTransfer(tx); - res.status(202).json({id,status:'pending',message:'Ownership changes only when this transaction is mined.'}); - void peers.broadcastTransfer(tx).catch(()=>{}); -}); -app.post('/api/trades',workLimit,async(req,res)=>{ - const trade=tradeSchema.parse(req.body); - const id=await ledger.submitTransaction(trade); - res.status(202).json({id,status:'pending',message:'The complete multi-card trade is pending; ownership changes atomically when it is mined.'}); - void peers.broadcastTransaction(trade).catch(()=>{}); -}); -app.post('/p2p/transaction',workLimit,async(req,res)=>{ - const transaction=transactionSchema.parse(req.body); - const id=await ledger.submitTransaction(transaction); - res.status(202).json({id,status:'pending'}); -}); -// One account store and one Dex key cache, shared by the account and game routes (#57). -const oidc=oidcConfig(), dexKeys=new DexKeys(oidc), accounts=new AccountStore(resolve(dataDir,'accounts.sqlite')); -const ownership={ledger,accounts,verifyIdToken:accountVerifier(oidc,dexKeys,accounts)}; -app.use('/api/account',createAccountRouter({config:oidc,keys:dexKeys,store:accounts})); -app.use('/api/game/collection',collectionRouter(ownership)); -app.use('/api/game',await (await import('../game/index.ts')).gameRouter({dataDir,enrichment:wikipediaEnrichment,ownership})); -app.use(['/api','/p2p'],(_req,res)=>{res.status(404).json({error:'Unknown testnet endpoint'});}); -app.use(express.static(resolve(root,'dist'))); -app.get('/{*path}',(_req,res)=>res.sendFile(resolve(root,'dist/index.html'))); -app.use((err:Error & {status?:number},_req:express.Request,res:express.Response,_next:express.NextFunction)=>{ - const input=err instanceof z.ZodError||err instanceof SyntaxError; - const message=input?'Invalid request encoding':err.message; - // No keys/passwords are accepted by any node endpoint. - if(err instanceof RpcError&&err.status===503)res.setHeader('Retry-After','1'); - res.status(err instanceof RpcError?err.status:err.status===413?413:err.status===415?415:400).json({error:message.slice(0,200)}); -}); -const port=Number(process.env.PORT??3001),host=process.env.HOST??'127.0.0.1'; -const server=app.listen(port,host,()=>{ - const a=server.address();console.log(`Wikideck RandomX ${IS_MAINNET?'mainnet candidate':'testnet'}: http://${host}:${typeof a==='object'&&a?a.port:port} · chain ${CHAIN_ID} · ${ledger.snapshot().blocks.length} blocks`); - peers.start(); -}); -const operations=startOperations(opsPort,()=>({network:NETWORK, - ledger:ledger.diagnostics(),native:engine.diagnostics(), - peers:{configured:peers.status().length,connected:peers.status().filter(p=>p.connected).length}, - rpc:{api:apiBudget.status(),p2p:peerBudget.status()}}),resolve(dataDir,PROFILE)); -server.maxConnections=256; -server.headersTimeout=10000; -server.requestTimeout=15000; -server.keepAliveTimeout=5000; -server.setTimeout(30000,socket=>socket.destroy()); -let stopping=false; -for(const signal of ['SIGTERM','SIGINT']as const)process.once(signal,()=>{ - if(stopping)return;stopping=true;peers.close();operations.close(); - const timeout=setTimeout(()=>process.exit(1),25000);timeout.unref(); - server.close(async()=>{ledger.close();engine.close();await lock.close();await unlink(lockPath).catch(()=>{});clearTimeout(timeout);process.exit(0);}); -}); -server.on('error',async()=>{stopping=true;operations.close();peers.close();ledger.close();engine.close();await lock.close();await unlink(lockPath).catch(()=>{});process.exit(1);}); +import express from 'express'; +import helmet from 'helmet'; +import rateLimit from 'express-rate-limit'; +import { resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { mkdir, open, unlink } from 'node:fs/promises'; +import { z } from 'zod'; +import { NETWORK } from '../../shared/testnet.ts'; +import { CHAIN_ID, blockSchema, tradeSchema, transferSchema, transactionSchema } from './consensus.ts'; +import { RandomX } from './randomx.ts'; +import { Ledger } from './ledger.ts'; +import { PeerNetwork } from './peers.ts'; +import { wikipediaEnrichment } from '../enrichment.ts'; +import { snapshotStats } from '../../shared/enrichment.ts'; +import { wikipediaArticles } from '../wiki.ts'; +import { IS_MAINNET, PROFILE } from '../../shared/network.ts'; +import { assertLaunchAllowed, launchManifest } from '../launch.ts'; +import { resourceConfig, RpcBudget, RpcError } from '../resources.ts'; +import { trustedProxies, operationsPort, startOperations } from '../operations.ts'; +import { accountVerifier, createAccountRouter } from '../account/router.ts'; +import { AccountStore } from '../account/store.ts'; +import { DexKeys, oidcConfig } from '../account/oidc.ts'; + +const root = fileURLToPath(new URL('../../', import.meta.url)); +assertLaunchAllowed(); +const limits = resourceConfig(); +const proxyTrust=trustedProxies(); +const opsPort=operationsPort(); +const apiBudget = new RpcBudget(limits); +const peerBudget = new RpcBudget({ ...limits, requestsPerSecond: 40, writesPerSecond: 10, active: 16 }); +const dataDir = process.env.WIKIDECK_DATA_DIR ?? resolve(root,'data'); +await mkdir(dataDir,{recursive:true}); +// Never permit two processes to rewrite the same ledger. A stale lock requires operator review. +const lockPath = resolve(dataDir,`${PROFILE}-node.lock`); +const lock = await open(lockPath,'wx',0o600).catch(()=>{throw new Error('Data directory locked. Stop its other node; remove stale testnet-node.lock only after verifying no node is running.');}); +await lock.writeFile(String(process.pid)); +let engine: RandomX; +try { engine = new RandomX(); } +catch(e) { await lock.close();await unlink(lockPath);throw e; } +let ledger: Ledger; +try { ledger = await Ledger.open(dataDir,engine); } +catch(e) {engine.close();await lock.close();await unlink(lockPath);throw e;} +let peers: PeerNetwork; +try { peers = new PeerNetwork({ ledger, urls:(process.env.WIKIDECK_PEERS ?? '').split(',').map(s=>s.trim()).filter(Boolean), + allowPrivate:process.env.WIKIDECK_ALLOW_PRIVATE_PEERS === '1' }); } +catch(e) {ledger.close();engine.close();await lock.close();await unlink(lockPath);throw e;} +const app = express(); +app.disable('x-powered-by'); +if(proxyTrust.length)app.set('trust proxy',proxyTrust); +app.use(helmet({contentSecurityPolicy:{directives:{defaultSrc:["'self'"],scriptSrc:["'self'"], + imgSrc:["'self'",'data:','https://upload.wikimedia.org','https://thumb.wikimedia.org'],styleSrc:["'self'","'unsafe-inline'"], + connectSrc:["'self'"],upgradeInsecureRequests:null}},crossOriginEmbedderPolicy:false, + strictTransportSecurity:process.env.NODE_ENV==='production'})); +app.get('/healthz',(_req,res)=>{res.setHeader('Cache-Control','no-store');res.json({status:'ok',network:NETWORK});}); +// Admission comes before body parsing and signature/schema work. Disable compressed request inflation. +app.use('/api',apiBudget.middleware()); +app.use('/p2p',peerBudget.middleware()); +app.use(['/api','/p2p'],rateLimit({windowMs:60000,limit:300,standardHeaders:'draft-8',legacyHeaders:false})); +app.use(['/api','/p2p'],(req,res,next)=>{ + res.setHeader('Cache-Control','no-store'); + if(req.method==='POST') { + if(!req.is('application/json')){res.status(415).json({error:'JSON required'});return;} + const origin=req.get('origin'); + const allowed=new Set(['http://127.0.0.1:5173','http://localhost:5173','http://127.0.0.1:3001','http://localhost:3001',process.env.PUBLIC_ORIGIN]); + if(origin&&!allowed.has(origin)){res.status(403).json({error:'Origin not allowed'});return;} + } + next(); +}); +app.use(['/api','/p2p'],express.json({limit:'68kb',strict:true,inflate:false})); +app.get('/api/state',(_req,res)=>res.json(ledger.legacySnapshot(limits.legacyBlocks))); +app.get('/api/summary',(_req,res)=>res.json(ledger.summary())); +app.get('/api/canonical',(req,res)=>{ + const q=z.object({tip:z.string().regex(/^[a-f0-9]{64}$/),offset:z.string().regex(/^\d{1,12}$/).optional(), + limit:z.string().regex(/^\d{1,2}$/).optional()}).strict().parse(req.query); + res.json(ledger.canonicalPage(q.tip,Number(q.offset??0),Number(q.limit??16))); +}); +app.get('/api/network',(_req,res)=>res.json(launchManifest())); +app.get('/api/chain',(_req,res)=>res.json(ledger.legacySnapshot(limits.legacyBlocks))); +app.get('/api/peers',(_req,res)=>res.json({peers:peers.status()})); +app.get('/api/decks/:address', rateLimit({windowMs:60000,limit:60,standardHeaders:'draft-8',legacyHeaders:false}), (req,res)=>{ + if(typeof req.params.address!=='string'||!/^[a-f0-9]{64}$/.test(req.params.address)){ + res.status(400).json({error:'Invalid wallet address'});return; + } + const offset=Number(req.query.offset??0),limit=Number(req.query.limit??24); + if(!Number.isSafeInteger(offset)||offset<0||!Number.isSafeInteger(limit)||limit<1||limit>24){ + res.status(400).json({error:'Invalid deck page'});return; + } + res.setHeader('Cache-Control','no-store'); + if(req.query.tip!==undefined && req.query.tip!==ledger.summary().tip){ + res.status(409).json({error:'Canonical tip changed; restart deck pagination.'});return; + } + res.json(ledger.deck(req.params.address,offset,limit)); +}); +// Optional live data is intentionally not used by genesis, block validation or NFT ownership. +app.get('/api/wikipedia/:pageId', rateLimit({windowMs:60000,limit:60,standardHeaders:'draft-8',legacyHeaders:false}), async(req,res)=>{ + if(typeof req.params.pageId!=='string'||!/^[1-9]\d{0,9}$/.test(req.params.pageId)){res.status(400).json({error:'Invalid Wikipedia page ID'});return;} + const pageId=Number(req.params.pageId); + try {res.json(await wikipediaEnrichment.get(pageId));} + catch {res.status(503).json({error:'Wikipedia enrichment is temporarily unavailable. NFT data is unaffected.'});} +}); +app.get('/p2p/status',(_req,res)=>res.json(ledger.peerStatus())); +app.get('/p2p/blocks',(req,res)=>{ + const offset=Number(req.query.offset??0); + if(!Number.isSafeInteger(offset)||offset<0){res.status(400).json({error:'Invalid offset'});return;} + const blocks=ledger.inventory(offset,32); + res.json({blocks,next:offset+blocks.lengthres.json({transactions:ledger.pendingTransactions()})); +app.get('/api/nfts/:id', (req,res)=>{ + if(typeof req.params.id!=='string'||!/^[a-f0-9]{64}$/.test(req.params.id)){ + res.status(400).json({error:'Invalid NFT identifier'});return; + } + const nft=ledger.nft(req.params.id); + if(!nft){res.status(404).json({error:'NFT not found on the canonical chain'});return;} + const block=nft.block; + res.json({network:NETWORK,chainId:CHAIN_ID,tokenId:block.tokenId,owner:nft.owner,sequence:nft.sequence,blockHash:block.hash, + confirmations:nft.confirmations,article:block.article,statsSnapshot:block.statsSnapshot, + stateRoot:ledger.block(ledger.summary().tip)?.stateRoot}); +}); +const templateLimit=rateLimit({windowMs:60000,limit:20,standardHeaders:'draft-8',legacyHeaders:false}); +app.post('/api/mining/template', templateLimit, async (req,res)=>{ + const body=z.object({owner:z.string().regex(/^[a-f0-9]{64}$/),publicKey:z.string().max(256)}).strict().parse(req.body); + try { + const article=await wikipediaArticles.random(); + const metrics=await wikipediaEnrichment.getFresh(article.pageId); + res.json(ledger.template(body.owner,body.publicKey,article,snapshotStats(metrics))); + } catch { + res.status(503).json({error:'Could not prepare a Wikipedia article and current statistics. Retry shortly.'}); + } +}); +const workLimit=rateLimit({windowMs:60000,limit:30,standardHeaders:'draft-8',legacyHeaders:false}); +app.post(['/api/blocks','/p2p/block'],workLimit,async(req,res)=>{ + const block=blockSchema.parse(req.body); + const result=await ledger.submit(block); + res.status(result.accepted?201:200).json(result); + if(result.accepted)void peers.broadcastBlock(block).catch(()=>{}); +}); +app.post('/api/transfer',workLimit,async(req,res)=>{ + const tx=transferSchema.parse(req.body); + const id=await ledger.submitTransfer(tx); + res.status(202).json({id,status:'pending',message:'Ownership changes only when this transaction is mined.'}); + void peers.broadcastTransfer(tx).catch(()=>{}); +}); +app.post('/api/trades',workLimit,async(req,res)=>{ + const trade=tradeSchema.parse(req.body); + const id=await ledger.submitTransaction(trade); + res.status(202).json({id,status:'pending',message:'The complete multi-card trade is pending; ownership changes atomically when it is mined.'}); + void peers.broadcastTransaction(trade).catch(()=>{}); +}); +app.post('/p2p/transaction',workLimit,async(req,res)=>{ + const transaction=transactionSchema.parse(req.body); + const id=await ledger.submitTransaction(transaction); + res.status(202).json({id,status:'pending'}); +}); +// One account store and one Dex key cache, shared by the account and game routes (#57). +const oidc=oidcConfig(), dexKeys=new DexKeys(oidc), accounts=new AccountStore(resolve(dataDir,'accounts.sqlite')); +const ownership={ledger,accounts,verifyIdToken:accountVerifier(oidc,dexKeys,accounts)}; +app.use('/api/account',createAccountRouter({config:oidc,keys:dexKeys,store:accounts})); +// GET /api/game/collection is mounted inside gameRouter (it shares the card identity store). +app.use('/api/game',await (await import('../game/index.ts')).gameRouter({dataDir,enrichment:wikipediaEnrichment,ownership})); +app.use(['/api','/p2p'],(_req,res)=>{res.status(404).json({error:'Unknown testnet endpoint'});}); +app.use(express.static(resolve(root,'dist'))); +app.get('/{*path}',(_req,res)=>res.sendFile(resolve(root,'dist/index.html'))); +app.use((err:Error & {status?:number},_req:express.Request,res:express.Response,_next:express.NextFunction)=>{ + const input=err instanceof z.ZodError||err instanceof SyntaxError; + const message=input?'Invalid request encoding':err.message; + // No keys/passwords are accepted by any node endpoint. + if(err instanceof RpcError&&err.status===503)res.setHeader('Retry-After','1'); + res.status(err instanceof RpcError?err.status:err.status===413?413:err.status===415?415:400).json({error:message.slice(0,200)}); +}); +const port=Number(process.env.PORT??3001),host=process.env.HOST??'127.0.0.1'; +const server=app.listen(port,host,()=>{ + const a=server.address();console.log(`Wikideck RandomX ${IS_MAINNET?'mainnet candidate':'testnet'}: http://${host}:${typeof a==='object'&&a?a.port:port} · chain ${CHAIN_ID} · ${ledger.snapshot().blocks.length} blocks`); + peers.start(); +}); +const operations=startOperations(opsPort,()=>({network:NETWORK, + ledger:ledger.diagnostics(),native:engine.diagnostics(), + peers:{configured:peers.status().length,connected:peers.status().filter(p=>p.connected).length}, + rpc:{api:apiBudget.status(),p2p:peerBudget.status()}}),resolve(dataDir,PROFILE)); +server.maxConnections=256; +server.headersTimeout=10000; +server.requestTimeout=15000; +server.keepAliveTimeout=5000; +server.setTimeout(30000,socket=>socket.destroy()); +let stopping=false; +for(const signal of ['SIGTERM','SIGINT']as const)process.once(signal,()=>{ + if(stopping)return;stopping=true;peers.close();operations.close(); + const timeout=setTimeout(()=>process.exit(1),25000);timeout.unref(); + server.close(async()=>{ledger.close();engine.close();await lock.close();await unlink(lockPath).catch(()=>{});clearTimeout(timeout);process.exit(0);}); +}); +server.on('error',async()=>{stopping=true;operations.close();peers.close();ledger.close();engine.close();await lock.close();await unlink(lockPath).catch(()=>{});process.exit(1);}); diff --git a/tests/host-pvp-binding.test.ts b/tests/host-pvp-binding.test.ts index 080f627..5ad3829 100644 --- a/tests/host-pvp-binding.test.ts +++ b/tests/host-pvp-binding.test.ts @@ -126,3 +126,24 @@ test('host-pvp binding: two scripted players finish a hosted 40-card duel on the assert.equal(api.result(replayed)?.winner ?? null, rec.winner); } }); + +test('host-pvp binding: gameRouter mounts GET /collection when ownership deps are given', async () => { + const deps = { + ledger: { nft: () => undefined, deck: () => ({ tip: 't', total: 0, next: null, nfts: [] }), summary: () => ({ tip: 't', height: 1 }) }, + accounts: { wallets: () => [] }, + verifyIdToken: async (t: string) => { if (t !== 'a.b.c') throw Object.assign(new Error('Token expired'), { status: 401 }); + return { sub: 'alice', issuer: 'https://dex.test', name: 'Alice', username: 'alice' }; }, + }; + const app = express(); + app.use('/api/game', await gameRouter({ dataDir: null, warmTrends: false, handleSignals: false, ownership: deps as any, + env: { WIKIDECK_MODES: 'edit-war', PUBLIC_ORIGIN: 'http://127.0.0.1:3101', WIKIDECK_GAME_PEERS: '' } })); + const server = app.listen(0); + try { + const base = `http://127.0.0.1:${(server.address() as any).port}/api/game`; + assert.equal((await fetch(`${base}/collection`)).status, 401); + const ok = await fetch(`${base}/collection`, { headers: { authorization: 'Bearer a.b.c' } }); + assert.equal(ok.status, 200); + assert.deepEqual((await ok.json()).cards, []); + assert.equal((await fetch(`${base}/modes`)).status, 200, 'the game routes still answer'); + } finally { server.close(); } +}); From dfb8abe0e2623e1016285c7b662172e6b7d2431b Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 00:45:50 -0500 Subject: [PATCH 3/5] host-pvp: toDuelCard fallback types cards from on-chain data (extract sentence, mint category hint), not all Doctrine (night-review P1) --- server/game/duel-binding.ts | 41 ++++++++++++++++++++++++++++++---- tests/host-pvp-binding.test.ts | 36 +++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/server/game/duel-binding.ts b/server/game/duel-binding.ts index 13993d4..8a9f019 100644 --- a/server/game/duel-binding.ts +++ b/server/game/duel-binding.ts @@ -10,6 +10,7 @@ */ import { derive, type DuelCard } from '../../shared/duel/derive.ts'; import { profileFromSources } from '../../shared/duel/profile.ts'; +import { classify } from '../../shared/duel/classify.ts'; import { toProfile, type IdentityStore } from './identity.ts'; import type { ChainCard, EnginePort } from './engine-port.ts'; @@ -20,15 +21,47 @@ export type DuelCardSources = { type AnyPort = EnginePort; -/** The single ChainCard → DuelCard conversion. Deterministic for a given block and identity. */ +/** + * Generic type hints from the mint-time `article.category` (server/wiki.ts `category()`), used + * only when neither a frozen identity nor the extract's first sentence types the card. They are + * class words, not facts about the subject, and never reach the Connected keys. + */ +const CATEGORY_HINTS: Readonly> = { + People: 'person', Nature: 'organism', Space: 'astronomical object', History: 'event', Science: 'concept', Culture: 'organization', +}; + +/** First sentence of the on-chain extract, as a description-like signal for the classifier. */ +function firstSentence(extract: string): string { + const text = extract.replace(/\s+/g, ' ').trim(); + return (/^.*?[.!?](?=\s|$)/.exec(text)?.[0] ?? text).slice(0, 300); +} + +/** + * The single ChainCard → DuelCard conversion. Deterministic for a given block and identity. + * Miss path (no frozen identity yet): classify from what the block itself carries, the extract's + * first sentence, then the mint-time category hint. No fetch; card faces stay real data only. + */ export function toDuelCard(card: ChainCard, sources: DuelCardSources = {}): DuelCard { - const article = card.article as { pageId: number; title: string; extract: string; revisionId: number } | undefined; + const article = card.article as { pageId: number; title: string; extract: string; revisionId: number; category?: string } | undefined; const statsSnapshot = card.statsSnapshot as { pageBytes: number; languageCount: number; stats: any } | undefined; if (!article || !statsSnapshot) throw new Error(`Card ${card.tokenId} has no on-chain article or stats snapshot`); + const chain = { tokenId: card.tokenId, article, statsSnapshot }; const frozen = sources.identity?.get(card.tokenId); - const profile = frozen ? toProfile(frozen) : profileFromSources({ enrichment: { + if (frozen) return derive(chain, toProfile(frozen) as any); + const base = profileFromSources({ enrichment: { description: null, wikidataId: null, categories: [], revisionId: article.revisionId ?? null } as any }); - return derive({ tokenId: card.tokenId, article, statsSnapshot }, profile as any); + const sentence = firstSentence(article.extract ?? ''); + let profile = { ...base, description: sentence || null }; + if (classify(profile).source === 'fallback') { + const hint = CATEGORY_HINTS[article.category ?? '']; + if (hint) profile = { ...profile, instanceOf: [{ id: '', label: hint }] }; + } + const out = derive(chain, profile as any); + // The sentence is the flavour text already; it is not a Wikidata short description. + delete (out as { description?: string }).description; + out.connect = { ...out.connect, p31: out.connect.p31.filter(id => id !== '') }; + (out as { typedBy?: string }).typedBy = 'fallback'; + return out; } /** Wrap duel-core's `duelEnginePort` with what the host adds: owned-card building and the copy limit. */ diff --git a/tests/host-pvp-binding.test.ts b/tests/host-pvp-binding.test.ts index 5ad3829..79d8a1a 100644 --- a/tests/host-pvp-binding.test.ts +++ b/tests/host-pvp-binding.test.ts @@ -147,3 +147,39 @@ test('host-pvp binding: gameRouter mounts GET /collection when ownership deps ar assert.equal((await fetch(`${base}/modes`)).status, 200, 'the game routes still answer'); } finally { server.close(); } }); + +/** server/wiki.ts's mint-time category(), reproduced for the sample (it is not exported). */ +function mintCategory(title: string, categories: string[]): string { + const text = `${title} ${categories.join(' ')}`.toLowerCase(); + if (/astronom|planet|space|galax|moon|star\b/.test(text)) return 'Space'; + if (/animal|plant|species|ocean|biolog|mollusc/.test(text)) return 'Nature'; + if (/births|deaths|people|scientist/.test(text)) return 'People'; + if (/physic|chem|math|science|technolog/.test(text)) return 'Science'; + if (/histor|ancient|archaeolog|empire/.test(text)) return 'History'; + return 'Culture'; +} + +test('host-pvp binding: without a frozen identity, cards are typed from on-chain data, not all Doctrine (night-review P1)', () => { + const types: Record = {}; + for (const s of SAMPLE) { + const c = chainCard(s); + (c.article as any).category = mintCategory(s.title, s.categories); + const d = toDuelCard(c, {}); + types[d.type] = (types[d.type] ?? 0) + 1; + assert.equal(d.typedBy, 'fallback'); + assert.equal(d.description, undefined, 'no fabricated short description'); + assert.ok(!d.connect.p31.includes(''), 'hints never become Connected keys'); + } + assert.ok((types.Doctrine ?? 0) < SAMPLE.length / 2, JSON.stringify(types)); + assert.ok((types.Figure ?? 0) >= 40, JSON.stringify(types)); + // The extract's first sentence wins over the category hint. + const s = SAMPLE[0]!; + const battle = chainCard(s); + (battle.article as any).extract = 'The Battle of Somewhere was a battle fought in 1548. It was long.'; + (battle.article as any).category = 'People'; + assert.equal(toDuelCard(battle, {}).type, 'Event'); + const pol = chainCard(s); + (pol.article as any).extract = 'Jane Doe is an American politician.'; + (pol.article as any).category = 'Culture'; + assert.equal(toDuelCard(pol, {}).type, 'Figure'); +}); From ae2805067a4a561bf8df9fe6a77d0aa90150487e Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 00:56:33 -0500 Subject: [PATCH 4/5] HOSTING.md: duel card typing (identity vs on-chain fallback) and the clock --- docs/game/HOSTING.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/game/HOSTING.md b/docs/game/HOSTING.md index 8c2713b..49aa7a3 100644 --- a/docs/game/HOSTING.md +++ b/docs/game/HOSTING.md @@ -56,6 +56,8 @@ a file, a URL, a script or a function. - **Quick match:** `POST /modes/:id/quick` joins the oldest open public lobby someone else opened (`matched: true`), or opens one (`matched: false`) to poll. - **Polling:** every lobby view has a `version`. `GET /lobbies/:id?v=` answers **204** while nothing changed. Poll; never hold requests open (the node caps concurrent `/api` requests). - **Ladder:** `GET /modes/:id/ladder` (same as `/leaderboard`): entries carry `rating` (Elo, or null if unrated) plus W/L/D. +- **Duel cards:** `server/game/duel-binding.ts` `toDuelCard` is the one path from an owned NFT to a duel card, for matches and for `GET /api/game/collection` card faces. It uses the frozen card identity (`/card-identity.sqlite`, testnet-ops' pre-bake) when present. Otherwise it types the card from what its block carries (the extract's first sentence, then the mint-time category), with no fetch. So a card's *collection* type can change **once**, when its identity is first frozen. Matches never change: the record stores the derived cards. Run the identity pre-bake before opening a node to PvP. +- **Clock:** duel turns get `turnSeconds` (+5 s grace) plus a per-seat `bankSeconds` bank; seat views carry `clock: {turnStartedAt, turnMs, bankMs}`. - **Records:** `GET /matches/:id` has the seed, rules, both decks (as the engine cards), every action, `engine`, `rated`, and for constructed modes `tokens` (the NFT ids each seat played), so the recap can replay the match. ## 2. Running your own modes From 983552c0868f84a04aa4ff76c2ccdaf3cc2d336c Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 01:07:18 -0500 Subject: [PATCH 5/5] http.ts: restore CRLF line endings (night-review) --- server/testnet/http.ts | 382 ++++++++++++++++++++--------------------- 1 file changed, 191 insertions(+), 191 deletions(-) diff --git a/server/testnet/http.ts b/server/testnet/http.ts index c6dabed..26ed932 100644 --- a/server/testnet/http.ts +++ b/server/testnet/http.ts @@ -1,191 +1,191 @@ -import express from 'express'; -import helmet from 'helmet'; -import rateLimit from 'express-rate-limit'; -import { resolve } from 'node:path'; -import { fileURLToPath } from 'node:url'; -import { mkdir, open, unlink } from 'node:fs/promises'; -import { z } from 'zod'; -import { NETWORK } from '../../shared/testnet.ts'; -import { CHAIN_ID, blockSchema, tradeSchema, transferSchema, transactionSchema } from './consensus.ts'; -import { RandomX } from './randomx.ts'; -import { Ledger } from './ledger.ts'; -import { PeerNetwork } from './peers.ts'; -import { wikipediaEnrichment } from '../enrichment.ts'; -import { snapshotStats } from '../../shared/enrichment.ts'; -import { wikipediaArticles } from '../wiki.ts'; -import { IS_MAINNET, PROFILE } from '../../shared/network.ts'; -import { assertLaunchAllowed, launchManifest } from '../launch.ts'; -import { resourceConfig, RpcBudget, RpcError } from '../resources.ts'; -import { trustedProxies, operationsPort, startOperations } from '../operations.ts'; -import { accountVerifier, createAccountRouter } from '../account/router.ts'; -import { AccountStore } from '../account/store.ts'; -import { DexKeys, oidcConfig } from '../account/oidc.ts'; - -const root = fileURLToPath(new URL('../../', import.meta.url)); -assertLaunchAllowed(); -const limits = resourceConfig(); -const proxyTrust=trustedProxies(); -const opsPort=operationsPort(); -const apiBudget = new RpcBudget(limits); -const peerBudget = new RpcBudget({ ...limits, requestsPerSecond: 40, writesPerSecond: 10, active: 16 }); -const dataDir = process.env.WIKIDECK_DATA_DIR ?? resolve(root,'data'); -await mkdir(dataDir,{recursive:true}); -// Never permit two processes to rewrite the same ledger. A stale lock requires operator review. -const lockPath = resolve(dataDir,`${PROFILE}-node.lock`); -const lock = await open(lockPath,'wx',0o600).catch(()=>{throw new Error('Data directory locked. Stop its other node; remove stale testnet-node.lock only after verifying no node is running.');}); -await lock.writeFile(String(process.pid)); -let engine: RandomX; -try { engine = new RandomX(); } -catch(e) { await lock.close();await unlink(lockPath);throw e; } -let ledger: Ledger; -try { ledger = await Ledger.open(dataDir,engine); } -catch(e) {engine.close();await lock.close();await unlink(lockPath);throw e;} -let peers: PeerNetwork; -try { peers = new PeerNetwork({ ledger, urls:(process.env.WIKIDECK_PEERS ?? '').split(',').map(s=>s.trim()).filter(Boolean), - allowPrivate:process.env.WIKIDECK_ALLOW_PRIVATE_PEERS === '1' }); } -catch(e) {ledger.close();engine.close();await lock.close();await unlink(lockPath);throw e;} -const app = express(); -app.disable('x-powered-by'); -if(proxyTrust.length)app.set('trust proxy',proxyTrust); -app.use(helmet({contentSecurityPolicy:{directives:{defaultSrc:["'self'"],scriptSrc:["'self'"], - imgSrc:["'self'",'data:','https://upload.wikimedia.org','https://thumb.wikimedia.org'],styleSrc:["'self'","'unsafe-inline'"], - connectSrc:["'self'"],upgradeInsecureRequests:null}},crossOriginEmbedderPolicy:false, - strictTransportSecurity:process.env.NODE_ENV==='production'})); -app.get('/healthz',(_req,res)=>{res.setHeader('Cache-Control','no-store');res.json({status:'ok',network:NETWORK});}); -// Admission comes before body parsing and signature/schema work. Disable compressed request inflation. -app.use('/api',apiBudget.middleware()); -app.use('/p2p',peerBudget.middleware()); -app.use(['/api','/p2p'],rateLimit({windowMs:60000,limit:300,standardHeaders:'draft-8',legacyHeaders:false})); -app.use(['/api','/p2p'],(req,res,next)=>{ - res.setHeader('Cache-Control','no-store'); - if(req.method==='POST') { - if(!req.is('application/json')){res.status(415).json({error:'JSON required'});return;} - const origin=req.get('origin'); - const allowed=new Set(['http://127.0.0.1:5173','http://localhost:5173','http://127.0.0.1:3001','http://localhost:3001',process.env.PUBLIC_ORIGIN]); - if(origin&&!allowed.has(origin)){res.status(403).json({error:'Origin not allowed'});return;} - } - next(); -}); -app.use(['/api','/p2p'],express.json({limit:'68kb',strict:true,inflate:false})); -app.get('/api/state',(_req,res)=>res.json(ledger.legacySnapshot(limits.legacyBlocks))); -app.get('/api/summary',(_req,res)=>res.json(ledger.summary())); -app.get('/api/canonical',(req,res)=>{ - const q=z.object({tip:z.string().regex(/^[a-f0-9]{64}$/),offset:z.string().regex(/^\d{1,12}$/).optional(), - limit:z.string().regex(/^\d{1,2}$/).optional()}).strict().parse(req.query); - res.json(ledger.canonicalPage(q.tip,Number(q.offset??0),Number(q.limit??16))); -}); -app.get('/api/network',(_req,res)=>res.json(launchManifest())); -app.get('/api/chain',(_req,res)=>res.json(ledger.legacySnapshot(limits.legacyBlocks))); -app.get('/api/peers',(_req,res)=>res.json({peers:peers.status()})); -app.get('/api/decks/:address', rateLimit({windowMs:60000,limit:60,standardHeaders:'draft-8',legacyHeaders:false}), (req,res)=>{ - if(typeof req.params.address!=='string'||!/^[a-f0-9]{64}$/.test(req.params.address)){ - res.status(400).json({error:'Invalid wallet address'});return; - } - const offset=Number(req.query.offset??0),limit=Number(req.query.limit??24); - if(!Number.isSafeInteger(offset)||offset<0||!Number.isSafeInteger(limit)||limit<1||limit>24){ - res.status(400).json({error:'Invalid deck page'});return; - } - res.setHeader('Cache-Control','no-store'); - if(req.query.tip!==undefined && req.query.tip!==ledger.summary().tip){ - res.status(409).json({error:'Canonical tip changed; restart deck pagination.'});return; - } - res.json(ledger.deck(req.params.address,offset,limit)); -}); -// Optional live data is intentionally not used by genesis, block validation or NFT ownership. -app.get('/api/wikipedia/:pageId', rateLimit({windowMs:60000,limit:60,standardHeaders:'draft-8',legacyHeaders:false}), async(req,res)=>{ - if(typeof req.params.pageId!=='string'||!/^[1-9]\d{0,9}$/.test(req.params.pageId)){res.status(400).json({error:'Invalid Wikipedia page ID'});return;} - const pageId=Number(req.params.pageId); - try {res.json(await wikipediaEnrichment.get(pageId));} - catch {res.status(503).json({error:'Wikipedia enrichment is temporarily unavailable. NFT data is unaffected.'});} -}); -app.get('/p2p/status',(_req,res)=>res.json(ledger.peerStatus())); -app.get('/p2p/blocks',(req,res)=>{ - const offset=Number(req.query.offset??0); - if(!Number.isSafeInteger(offset)||offset<0){res.status(400).json({error:'Invalid offset'});return;} - const blocks=ledger.inventory(offset,32); - res.json({blocks,next:offset+blocks.lengthres.json({transactions:ledger.pendingTransactions()})); -app.get('/api/nfts/:id', (req,res)=>{ - if(typeof req.params.id!=='string'||!/^[a-f0-9]{64}$/.test(req.params.id)){ - res.status(400).json({error:'Invalid NFT identifier'});return; - } - const nft=ledger.nft(req.params.id); - if(!nft){res.status(404).json({error:'NFT not found on the canonical chain'});return;} - const block=nft.block; - res.json({network:NETWORK,chainId:CHAIN_ID,tokenId:block.tokenId,owner:nft.owner,sequence:nft.sequence,blockHash:block.hash, - confirmations:nft.confirmations,article:block.article,statsSnapshot:block.statsSnapshot, - stateRoot:ledger.block(ledger.summary().tip)?.stateRoot}); -}); -const templateLimit=rateLimit({windowMs:60000,limit:20,standardHeaders:'draft-8',legacyHeaders:false}); -app.post('/api/mining/template', templateLimit, async (req,res)=>{ - const body=z.object({owner:z.string().regex(/^[a-f0-9]{64}$/),publicKey:z.string().max(256)}).strict().parse(req.body); - try { - const article=await wikipediaArticles.random(); - const metrics=await wikipediaEnrichment.getFresh(article.pageId); - res.json(ledger.template(body.owner,body.publicKey,article,snapshotStats(metrics))); - } catch { - res.status(503).json({error:'Could not prepare a Wikipedia article and current statistics. Retry shortly.'}); - } -}); -const workLimit=rateLimit({windowMs:60000,limit:30,standardHeaders:'draft-8',legacyHeaders:false}); -app.post(['/api/blocks','/p2p/block'],workLimit,async(req,res)=>{ - const block=blockSchema.parse(req.body); - const result=await ledger.submit(block); - res.status(result.accepted?201:200).json(result); - if(result.accepted)void peers.broadcastBlock(block).catch(()=>{}); -}); -app.post('/api/transfer',workLimit,async(req,res)=>{ - const tx=transferSchema.parse(req.body); - const id=await ledger.submitTransfer(tx); - res.status(202).json({id,status:'pending',message:'Ownership changes only when this transaction is mined.'}); - void peers.broadcastTransfer(tx).catch(()=>{}); -}); -app.post('/api/trades',workLimit,async(req,res)=>{ - const trade=tradeSchema.parse(req.body); - const id=await ledger.submitTransaction(trade); - res.status(202).json({id,status:'pending',message:'The complete multi-card trade is pending; ownership changes atomically when it is mined.'}); - void peers.broadcastTransaction(trade).catch(()=>{}); -}); -app.post('/p2p/transaction',workLimit,async(req,res)=>{ - const transaction=transactionSchema.parse(req.body); - const id=await ledger.submitTransaction(transaction); - res.status(202).json({id,status:'pending'}); -}); -// One account store and one Dex key cache, shared by the account and game routes (#57). -const oidc=oidcConfig(), dexKeys=new DexKeys(oidc), accounts=new AccountStore(resolve(dataDir,'accounts.sqlite')); -const ownership={ledger,accounts,verifyIdToken:accountVerifier(oidc,dexKeys,accounts)}; -app.use('/api/account',createAccountRouter({config:oidc,keys:dexKeys,store:accounts})); -// GET /api/game/collection is mounted inside gameRouter (it shares the card identity store). -app.use('/api/game',await (await import('../game/index.ts')).gameRouter({dataDir,enrichment:wikipediaEnrichment,ownership})); -app.use(['/api','/p2p'],(_req,res)=>{res.status(404).json({error:'Unknown testnet endpoint'});}); -app.use(express.static(resolve(root,'dist'))); -app.get('/{*path}',(_req,res)=>res.sendFile(resolve(root,'dist/index.html'))); -app.use((err:Error & {status?:number},_req:express.Request,res:express.Response,_next:express.NextFunction)=>{ - const input=err instanceof z.ZodError||err instanceof SyntaxError; - const message=input?'Invalid request encoding':err.message; - // No keys/passwords are accepted by any node endpoint. - if(err instanceof RpcError&&err.status===503)res.setHeader('Retry-After','1'); - res.status(err instanceof RpcError?err.status:err.status===413?413:err.status===415?415:400).json({error:message.slice(0,200)}); -}); -const port=Number(process.env.PORT??3001),host=process.env.HOST??'127.0.0.1'; -const server=app.listen(port,host,()=>{ - const a=server.address();console.log(`Wikideck RandomX ${IS_MAINNET?'mainnet candidate':'testnet'}: http://${host}:${typeof a==='object'&&a?a.port:port} · chain ${CHAIN_ID} · ${ledger.snapshot().blocks.length} blocks`); - peers.start(); -}); -const operations=startOperations(opsPort,()=>({network:NETWORK, - ledger:ledger.diagnostics(),native:engine.diagnostics(), - peers:{configured:peers.status().length,connected:peers.status().filter(p=>p.connected).length}, - rpc:{api:apiBudget.status(),p2p:peerBudget.status()}}),resolve(dataDir,PROFILE)); -server.maxConnections=256; -server.headersTimeout=10000; -server.requestTimeout=15000; -server.keepAliveTimeout=5000; -server.setTimeout(30000,socket=>socket.destroy()); -let stopping=false; -for(const signal of ['SIGTERM','SIGINT']as const)process.once(signal,()=>{ - if(stopping)return;stopping=true;peers.close();operations.close(); - const timeout=setTimeout(()=>process.exit(1),25000);timeout.unref(); - server.close(async()=>{ledger.close();engine.close();await lock.close();await unlink(lockPath).catch(()=>{});clearTimeout(timeout);process.exit(0);}); -}); -server.on('error',async()=>{stopping=true;operations.close();peers.close();ledger.close();engine.close();await lock.close();await unlink(lockPath).catch(()=>{});process.exit(1);}); +import express from 'express'; +import helmet from 'helmet'; +import rateLimit from 'express-rate-limit'; +import { resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { mkdir, open, unlink } from 'node:fs/promises'; +import { z } from 'zod'; +import { NETWORK } from '../../shared/testnet.ts'; +import { CHAIN_ID, blockSchema, tradeSchema, transferSchema, transactionSchema } from './consensus.ts'; +import { RandomX } from './randomx.ts'; +import { Ledger } from './ledger.ts'; +import { PeerNetwork } from './peers.ts'; +import { wikipediaEnrichment } from '../enrichment.ts'; +import { snapshotStats } from '../../shared/enrichment.ts'; +import { wikipediaArticles } from '../wiki.ts'; +import { IS_MAINNET, PROFILE } from '../../shared/network.ts'; +import { assertLaunchAllowed, launchManifest } from '../launch.ts'; +import { resourceConfig, RpcBudget, RpcError } from '../resources.ts'; +import { trustedProxies, operationsPort, startOperations } from '../operations.ts'; +import { accountVerifier, createAccountRouter } from '../account/router.ts'; +import { AccountStore } from '../account/store.ts'; +import { DexKeys, oidcConfig } from '../account/oidc.ts'; + +const root = fileURLToPath(new URL('../../', import.meta.url)); +assertLaunchAllowed(); +const limits = resourceConfig(); +const proxyTrust=trustedProxies(); +const opsPort=operationsPort(); +const apiBudget = new RpcBudget(limits); +const peerBudget = new RpcBudget({ ...limits, requestsPerSecond: 40, writesPerSecond: 10, active: 16 }); +const dataDir = process.env.WIKIDECK_DATA_DIR ?? resolve(root,'data'); +await mkdir(dataDir,{recursive:true}); +// Never permit two processes to rewrite the same ledger. A stale lock requires operator review. +const lockPath = resolve(dataDir,`${PROFILE}-node.lock`); +const lock = await open(lockPath,'wx',0o600).catch(()=>{throw new Error('Data directory locked. Stop its other node; remove stale testnet-node.lock only after verifying no node is running.');}); +await lock.writeFile(String(process.pid)); +let engine: RandomX; +try { engine = new RandomX(); } +catch(e) { await lock.close();await unlink(lockPath);throw e; } +let ledger: Ledger; +try { ledger = await Ledger.open(dataDir,engine); } +catch(e) {engine.close();await lock.close();await unlink(lockPath);throw e;} +let peers: PeerNetwork; +try { peers = new PeerNetwork({ ledger, urls:(process.env.WIKIDECK_PEERS ?? '').split(',').map(s=>s.trim()).filter(Boolean), + allowPrivate:process.env.WIKIDECK_ALLOW_PRIVATE_PEERS === '1' }); } +catch(e) {ledger.close();engine.close();await lock.close();await unlink(lockPath);throw e;} +const app = express(); +app.disable('x-powered-by'); +if(proxyTrust.length)app.set('trust proxy',proxyTrust); +app.use(helmet({contentSecurityPolicy:{directives:{defaultSrc:["'self'"],scriptSrc:["'self'"], + imgSrc:["'self'",'data:','https://upload.wikimedia.org','https://thumb.wikimedia.org'],styleSrc:["'self'","'unsafe-inline'"], + connectSrc:["'self'"],upgradeInsecureRequests:null}},crossOriginEmbedderPolicy:false, + strictTransportSecurity:process.env.NODE_ENV==='production'})); +app.get('/healthz',(_req,res)=>{res.setHeader('Cache-Control','no-store');res.json({status:'ok',network:NETWORK});}); +// Admission comes before body parsing and signature/schema work. Disable compressed request inflation. +app.use('/api',apiBudget.middleware()); +app.use('/p2p',peerBudget.middleware()); +app.use(['/api','/p2p'],rateLimit({windowMs:60000,limit:300,standardHeaders:'draft-8',legacyHeaders:false})); +app.use(['/api','/p2p'],(req,res,next)=>{ + res.setHeader('Cache-Control','no-store'); + if(req.method==='POST') { + if(!req.is('application/json')){res.status(415).json({error:'JSON required'});return;} + const origin=req.get('origin'); + const allowed=new Set(['http://127.0.0.1:5173','http://localhost:5173','http://127.0.0.1:3001','http://localhost:3001',process.env.PUBLIC_ORIGIN]); + if(origin&&!allowed.has(origin)){res.status(403).json({error:'Origin not allowed'});return;} + } + next(); +}); +app.use(['/api','/p2p'],express.json({limit:'68kb',strict:true,inflate:false})); +app.get('/api/state',(_req,res)=>res.json(ledger.legacySnapshot(limits.legacyBlocks))); +app.get('/api/summary',(_req,res)=>res.json(ledger.summary())); +app.get('/api/canonical',(req,res)=>{ + const q=z.object({tip:z.string().regex(/^[a-f0-9]{64}$/),offset:z.string().regex(/^\d{1,12}$/).optional(), + limit:z.string().regex(/^\d{1,2}$/).optional()}).strict().parse(req.query); + res.json(ledger.canonicalPage(q.tip,Number(q.offset??0),Number(q.limit??16))); +}); +app.get('/api/network',(_req,res)=>res.json(launchManifest())); +app.get('/api/chain',(_req,res)=>res.json(ledger.legacySnapshot(limits.legacyBlocks))); +app.get('/api/peers',(_req,res)=>res.json({peers:peers.status()})); +app.get('/api/decks/:address', rateLimit({windowMs:60000,limit:60,standardHeaders:'draft-8',legacyHeaders:false}), (req,res)=>{ + if(typeof req.params.address!=='string'||!/^[a-f0-9]{64}$/.test(req.params.address)){ + res.status(400).json({error:'Invalid wallet address'});return; + } + const offset=Number(req.query.offset??0),limit=Number(req.query.limit??24); + if(!Number.isSafeInteger(offset)||offset<0||!Number.isSafeInteger(limit)||limit<1||limit>24){ + res.status(400).json({error:'Invalid deck page'});return; + } + res.setHeader('Cache-Control','no-store'); + if(req.query.tip!==undefined && req.query.tip!==ledger.summary().tip){ + res.status(409).json({error:'Canonical tip changed; restart deck pagination.'});return; + } + res.json(ledger.deck(req.params.address,offset,limit)); +}); +// Optional live data is intentionally not used by genesis, block validation or NFT ownership. +app.get('/api/wikipedia/:pageId', rateLimit({windowMs:60000,limit:60,standardHeaders:'draft-8',legacyHeaders:false}), async(req,res)=>{ + if(typeof req.params.pageId!=='string'||!/^[1-9]\d{0,9}$/.test(req.params.pageId)){res.status(400).json({error:'Invalid Wikipedia page ID'});return;} + const pageId=Number(req.params.pageId); + try {res.json(await wikipediaEnrichment.get(pageId));} + catch {res.status(503).json({error:'Wikipedia enrichment is temporarily unavailable. NFT data is unaffected.'});} +}); +app.get('/p2p/status',(_req,res)=>res.json(ledger.peerStatus())); +app.get('/p2p/blocks',(req,res)=>{ + const offset=Number(req.query.offset??0); + if(!Number.isSafeInteger(offset)||offset<0){res.status(400).json({error:'Invalid offset'});return;} + const blocks=ledger.inventory(offset,32); + res.json({blocks,next:offset+blocks.lengthres.json({transactions:ledger.pendingTransactions()})); +app.get('/api/nfts/:id', (req,res)=>{ + if(typeof req.params.id!=='string'||!/^[a-f0-9]{64}$/.test(req.params.id)){ + res.status(400).json({error:'Invalid NFT identifier'});return; + } + const nft=ledger.nft(req.params.id); + if(!nft){res.status(404).json({error:'NFT not found on the canonical chain'});return;} + const block=nft.block; + res.json({network:NETWORK,chainId:CHAIN_ID,tokenId:block.tokenId,owner:nft.owner,sequence:nft.sequence,blockHash:block.hash, + confirmations:nft.confirmations,article:block.article,statsSnapshot:block.statsSnapshot, + stateRoot:ledger.block(ledger.summary().tip)?.stateRoot}); +}); +const templateLimit=rateLimit({windowMs:60000,limit:20,standardHeaders:'draft-8',legacyHeaders:false}); +app.post('/api/mining/template', templateLimit, async (req,res)=>{ + const body=z.object({owner:z.string().regex(/^[a-f0-9]{64}$/),publicKey:z.string().max(256)}).strict().parse(req.body); + try { + const article=await wikipediaArticles.random(); + const metrics=await wikipediaEnrichment.getFresh(article.pageId); + res.json(ledger.template(body.owner,body.publicKey,article,snapshotStats(metrics))); + } catch { + res.status(503).json({error:'Could not prepare a Wikipedia article and current statistics. Retry shortly.'}); + } +}); +const workLimit=rateLimit({windowMs:60000,limit:30,standardHeaders:'draft-8',legacyHeaders:false}); +app.post(['/api/blocks','/p2p/block'],workLimit,async(req,res)=>{ + const block=blockSchema.parse(req.body); + const result=await ledger.submit(block); + res.status(result.accepted?201:200).json(result); + if(result.accepted)void peers.broadcastBlock(block).catch(()=>{}); +}); +app.post('/api/transfer',workLimit,async(req,res)=>{ + const tx=transferSchema.parse(req.body); + const id=await ledger.submitTransfer(tx); + res.status(202).json({id,status:'pending',message:'Ownership changes only when this transaction is mined.'}); + void peers.broadcastTransfer(tx).catch(()=>{}); +}); +app.post('/api/trades',workLimit,async(req,res)=>{ + const trade=tradeSchema.parse(req.body); + const id=await ledger.submitTransaction(trade); + res.status(202).json({id,status:'pending',message:'The complete multi-card trade is pending; ownership changes atomically when it is mined.'}); + void peers.broadcastTransaction(trade).catch(()=>{}); +}); +app.post('/p2p/transaction',workLimit,async(req,res)=>{ + const transaction=transactionSchema.parse(req.body); + const id=await ledger.submitTransaction(transaction); + res.status(202).json({id,status:'pending'}); +}); +// One account store and one Dex key cache, shared by the account and game routes (#57). +const oidc=oidcConfig(), dexKeys=new DexKeys(oidc), accounts=new AccountStore(resolve(dataDir,'accounts.sqlite')); +const ownership={ledger,accounts,verifyIdToken:accountVerifier(oidc,dexKeys,accounts)}; +app.use('/api/account',createAccountRouter({config:oidc,keys:dexKeys,store:accounts})); +// GET /api/game/collection is mounted inside gameRouter (it shares the card identity store). +app.use('/api/game',await (await import('../game/index.ts')).gameRouter({dataDir,enrichment:wikipediaEnrichment,ownership})); +app.use(['/api','/p2p'],(_req,res)=>{res.status(404).json({error:'Unknown testnet endpoint'});}); +app.use(express.static(resolve(root,'dist'))); +app.get('/{*path}',(_req,res)=>res.sendFile(resolve(root,'dist/index.html'))); +app.use((err:Error & {status?:number},_req:express.Request,res:express.Response,_next:express.NextFunction)=>{ + const input=err instanceof z.ZodError||err instanceof SyntaxError; + const message=input?'Invalid request encoding':err.message; + // No keys/passwords are accepted by any node endpoint. + if(err instanceof RpcError&&err.status===503)res.setHeader('Retry-After','1'); + res.status(err instanceof RpcError?err.status:err.status===413?413:err.status===415?415:400).json({error:message.slice(0,200)}); +}); +const port=Number(process.env.PORT??3001),host=process.env.HOST??'127.0.0.1'; +const server=app.listen(port,host,()=>{ + const a=server.address();console.log(`Wikideck RandomX ${IS_MAINNET?'mainnet candidate':'testnet'}: http://${host}:${typeof a==='object'&&a?a.port:port} · chain ${CHAIN_ID} · ${ledger.snapshot().blocks.length} blocks`); + peers.start(); +}); +const operations=startOperations(opsPort,()=>({network:NETWORK, + ledger:ledger.diagnostics(),native:engine.diagnostics(), + peers:{configured:peers.status().length,connected:peers.status().filter(p=>p.connected).length}, + rpc:{api:apiBudget.status(),p2p:peerBudget.status()}}),resolve(dataDir,PROFILE)); +server.maxConnections=256; +server.headersTimeout=10000; +server.requestTimeout=15000; +server.keepAliveTimeout=5000; +server.setTimeout(30000,socket=>socket.destroy()); +let stopping=false; +for(const signal of ['SIGTERM','SIGINT']as const)process.once(signal,()=>{ + if(stopping)return;stopping=true;peers.close();operations.close(); + const timeout=setTimeout(()=>process.exit(1),25000);timeout.unref(); + server.close(async()=>{ledger.close();engine.close();await lock.close();await unlink(lockPath).catch(()=>{});clearTimeout(timeout);process.exit(0);}); +}); +server.on('error',async()=>{stopping=true;operations.close();peers.close();ledger.close();engine.close();await lock.close();await unlink(lockPath).catch(()=>{});process.exit(1);});