feat(api): wallet ledger with database-enforced race protection (#23) #88
No reviewers
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
primatekuntech/kws!88
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/wallet-ledger"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Closes #23. First P2 (money core) PR.
What
Customer and organization wallets with an append-only ledger, protected against race conditions by the database itself.
Guard rails (migration 0010; they hold even if Go code is wrong, a second service is added, or someone runs SQL by hand)
wallets.balancecan only change as a side effect of a ledger insert. A directUPDATE, or opening a wallet with money in it, is rejected.balance_afteritself (a writer's own figure is ignored) and refuses a payment larger than the balance. Concurrent inserts for one wallet queue on the lock. The same trigger numbers each wallet's entries 1, 2, 3... without gaps.balance >= 0is also a CHECK. Awallet_balance_driftview proves balance = sum of entries.The Go layer (
internal/wallet) addscurrency must match the wallet (legacy debited without checking); idempotent operations by key (a retried payment, top-up or refund returns the original entry and changes nothing; the same key with a different amount is refused); an overdraft returns
ErrInsufficientFundswithout poisoning the caller's transaction; a 3 s lock wait thenErrBusy(retry) instead of a hang;Applyruns inside the caller's transaction so a wallet change commits or rolls back with the transaction and tickets (R-WAL-2).Customer API (spec + contract tests updated, 77 operations)
GET /v1/me/wallets,GET /v1/me/wallets/{id}/entries?limit&before(cursor by entry number). The owner always comes from the authenticated user; another customer's wallet is a 404, not a 403. Never cached.Answering "does the lock hurt at 1,000 concurrent users?" (measured,
BenchmarkWalletPayments)Research: how other systems handle this (ADR 0007 has the table and links)
Modern Treasury (row locks on user accounts, async for hot shared accounts), Brandur on Stripe (idempotency keys), PostgreSQL SSI, TigerBeetle (single-threaded batching, two-phase pending transfers). Conclusion: a short row lock is the standard answer for user balances; optimistic locking, SERIALIZABLE, queue-per-account and holds solve other problems or need infrastructure we do not have. Mostly vendor docs and blogs, not independent studies.
Standards review (docs/development-standards.md)
1.005silently) were fixed in the tests, not the schema.balance_after-> fails; the app skipping its overdraft check -> "current transaction is aborted".Ledger,ProvisionerandReaderare separate narrow interfaces; the domain has no SQL; checkout will depend onLedgeronly.make lint0 issues. Both generated clients still compile. Security scans run onmainafter merge.Rules for callers (ADR 0007, CLAUDE.md)
No slow work between taking a wallet lock and committing; one wallet per transaction, or several in ascending id order; no running totals; no per-sale update of a shared balance.
Not in this PR
HTTP-level idempotency keys (#24), organization wallet endpoints (organizations, #37), manual adjustments, two-phase holds, a drift alert job, wiring wallet payment into checkout (#26/#30).
🤖 Generated with Claude Code