Commit Graph

70 Commits

Author SHA1 Message Date
thesimplekid
ae6c107809 feat: bolt12 2025-07-13 18:48:35 +01:00
vnprc
180a7c41b7 fix: return the correct error when fetching config values
do not always return UnknownQuoteTTL
return UnknownMintInfo when appropriate
add a new UnknownConfigKey for unknown key values
unit tests to cover this functionality
2025-07-11 19:59:08 -04:00
C
238b09d56a Split the database trait into read and transactions. (#826)
* Split the database trait into read and transactions.

The transaction traits will encapsulate all database changes and also expect
READ-and-lock operations to read and lock records from the database for
exclusive access, thereby avoiding race conditions.

The Transaction trait expects a `rollback` operation on Drop unless the
transaction has been committed.

* fix: melt quote duplicate error

This change stops a second melt quote from being created
if there is an existing valid melt quote for an invoice already.
If the first melt quote has expired then we allow for a new melt quote to be created.

---------

Co-authored-by: thesimplekid <tsk@thesimplekid.com>
2025-06-28 12:07:47 +01:00
thesimplekid
34d8ab763b fix: name of blinded_message col in blind_signature 2025-06-26 12:28:22 +01:00
thesimplekid
ee1a978285 Merge pull request #834 from asmogo/fix_get_proofs_by_keyset_id
fix: add missing comma in SQL query
2025-06-24 11:44:34 +01:00
asmo
7a1879e6b4 fix: add missing comma in SQL query 2025-06-24 08:58:46 +02:00
lollerfirst
c61fd3830a Keysets V2 (#702)
---------
Co-authored-by: thesimplekid <tsk@thesimplekid.com>
2025-06-19 15:36:16 +01:00
C
ad5f29c9a6 Remove melt_request (#819)
* Fix SQLite race condition

Bug: https://github.com/crodas/cdk/actions/runs/15732950296/job/44339804072#step:5:1853

Reason: When melting in parallel, many update the melt status and attempt to
add proofs and they fail when adding the proof and the rollback code kicks in.
The loser process removes all the proofs, and the winner process has no proof
later on.

Fix: Modify `update_melt_quote_state` requirements and implementation to allow
only one winner.

This will be solved by design with a transaction writer trait

* Remove `melt_request`

Fixes #809

* Remove `get_melt_request` from db trait
2025-06-19 15:03:20 +01:00
Cesar Rodas
5ff30d879c Remove multiple ON CONFLICT from SQLite
Fixes #810
2025-06-19 08:51:11 +01:00
Cesar Rodas
a681c6e054 Fix SQLite race condition
Bug: https://github.com/crodas/cdk/actions/runs/15732950296/job/44339804072#step:5:1853

Reason: When melting in parallel, many update the melt status and attempt to
add proofs and they fail when adding the proof and the rollback code kicks in.
The loser process removes all the proofs, and the winner process has no proof
later on.

Fix: Modify `update_melt_quote_state` requirements and implementation to allow
only one winner.

This will be solved by design with a transaction writer trait
2025-06-18 16:20:24 -03:00
Cesar Rodas
150f2596e7 Enhance add_proofs to fail with a custom error when the proof already exists 2025-06-17 17:49:31 -03:00
Cesar Rodas
7146cb8934 Fixed race condition
Bug: https://github.com/cashubtc/cdk/actions/runs/15683152414/job/44190084378?pr=822#step:5:19212

Reason: a race condition between removing proofs while melting and the quote states being updated.

Solution:

1. Error on duplicate proofs
2. Read quote when updating to avoid race conditions and rollbacks

Real solution: A transaction trait in the storage layer. That is coming next
2025-06-17 01:17:35 -03:00
C
5a6b28816a Migrate from sqlx to rusqlite (#783)
* Migrate from `sqlx` to rusqlite

1. Add rusqlite with rusqlite with a working thread
2. Add wallet without a thread (synchronous)
3. Add custom migration

Co-authored-by: thesimplekid <tsk@thesimplekid.com>
2025-06-14 12:49:50 +01:00
thesimplekid
b63dc1045d refactor: nut04 and nut05 (#749) 2025-05-19 09:49:11 +01:00
Cesar Rodas
25fad98aa8 Fix formatting 2025-04-22 11:43:25 -03:00
Cesar Rodas
abdde307c6 Fix race conditions with proof state updates.
Add a strict set of updates to prevent incorrect state changes and correct
usage. Supporting the transaction at the trait level prevented some cases, but
having a strict set of state change flows is better.

This bug was found while developing the signatory. The keys are read from
memory, triggering race conditions at the database, and some `Pending` states
are selected (instead of just selecting `Unspent`).

This PR also introduces a set of generic database tests to be executed for all
database implementations, this test suite will make sure writing and
maintaining new database drivers
2025-04-19 18:15:42 -03:00
thesimplekid
0b9ca1a474 Time time series (#708)
* feat: Add created_time and paid_time fields to MintQuote struct

* feat: Add serde default of 0 for created_time in MintQuote

* feat: Add created_time and paid_time to MintQuote and MeltQuote structs

* feat: Add paid_time update when setting melt quote state to Paid

* fix: Update melt quote state with current Unix timestamp

* feat: Add paid_time update for mint quote when state is set to Paid

* feat: Add issued_time field to MintQuote conversion from SQLite row

* feat: Add issued_time tracking for MintQuoteState::Issued state

* feat: Add migration script for mint time of quotes

* feat: Add timestamp columns to mint_quote and melt_quote tables

* feat: Add timestamp columns to `add_mint_quote` method

* refactor: Improve code formatting and readability in mint quote state update logic

* feat: Add created_time and paid_time columns to melt_quote query

* feat: time on mint and melt quotes

* feat: Add migration script for mint created time signature

feat: Add created_time column to blind_signature table

feat: Add created_time to blind_signature insertion

feat: Add created_time column to proof table and update insert query

feat: time on mint and melt quotes

* feat: Add new table to track blind signature creation time

* feat: Add timestamp tracking for proofs in ReDB database

* feat: redb proof time

* chore: fmt
2025-04-07 12:51:14 +01:00
C
43ab1fdde1 Do not create the wallet struct directly; instead, call new. (#707)
The bug comes with the SQLx-sqlite pool bug, where several connections are
created by default, but the `new` function takes care of that, fixing that bug
by making a single instance of the database.

If constructed directly, the pool would create several connections to the
database, which in most instances is fine, but with SQLite :memory: each
connection is entirely independent.

Also follow documentation to make sure that failed `acquire` will not end up
dropping connections by setting  test_before_acquire to false

     However, if your workload is sensitive to dropped connections such as using an in-memory
     SQLite database with a pool size of 1, you can pretty easily ensure that a cancelled
     `acquire()` call will never drop connections by tweaking your [`PoolOptions`]:

     * Set [`test_before_acquire(false)`][PoolOptions::test_before_acquire]
     * Never set [`before_acquire`][PoolOptions::before_acquire] or
       [`after_connect`][PoolOptions::after_connect].
2025-04-06 07:13:14 +01:00
thesimplekid
5484e7c33a Merge pull request #690 from thesimplekid/request_without_dleq
Request without dleq
2025-03-27 08:45:02 +00:00
codingpeanut157
47903c3bfd split MintDatabase into separate narrower scoped traits
- MintKeysDatabase
- MintQuotesDatabase
- MintProofsDatabase
- MintSignaturesDatabase

This commit splits the MintDatabase trait with 30+ methods into a series
of smaller traits, each dedicate to a specific subsystem of the mint
service.
2025-03-26 16:53:07 +01:00
thesimplekid
be93ff2384 Clear and Blind Auth (#510)
* feat: auth

* chore: corret error codes

* chore: corret error codes

* fix: feature auth in cdk-axum

* refactor: auth logging

* feat: include dleq in auth proof

* feat: mint max auth proofs

* chore: clippy
2025-03-24 11:13:22 +00:00
benthecarman
8cd4ea301a chore: Update sqlx to 0.7.4 2025-03-10 19:01:57 -05:00
thesimplekid
162507c492 feat: payment processor 2025-03-10 14:44:57 +00:00
benthecarman
40c53e83df feat: Add support for sqlcipher 2025-03-09 15:08:43 -05:00
thesimplekid
467cc0a027 feat: Add migration for keyset_id as foreign key in SQLite database (#634) 2025-03-08 22:46:12 +00:00
thesimplekid (aider)
d41d3a7c94 refactor: Add state check before deleting proofs to prevent removing spent proofs 2025-03-06 09:23:40 +00:00
C
f7d9a1b5db Drop the in-memory database (#613)
* Drop the in-memory database

Fixes #607

This PR drops the implementation of in-memory database traits.

They are useful for testing purposes since the tests should test our codebase
and assume the database works as expected (although a follow-up PR should write
a sanity test suite for all database trait implementors).

As complexity is worth with database requirements to simplify complexity and
add more robustness, for instance, with the following plans to add support for
transactions or buffered writes, it would become more complex and
time-consuming to support a correct database trait. This PR drops the
implementation and replaces it with a SQLite memory instance

* Remove OnceCell<Mint>

Without this change, a single Mint is shared for all tests, and the first tests
to run and shutdown makes the other databases (not-reachable, as dropping the
tokio engine would also drop the database instance).

There is no real reason, other than perhaps performance. The mint should
perhaps run in their own tokio engine and share channels as API interfaces, or
a new instance should be created in each tests

* Fixed bug with foreign keys

[1] https://gist.github.com/crodas/bad00997c63bd5ac58db3c5bd90747ed

* Show more debug on failure

* Remove old code

* Remove old references to WalletMemoryDatabase
2025-03-04 19:44:34 +00:00
C
63393056a0 Do not use INSERT OR REPLACE in SQLite. (#620)
Instead, use `INSERT` and `ON CONFLICT`.  The reason is that in case of
conflicts, the `REPLACE` will trigger a DELETE and then perform an INSERT, as
outlined in the documentation[1], and that may cause a cascade of deletion due
to our FOREIGN KEYs.

Here is the official documentation:

```
When a UNIQUE or PRIMARY KEY constraint violation occurs, the REPLACE algorithm
deletes pre-existing rows that are causing the constraint violation prior to
inserting or updating the current row and the command continues executing
normally. If a NOT NULL constraint violation occurs, the REPLACE conflict
resolution replaces the NULL value with the default value for that column, or
if the column has no default value, then the ABORT algorithm is used. If
a CHECK constraint or foreign key constraint violation occurs, the REPLACE
conflict resolution algorithm works like ABORT.  When the REPLACE conflict
resolution strategy deletes rows in order to satisfy a constraint, delete
triggers fire if and only if recursive triggers are enabled.  The update
hook is not invoked for rows that are deleted by the REPLACE conflict
resolution strategy. Nor does REPLACE increment the change counter. The
exceptional behaviors defined in this paragraph might change in a future
release.
```

[1] https://www.sqlite.org/lang_conflict.html
2025-02-28 11:35:29 +00:00
callebtc
b818054f9a Add filtering for mint quote states in database (#586)
* filter for mint quote states in db

---------

Co-authored-by: thesimplekid <tsk@thesimplekid.com>
Co-authored-by: ok300 <106775972+ok300@users.noreply.github.com>
2025-02-08 09:28:54 +00:00
thesimplekid
7b69aa966a feat: add remove proofs to mint db 2025-02-05 10:48:33 +00:00
thesimplekid
c455809812 refactor: move quote_ttl to database 2025-01-29 23:58:13 +00:00
thesimplekid
5481286ec9 refactor: move mint_info to database 2025-01-29 23:29:25 +00:00
thesimplekid
0674144001 refactor: remove mint url from config 2025-01-29 20:38:53 +00:00
C
8fe0982c6d Introduce cashu to host the shared types, traits and other common code (#519)
---------

Co-authored-by: thesimplekid <tsk@thesimplekid.com>
2025-01-12 12:50:05 +00:00
thesimplekid
6a8a5a7941 fix: mpp (#523) 2025-01-05 14:42:44 +00:00
thesimplekid
003a8f1b47 feat: signature on mint witness 2024-12-23 10:25:42 -05:00
Timothée Delabrouille
7d87c4806c feat: use Uuid as mint and melt quote ids (#469) 2024-12-05 09:58:53 +00:00
Cesar Rodas
af2fe580f4 Minor improvement in the SQLite crate
This is a minor improvement over the SQLite crate, which performs fewer SQL
statements and fetches multiple results instead.

This will also remove some redundant commit() and rollback calls. Rollback
already happens on drop, and there is no need for a commit if the database
hasn't changed, as the transaction is used as a locking mechanism in this
context.
2024-11-27 08:33:16 +00:00
ok300
f6533a08de Add Proofs trait to consolidate aggregate fns 2024-10-17 23:28:56 +01:00
thesimplekid
7ea568e615 feat: sql store signatures dleq 2024-09-30 12:20:43 +02:00
thesimplekid
ca6fdb5bff feat: return change in check quote 2024-09-30 09:47:23 +02:00
thesimplekid
5139c47dac feat: check outgoing payment status flow 2024-09-25 20:28:05 +02:00
thesimplekid
974796400d feat: get proofs for quote id 2024-09-23 14:05:28 +02:00
thesimplekid
fa8776998a feat(mint): store mint_quote_id for blind sig 2024-09-21 11:13:30 +02:00
thesimplekid
117443d126 feat: store melt_quote_id for proof 2024-09-21 11:13:30 +02:00
thesimplekid
f2867188a8 fix: sqlx sqlite settings
feat: use transactions in all sql fns
2024-09-21 11:10:38 +02:00
thesimplekid
5f87df2cef refactor: use MintUrl::from_str 2024-09-03 11:36:28 +01:00
thesimplekid
bcb4a5927d refactor: remove the use of flat maps 2024-08-17 14:35:52 +02:00
thesimplekid
ba19663531 feat(mint): issued and redeamed by keyset 2024-07-25 08:43:14 -04:00
thesimplekid
97fd83c8f4 feat(mint): add get blinded_sig by keyset 2024-07-22 16:43:26 +01:00