Reserve native raid delivery capacity before any debit

This commit is contained in:
unimatrixgit 2026-11-29 16:33:17 +01:00
commit 4af783012b

View file

@ -32,6 +32,14 @@ export function createImperialRaidOrders(db,{resolveActor,resolveContext,charge,
return work(tx);
});}
const order=(tx,id)=>tx.first('SELECT * FROM jarvis_imperial_raid_orders WHERE id=? FOR UPDATE',[id]);
async function pollOrders(store,server,{candidate=null,lock=false}={}){
const rows=await store.query("SELECT accepted_json FROM jarvis_imperial_raid_orders WHERE state IN ('accepted','warning','started') ORDER BY created_at,id LIMIT 501"+(lock?' FOR UPDATE':''));
if(rows.length>500)fail('The active raid register requires reconciliation.');
const orders=rows.map(r=>parse(r.accepted_json)).filter(p=>p?.server===server);if(candidate)orders.push(candidate);
const response={ok:true,schema:1,orders};
if(orders.length>100||Buffer.byteLength(JSON.stringify(response),'utf8')>262144)fail('This server’s raid delivery register is full. Resolve existing raids before another paid order.');
return response;
}
async function bind(tx,input){
const actor=await resolveActor(tx,input.uuid,{lock:true});if(!actor?.id)fail('Link an active personal Minecraft account first.',403);
const row=await order(tx,input.requestId),hash=imperialRaidInputHash(input);
@ -109,6 +117,9 @@ export function createImperialRaidOrders(db,{resolveActor,resolveContext,charge,
}catch(error){if(!error.status)throw error;return rejectPrepared(tx,row,error.message);}
// Do not catch posting failures inside this transaction: a timeout, partial
// write or failed key insert must roll EVERYTHING back, leaving the same intent.
// Reserve actual native delivery capacity BEFORE any debit. Both UUID fields
// below have the exact final serialized size; no silent page/truncation.
await pollOrders(tx,input.server,{candidate:{...p,receiptKeyId:p.preflightNonce,bankOrderId:p.id,acceptedAt:now},lock:true});
const receipt=await charge(tx,{...p,actorId:row.actor_id});
if(!/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(receipt?.receiptKeyId||'')||receipt.bankOrderId!==row.id)throw Error('Original atomic paid raid receipt required');
const resultOrder={...p,receiptKeyId:receipt.receiptKeyId,bankOrderId:receipt.bankOrderId,acceptedAt:now};
@ -161,9 +172,7 @@ export function createImperialRaidOrders(db,{resolveActor,resolveContext,charge,
return {ok:true,schema:1,context:{actorUuid:input.uuid,orderingClanId:input.orderingClanId,targetClanId:input.targetClanId,orderingClanName:c.orderingClanName,targetClanName:c.targetClanName,server:input.server,accounts:q.accounts,uniqueSourceAccountId:q.accounts.length===1?String(q.accounts[0].id):null,feeMinor:q.feeMinor,currency:'DM',chainId:q.chainId,retaliationLevel:q.level,home:c.home,policy:publicPolicy()}};
}
if(input.action==='poll'){
const rows=await db.query("SELECT accepted_json FROM jarvis_imperial_raid_orders WHERE state IN ('accepted','warning','started') ORDER BY created_at,id LIMIT 501");
if(rows.length>500)fail('The active raid register requires reconciliation.');
return {ok:true,schema:1,orders:rows.map(r=>parse(r.accepted_json)).filter(p=>p?.server===input.server)};
return pollOrders(db,input.server);
}
return locked(async tx=>{
if(input.action==='prepare')return prepare(tx,input);