Repository navigation
fix(withdraw): gate balance increment on event INSERT rowcount to pre… - #1410
Open
Martinsbab wants to merge 7 commits into
Open
Martinsbab wants to merge 7 commits into
Martinsbab wants to merge 7 commits into
Conversation
…vent double-counting
The withdraw handler atomically incremented withdrawnAmount on every request,
regardless of whether the corresponding StreamEvent already existed. Because
sorobanWithdraw returns a deterministic txHash ('simulated-withdraw-' +
streamId), retries and concurrent duplicate requests would each re-increment
the balance — a real fund-accounting bug.
Move the StreamEvent creation inside the $transaction using a conditional
INSERT with WHERE NOT EXISTS + RETURNING. The INSERT's rowcount (1 = new,
0 = duplicate) gates the balance UPDATE, guaranteeing exactly-once accounting
per claimable window. This is race-condition-safe because the unique
constraint on (transactionHash, eventType) prevents duplicate events at the
database level, and the rowcount is evaluated within the same transaction.
Updates existing tests in eventRace.test.ts and withdraw.handler.test.ts,
and adds two new integration tests proving idempotent withdrawal behavior.
Closes LabsCrypt#1216
🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
…elope Restore the withdraw handler idempotency tests to match the constitent error envelope produced by sendApiError (code + message) and the PR's 'gates balance increment on event INSERT rowcount' integration test (recover a missing firstClaimed declaration and replace the response-amount double-counting assertion, which can never be less than the full claimable amount, with an assertion that the DB balance is unchanged on a duplicate). Closes LabsCrypt#1216 LabsCrypt#1410 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
The Prisma schema defined IndexerDeadLetterEvent twice (lines 71 and 164),
causing to fail with P1012 ('model already exists'). The
second definition (with eventType/txHash/ledgerSequence/payload) supersedes
the first, so the duplicate is removed while keeping the complete model
so the Prisma client and npm run build work.
This unblocks the Backend CI and Backend Docker Image CI build steps.
Co-Authored-By: Codebuff <noreply@codebuff.com>
The simulation rewrite (LabsCrypt#1847) removed the balance-writing SQL but left the test's raw-SQL mock referencing a deleted `insertedHashes` set, and dropped the `nowSec` declaration that a later assertion still uses. Both broke `tsc` in Backend CI. Remove the unreachable INSERT/UPDATE mock (the route never runs raw SQL, and the test asserts that), declare `nowSec` once per test, and set the claimable mock explicitly in the three tests that need it. `vi.clearAllMocks()` does not clear return values, so the 409 case's `actionable: false` was leaking into later tests and turning expected 200s into 409s. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes an accounting bug where duplicate or retried withdrawal requests could increment
withdrawnAmountmore than once for the same claimable withdrawal.Problem
The withdrawal flow uses a deterministic transaction hash for simulated withdrawals. The
StreamEventupsert correctly handles duplicate events by treating an existing event as a no-op.However, the adjacent
prisma.stream.updatewas not protected by the same idempotency check. As a result, retrying the same withdrawal request could incrementwithdrawnAmountrepeatedly even though the withdrawal event had already been recorded.Changes
withdrawnAmount.Testing
withdrawnAmountonly once.Issue
Closes #1216