fix: handle Stellar tx_bad_seq errors with sequence refresh and retry (#141) - #158
Merged
Merged
Conversation
…SwiftChainn#141) Closes SwiftChainn#141 ## Problem Stellar transaction submissions could fail with tx_bad_seq (sequence number mismatch) when a concurrent submission from the same account incremented the sequence number between the XDR build and the submission. The error was not caught or retried, causing permanent transaction failures under concurrent load. ## Solution Created src/services/stellarService.ts (the implementation directory per issue), which provides a dedicated submitEscrowLock() method with: - Two-path tx_bad_seq detection (XDR decode via xdr.TransactionResult + string-match fallback) covering all SDK error shapes. - Automatic sequence refresh: on tx_bad_seq the account is re-fetched from the RPC node to obtain the current sequence number. - Full transaction rebuild: the operation is reconstructed from live DB data (no hardcoded values) and re-simulated via prepareTransaction to attach updated resource fees and footprint. - Bounded retry loop capped by STELLAR_BAD_SEQ_MAX_RETRIES (default 3). - pollForCompletion(): polls getTransaction() with exponential back-off (500 ms -> 5 s cap) until SUCCESS, FAILED, or polling window exhausted. ## Changes ### src/services/stellarService.ts (NEW — implementation directory) - StellarService class with: submitEscrowLock(input) — submit signed XDR with bad-seq retry rebuildWithFreshSequence() — public helper, re-fetches account + rebuilds from DB + re-simulates pollForCompletion(hash) — waits for ledger inclusion isBadSeqError() — detects tx_bad_seq in both response and thrown error shapes - SubmitEscrowLockInput and SubmitEscrowLockResult interfaces exported. - All delivery/contract data sourced from MongoDB (no inline mocks). ### src/config/env.ts - Added 4 env vars to EnvConfig interface and Zod envSchema: SOROBAN_RPC_MAX_RETRIES (default 3) SOROBAN_RPC_RETRY_BASE_MS (default 250) SOROBAN_RPC_RETRY_MAX_MS (default 8000) STELLAR_BAD_SEQ_MAX_RETRIES (default 3) - Fixes the pre-existing gap where soroban.service.ts referenced these vars but they were absent from the validated schema. ### src/validators/transactionValidator.ts - Added submitTransactionSchema (deliveryId, payerAddress, signedXdr). - Added SubmitTransactionBody type export. ### src/controllers/transactionController.ts - Imported stellarService and SubmitTransactionBody. - Added submitEscrowLockTransaction() handler (POST /transactions/submit). ### src/routes/transactionRoutes.ts - Added POST /api/v1/transactions/submit route: apiLimiter -> validateRequest(submitTransactionSchema) -> transactionController.submitEscrowLockTransaction - Full OpenAPI doc comment with all response codes. ### .env.example - Documented all 4 new env vars with explanations. ## Retry behaviour | Scenario | Result | |---------------------------------|-------------------------------------------| | First submission succeeds | 200 with hash + ledger | | tx_bad_seq, retry succeeds | 200 with retriedOnBadSeq=true | | tx_bad_seq exhausts all retries | 409 Conflict | | Other submission error | 502 Bad Gateway | | Not confirmed in poll window | 504 Gateway Timeout |
|
@mmotunrayo 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.
Closes #141
Problem
Stellar transaction submissions could fail with
tx_bad_seq(sequence number mismatch) when a concurrent submission from the same account incremented the sequence number between the XDR build and the submission. The error was not caught or retried, causing permanent transaction failures under concurrent load.Solution
Created
src/services/stellarService.ts(the implementation directory per issue), which provides a dedicatedsubmitEscrowLock()method with:tx_bad_seqdetection (XDR decode viaxdr.TransactionResult+ string-match fallback) covering all SDK error shapes.tx_bad_seqthe account is re-fetched from the RPC node to obtain the current sequence number.prepareTransactionto attach updated resource fees and footprint.STELLAR_BAD_SEQ_MAX_RETRIES(default 3).pollForCompletion(): pollsgetTransaction()with exponential back-off (500 ms -> 5 s cap) untilSUCCESS,FAILED, or polling window exhausted.Changes
src/services/stellarService.ts(NEW — implementation directory)StellarServiceclass with:submitEscrowLock(input)— submit signed XDR with bad-seq retryrebuildWithFreshSequence()— public helper, re-fetches account + rebuilds from DB + re-simulatespollForCompletion(hash)— waits for ledger inclusionisBadSeqError()— detectstx_bad_seqin both response and thrown error shapesSubmitEscrowLockInputandSubmitEscrowLockResultinterfaces exported.src/config/env.tsAdded 4 env vars to
EnvConfiginterface and ZodenvSchema:SOROBAN_RPC_MAX_RETRIES3SOROBAN_RPC_RETRY_BASE_MS250SOROBAN_RPC_RETRY_MAX_MS8000STELLAR_BAD_SEQ_MAX_RETRIES3Fixes the pre-existing gap where
soroban.service.tsreferenced these vars but they were absent from the validated schema.src/validators/transactionValidator.tssubmitTransactionSchema(deliveryId,payerAddress,signedXdr).SubmitTransactionBodytype export.src/controllers/transactionController.tsstellarServiceandSubmitTransactionBody.submitEscrowLockTransaction()handler (POST /transactions/submit).src/routes/transactionRoutes.tsPOST /api/v1/transactions/submitroute: