Skip to content

feat: cashu escrow DB helpers — Cashu foundation CF-4#797

Open
grunch wants to merge 1 commit into
mainfrom
feat/cashu-cf4-db-helpers
Open

feat: cashu escrow DB helpers — Cashu foundation CF-4#797
grunch wants to merge 1 commit into
mainfrom
feat/cashu-cf4-db-helpers

Conversation

@grunch

@grunch grunch commented Jul 1, 2026

Copy link
Copy Markdown
Member

Summary

CF-4 of the Cashu foundation (docs/cashu/01-fundamentals.md §6). Additive query helpers over the already-migrated cashu_* columns; new, unreferenced functions — zero behaviour change. Frozen-contract signatures from §10 of the spec.

  • update_order_cashu_escrow(pool, order_id, mint_url, token, locked_at, expected_status, new_status) -> Result<bool> — atomic compare-and-set: persists the three columns and transitions the status in one UPDATE … WHERE id = ? AND status = ? AND cashu_escrow_locked_at IS NULL. No lock-without-advance window; a replayed or concurrent submission matches zero rows instead of double-writing.
  • find_locked_cashu_orders(pool)WHERE cashu_escrow_locked_at IS NOT NULL, deliberately without a status predicate: the CAS advances the status in the same write as the lock, so filtering on a status would skip legitimately locked rows that already moved on (per the spec's design note, incl. the M-1 review fix).

No migration ships here — the columns landed on main in 20260530120000_cashu_escrow_fields.sql.

Test plan

  • cargo fmt --all
  • cargo clippy --all-targets --all-features -- -D warnings
  • cargo test — 506 passed (503 pre-existing unmodified + 3 new):
    • CAS locks once; replay matches zero rows and the original lock survives (attempted overwrite with different mint/token is ignored)
    • status mismatch matches zero rows, columns stay NULL
    • finder includes locked orders regardless of status (locked → moved to FiatSent → still found) and excludes unlocked ones

Independent Wave-1 PR of the CF-0…CF-5 series (CF-0 #795, CF-1 #796). Remaining: CF-2 (CashuClient/cdk), CF-3 (mint harness), CF-5 (integration, last).

Cashu foundation CF-4 (docs/cashu/01-fundamentals.md, section 6):

- update_order_cashu_escrow: atomic compare-and-set that persists the
  three cashu_* columns AND advances the status in one UPDATE guarded by
  'WHERE id = ? AND status = ? AND cashu_escrow_locked_at IS NULL' — no
  lock-without-advance window, replay/race safe; returns whether a row
  matched.
- find_locked_cashu_orders: WHERE cashu_escrow_locked_at IS NOT NULL,
  deliberately without a status predicate (the CAS advances status in
  the same write as the lock, so a status filter would skip legitimately
  locked rows that moved on).

Columns already exist on main (20260530120000_cashu_escrow_fields.sql);
no schema change. New, unreferenced functions — no behaviour change.

Tests: CAS locks once + replay is a no-op preserving the original lock;
status mismatch matches zero rows leaving columns untouched; the finder
includes locked orders regardless of status (e.g. FiatSent) and excludes
unlocked ones. 506 tests pass (503 pre-existing + 3 new).
@coderabbitai

coderabbitai Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@grunch, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 53 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e8129738-ba50-4c8c-a31f-ed18db93071d

📥 Commits

Reviewing files that changed from the base of the PR and between 53e0086 and c393f0a.

📒 Files selected for processing (1)
  • src/db.rs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/cashu-cf4-db-helpers

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@grunch

grunch commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@grunch

grunch commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: c393f0afef

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@codaMW codaMW left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed against mostro-core 0.14. Clean additive DB-helper PR verified the two correctness-critical claims (atomic replay-safe CAS + deliberately status-agnostic finder), not just that it compiles.
Mechanics

• Builds clean (Rust 1.94); cargo clippy --all-targets --all-features -- -D warnings clean.
• cargo test: 506 pass (503 pre-existing + 3 new). The one failure is test_lnurl_validation_with_test_server (AddrInUse) the pre-existing port-bind flake, unrelated.

update_order_cashu_escrow atomic CAS verified
• Single UPDATE … WHERE id = ? AND status = ? AND cashu_escrow_locked_at IS NULL one statement, no read-then-write window. The locked_at IS NULL guard makes a replay/concurrent submit match zero rows instead of double-writing; the cashu_escrow_cas_locks_once_and_replay_is_noop test confirms the original lock survives an attempted overwrite with a different mint/token.

• Persists the three cashu_* columns and the status transition in the same write (no lock-without-advance window), returns rows_affected() > 0 so the caller knows if it won.
• Status is bound via to_string(), which is kebab-case (Status has #[serde(rename_all = "kebab-case")] + a matching Display), consistent with how status is persisted/queried elsewhere (e.g. the 'settled-hold-invoice' literal in db.rs) so the status = ? guard matches real rows. Checked this specifically since a format divergence would silently no-op the CAS; it's consistent.

find_locked_cashu_orders status-agnostic by design, verified
• WHERE cashu_escrow_locked_at IS NOT NULL with no status predicate. Correct per the M-1 note: since the CAS advances status in the same write as the lock, a status filter would skip legitimately locked rows that already moved on. The test proves a locked -> FiatSent order is still returned.

Scope: columns pre-exist (20260530120000_cashu_escrow_fields.sql on main), helpers are unreferenced, zero behavior change so build/clippy + the 3-unit tests against a real pool are the right level; no migration or trade E2E needed here.
ACK: the CAS is the load-bearing bit and it's genuinely race-safe.

@AndreaDiazCorreia AndreaDiazCorreia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK

@Catrya Catrya left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK

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.

4 participants