Skip to content

assignReward decrements MaintenancePool.balance before poolWithdraw() and never restores it when the withdrawal fails #459

Description

@chonilius

Problem

In MaintenancePoolService.assignReward(), the #274 atomic check-and-decrement runs before the on-chain payout:

const updateResult = await this.poolRepo
  .createQueryBuilder()
  .update(MaintenancePool)
  .set({ balance: () => `balance - :amount` })
  .where('id = :id AND balance >= :amount', { id, amount: Number(amount) })
  ...
  .execute();
if (updateResult.affected === 0) throw new BadRequestException(...);

const payment = await this.escrowService.poolWithdraw(pool.escrowId, amount, recipientAddress, recipientId);
return payment;

poolWithdraw() has several ways to throw after the balance has already been reduced:

  • assertLocked(escrow)
  • assertValidAmount(amount)
  • assertRecipientsMatchUsers(...): a recipientAddress that doesn't match recipientId's wallet
  • a Soroban simulate, send or poll failure inside invokeOnLockedEscrow
  • the final paymentRepo.save

None of these paths gives the reserved amount back, so every failed reward permanently shrinks the pool's DB balance, even though no USDC left the contract. Over time MaintenancePool.balance drifts below the real on-chain balance. Rewards then start failing with "exceeds pool balance" while funds are still in escrow, and there's no admin path to reconcile.

Suggested fix

  • Wrap the payout in try/catch. On failure, restore the reservation with increment({ id }, 'balance', Number(amount)) and rethrow.
  • Only do that when the on-chain call is known not to have gone through. invokeOnLockedEscrow's state distinguishes "never submitted" from "submitted, outcome unknown".
  • Move the cheap validation (recipient/user match, amount) before the decrement so bad input never touches the balance.
  • Add tests with poolWithdraw rejecting that assert the pool balance is unchanged.

Related: #51, #274 (introduced the reserve-first ordering), #163.

Activity

  1. added
    bugSomething isn't working
    very hardVery difficult task, expert-level effort required
    architectureArchitecture/design issue
    Stellar WaveIssues in the Stellar wave program
    on Sep 27, 2026
  2. Fabulouz34 commented on Sep 27, 2026

    @Fabulouz34

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

    Hi maintainer please I will appreciate it if I'm given this issue to work on. Please assign me I'm ready to get started
    Thank you

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

  3. Ultra-Tech-code commented on Sep 27, 2026

    @Ultra-Tech-code
    Contributor

    @Ultra-Tech-code has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

    Hi Maintainer,

    I’m a backend and web3 developer with experience in financial flows, database transactions, Soroban integrations, and security-focused testing. I’d like to work on #459 and fix the maintenance pool balance drift on failed payouts.

    My approach:

    • Move cheap validation ahead of the balance reservation so invalid recipients and amounts cannot reduce the pool balance.
    • Wrap the on-chain payout flow with explicit handling for failures where the transaction is known not to have been submitted successfully.
    • Restore the reserved balance only when the on-chain outcome confirms that no payout occurred, avoiding double restoration for unknown outcomes.
    • Preserve the existing atomic balance check-and-decrement behavior for concurrent reward assignments.
    • Add tests covering validation failures, payout rejection, on-chain failure paths, and successful payouts to verify the database balance remains consistent.

    Kindly assign this issue to me.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @Ultra-Tech-code to this issue.

  4. Proxima84-code commented on Sep 28, 2026

    @Proxima84-code

    I have submitted a clean fix in PR above. Wrapped poolWithdraw() in a compensating try/catch block that atomically restores the decremented pool balance via poolRepo.increment() whenever on-chain execution or persistence fails. Verified with 17/17 passing unit tests.

  5. Proxima84-code commented on Sep 28, 2026

    @Proxima84-code

    I have submitted a clean fix in PR above. Wrapped poolWithdraw() in a compensating try/catch block that atomically restores the decremented pool balance via poolRepo.increment() whenever on-chain execution or persistence fails. Verified with 17/17 passing unit tests.

  6. drips-wave commented on Sep 29, 2026

    @drips-wave

    Congratulations, @Fabulouz34! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on September 30, 2026.

    🧑‍💻 @Fabulouz34: 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 🌊

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

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave programarchitectureArchitecture/design issuebugSomething isn't workingvery hardVery difficult task, expert-level effort required

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions