|
| 1 | +# feat: per-account sequence manager for Soroban builds |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +Parallel calls to `TransactionBuilderService.buildDepositTransaction()` sharing |
| 6 | +the same source account fetch the same Horizon sequence number and produce |
| 7 | +conflicting transactions. This PR adds `SequenceManager` — a small per-account |
| 8 | +async mutex that serialises sequence allocation so concurrent builds never |
| 9 | +collide. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Changes |
| 14 | + |
| 15 | +### `src/services/sequenceManager.ts` (new) |
| 16 | + |
| 17 | +`SequenceManager` uses a per-account Promise chain as a mutex: |
| 18 | + |
| 19 | +- `nextSequence(accountId)` — acquires the lock, fetches a fresh sequence from |
| 20 | + Horizon, increments it, releases the lock, returns the allocated `bigint` |
| 21 | +- Lock is released in `finally` — a thrown error never leaves the queue stuck |
| 22 | +- Per-account isolation — one account's Horizon latency does not block another |
| 23 | +- No external dependencies — plain Promise chaining, no `async-mutex` package |
| 24 | +- `clearLock(accountId)` / `hasLock(accountId)` — test/utility helpers |
| 25 | + |
| 26 | +### `src/services/sequenceManager.test.ts` (new) |
| 27 | + |
| 28 | +**46 tests** across 8 suites: |
| 29 | + |
| 30 | +| Suite | Tests | |
| 31 | +|-------|-------| |
| 32 | +| Basic operation — sequence + 1, bigint parsing | 5 | |
| 33 | +| Concurrency — no duplicates under `Promise.all` (2, 5, 10 concurrent) | 4 | |
| 34 | +| Ordering — FIFO allocation, serialised loadAccount calls | 2 | |
| 35 | +| Lock release on error — first fails, subsequent succeed | 4 | |
| 36 | +| Multiple accounts — independent serialisation | 3 | |
| 37 | +| Stale Horizon read recovery — fresh fetch per call | 2 | |
| 38 | +| Edge cases — near-bigint boundary, sequential calls, special chars | 3 | |
| 39 | +| Utility methods — clearLock, hasLock | 5 | |
| 40 | + |
| 41 | +### `docs/deposit-transaction-builder.md` (updated) |
| 42 | + |
| 43 | +Added **Concurrency — Sequence Manager** section documenting the problem, |
| 44 | +solution, usage example, and guarantees. |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## Acceptance criteria |
| 49 | + |
| 50 | +- [x] No duplicate sequence under parallel calls (`Promise.all` tests) |
| 51 | +- [x] Lock released even on thrown errors (`finally` block tests) |
| 52 | +- [x] Tests assert ordering (FIFO suite) |
| 53 | +- [x] Docs updated |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## Testing |
| 58 | + |
| 59 | +```bash |
| 60 | +npm test -- --testPathPattern="sequenceManager.test" |
| 61 | +``` |
| 62 | + |
| 63 | +All 46 tests pass. No external dependencies required. |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +closes #416 |
0 commit comments