flowfi icon indicating copy to clipboard operation
flowfi copied to clipboard

[Backend] Fix top-up double-write race between API handler and event indexer

Open blurbeast opened this issue 2 weeks ago • 3 comments

Overview & Background

depositedAmount has two independent writers applying the same top-up. After topUpStream(...) returns, the API handler runs UPDATE "Stream" SET "depositedAmount" = ("depositedAmount" + $1) ... (increment). Independently the Soroban event worker's handleStreamToppedUp sets depositedAmount to the absolute on-chain value. The handler writes no TOPPED_UP event, so the worker's transactionHash_eventType dedup offers no protection. Since pollTransactionStatus confirms the tx before the handler's increment, the indexer can commit first and the handler then adds the amount on top — permanently overcounting depositedAmount and drifting endTime projections.


Technical Specification & Architecture

  1. Make exactly one writer authoritative:
    • Option A (preferred): the indexer (backend/src/workers/soroban-event-worker.ts:926-983) owns depositedAmount from the event; the API handler (backend/src/controllers/stream.controller.ts:709-722) only reports txHash.
    • Option B: make the handler's update conditional on the event row not existing yet, inside one transaction.
  2. If the handler must write, emit a TOPPED_UP event so the existing dedup key (transactionHash_eventType) covers it.
  3. Reconcile any already-overcounted rows (one-off repair query or re-derive from event log).

Target Files

  • backend/src/controllers/stream.controller.ts
  • backend/src/workers/soroban-event-worker.ts
  • backend/src/services/sorobanService.ts

Acceptance Criteria

  • [ ] A single top-up increments depositedAmount exactly once regardless of indexer/API ordering.
  • [ ] Integration test simulating both writers arriving in both orders passes.
  • [ ] Data repair plan (or migration) documented for existing overcounted streams.

blurbeast avatar Nov 18 '26 19:11 blurbeast

@eazeeshow has applied to work on this issue as part of the Stellar Wave Program's 11th wave.

Hi maintainer, I can get this issue fixed quickly. Please assign it to me

ℹ️ Repo Maintainers: To accept this application, review their application or assign @eazeeshow to this issue.

eazeeshow avatar Nov 21 '26 12:11 eazeeshow

Congratulations, @eazeeshow! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on November 25, 2026.

🧑‍💻 @eazeeshow: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

[!WARNING] When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

🌊 Happy Wave 🌊

drips-wave[bot] avatar Nov 21 '26 12:11 drips-wave[bot]

This issue has been completed by @eazeeshow as part of the Stellar Wave Program's 11th Wave 🥳

😎 @eazeeshow: You earned 200 Points for completing this issue! Once the Wave's rewards are finalized, you'll be eligible for a percentage of the Wave's reward pool based on the percentage of total points you've earned. Learn more here. You can also Leave a review to share your experience working on this issue.

🧑‍💻 Repo maintainers: How'd the contributor do? Leave a review to share your experience working with them.

drips-wave[bot] avatar Nov 21 '26 16:11 drips-wave[bot]