Skip to content

fix(payouts): claim-based batch execution, conditional transitions and retry repair - #1634

Open
comandanto wants to merge 1 commit into
CredenceOrg:mainfrom
comandanto:fix/batch-payout-claim-transitions
Open

comandanto wants to merge 1 commit into
CredenceOrg:mainfrom
comandanto:fix/batch-payout-claim-transitions

Conversation

@comandanto

Copy link
Copy Markdown

Summary

Closes #1609

This PR resolves the duplicate-retry and status-corruption defects in BatchPayoutProcessor by replacing blind upserts with atomic, conditional claims and forward-only state transitions.

Root Cause Analysis

The blind upsert (INSERT ... ON CONFLICT (transaction_hash) DO UPDATE SET status = EXCLUDED.status) previously used to record payout items defeated retries and corrupted settled ledger entries:

  1. Blind retry overwrite: When a previously failed item was retried, the upsert flipped status back to pending. Because a conflict occurred, PostgreSQL reported xmax > 0, marking isDuplicate = true. The processor logged "Skipping duplicate" and returned immediately without executing, leaving the item trapped in pending forever.
  2. Settled row corruption: Resubmitting an already settled batch unconditionally downgraded status = 'settled' to 'pending' before reporting duplicate.
  3. Typo in retry helper: getRetryableItems() referenced retryHashs instead of retryHashes, crashing with ReferenceError.
  4. Base64 test encoding: src/jobs/batchPayoutProcessor.test.ts was committed base64-encoded and used mockResolved instead of Vitest's mockResolvedValue.

Design & Changes

  1. Atomic Claim (claimForPayout):
    • Implemented in SettlementsRepository using a single-statement conditional write:
      INSERT INTO settlements (bond_id, amount, transaction_hash, settled_at, status)
      VALUES ($1, $2, $3, $4, 'pending')
      ON CONFLICT (transaction_hash)
      DO UPDATE SET
        amount     = EXCLUDED.amount,
        status     = 'pending',
        settled_at = EXCLUDED.settled_at,
        updated_at = NOW()
      WHERE settlements.status = 'failed'
      RETURNING id, status
    • Claims only succeed for new items or items in failed state. Items in pending or settled are untouched and return 0 rows.
    • When the claim is lost, current status is queried to report whether the item is settled or in-flight/orphaned.
  2. Conditional State Transitions (transitionStatus):
    • Implemented conditional updates WHERE transaction_hash = $1 AND status = $from to enforce forward-only progression (pending -> settled, pending -> failed).
  3. Retry Typo Fix:
    • Corrected retryHashs -> retryHashes in getRetryableItems().
  4. Test Suite Repair & Expansion:
    • Decoded src/jobs/batchPayoutProcessor.test.ts and corrected mock APIs (mockResolvedValue, mockRejectedValue).
    • Added unit tests for already-settled items, concurrent claims, orphan threshold checks, and retry-after-failure.
    • Extended tests/integration/batchPayoutAtomicity.integration.test.ts duplicate-retry test to assert settled rows remain intact.

Test Verification

 RUN  v4.1.12 Credence-Backend

 ✓ src/jobs/batchPayoutProcessor.test.ts (34 tests) 17ms

 Test Files  1 passed (1)
      Tests  34 passed (34)

TypeScript check (npx tsc --noEmit) passes with 0 errors in src/jobs/batchPayoutProcessor*.ts.

…d retry repair

- Fix ReferenceError on retryHashs typo in getRetryableItems
- Implement claimForPayout with single-statement conditional write in SettlementsRepository
- Prevent settled rows from being downgraded to pending on batch resubmission
- Implement conditional transitionStatus for pending -> settled and pending -> failed
- Decode and repair batchPayoutProcessor.test.ts (mockResolved -> mockResolvedValue)
- Add comprehensive unit tests for claim, skip and transition semantics (34/34 passing)
- Extend batchPayoutAtomicity integration test to verify status stays settled after retry

Closes CredenceOrg#1609
@comandanto

Copy link
Copy Markdown
Author

hey, friendly ping: is there anything I should change to get this reviewed or merged? happy to rebase or adjust. thx

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.

fix(payouts): batch payout retries never execute, resubmitting a batch downgrades settled rows to pending, and getRetryableItems throws

1 participant