fix(settlement): prevent duplicates with unique keys and upsert - #1233
Open
DeePrincipal-dev-lang wants to merge 1 commit into
Open
fix(settlement): prevent duplicates with unique keys and upsert#1233DeePrincipal-dev-lang wants to merge 1 commit into
DeePrincipal-dev-lang wants to merge 1 commit into
Conversation
- Update src/db/schema.ts: replace composite UNIQUE (bond_id, transaction_hash) with UNIQUE (transaction_hash) to match migration 006_settlement_tx_idempotency; add composite index idx_settlements_bond_tx (bond_id, transaction_hash) for efficient bond-scoped queries - Add tests/integration/settlementsRepository.integration.test.ts: full integration test suite against real Postgres covering basic idempotency, field updates on conflict, global tx hash uniqueness across bonds, concurrent race conditions (10 parallel upserts), and DB-level constraint enforcement Closes CredenceOrg#965
|
@DeePrincipal-dev-lang 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.
Problem
Under replay or concurrent race conditions, the same on-chain transaction could produce
duplicate settlement rows. The old schema enforced uniqueness only on the composite (bond_id,
transaction_hash) pair, meaning the same transaction_hash arriving from a different bond — or
replayed concurrently — could insert a second row.
Changes
src/db/schema.ts
table, aligning the in-memory schema used in tests with what migration
006_settlement_tx_idempotency already applies to production.
bond-scoped settlement queries.
tests/integration/settlementsRepository.integration.test.ts (new)
Full integration test suite against real Postgres (testcontainers or TEST_DATABASE_URL)
covering:
with the same id; N sequential replays produce exactly one row
to the first inserter
row; original bond_id is preserved
the same settlement id; exactly one caller gets isDuplicate=false
23505; ON CONFLICT DO UPDATE succeeds where a plain insert would fail
How it works
The SettlementsRepository.upsert() uses:
INSERT INTO settlements (bond_id, amount, transaction_hash, settled_at, status)
VALUES (...)
ON CONFLICT (transaction_hash) DO UPDATE
SET amount = EXCLUDED.amount,
status = EXCLUDED.status,
settled_at = EXCLUDED.settled_at,
updated_at = NOW()
RETURNING ...
This makes every settlement write fully idempotent on transaction_hash. Replayed Horizon events
and concurrent requests collapse into a single row at the database level — no application-level
locking required.
Testing
Unit tests (pg-mem, no Docker needed)
npm test src/db/repositories/settlementsRepository.test.ts
Integration tests (requires Postgres via testcontainers or TEST_DATABASE_URL)
TEST_DATABASE_URL=postgresql://credence:credence@localhost:5433/credence_test
npm test tests/integration/settlementsRepository.integration.test.ts
closes #965