diff options
| author | Adam Soltys <[email protected]> | 2026-06-13 14:21:44 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-06-13 14:21:44 -0700 |
| commit | ca28c19fe26d3f81ae62d2d3f0dc8b175849a9a8 (patch) | |
| tree | 2be3077a7e59ce1645b9209b52d507735a8c55d5 /lib/payments.ts | |
| parent | 43427afe5b0b16d887163bd788360ac29105d69c (diff) | |
| download | coinos-server-ca28c19fe26d3f81ae62d2d3f0dc8b175849a9a8.tar.gz coinos-server-ca28c19fe26d3f81ae62d2d3f0dc8b175849a9a8.zip | |
payments: guard /replay double-credit race with SET NX idempotency
getPayment() pre-check in the lightning listener/replay isn't atomic, so
concurrent re-credits of the same settlement each ran incrBy. Claim each
lightning/bolt12 settlement with an atomic SET NX on credited:<ref>
(ref = preimage) and bail if already claimed. Also disable the /replay
route that triggered it.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Diffstat (limited to 'lib/payments.ts')
| -rw-r--r-- | lib/payments.ts | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/payments.ts b/lib/payments.ts index aeebcc48..30403b6c 100644 --- a/lib/payments.ts +++ b/lib/payments.ts @@ -342,6 +342,21 @@ export const credit = async ({ await sa(`payment:${hash}`, id); } + // Idempotency guard for external settlements. The getPayment() pre-check in + // the lightning listener/replay is not atomic: N concurrent re-credits of the + // same payment all read it as uncredited before any writes the pointer, so + // each runs the incrBy below — the /replay double-credit race. SET NX is + // atomic, so only the first caller claims the settlement and credits; the rest + // bail here, before the incrBy. `ref` is the lightning preimage, unique per + // settlement; internal/fund/ecash pass ref=uid and are exempt. + if ([PaymentType.lightning, PaymentType.bolt12].includes(type) && ref) { + const claimed = await db.set(`credited:${ref}`, id, { NX: true }); + if (!claimed) { + warn("duplicate credit blocked", hash, ref); + return; + } + } + const m = await db.multi(); let creditType = type; |
