fix(withdraw): gate balance increment on event INSERT rowcount to pre… - #1410
Open
Martinsbab wants to merge 1 commit into
Open
fix(withdraw): gate balance increment on event INSERT rowcount to pre…#1410Martinsbab wants to merge 1 commit into
Martinsbab wants to merge 1 commit 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>
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