Commit Graph

16 Commits

Author SHA1 Message Date
thesimplekid
5d98fdf353 Keyset counter (#950)
* feat: refresh keysets

* fix(cdk): resolve keyset counter skipping index 0 in deterministic secret generation

- Modified Database::get_keyset_counter to return u32 instead of Option<u32>
- Added database migrations to increment existing keyset counters by 1
- Removed counter increment logic from wallet operations to use actual counter value
- Ensures deterministic secret generation starts from index 0 instead of skipping it
2025-08-13 08:54:45 +01:00
thesimplekid
69d0cf0818 Merge pull request #952 from thesimplekid/add_payment_method
fix: add payment method to quote
2025-08-13 08:38:04 +01:00
thesimplekid
e87d818137 chore: drop unused mint table melt_request 2025-08-12 13:21:33 +01:00
thesimplekid
12452680ac fix: add payment method to quote 2025-08-12 13:13:23 +01:00
Cesar Rodas
937d9ac43b Introduce run_db_operation_sync and run_db_operation
These functions are designed as a single funnel to talk to the database,
whether it is synchronous or asynchronous.

This single funnel will log SQL queries and slow operations, providing a clear
and unified debug message for the problematic query, so it can be optimized
accordingly (for instance, missing indexes or unbound SQL requests).
2025-08-11 11:55:52 -03:00
Cesar Rodas
2e1099adbe Fixed bug to avoid empty calls get_proofs_states 2025-08-07 22:24:30 -03:00
C
ad8f1ece5c Working on a better database abstraction (#931)
* Working on a better database abstraction

After [this question in the chat](https://matrix.to/#/!oJFtttFHGfnTGrIjvD:matrix.cashu.space/$oJFtttFHGfnTGrIjvD:matrix.cashu.space/$I5ZtjJtBM0ctltThDYpoCwClZFlM6PHzf8q2Rjqmso8)
regarding a database transaction within the same function, I realized a few
design flaws in our SQL database abstraction, particularly regarding
transactions.

1. Our upper abstraction got it right, where a transaction is bound with `&mut
   self`, so Rust knows how to handle its lifetime with' async/await'.
2. The raw database does not; instead, it returns &self, and beginning a
   transaction takes &self as well, which is problematic for Rust, but that's not
   all. It is fundamentally wrong. A transaction should take &mut self when
   beginning a transaction, as that connection is bound to a transaction and
   should not be returned to the pool. Currently, that responsibility lies with
   the implementor. If a mistake is made, a transaction could be executed in two
   or more connections.
3. The way a database is bound to our store layer is through a single struct,
   which may or may not internally utilize our connection pool. This is also
   another design flow, in this PR, a connection pool is owned, and to use a
   connection, it should be requested, and that connection is reference with
   mutable when beginning a transaction

* Improve the abstraction with fewer generics

As suggested by @thesimplekid

* Add BEGIN IMMEDIATE for SQLite
2025-08-06 07:58:03 +01:00
thesimplekid
3c4fce5c45 feat: add keyset u32 mapping migration (#926)
* feat: add keyset u32 mapping migration and duplicate handling

- Add new database migration (version 3) to include u32 representation for keysets
- Implement migration for both redb and SQL databases
- Add duplicate detection and handling for keyset entries
- Create unique index constraint for keyset_u32 column in SQL
- Update keyset storage to include u32 identifiers
- Handle backwards compatibility for existing databases

* chore: clippy

* refactor(cashu): simplify keyset ID verification logic

- Consolidate match expression into a single expression
- Use direct comparison with ensure_cdk macro
- Improve readability of keyset ID validation

* refactor(cdk): rename `fetch_keyset_keys` to `load_keyset_keys` for clarity

- Renamed `fetch_keyset_keys` to `load_keyset_keys` across multiple modules to better reflect its behavior of loading keys from local storage or fetching from mint when missing.
- Added debug logging to indicate when keys are being fetched from the mint.
- Simplified key loading logic in `update_mint_keysets` by removing redundant existence checks.

* chore: remove unused vec
2025-07-31 10:04:38 -04:00
Cesar Rodas
ed7b64b71a Add missing migrations for wallet and auth db 2025-07-29 20:28:09 -03:00
Cesar Rodas
0041c135f7 Fix get_mint_quote_by_request_lookup_id
Fixes #916

The two functions (read and transaction) got out of sync
2025-07-29 11:31:29 -03:00
Cesar Rodas
659518f241 Improve migrations
Add the namespaced migrations and global migrations
2025-07-29 11:31:29 -03:00
C
99ced01e67 Apply suggestions from code review
Co-authored-by: thesimplekid <tsk@thesimplekid.com>
2025-07-29 11:31:29 -03:00
Cesar Rodas
fb4c470a9a Rename still_active to stale 2025-07-29 11:31:29 -03:00
Cesar Rodas
1f85d0fd7a Changes
1. Fix migration to avoid re-running migration twice
2. Remove tokio dependency
2025-07-29 11:31:28 -03:00
Cesar Rodas
034af74013 Add Cache to SQL stmt
The cache will store the placeholders and if possible the RAW SQL with position
placeholders, to avoid repetitive computations
2025-07-29 11:31:28 -03:00
Cesar Rodas
349c773406 Introduce cdk-sql-common
The primary purpose of this new crate is to have a common and shared codebase
for all SQL storage systems. It would force us to write standard SQL using best
practices for all databases.

This crate has been extracted from #878
2025-07-29 11:31:23 -03:00