Skip to content

stellar: add retry logic for tx_bad_seq, tx_insufficient_fee, and net… - #316

Open
CASHBUDDIE wants to merge 1 commit into
Pidoko257:mainfrom
CASHBUDDIE:fix/stellar-retry-transaction-submission
Open

stellar: add retry logic for tx_bad_seq, tx_insufficient_fee, and net…#316
CASHBUDDIE wants to merge 1 commit into
Pidoko257:mainfrom
CASHBUDDIE:fix/stellar-retry-transaction-submission

Conversation

@CASHBUDDIE

Copy link
Copy Markdown

…work timeouts

## Description

Add robust retry logic to Stellar transaction submission to handle transient Horizon errors: refresh sequence on `tx_bad_seq`, bump fee on `tx_insufficient_fee`, and retry network timeouts with exponential backoff.

## Related Issue

Fixes #57  

## Type of Change

- [x] Bug fix
- [ ] New feature
- [ ] Documentation update
- [ ] Code refactoring
- [ ] Performance improvement

## Changes Made

- Implemented intelligent retrying inside `submitTransaction` in [src/stellar/transactions.ts](src/stellar/transactions.ts#L1-L400):
  - On `tx_bad_seq`: calls `updateFeePayerSequence()` (attempts to refresh fee payer sequence), re-parses the envelope and retries submission (bounded by network retry limit).
  - On `tx_insufficient_fee`: when the envelope is a `FeeBumpTransaction`, rebuilds the fee-bump with a 20% higher total fee (computes new base fee from charged operation count), re-signs with the fee payer and retries up to 3 times.
  - On network timeouts / server 5xx / connection resets: retries with exponential backoff (base 200ms, doubled each attempt) up to 5 attempts.
  - Other structured Stellar errors return immediately with the Horizon transaction result code (no retry).
- Added small local helpers in `submitTransaction`: `isNetworkError` and `stellarTxCode` to centralize error classification.
- Preserved existing fee-limit validation (`validateEnvelopeFeeLimit`) and fee-payer sequence update after successful submission.
- Minimal, focused change: no public API changes; only implements more resilient submission behavior.

## Testing

How did you test these changes?

- Unit tests: The repo includes existing tests that exercise fee-bump builds and submission behavior (`src/stellar/__tests__/transactions.test.ts`). I validated logic by running inspections against these tests and by reasoning through expected flows for each error case.
- Local run instructions (recommended):
```bash
# Install deps
npm install

# Run all tests
npm test

# Or run just the transactions tests
npm test -- transactions.test.ts -i --runInBand
  • Note: In this editing environment I attempted to run the test command but npm was unavailable; please run the commands above locally or in CI to verify behavior fully.
  • Suggested additional test cases (if you want to add more):
    • Mock server response that returns tx_bad_seq and verify that updateFeePayerSequence is called and the submission is retried.
    • Mock tx_insufficient_fee on a FeeBumpTransaction and verify the fee is increased by ~20% and retried up to 3 times.
    • Simulate network timeouts / 500 responses and verify exponential backoff occurs up to 5 attempts.
    • Verify other Horizon error codes (e.g., tx_failed) are returned immediately without retries.

Checklist

  • Code follows project style
  • Self-reviewed my code
  • Commented complex code
  • Updated documentation
  • No new warnings
  • Added tests (if applicable)

N/A

Additional Notes

  • Branch: fix/stellar-retry-transaction-submission
  • Commit: stellar: add retry logic for tx_bad_seq, tx_insufficient_fee, and network timeouts
  • File changed: src/stellar/transactions.ts
  • The change aims to make transaction submission resilient to transient Horizon conditions while keeping strict behavior for non-retryable errors. Please run the test suite in your environment / CI and I can follow up with any adjustments or add targeted unit tests if you want.

@drips-wave

drips-wave Bot commented Jul 28, 2026

Copy link
Copy Markdown

@CASHBUDDIE 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! 🚀

Learn more about application limits

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.

1 participant