Repository navigation
Conversation
Closes degenspot#572 ## What Adds durable idempotency handling to backend endpoints that prepare, relay, or record transaction-related operations. ## Changes ### New files - src/idempotency/idempotency-record.entity.ts Composite-PK entity (key × route × principal) tracking state, fingerprint, stored response body, and expiry. - src/idempotency/idempotency.service.ts Atomic claim via SERIALIZABLE transaction, fingerprint (SHA-256 of principal+route+canonical body), complete/fail lifecycle, hourly cleanup cron that skips IN_PROGRESS records. - src/idempotency/idempotency.interceptor.ts Global NestInterceptor: validates Idempotency-Key header, routes to claim/replay/inProgress/conflict, sets response headers, and persists the outcome. Activated only on routes decorated with @UseIdempotency. - src/idempotency/use-idempotency.decorator.ts @UseIdempotency() route decorator. - src/idempotency/idempotency.events.ts Typed EventEmitter2 event constants and payload interface. - src/idempotency/idempotency-metrics.service.ts In-process counters (claimed, replayed, conflicted, inProgress, failed, retryable) driven by EventEmitter2 @onevent listeners. - src/idempotency/idempotency.controller.ts GET /idempotency/metrics — admin-protected counter snapshot. - src/idempotency/idempotency.module.ts - src/database/migrations/1760000050000-CreateIdempotencyRecordsTable.ts Creates idempotency_records table with composite PK, state enum, fingerprint, responseBody (jsonb), expiry indexes. ### Tests - src/idempotency/idempotency.service.spec.ts (36 tests): sequential replay, concurrent duplicates, payload mismatch, cleanup, retryable failure (408/429/502/503/504), terminal failure, fingerprint stability, sensitive-field sanitisation. - src/idempotency/idempotency.interceptor.spec.ts (8 tests): pass-through, missing key, key too long, claimed, replayed, in-progress, conflict, handler error. ### Wired endpoints - POST /relay/tx - POST /calls/prepare - POST /oracle/sign - POST /oracle/sign/async ### Modified files - src/app.module.ts — imports IdempotencyModule, registers interceptor globally - src/relay/relay.controller.ts — @UseIdempotency on POST /relay/tx - src/calls/calls.controller.ts — @UseIdempotency on POST /calls/prepare - src/oracle-signing/oracle.controller.ts — @UseIdempotency on both sign endpoints ## Key design decisions - Composite PK (key + route + principal) isolates keys per caller and endpoint. - SERIALIZABLE transaction prevents concurrent claims racing to INSERT. - RETRYABLE state (408/429/502/503/504) allows re-use of the same key after transient failures. - Sensitive fields (xdr, signature, token, secret, privateKey, …) stripped before storage. - Cleanup cron skips IN_PROGRESS records to avoid racing active requests. - In-process metrics counters (no Prometheus dependency needed).
PeterOche
approved these changes
Aug 22, 2026
12 tasks
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
Adds durable idempotency handling to backend endpoints that prepare, relay, or record transaction-related operations.
Closes #572
Changes made
New files
Composite-PK entity (key × route × principal) tracking state,
fingerprint, stored response body, and expiry.
Atomic claim via SERIALIZABLE transaction, fingerprint (SHA-256 of
principal+route+canonical body), complete/fail lifecycle, hourly
cleanup cron that skips IN_PROGRESS records.
Global NestInterceptor: validates Idempotency-Key header, routes to
claim/replay/inProgress/conflict, sets response headers, and persists
the outcome. Activated only on routes decorated with @UseIdempotency.
@UseIdempotency() route decorator.
Typed EventEmitter2 event constants and payload interface.
In-process counters (claimed, replayed, conflicted, inProgress,
failed, retryable) driven by EventEmitter2 @onevent listeners.
GET /idempotency/metrics: admin-protected counter snapshot.
Creates idempotency_records table with composite PK, state enum,
fingerprint, responseBody (jsonb), expiry indexes.
Tests
pass-through, missing key, key too long, claimed, replayed, in-progress,
conflict, handler error.
Wired endpoints
Key design decisions