fix(withdraw): gate balance increment on event INSERT rowcount to prevent double-counting - #1
Open
freebuff-web[bot] wants to merge 1 commit into
Open
fix(withdraw): gate balance increment on event INSERT rowcount to prevent double-counting#1freebuff-web[bot] wants to merge 1 commit into
freebuff-web[bot] 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.
Problem
The withdraw handler (
backend/src/routes/v1/streams/withdraw.ts) atomically incrementedwithdrawnAmounton every request, regardless of whether the correspondingStreamEventalready existed. BecausesorobanWithdrawreturns a deterministictxHash('simulated-withdraw-' + streamId), retries and concurrent duplicate requests would each re-increment the balance — a real fund-accounting bug.Root cause: The
StreamEventupsert (which was idempotent via the unique constraint on(transactionHash, eventType)) happened after the blind balance increment. A retry would:withdrawnAmountagain (no guard)This means
withdrawnAmountwas double-counted on every retry.Solution
Move the
StreamEventcreation inside the$transactionusing a conditionalINSERT ... WHERE NOT EXISTS ... RETURNING. The INSERT's rowcount (1= new event,0= duplicate) gates the balanceUPDATE, guaranteeing exactly-once accounting per claimable window.This is race-condition-safe because:
(transactionHash, eventType)prevents duplicate events at the database levelFiles Changed
backend/src/routes/v1/streams/withdraw.ts— Core fix: moved event creation inside transaction, gated balance increment on INSERT rowcountbackend/tests/withdraw.handler.test.ts— Updated existing test + added new idempotency testbackend/tests/eventRace.test.ts— Updated race condition test to verify new INSERT-based approachbackend/tests/integration/streams/withdraw.test.ts— Updated existing tests + added two new integration tests proving idempotent withdrawalTests Added
withdraw.handler.test.ts): "should not increment withdrawnAmount when the event already exists (idempotent)" — proves the balance stays at 150 (not 200) when a duplicate request arriveseventRace.test.ts): "skips balance increment when event already exists (worker race condition)": proves that when the worker creates the event first, the handler's balance increment is skippedValidation
tsc --noEmit)Closes LabsCrypt#1216
🤖 Generated with Codebuff
Co-Authored-By: Codebuff noreply@codebuff.com