Repository navigation
Retry transaction status transitions on write conflicts + Zod validation pipe acceptance (Closes #412, Closes #418) - #473
Merged
Conversation
…conflicts Concurrent workers (fee-bump retries, approvals landing mid-execute, racing cancels) could clobber each other's transaction status writes. The DRAFT/PENDING -> SUBMITTED and DRAFT/PENDING -> CANCELLED transitions now run as SERIALIZABLE units of work through PrismaTransactionService.runWithRetry: a serialization conflict (P2034) is replayed with exponential backoff, and the replay re-reads the row so a lost race surfaces a ConflictException before any funds move on-chain. Repository reads/writes accept the transactional client so check and update commit or roll back together. Closes the transactions-module integration gap for ASTROIDX556#412. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
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.
Summary
This PR delivers the remaining work for two backend issues in one change set: the Prisma transaction-retry integration (#412) and the Zod request-validation pipe (#418).
Closes #412
Closes #418
Issue #412 — Refactor Prisma transaction retry wrapper for concurrent Soroban transaction status updates
Problem: High-throughput agent operations race on transaction status rows — fee-bump retries, approvals landing mid-
execute, and concurrentcancelcalls all did a read → check → write sequence with no atomicity. Two workers could both observeDRAFT, and the loser's write silently clobbered the winner's: a cancellation could overwrite a transaction already submitted on-chain, and two executes could both pass the status gate before either marked the rowSUBMITTED.Solution: the reusable retry utility from
src/database/prisma-transaction.service.ts(runWithRetry, P2034-aware, exponential backoff) is now integrated into the transactions module where multi-step status updates occur.What changed
src/modules/transactions/transaction.repository.tsfindById()andupdate()accept an optional transactional client (PrismaService | Prisma.TransactionClient, defaulting to the sharedPrismaService), so a status check and its write commit — or roll back — together. Same pattern asBudgetRepository.src/modules/transactions/transaction.service.tsPrismaTransactionService(provided by the globalDatabaseModule— no module wiring changes needed).execute(): theDRAFT/PENDING → SUBMITTEDtransition is now a guarded read-check-write executed insiderunWithRetryat SERIALIZABLE isolation. A serialization conflict (P2034) is replayed with exponential backoff; the replay re-reads the row, seesSUBMITTED, and throwsConflictExceptionbefore any funds move on-chain — no double submission.cancel(): same guarded pattern forDRAFT/PENDING → CANCELLED, so a cancel can never overwrite a transaction a concurrent worker just submitted or completed.NotFoundException/ConflictExceptionsemantics are unchanged and, being non-conflict errors, are never replayed.EXECUTED_STATUSES,CANCELLABLE_STATUSES) shared by the fast-path check and the in-transaction guard.src/modules/transactions/tests/transaction.service.spec.ts— updated construction sites for the new constructor dependency (passthrough transaction manager, same pattern asbudget.service-lock.spec.ts).src/modules/transactions/tests/transaction-status-retry.spec.ts(new) — unit tests that simulate concurrent database conflicts against a realPrismaTransactionServicewhose$transactionaborts the first N attempts withP2034:Serializableisolation;SUBMITTEDrow →ConflictException, no write, no event emitted;NotFoundExceptionwithout retrying;P2034surfaces after the default 3 attempts;execute(): conflict replayed → submits on-chain exactly once; lost race →ConflictExceptionandstellar.submitPaymentnever called; already-executed row fast-fails without opening a transaction.Acceptance criteria
src/database/with configurable exponential retry for transient conflicts (runWithRetry, attempts/backoff configurable,P2034)npm run db:verifyscripts unaffected (no migrations touched)Issue #418 — Implement Zod Validation Pipe for Request Body Deserialization across Domain Controllers
State: the acceptance criteria for this issue are satisfied in
mainby the cumulative work of earlier merged PRs (#166, #192, #274, #353, #378). This PR verifies the criteria end-to-end against the current branch and keeps them intact; no pipe re-implementation was required.Acceptance criteria verification
ZodValidationPipeinsrc/common/pipes/zod-validation.pipe.tsimplementingPipeTransform— exists, generic overZodType<T>, parses via.safeParse()(never throws unhandled), supports custom error maps, per-path custom messages and locale options; strict TypeScript, noany.ZodValidationException extends BadRequestExceptioncarrying{ code: 'VALIDATION_ERROR', message, details: [{ path, message }] }, which the global exception filter renders as the standard 400 problem-details envelope ({ success: false, error: ... }).src/modules/wallets/wallet.controller.ts(createWalletSchema,updateWalletSchema, pagination query) andsrc/modules/policies/policy.controller.ts(createPolicySchema,updatePolicySchema,simulatePolicySchema, pagination query); additionally applied across auth, agents, transactions, budgets, approvals, memory, webhooks, organizations, notifications, risk, audit, admin and developer controllers, documented indocs/validation.md.src/common/pipes/zod-validation.pipe.spec.ts— cover valid payloads (incl. defaults/coercion/stripping), missing required fields, type mismatches, nested/array paths, custom messages and error maps; pluszod-validation.pipe.integration.spec.tsandproblem-details.integration.spec.tscovering the{ success: false, error: ... }/ HTTP 400 envelope end-to-end.Acceptance criteria
ZodValidationPipecreated with structuredpath/messagedetailsValidation
npm run testnpm run typechecknpm run lintupstream/main(571a451); re-fetched immediately before push — 0 commits behind, no merge conflicts.npm run db:verifyunaffected).