import { DatabaseSync } from 'node:sqlite'; import { mkdtemp, rm, stat } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { monitorEventLoopDelay, performance } from 'node:perf_hooks'; import { randomUUID } from 'node:crypto'; import { setImmediate as yieldToEventLoop } from 'node:timers/promises'; import { Market } from '../server/market/store.ts'; import { CHAIN_ID, GENESIS_HASH, transactionId } from '../server/testnet/consensus.ts'; import { NETWORK } from '../shared/testnet.ts'; // Synthetic market journal only. It is isolated in a temporary SQLite database and never // connects to a Ledger, peer, payment adapter, wallet, or production data directory. const text = process.argv[2] ?? '10000'; if (!/^[1-9]\d{0,5}$/.test(text) || Number(text) > 100000) throw new Error('Fixture size must be 1..100000 orders'); const count = Number(text), scenario = process.argv[3] ?? 'terminal'; if (!['terminal', 'confirmed-delivery'].includes(scenario)) throw new Error('Scenario must be terminal or confirmed-delivery'); const directory = await mkdtemp(join(tmpdir(), 'wikideck-market-reconciliation-')); const streamId = randomUUID(), tip = 'a'.repeat(64); const deliveryRows = new Map(); const seller = '1'.repeat(64), buyer = '2'.repeat(64); let market: Market | undefined; try { // Indexed-only adapter: the benchmark must not fall back to materializing chain snapshots. const chain = { snapshot: () => { throw new Error('Benchmark must not request a full chain snapshot'); }, summary: () => ({ tip, revision: 0, height: 2 }), nft: () => undefined, canonicalTransaction: (id: string) => deliveryRows.get(id), pendingTransactions: () => [], canonicalHead: () => 0, canonicalEvents: (after: number) => ({ streamId, previousAnchor: GENESIS_HASH, anchor: GENESIS_HASH, events: [], cursor: after, head: 0, caughtUp: true }), submitTransfer: async () => { throw new Error('Benchmark never submits transfers'); }, }; market = await Market.open(directory, chain, { feeBps: 250, confirmations: 2 }); const db = new DatabaseSync(join(directory, NETWORK.includes('mainnet') ? 'mainnet' : 'testnet', 'market.sqlite')); try { const insertListing = db.prepare('INSERT INTO listings(id,card,status,data) VALUES(?,?,?,?)'); const insertOrder = db.prepare('INSERT INTO orders(id,data) VALUES(?,?)'); db.exec('BEGIN IMMEDIATE'); try { for (let i = 0; i < count; i++) { const id = i.toString().padStart(12, '0'); const listingId = `listing-${id}`, orderId = `order-${id}`, cardHash = i.toString(16).padStart(64, '0'); const terminal = scenario === 'terminal'; const listingStatus = terminal ? 'cancelled' : 'reserved'; const listing = { id: listingId, cardHash, seller, sequence: 0, kind: 'fixed', status: listingStatus }; const order = { id: orderId, listingId, seller, buyer, cardHash, sequence: 0, status: terminal ? 'refunded' : 'paid', payout: null, deliveryConfirmations: 0, requiredConfirmations: 2, createdAt: 1, payBy: 2, deliverBy: 3 }; if (!terminal) { const delivery = { network: NETWORK, chainId: CHAIN_ID, cardHash, from: seller, to: buyer, sequence: 0, publicKey: '', signature: '' }; deliveryRows.set(transactionId(delivery), { height: 1, blockHash: tip }); } insertListing.run(listingId, cardHash, listingStatus, JSON.stringify(listing)); insertOrder.run(orderId, JSON.stringify(order)); } db.exec('COMMIT'); } catch (error) { db.exec('ROLLBACK'); throw error; } } finally { db.close(); } const latencies: number[] = []; const maxPasses = Math.ceil(count / 128) * 2 + 4; const eventLoopDelay = monitorEventLoopDelay({ resolution: 10 }); eventLoopDelay.enable(); const start = performance.now(); try { for (let pass = 0; pass < maxPasses && (pass === 0 || market.hasReconciliationBacklog()); pass++) { const before = performance.now(); market.browse(); latencies.push(performance.now() - before); await yieldToEventLoop(); // Permit the delay monitor to observe synchronous batch stalls. } } finally { eventLoopDelay.disable(); } const elapsedMs = performance.now() - start; if (market.hasReconciliationBacklog()) throw new Error(`Reconciliation did not finish within ${maxPasses} bounded passes`); market.close(); market = undefined; const bytes = (await stat(join(directory, NETWORK.includes('mainnet') ? 'mainnet' : 'testnet', 'market.sqlite'))).size; latencies.sort((a, b) => a - b); const percentile = (p: number) => latencies[Math.min(latencies.length - 1, Math.ceil(latencies.length * p) - 1)]; console.log(JSON.stringify({ fixture: `synthetic-${scenario}-orders-and-listings`, orders: count, listings: count, batchSizePerTable: 128, reconciliationPasses: latencies.length, elapsedMs, rowsPerSecond: Math.round((count * 2 * 1000) / elapsedMs), passLatencyP50Ms: percentile(0.50), passLatencyP95Ms: percentile(0.95), passLatencyMaxMs: latencies.at(-1), sqliteBytes: bytes, eventLoopDelayResolutionMs: 10, eventLoopDelayP95Ms: eventLoopDelay.percentile(95) / 1e6, eventLoopDelayMaxMs: eventLoopDelay.max / 1e6, warning: 'Synthetic fixture on this development host only; canonical deliveries are adapter fixtures, not Ledger evidence. Excludes concurrent HTTP load, PostgreSQL, and production SLO certification.' }, null, 2)); } finally { market?.close(); await rm(directory, { recursive: true, force: true }); }