feat(escrow): add idempotency layer for payment operations #218 - #235
Merged
SudiptaPaul-31 merged 1 commit intoSep 27, 2026
Merged
Conversation
Ensure blockchain/payment requests are processed exactly once so retries or concurrent requests cannot create duplicate escrow contracts, double releases or inconsistent financial records. - Add idempotency_records migration with a unique (idempotency_key, operation_type) constraint, TTL index and a partial unique index on escrow_transaction_logs payment hashes. - Add lib/idempotency (validation, hashing, repository, service, withIdempotency route middleware) exposing clear IDEMPOTENCY_* error codes. - Wire idempotency into /api/escrow create, fund, release and refund routes. - Cache successful responses and replay them; release the claim on failure so retries work; reject concurrent duplicates with 409. - Add configurable TTL (IDEMPOTENCY_TTL_HOURS) and an idempotency:cleanup script. - Add unit tests covering validation, service, middleware and repository.
|
@doncross03 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Overview
This PR introduces an idempotency layer for escrow payment operations so blockchain/payment requests are processed exactly once. It prevents duplicate escrow creation, double payment releases and inconsistent financial records caused by retries or concurrent requests.
Related Issue
Closes #218
Changes
🔐 Idempotency core
lib/db/migrations/010_idempotency_records.sqlidempotency_recordstable withidempotencyKey,operationType,requestHash,requestPayload,responsePayload,responseStatus,status,createdAt,expiresAt.(idempotency_key, operation_type)constraint + expiry index for TTL cleanup.escrow_transaction_logs(transaction_hash)fordeposit/milestone_release/refund.lib/idempotency/validation.ts— key extraction (Idempotency-Keyheader oridempotencyKeybody), format validation and order-independent SHA-256 payload hashing.repository.ts— atomicINSERT … ON CONFLICTclaim, plus complete / markFailed / remove / purgeExpired.service.ts— claim → execute → store/replay lifecycle with typed storage errors.middleware.ts—withIdempotency()route wrapper.errors.ts— machine-readable codes:IDEMPOTENCY_KEY_REQUIRED,IDEMPOTENCY_KEY_INVALID,IDEMPOTENCY_KEY_REUSED,IDEMPOTENCY_IN_PROGRESS,IDEMPOTENCY_STORAGE_ERROR.🔗 Route integration
app/api/escrow/create/route.ts,app/api/escrow/fund/route.ts,app/api/escrow/release/route.ts,app/api/escrow/refund/route.ts🧹 Retention & configuration
scripts/idempotency-cleanup.ts+pnpm idempotency:cleanupenv.example— documentsIDEMPOTENCY_TTL_HOURS(default 24).package.json— adds the cleanup command.🧪 Tests
__tests__/idempotency/validation.test.ts,service.test.ts,middleware.test.ts,repository.test.tsVerification Results
Acceptance Criteria
Idempotency-Replayed: trueheader on replayINSERT … ON CONFLICTclaim on the unique(key, operation)constraint; duplicates receive409 IDEMPOTENCY_IN_PROGRESSwithRetry-AfterIDEMPOTENCY_TTL_HOURS(default 24, clamped 1–720) +pnpm idempotency:cleanupIDEMPOTENCY_KEY_REQUIRED/IDEMPOTENCY_KEY_INVALID(400),IDEMPOTENCY_KEY_REUSED/IDEMPOTENCY_IN_PROGRESS(409)(idempotency_key, operation_type)+ partial unique index on payment transaction hashesNotes for reviewers
pnpm migrateto apply010_idempotency_records.sql.escrow_transaction_logswill fail to create if the target database already contains duplicate payment hashes — worth checking before deploy.