mirror of
https://github.com/Such-Software/wownerogue.git
synced 2026-11-19 21:43:55 +01:00
fix: resolve security/correctness review + AAA graphics pass
Backend (Tier 0/1/3): single-payout double-pay -> needs_review; admin-retry tx_hash guard; UNIQUE index on games.payment_id (migration 023) + atomic claim; reconnect no longer resets block-timeout; completeGame keys on user_id+dungeon_seed (not socket_id); token-gated /api/user/:socketId/* endpoints; payout min/max enforcement; orphaned-active-game boot recovery; sessionManager cleanup FK fix; XFF rightmost-hop; global uncaughtException/unhandledRejection guard; server binds 127.0.0.1; provably-fair client-seed (HMAC) + RNG [0,1) bound; difficulty preset ranges reach the digger; chat no longer leaks full socket.id; removed dangerous-keyword SQL theater. Match mode (Tier 2, still MATCH_ENABLED-gated): fixed match_queue/match_move crash paths + scheduler dead-code + leaked ceiling timer; pot collected before start with abort+refund; BigInt/basis-point fee math, capped payout; no-address winner claimable; winnerId==placement[0]; finalize schema + payout-first isolation; reconnect re-key with ownership; refunds on all cancel paths; real countdown; map/timer cleanup. +16 tests. Graphics: new overlay FX layer (torch light, vignette, embers, sparkle/burst/flash/shake, self-suspending rAF); gameplay juice; atmospheric screens; CSS design-token chrome with framed modals; richer Kenney tile renderer; imported Kenney dungeon/interior sheets + fx particles. Verification: 45 suites / 312 tests pass; 40 changed JS files parse; 20 server modules load clean; no regressions.
This commit is contained in:
+23
-8
@@ -110,13 +110,24 @@ class DungeonGenerator {
|
||||
}
|
||||
|
||||
static generate(width, height, options = {}) {
|
||||
// G4: resolve the difficulty-preset overrides so the digger and treasure
|
||||
// placement honour them (getConfig applies preset + env-var overrides on top
|
||||
// of DUNGEON_CONFIGS). These become defaults that explicit `options` can override.
|
||||
const presetConfig = this.getConfig(options.cryptoType || null);
|
||||
const defaultOptions = {
|
||||
floorVariation: DUNGEON_CONFIGS.FLOOR_VARIATION,
|
||||
torchEnabled: DUNGEON_CONFIGS.TORCH_ENABLED,
|
||||
torchDensity: DUNGEON_CONFIGS.TORCH_DENSITY,
|
||||
primaryFloor: DUNGEON_CONFIGS.PRIMARY_FLOOR,
|
||||
secondaryFloor: DUNGEON_CONFIGS.SECONDARY_FLOOR,
|
||||
torchTile: DUNGEON_CONFIGS.TORCH_TILE
|
||||
torchTile: DUNGEON_CONFIGS.TORCH_TILE,
|
||||
// Preset-derived digger parameters (difficulty overrides)
|
||||
roomWidthRange: presetConfig.ROOM_WIDTH_RANGE,
|
||||
roomHeightRange: presetConfig.ROOM_HEIGHT_RANGE,
|
||||
corridorLengthRange: presetConfig.CORRIDOR_LENGTH_RANGE,
|
||||
dugPercentage: presetConfig.DUG_PERCENTAGE,
|
||||
// Preset-derived treasure room placement ratio
|
||||
roomPositionRatio: presetConfig.difficulty.treasure.roomPositionRatio
|
||||
};
|
||||
|
||||
const config = { ...defaultOptions, ...options };
|
||||
@@ -160,12 +171,14 @@ class DungeonGenerator {
|
||||
ROT.RNG.setSeed(seedInt);
|
||||
}
|
||||
|
||||
// Create dungeon using ROT.js Map.Digger
|
||||
// Create dungeon using ROT.js Map.Digger.
|
||||
// G4: honour the difficulty-preset overrides carried on config (falling back to
|
||||
// the module constants) instead of always using DUNGEON_CONFIGS.
|
||||
const digger = new ROT.Map.Digger(width, height, {
|
||||
roomWidth: DUNGEON_CONFIGS.ROOM_WIDTH_RANGE,
|
||||
roomHeight: DUNGEON_CONFIGS.ROOM_HEIGHT_RANGE,
|
||||
corridorLength: DUNGEON_CONFIGS.CORRIDOR_LENGTH_RANGE,
|
||||
dugPercentage: DUNGEON_CONFIGS.DUG_PERCENTAGE
|
||||
roomWidth: config.roomWidthRange || DUNGEON_CONFIGS.ROOM_WIDTH_RANGE,
|
||||
roomHeight: config.roomHeightRange || DUNGEON_CONFIGS.ROOM_HEIGHT_RANGE,
|
||||
corridorLength: config.corridorLengthRange || DUNGEON_CONFIGS.CORRIDOR_LENGTH_RANGE,
|
||||
dugPercentage: (config.dugPercentage !== undefined) ? config.dugPercentage : DUNGEON_CONFIGS.DUG_PERCENTAGE
|
||||
});
|
||||
|
||||
// Initialize the empty map with walls (1)
|
||||
@@ -193,10 +206,12 @@ class DungeonGenerator {
|
||||
const exitCenter = exitRoom.getCenter();
|
||||
const exit = [exitCenter[0], exitCenter[1]];
|
||||
|
||||
// Place treasure in a middle room (not first or last)
|
||||
// Place treasure in a middle room (not first or last).
|
||||
// G4: use the difficulty-aware treasure room index instead of a hardcoded midpoint.
|
||||
let treasureRoom;
|
||||
if (rooms.length > 2) {
|
||||
treasureRoom = rooms[Math.floor(rooms.length / 2)];
|
||||
const treasureIndex = getTreasureRoomIndex(rooms, config.roomPositionRatio);
|
||||
treasureRoom = rooms[treasureIndex];
|
||||
} else {
|
||||
// Fallback if we have fewer than 3 rooms
|
||||
treasureRoom = rooms[0];
|
||||
|
||||
+20
-1
@@ -319,9 +319,28 @@ class GameManager {
|
||||
const result = await db.query(`
|
||||
INSERT INTO games (user_id, socket_id, game_mode, status, start_block_height, dungeon_seed, created_at)
|
||||
VALUES ((SELECT id FROM users WHERE socket_id = $1), $2, $3, 'active', $4, $5, NOW())
|
||||
RETURNING id
|
||||
RETURNING id, user_id
|
||||
`, [socketId, socketId, gameMode, blockHeight, game.id]);
|
||||
game.dbId = result.rows[0]?.id || null;
|
||||
|
||||
// Stamp the stable DB users.id onto the in-memory game object so
|
||||
// suspend/restore and completeGame can key on it WITHOUT any socket_id
|
||||
// lookup (socket ids are volatile across reconnects). game.userId is the
|
||||
// canonical field; game.dbUserId is kept as an alias for the disconnect/
|
||||
// suspend path that already reads it.
|
||||
const stableUserId = result.rows[0]?.user_id ?? null;
|
||||
if (stableUserId != null) {
|
||||
game.userId = stableUserId;
|
||||
if (game.dbUserId == null) game.dbUserId = stableUserId;
|
||||
}
|
||||
|
||||
// Entry block height captured at creation so the suspend path can persist
|
||||
// it (blockRec) and the reconnect timeout logic stays consistent.
|
||||
game.blockRec = blockHeight;
|
||||
|
||||
// Reference to the DB pool so restoreGame can keep games.socket_id aligned
|
||||
// to the reconnecting socket without needing its own DB handle.
|
||||
game.db = db;
|
||||
} catch (err) {
|
||||
console.error('Game insert failed:', err.message);
|
||||
game.dbId = null;
|
||||
|
||||
+177
-46
@@ -803,9 +803,24 @@ class GameModeManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Process game start (deduct credits or link payment)
|
||||
* Process game start (deduct credits or link payment).
|
||||
*
|
||||
* C7: `options` is the optional trailing opts bag and may carry a `clientSeed`. The
|
||||
* dungeon's effective RNG seed is derived at game CREATION via
|
||||
* provablyFair.deriveSeed(serverSeed, clientSeed); this start entry point accepts and
|
||||
* forwards the (already-validated) client seed so the flow stays in sync and the value
|
||||
* is surfaced on the start result. Non-breaking: clientSeed is optional (defaults to '').
|
||||
*/
|
||||
async processGameStart(socketId, gameId, options = {}) {
|
||||
const clientSeed = typeof options.clientSeed === 'string' ? options.clientSeed : '';
|
||||
const result = await this._dispatchGameStart(socketId, gameId, options);
|
||||
if (result && typeof result === 'object' && clientSeed && result.clientSeed === undefined) {
|
||||
result.clientSeed = clientSeed;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async _dispatchGameStart(socketId, gameId, options = {}) {
|
||||
try {
|
||||
const user = await this.getOrCreateUser(socketId);
|
||||
|
||||
@@ -979,29 +994,63 @@ class GameModeManager {
|
||||
|
||||
async _processGameStartWithPayment(user, payment, gameId) {
|
||||
const snap = this._computePayoutSnapshot('PAID_SINGLE');
|
||||
await this.db.withTransaction(async (client) => {
|
||||
await client.query(`
|
||||
UPDATE games
|
||||
SET payment_id = $1, game_mode = 'PAID_SINGLE', payout_address = $3,
|
||||
payout_escape_amount = $4, payout_treasure_amount = $5,
|
||||
payout_escape_mult = $6, payout_treasure_mult = $7
|
||||
WHERE dungeon_seed = $2
|
||||
`, [payment.id, gameId, user.payout_address || null, snap.escapeAmount, snap.treasureAmount, snap.escapeMult, snap.treasureMult]);
|
||||
// M3: claiming a confirmed single_game payment must be atomic. Re-verify the payment
|
||||
// is still unclaimed and lock it FOR UPDATE inside the transaction, so two concurrent
|
||||
// starts can't both consume the same payment. The unique index on games.payment_id
|
||||
// (migration 023) is the final backstop; a 23505 there is treated as "already consumed".
|
||||
try {
|
||||
const result = await this.db.withTransaction(async (client) => {
|
||||
const claim = await client.query(`
|
||||
SELECT id FROM payments
|
||||
WHERE id = $1 AND status = 'confirmed' AND payment_type = 'single_game'
|
||||
AND NOT EXISTS (SELECT 1 FROM games WHERE games.payment_id = payments.id)
|
||||
FOR UPDATE
|
||||
`, [payment.id]);
|
||||
|
||||
await client.query(`
|
||||
UPDATE users
|
||||
SET total_games_played = total_games_played + 1,
|
||||
updated_at = NOW()
|
||||
WHERE id = $1
|
||||
`, [user.id]);
|
||||
});
|
||||
if (claim.rows.length === 0) {
|
||||
// Locked+re-checked: a concurrent start already linked this payment (or it is
|
||||
// no longer claimable). Abort WITHOUT linking or creating a duplicate game.
|
||||
return { alreadyConsumed: true };
|
||||
}
|
||||
|
||||
console.log(`Linked game ${gameId} to payment ${payment.id}`);
|
||||
return {
|
||||
success: true,
|
||||
paymentId: payment.id,
|
||||
effectiveMode: 'PAID_SINGLE'
|
||||
};
|
||||
await client.query(`
|
||||
UPDATE games
|
||||
SET payment_id = $1, game_mode = 'PAID_SINGLE', payout_address = $3,
|
||||
payout_escape_amount = $4, payout_treasure_amount = $5,
|
||||
payout_escape_mult = $6, payout_treasure_mult = $7
|
||||
WHERE dungeon_seed = $2
|
||||
`, [payment.id, gameId, user.payout_address || null, snap.escapeAmount, snap.treasureAmount, snap.escapeMult, snap.treasureMult]);
|
||||
|
||||
await client.query(`
|
||||
UPDATE users
|
||||
SET total_games_played = total_games_played + 1,
|
||||
updated_at = NOW()
|
||||
WHERE id = $1
|
||||
`, [user.id]);
|
||||
|
||||
return { linked: true };
|
||||
});
|
||||
|
||||
if (result.alreadyConsumed) {
|
||||
console.warn(`⚠️ Payment ${payment.id} already consumed by a concurrent start; aborting duplicate for game ${gameId}.`);
|
||||
return { success: false, reason: 'Payment already consumed', alreadyConsumed: true };
|
||||
}
|
||||
|
||||
console.log(`Linked game ${gameId} to payment ${payment.id}`);
|
||||
return {
|
||||
success: true,
|
||||
paymentId: payment.id,
|
||||
effectiveMode: 'PAID_SINGLE'
|
||||
};
|
||||
} catch (error) {
|
||||
// 23505 = unique_violation on games.payment_id: a concurrent start linked this
|
||||
// payment first. Treat as already-consumed, never an uncaught throw / duplicate.
|
||||
if (error && error.code === '23505') {
|
||||
console.warn(`⚠️ payment_id unique violation linking game ${gameId} to payment ${payment.id}; treating as already consumed.`);
|
||||
return { success: false, reason: 'Payment already consumed', alreadyConsumed: true };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// processGameCompletion() removed — dead code, never called.
|
||||
@@ -1171,7 +1220,7 @@ class GameModeManager {
|
||||
* with no live session) — `socket_id` is mutable and non-unique and must never be the
|
||||
* primary money key.
|
||||
*/
|
||||
async getOrCreateUser(socketId, ipAddress = null) {
|
||||
async getOrCreateUser(socketId, { create = true } = {}) {
|
||||
try {
|
||||
// 1. Prefer the stable identity established by SessionManager (anon_token -> id).
|
||||
if (this.sessionManager && typeof this.sessionManager.getBySocket === 'function') {
|
||||
@@ -1205,6 +1254,12 @@ class GameModeManager {
|
||||
return userResult.rows[0];
|
||||
}
|
||||
|
||||
// Read-only callers (REST reads) pass { create: false }: never mint an orphan
|
||||
// row for a socket that has no user yet — return null and let them 404/handle it.
|
||||
if (!create) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 3. Last resort: create a new user. This only fires when no session was
|
||||
// established for the socket (abnormal in normal flow, since every connection
|
||||
// creates a session) — log it so orphan-row creation is visible.
|
||||
@@ -1212,7 +1267,7 @@ class GameModeManager {
|
||||
INSERT INTO users (socket_id, ip_address)
|
||||
VALUES ($1, $2)
|
||||
RETURNING *
|
||||
`, [socketId, ipAddress]);
|
||||
`, [socketId, null]);
|
||||
|
||||
console.warn(`👤 Created new user without a session (socket ${socketId}) — no anon_token identity resolved.`);
|
||||
return userResult.rows[0];
|
||||
@@ -1374,6 +1429,29 @@ class GameModeManager {
|
||||
};
|
||||
}
|
||||
|
||||
/** Fire-and-forget operator alert (no-op when no alertService is wired). */
|
||||
_alert(type, subject, html) {
|
||||
if (this.alertService && typeof this.alertService.sendAlert === 'function') {
|
||||
this.alertService.sendAlert(type, { subject, html }).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the configured payout min/max (atomic BigInt) for a recorded game mode from
|
||||
* the runtime config snapshot, or null when unavailable (older config / no snapshot).
|
||||
*/
|
||||
_payoutBoundsForMode(recordedPaymentMode) {
|
||||
const rules = this.configSnapshot?.payouts?.rules;
|
||||
if (!rules) return null;
|
||||
const rule = recordedPaymentMode === 'PAID_CREDITS' ? rules.credits : rules.direct;
|
||||
if (!rule) return null;
|
||||
const toBigOrNull = (v) => {
|
||||
if (v === undefined || v === null) return null;
|
||||
try { return money.toBig(v); } catch { return null; }
|
||||
};
|
||||
return { min: toBigOrNull(rule.minPayout), max: toBigOrNull(rule.maxPayout) };
|
||||
}
|
||||
|
||||
/**
|
||||
* Complete a game (called from socket handlers when game ends)
|
||||
* @param {string} socketId - Player's socket ID
|
||||
@@ -1389,22 +1467,27 @@ class GameModeManager {
|
||||
}
|
||||
|
||||
try {
|
||||
// Retrieve the game record to get the actual payment_mode used at game start
|
||||
// Retrieve the game record to get the actual payment_mode used at game start.
|
||||
// M4/C4: match by dungeon_seed ALONE (the UUID is unique) — never by the mutable
|
||||
// socket_id — and carry the stable users.id (games.user_id) so the paying user is
|
||||
// resolved by identity, not by the socket that happened to finish the game.
|
||||
let recordedPaymentMode = this.gameMode; // fallback to global
|
||||
let gameDbId = null;
|
||||
let gameUserId = null; // stable users.id carried on the game row
|
||||
let lockedPayoutAddress = null; // address locked at game start
|
||||
let payoutSnapshot = null; // payout terms snapshotted at game start
|
||||
try {
|
||||
const gameRecord = await this.db.query(`
|
||||
SELECT id, game_mode, payout_address,
|
||||
SELECT id, user_id, game_mode, payout_address,
|
||||
payout_escape_amount, payout_treasure_amount,
|
||||
payout_escape_mult, payout_treasure_mult
|
||||
FROM games
|
||||
WHERE dungeon_seed = $1 AND socket_id = $2
|
||||
WHERE dungeon_seed = $1
|
||||
LIMIT 1
|
||||
`, [gameId, socketId]);
|
||||
`, [gameId]);
|
||||
if (gameRecord.rows.length > 0) {
|
||||
gameDbId = gameRecord.rows[0].id;
|
||||
gameUserId = gameRecord.rows[0].user_id ?? null;
|
||||
recordedPaymentMode = (gameRecord.rows[0].game_mode || this.gameMode).toUpperCase();
|
||||
lockedPayoutAddress = gameRecord.rows[0].payout_address || null;
|
||||
const r = gameRecord.rows[0];
|
||||
@@ -1428,14 +1511,13 @@ class GameModeManager {
|
||||
try {
|
||||
await this.db.query(`
|
||||
UPDATE games SET status = $1, treasure_found = $2, moves_made = COALESCE($3, moves_made), duration_seconds = COALESCE($4, duration_seconds), completed_at = NOW()
|
||||
WHERE dungeon_seed = $5 AND socket_id = $6
|
||||
WHERE dungeon_seed = $5
|
||||
`, [
|
||||
won ? 'won' : 'lost',
|
||||
treasureFound,
|
||||
metrics.moves ?? null,
|
||||
metrics.durationSeconds ?? null,
|
||||
gameId,
|
||||
socketId
|
||||
gameId
|
||||
]);
|
||||
} catch (e) {
|
||||
// Non-fatal if games table differs during early dev.
|
||||
@@ -1450,6 +1532,14 @@ class GameModeManager {
|
||||
const payoutEligibleStartMode = (recordedPaymentMode === 'PAID_SINGLE' && this.directPayoutEnabled)
|
||||
|| (recordedPaymentMode === 'PAID_CREDITS' && this.creditsPayoutEnabled);
|
||||
if (payoutEligibleStartMode && won) {
|
||||
// M4/C4: never insert a payout without a resolved game row — a null game_id
|
||||
// would defeat the payouts dedup (game_id) guard and could double-pay.
|
||||
if (!gameDbId) {
|
||||
console.error(`❌ completeGame: no game row for dungeon_seed ${gameId}; refusing to insert a payout.`);
|
||||
this._alert('payout_unresolved_game', '⚠️ Payout skipped — game row not found', `<p>completeGame could not resolve a games row for dungeon_seed <b>${gameId}</b> (socket ${socketId}). No payout was inserted.</p>`);
|
||||
return { success: true, mode: recordedPaymentMode, payout: null, reason: 'Game row not resolved', score: metrics.score ?? null };
|
||||
}
|
||||
|
||||
// Use the payout terms snapshotted at game start (so a mid-game config change
|
||||
// can't alter an in-flight payout). Fall back to live calculation only for
|
||||
// older games that predate the snapshot columns.
|
||||
@@ -1464,21 +1554,51 @@ class GameModeManager {
|
||||
multiplier = live.multiplier;
|
||||
}
|
||||
|
||||
// Check if payout was already processed for this game (prevent duplicates)
|
||||
if (gameDbId) {
|
||||
const existingPayout = await this.db.query(`
|
||||
SELECT id FROM payouts WHERE game_id = $1 AND status IN ('pending', 'processing', 'completed')
|
||||
`, [gameDbId]);
|
||||
if (existingPayout.rows.length > 0) {
|
||||
console.log(`⚠️ Payout already exists for game ${gameId}, skipping duplicate`);
|
||||
return { success: true, mode: recordedPaymentMode, payout: null, reason: 'Payout already processed', score: metrics.score ?? null };
|
||||
// M5: clamp/validate the payout against the configured min/max for this mode.
|
||||
// Over-max -> cap + alert (never send more than policy allows). Under-min ->
|
||||
// skip entirely (dust not worth an on-chain fee). Bounds are atomic BigInt.
|
||||
const bounds = this._payoutBoundsForMode(recordedPaymentMode);
|
||||
if (bounds) {
|
||||
let amtBig = null;
|
||||
try { amtBig = money.toBig(payoutAmount); } catch { amtBig = null; }
|
||||
if (amtBig !== null) {
|
||||
if (bounds.max !== null && amtBig > bounds.max) {
|
||||
console.warn(`⚠️ Payout ${amtBig} for game ${gameId} exceeds configured max ${bounds.max}; capping.`);
|
||||
this._alert('payout_over_max', '⚠️ Payout capped at configured maximum', `<p>Computed payout <b>${amtBig}</b> for game <b>${gameId}</b> exceeded the configured max <b>${bounds.max}</b> and was capped. Mode: ${recordedPaymentMode}.</p>`);
|
||||
amtBig = bounds.max;
|
||||
payoutAmount = amtBig.toString();
|
||||
}
|
||||
if (bounds.min !== null && amtBig < bounds.min) {
|
||||
console.log(`Payout ${amtBig} for game ${gameId} below configured min ${bounds.min}; skipping payout.`);
|
||||
return { success: true, mode: recordedPaymentMode, payout: null, reason: 'Below minimum payout', score: metrics.score ?? null };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Use payout address locked at game start, fall back to current user address
|
||||
const userResult = await this.db.query(`SELECT * FROM users WHERE socket_id = $1 LIMIT 1`, [socketId]);
|
||||
const userRow = userResult.rows[0];
|
||||
const payoutAddress = lockedPayoutAddress || (userRow && userRow.payout_address);
|
||||
// Check if payout was already processed for this game (prevent duplicates)
|
||||
const existingPayout = await this.db.query(`
|
||||
SELECT id FROM payouts WHERE game_id = $1 AND status IN ('pending', 'processing', 'completed')
|
||||
`, [gameDbId]);
|
||||
if (existingPayout.rows.length > 0) {
|
||||
console.log(`⚠️ Payout already exists for game ${gameId}, skipping duplicate`);
|
||||
return { success: true, mode: recordedPaymentMode, payout: null, reason: 'Payout already processed', score: metrics.score ?? null };
|
||||
}
|
||||
|
||||
// M4/C4: resolve the paying user by the STABLE users.id carried on the game
|
||||
// row (games.user_id), never by the mutable socket that finished the game.
|
||||
let userRow = null;
|
||||
if (gameUserId != null) {
|
||||
const userResult = await this.db.query(`SELECT * FROM users WHERE id = $1 LIMIT 1`, [gameUserId]);
|
||||
userRow = userResult.rows[0] || null;
|
||||
}
|
||||
if (!userRow) {
|
||||
console.error(`❌ completeGame: could not resolve paying user (game user_id=${gameUserId}) for game ${gameId}; refusing payout.`);
|
||||
this._alert('payout_unresolved_user', '⚠️ Payout skipped — paying user not resolved', `<p>completeGame could not resolve the paying user (games.user_id=<b>${gameUserId}</b>) for game <b>${gameId}</b>. No payout was inserted.</p>`);
|
||||
return { success: true, mode: recordedPaymentMode, payout: null, reason: 'Paying user not resolved', score: metrics.score ?? null };
|
||||
}
|
||||
|
||||
// Use payout address locked at game start, fall back to the user's address
|
||||
const payoutAddress = lockedPayoutAddress || userRow.payout_address;
|
||||
if (userRow && payoutAddress) {
|
||||
// Create pending payout record, then schedule batch processing.
|
||||
// Multiple wins within 5 seconds are batched into one transfer_split call,
|
||||
@@ -1643,14 +1763,25 @@ class GameModeManager {
|
||||
console.log(`💸 Single payout ${p.id} completed: ${result.txHash}`);
|
||||
} catch (err) {
|
||||
// Insufficient-unlocked-funds is a pre-broadcast error -> retry on the next
|
||||
// batch run (revert to pending). Other failures -> 'failed' (retry service,
|
||||
// which re-checks the chain before re-sending).
|
||||
const status = isInsufficientFundsError(err.message) ? 'pending' : 'failed';
|
||||
// batch run (revert to pending). ANY OTHER error is ambiguous: processPayout
|
||||
// (transfer_split) may have broadcast on-chain even though the RPC response
|
||||
// errored, and this row has a null tx_hash so the retry service's blockchain
|
||||
// guard can't protect it — an auto-retry could DOUBLE-PAY. Mark 'needs_review'
|
||||
// (retry service skips it) + alert, mirroring the batch path below.
|
||||
const fundsIssue = isInsufficientFundsError(err.message);
|
||||
const status = fundsIssue ? 'pending' : 'needs_review';
|
||||
console.error(`❌ Single payout ${p.id} failed -> ${status}:`, err.message);
|
||||
await this.db.query(
|
||||
`UPDATE payouts SET status = $1, last_error = $2, last_retry_at = NOW() WHERE id = $3`,
|
||||
[status, err.message, p.id]
|
||||
).catch(() => {});
|
||||
if (!fundsIssue && this.alertService && typeof this.alertService.sendAlert === 'function') {
|
||||
this.alertService.sendAlert('single_payout_failed', {
|
||||
subject: '⚠️ Payout failed — manual review required',
|
||||
html: `<p>Single payout <b>${p.id}</b> failed and was marked <b>needs_review</b> to avoid a possible double-payout.</p>`
|
||||
+ `<p>Amount: ${p.amount} → ${p.payout_address}</p><p>Error: ${err.message}</p>`
|
||||
}).catch(() => {});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Multiple payouts — batch via transfer_split with multiple destinations
|
||||
|
||||
+45
-13
@@ -52,6 +52,24 @@ function verifySeed(seed, commitment) {
|
||||
return hashSeed(seed) === commitment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Derive the effective RNG seed from a server seed and an (optional) client seed.
|
||||
* Uses HMAC-SHA256 with the server seed as key and the client seed as message so
|
||||
* that neither party can unilaterally control the outcome:
|
||||
* - the server commits to hash(serverSeed) BEFORE seeing the client seed, and
|
||||
* - the client seed still perturbs the final layout.
|
||||
* The commitment stays hash(serverSeed); a verifier with (serverSeed, clientSeed)
|
||||
* can reproduce this derived seed and therefore the entire dungeon.
|
||||
* @param {string} serverSeed - The secret server seed (revealed after the game)
|
||||
* @param {string} [clientSeed=''] - Optional client-supplied seed
|
||||
* @returns {string} 64-character hex string used as the effective RNG seed
|
||||
*/
|
||||
function deriveSeed(serverSeed, clientSeed = '') {
|
||||
return crypto.createHmac('sha256', String(serverSeed))
|
||||
.update(String(clientSeed ?? ''))
|
||||
.digest('hex');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a seeded random number generator
|
||||
* Uses the seed to create deterministic "random" numbers
|
||||
@@ -73,9 +91,10 @@ function createSeededRNG(seed) {
|
||||
.digest();
|
||||
counter++;
|
||||
|
||||
// Convert first 4 bytes to a number between 0 and 1
|
||||
// Convert first 4 bytes to a number in [0, 1). Divide by 2^32 (not
|
||||
// 0xFFFFFFFF) so the maximum value stays strictly below 1.
|
||||
const num = hash.readUInt32BE(0);
|
||||
return num / 0xFFFFFFFF;
|
||||
return num / 0x100000000;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -121,16 +140,21 @@ function seededShuffle(rng, array) {
|
||||
/**
|
||||
* Create a game proof object for a new game
|
||||
* @param {string} gameId - Unique game identifier
|
||||
* @returns {object} Proof object with seed, commitment, and creation time
|
||||
* @param {string} [clientSeed=''] - Optional client-supplied seed (non-breaking)
|
||||
* @returns {object} Proof object with seeds, commitment, and creation time
|
||||
*/
|
||||
function createGameProof(gameId) {
|
||||
const seed = generateSeed();
|
||||
const commitment = hashSeed(seed);
|
||||
function createGameProof(gameId, clientSeed = '') {
|
||||
const serverSeed = generateSeed();
|
||||
const commitment = hashSeed(serverSeed); // commits to serverSeed only
|
||||
// Effective RNG seed folds in the client seed so neither party controls it.
|
||||
const seed = deriveSeed(serverSeed, clientSeed);
|
||||
const timestamp = Date.now();
|
||||
|
||||
|
||||
return {
|
||||
gameId,
|
||||
seed, // Keep secret until game ends
|
||||
serverSeed, // Keep secret until game ends
|
||||
clientSeed, // Client contribution (may be '')
|
||||
seed, // Derived effective seed used to generate the dungeon
|
||||
commitment, // Show to player before game starts
|
||||
timestamp,
|
||||
verified: false
|
||||
@@ -160,9 +184,15 @@ function getPreGameCommitment(proof) {
|
||||
* @returns {object} Full verification data for client
|
||||
*/
|
||||
function getPostGameReveal(proof, gameResult = {}) {
|
||||
const serverSeed = proof.serverSeed ?? proof.seed;
|
||||
const clientSeed = proof.clientSeed ?? '';
|
||||
// Effective seed a verifier regenerates the dungeon from.
|
||||
const effectiveSeed = proof.seed ?? deriveSeed(serverSeed, clientSeed);
|
||||
return {
|
||||
gameId: proof.gameId,
|
||||
seed: proof.seed,
|
||||
serverSeed,
|
||||
clientSeed,
|
||||
seed: effectiveSeed,
|
||||
commitment: proof.commitment,
|
||||
timestamp: proof.timestamp,
|
||||
gameResult: {
|
||||
@@ -171,12 +201,13 @@ function getPostGameReveal(proof, gameResult = {}) {
|
||||
moves: gameResult.moves ?? 0,
|
||||
duration: gameResult.duration ?? 0
|
||||
},
|
||||
verificationUrl: `/verify/${proof.seed}`,
|
||||
verificationUrl: `/verify/${effectiveSeed}`,
|
||||
instructions: [
|
||||
"To verify this game was fair:",
|
||||
`1. Compute SHA-256 of the seed and confirm it equals the commitment: ${proof.commitment}`,
|
||||
"2. The seed deterministically generates the dungeon layout, treasure, and monster behaviour",
|
||||
`3. Open ${`/verify/${proof.seed}`} to regenerate the dungeon from the seed and compare its layout fingerprint`
|
||||
`1. Compute SHA-256 of the serverSeed and confirm it equals the commitment: ${proof.commitment}`,
|
||||
"2. Compute HMAC-SHA256(key=serverSeed, msg=clientSeed) to derive the effective seed",
|
||||
"3. The effective seed deterministically generates the dungeon layout, treasure, and monster behaviour",
|
||||
`4. Open ${`/verify/${effectiveSeed}`} to regenerate the dungeon from the effective seed and compare its layout fingerprint`
|
||||
]
|
||||
};
|
||||
}
|
||||
@@ -207,6 +238,7 @@ module.exports = {
|
||||
generateSeed,
|
||||
hashSeed,
|
||||
verifySeed,
|
||||
deriveSeed,
|
||||
createSeededRNG,
|
||||
seedToInt,
|
||||
seededRandomInt,
|
||||
|
||||
Reference in New Issue
Block a user