Skip to content

fix(worker): ensure idempotent stream completion and event replay handling - #1951

Open
king-daveed wants to merge 2 commits into
LabsCrypt:mainfrom
king-daveed:fix/tokens-withdrawn-idempotency-802
Open

king-daveed wants to merge 2 commits into
LabsCrypt:mainfrom
king-daveed:fix/tokens-withdrawn-idempotency-802

Conversation

@king-daveed

Copy link
Copy Markdown
Contributor

Summary

This PR addresses and resolves #802 by auditing and guaranteeing end-to-end event replay idempotency in the Soroban event indexing subsystem:

  1. Stream Event Replay Idempotency:

    • In backend/src/workers/soroban-event-worker.ts, guarded handleStreamCompleted with an upfront existingEvent duplicate check before executing tx.stream.update, preventing redundant stream mutations on duplicate or replayed events.
    • Audited handleStreamCancelled and handleTokensWithdrawn to ensure all event handlers check duplicate state in the same transaction as stream mutations.
    • Updated documentation in backend/src/services/indexerService.ts confirming that replay from ledger is fully idempotent across all event types.
  2. Testing & Coverage:

    • Added unit test in backend/tests/soroban-event-worker.test.ts verifying that stream_completed event replay does not re-invoke tx.stream.update or duplicate stream events.
    • Verified that tokens_withdrawn and stream_topped_up duplicate checks continue to prevent double-incrementing withdrawnAmount and depositedAmount.
    • Cleaned up pre-existing linter warnings in tests/token-registry.service.test.ts and tests/webhook-auth.test.ts.

Closes #802

king-daveed and others added 2 commits November 24, 2026 14:33
The stream_completed handler checked for the duplicate StreamEvent row before
updating the stream, which broke the documented replay contract from LabsCrypt#1763: a
replayed completion must re-apply the idempotent stream update, surface P2025
when the Stream row is gone, and still broadcast. Restore the update-first
ordering and assert the replay behavior in the handler test.
@king-daveed
king-daveed force-pushed the fix/tokens-withdrawn-idempotency-802 branch from 84de818 to 6251b50 Compare November 24, 2026 14:46

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Backend] handleTokensWithdrawn increments withdrawnAmount before the dedup guard - replay/reset double-counts and corrupts claimable

2 participants