Use an authorised shared lock for Exchange delivery reads
All checks were successful
DuneRealms Web CI / web (push) Successful in 6m38s

This commit is contained in:
unimatrixgit 2026-12-06 13:50:23 +01:00
commit 6c525f8a64
2 changed files with 21 additions and 1 deletions

View file

@ -38,7 +38,8 @@ export function createGameExchangeService(bank,{gameSchema,authority,resolveSell
// Bank entries, stock, sales history and the native delivery receipt commit on
// the same MySQL server. No cross-database compensation or wallet mirror.
return bank.transaction(async tx=>{
const prior=await tx.first('SELECT * FROM '+deliveries+' WHERE id=? FOR UPDATE',[input.requestId]);
// Postbank may read and insert deliveries; only the native consumer updates them.
const prior=await tx.first('SELECT * FROM '+deliveries+' WHERE id=? FOR SHARE',[input.requestId]);
if(prior){
if(prior.buyer_uuid!==player||prior.buyer_account_id!==actor.id||String(prior.listing_id)!==input.listingId||Number(prior.quantity)!==input.amount||Number(prior.unit_minor)!==input.priceMinor)throw new BankError('This purchase reference was already used.',409);
const paid=await tx.first('SELECT state,fingerprint FROM pb_orders WHERE id=?',[input.requestId]);

View file

@ -55,3 +55,22 @@ test('a checkout offer with a stale unit price or quantity moves no stock or fun
assert.equal((await f.db.first('SELECT COUNT(*) n FROM dunegameplay.dune_exchange_deliveries')).n,0);
}finally{f.db.close();}
});
test('checkout replays through a read/insert-only native delivery grant',async()=>{
const f=await fixture();
try{
const query=f.db.query.bind(f.db),locks=[];
f.db.query=async(sql,args)=>{
if(/^SELECT .*dune_exchange_deliveries.* FOR (?:SHARE|UPDATE)$/.test(sql)){
if(sql.endsWith('FOR UPDATE'))throw Error('MySQL 1142: delivery update privilege required');
locks.push(sql);
}
return query(sql,args);
};
const input=f.input();
assert.equal((await f.service.buy(f.users[0],f.users[0].uuid,input)).state,'queued');
assert.equal((await f.service.buy(f.users[0],f.users[0].uuid,input)).replayed,true);
assert.equal(locks.length,2);
assert.equal((await f.db.first('SELECT COUNT(*) n FROM dunegameplay.dune_exchange_deliveries')).n,1);
}finally{f.db.close();}
});