Skip to main content

cdk_common/database/mint/
mod.rs

1//! CDK Database
2
3use std::collections::HashMap;
4use std::ops::{Deref, DerefMut};
5
6use async_trait::async_trait;
7use cashu::quote_id::QuoteId;
8use cashu::Amount;
9
10use super::{DbTransactionFinalizer, Error};
11use crate::mint::{
12    self, MeltQuote, MintKeySetInfo, MintQuote as MintMintQuote, Operation, ProofsWithState,
13};
14use crate::nuts::{
15    BlindSignature, BlindedMessage, CurrencyUnit, Id, MeltQuoteState, Proof, Proofs, PublicKey,
16    State,
17};
18use crate::payment::PaymentIdentifier;
19
20mod auth;
21
22#[cfg(feature = "test")]
23pub mod test;
24
25pub use auth::{DynMintAuthDatabase, MintAuthDatabase, MintAuthTransaction};
26
27// Re-export KVStore types from shared module for backward compatibility
28pub use super::kvstore::{
29    validate_kvstore_params, validate_kvstore_string, KVStore, KVStoreDatabase, KVStoreTransaction,
30    KVSTORE_NAMESPACE_KEY_ALPHABET, KVSTORE_NAMESPACE_KEY_MAX_LEN,
31};
32
33/// A wrapper indicating that a resource has been acquired with a database lock.
34///
35/// This type is returned by database operations that lock rows for update
36/// (e.g., `SELECT ... FOR UPDATE`). It serves as a compile-time marker that
37/// the wrapped resource was properly locked before being returned, ensuring
38/// that subsequent modifications are safe from race conditions.
39///
40/// # Usage
41///
42/// When you need to modify a database record, first acquire it using a locking
43/// query method. The returned `Acquired<T>` guarantees the row is locked for
44/// the duration of the transaction.
45///
46/// ```ignore
47/// // Acquire a quote with a row lock
48/// let mut quote: Acquired<MintQuote> = tx.get_mint_quote_for_update(&quote_id).await?;
49///
50/// // Safely modify the quote (row is locked)
51/// quote.state = QuoteState::Paid;
52///
53/// // Persist the changes
54/// tx.update_mint_quote(&mut quote).await?;
55/// ```
56///
57/// # Deref Behavior
58///
59/// `Acquired<T>` implements `Deref` and `DerefMut`, allowing transparent access
60/// to the inner value's methods and fields.
61#[derive(Debug)]
62pub struct Acquired<T> {
63    inner: T,
64}
65
66impl<T> From<T> for Acquired<T> {
67    /// Wraps a value to indicate it has been acquired with a lock.
68    ///
69    /// This is typically called by database layer implementations after
70    /// executing a locking query.
71    fn from(value: T) -> Self {
72        Acquired { inner: value }
73    }
74}
75
76impl<T> Acquired<T> {
77    /// Consumes the wrapper and returns the inner resource.
78    ///
79    /// Use this when you need to take ownership of the inner value,
80    /// for example when passing it to a function that doesn't accept
81    /// `Acquired<T>`.
82    pub fn inner(self) -> T {
83        self.inner
84    }
85}
86
87impl<T> Deref for Acquired<T> {
88    type Target = T;
89
90    /// Returns a reference to the inner resource.
91    fn deref(&self) -> &Self::Target {
92        &self.inner
93    }
94}
95
96impl<T> DerefMut for Acquired<T> {
97    /// Returns a mutable reference to the inner resource.
98    fn deref_mut(&mut self) -> &mut Self::Target {
99        &mut self.inner
100    }
101}
102
103/// Information about a melt request stored in the database
104#[derive(Debug, Clone, PartialEq, Eq)]
105pub struct MeltRequestInfo {
106    /// Total amount of all input proofs in the melt request
107    pub inputs_amount: Amount<CurrencyUnit>,
108    /// Fee amount associated with the input proofs
109    pub inputs_fee: Amount<CurrencyUnit>,
110    /// Blinded messages for change outputs
111    pub change_outputs: Vec<BlindedMessage>,
112}
113
114/// Result of locking a melt quote and all related quotes atomically.
115///
116/// This struct is returned by [`QuotesTransaction::lock_melt_quote_and_related`]
117/// and contains both the target quote and all quotes sharing the same `request_lookup_id`.
118#[derive(Debug)]
119pub struct LockedMeltQuotes {
120    /// The target quote that was requested, if found
121    pub target: Option<Acquired<MeltQuote>>,
122    /// All quotes sharing the same `request_lookup_id` (including the target)
123    pub all_related: Vec<Acquired<MeltQuote>>,
124}
125
126/// KeysDatabaseWriter
127#[async_trait]
128pub trait KeysDatabaseTransaction<'a, Error>: DbTransactionFinalizer<Err = Error> {
129    /// Add Active Keyset
130    async fn set_active_keyset(&mut self, unit: CurrencyUnit, id: Id) -> Result<(), Error>;
131
132    /// Add [`MintKeySetInfo`]
133    async fn add_keyset_info(&mut self, keyset: MintKeySetInfo) -> Result<(), Error>;
134}
135
136/// Mint Keys Database trait
137#[async_trait]
138pub trait KeysDatabase {
139    /// Mint Keys Database Error
140    type Err: Into<Error> + From<Error>;
141
142    /// Begins a transaction
143    async fn begin_transaction<'a>(
144        &'a self,
145    ) -> Result<Box<dyn KeysDatabaseTransaction<'a, Self::Err> + Send + Sync + 'a>, Error>;
146
147    /// Get Active Keyset
148    async fn get_active_keyset_id(&self, unit: &CurrencyUnit) -> Result<Option<Id>, Self::Err>;
149
150    /// Get all Active Keyset
151    async fn get_active_keysets(&self) -> Result<HashMap<CurrencyUnit, Id>, Self::Err>;
152
153    /// Get [`MintKeySetInfo`]
154    async fn get_keyset_info(&self, id: &Id) -> Result<Option<MintKeySetInfo>, Self::Err>;
155
156    /// Get [`MintKeySetInfo`]s
157    async fn get_keyset_infos(&self) -> Result<Vec<MintKeySetInfo>, Self::Err>;
158}
159
160/// Mint Quote Database writer trait
161#[async_trait]
162pub trait QuotesTransaction {
163    /// Mint Quotes Database Error
164    type Err: Into<Error> + From<Error>;
165
166    /// Add melt_request with quote_id, inputs_amount, and inputs_fee
167    async fn add_melt_request(
168        &mut self,
169        quote_id: &QuoteId,
170        inputs_amount: Amount<CurrencyUnit>,
171        inputs_fee: Amount<CurrencyUnit>,
172    ) -> Result<(), Self::Err>;
173
174    /// Add blinded_messages for a quote_id
175    async fn add_blinded_messages(
176        &mut self,
177        quote_id: Option<&QuoteId>,
178        blinded_messages: &[BlindedMessage],
179        operation: &Operation,
180    ) -> Result<(), Self::Err>;
181
182    /// Delete blinded_messages by their blinded secrets
183    async fn delete_blinded_messages(
184        &mut self,
185        blinded_secrets: &[PublicKey],
186    ) -> Result<(), Self::Err>;
187
188    /// Get melt_request and associated blinded_messages by quote_id
189    async fn get_melt_request_and_blinded_messages(
190        &mut self,
191        quote_id: &QuoteId,
192    ) -> Result<Option<MeltRequestInfo>, Self::Err>;
193
194    /// Delete melt_request and associated blinded_messages by quote_id
195    async fn delete_melt_request(&mut self, quote_id: &QuoteId) -> Result<(), Self::Err>;
196
197    /// Get [`MintMintQuote`] and lock it for update in this transaction
198    async fn get_mint_quote(
199        &mut self,
200        quote_id: &QuoteId,
201    ) -> Result<Option<Acquired<MintMintQuote>>, Self::Err>;
202
203    /// Add [`MintMintQuote`]
204    async fn add_mint_quote(
205        &mut self,
206        quote: MintMintQuote,
207    ) -> Result<Acquired<MintMintQuote>, Self::Err>;
208
209    /// Persists any pending changes made to the mint quote.
210    ///
211    /// This method extracts changes accumulated in the quote (via [`mint::MintQuote::take_changes`])
212    /// and persists them to the database. Changes may include new payments received or new
213    /// issuances recorded against the quote.
214    ///
215    /// If no changes are pending, this method returns successfully without performing
216    /// any database operations.
217    ///
218    /// # Arguments
219    ///
220    /// * `quote` - A mutable reference to an acquired (row-locked) mint quote. The quote
221    ///   must be locked to ensure transactional consistency when persisting changes.
222    ///
223    /// # Implementation Notes
224    ///
225    /// Implementations should call [`mint::MintQuote::take_changes`] to retrieve pending
226    /// changes, then persist each payment and issuance record, and finally update the
227    /// quote's aggregate counters (`amount_paid`, `amount_issued`) in the database.
228    async fn update_mint_quote(
229        &mut self,
230        quote: &mut Acquired<mint::MintQuote>,
231    ) -> Result<(), Self::Err>;
232
233    /// Get [`mint::MeltQuote`] and lock it for update in this transaction
234    async fn get_melt_quote(
235        &mut self,
236        quote_id: &QuoteId,
237    ) -> Result<Option<Acquired<mint::MeltQuote>>, Self::Err>;
238
239    /// Add [`mint::MeltQuote`]
240    async fn add_melt_quote(&mut self, quote: mint::MeltQuote) -> Result<(), Self::Err>;
241
242    /// Retrieves all melt quotes matching a payment lookup identifier and locks them for update.
243    ///
244    /// This method returns multiple quotes because certain payment methods (notably BOLT12 offers)
245    /// can generate multiple payment attempts that share the same lookup identifier. Locking all
246    /// related quotes prevents race conditions where concurrent melt operations could interfere
247    /// with each other, potentially leading to double-spending or state inconsistencies.
248    ///
249    /// The returned quotes are locked within the current transaction to ensure safe concurrent
250    /// modification. This is essential during melt saga initiation and finalization to guarantee
251    /// atomic state transitions across all related quotes.
252    ///
253    /// # Arguments
254    ///
255    /// * `request_lookup_id` - The payment identifier used by the Lightning backend to track
256    ///   payment state (e.g., payment hash, offer ID, or label).
257    async fn get_melt_quotes_by_request_lookup_id(
258        &mut self,
259        request_lookup_id: &PaymentIdentifier,
260    ) -> Result<Vec<Acquired<MeltQuote>>, Self::Err>;
261
262    /// Locks a melt quote and all related quotes sharing the same request_lookup_id atomically.
263    ///
264    /// This method prevents deadlocks by acquiring all locks in a single query with consistent
265    /// ordering, rather than locking the target quote first and then related quotes separately.
266    ///
267    /// # Deadlock Prevention
268    ///
269    /// When multiple transactions try to melt quotes sharing the same `request_lookup_id`,
270    /// acquiring locks in two steps (first the target quote, then all related quotes) can cause
271    /// circular wait deadlocks. This method avoids that by:
272    /// 1. Using a subquery to find the `request_lookup_id` for the target quote
273    /// 2. Locking ALL quotes with that `request_lookup_id` in one atomic operation
274    /// 3. Ordering locks consistently by quote ID
275    ///
276    /// # Arguments
277    ///
278    /// * `quote_id` - The ID of the target melt quote
279    ///
280    /// # Returns
281    ///
282    /// A [`LockedMeltQuotes`] containing:
283    /// - `target`: The target quote (if found)
284    /// - `all_related`: All quotes sharing the same `request_lookup_id` (including the target)
285    ///
286    /// If the quote has no `request_lookup_id`, only the target quote is returned and locked.
287    async fn lock_melt_quote_and_related(
288        &mut self,
289        quote_id: &QuoteId,
290    ) -> Result<LockedMeltQuotes, Self::Err>;
291
292    /// Updates the request lookup id for a melt quote.
293    ///
294    /// Requires an [`Acquired`] melt quote to ensure the row is locked before modification.
295    async fn update_melt_quote_request_lookup_id(
296        &mut self,
297        quote: &mut Acquired<mint::MeltQuote>,
298        new_request_lookup_id: &PaymentIdentifier,
299    ) -> Result<(), Self::Err>;
300
301    /// Update [`mint::MeltQuote`] state.
302    ///
303    /// Requires an [`Acquired`] melt quote to ensure the row is locked before modification.
304    /// Returns the previous state.
305    async fn update_melt_quote_state(
306        &mut self,
307        quote: &mut Acquired<mint::MeltQuote>,
308        new_state: MeltQuoteState,
309        payment_proof: Option<String>,
310    ) -> Result<MeltQuoteState, Self::Err>;
311
312    /// Get all [`MintMintQuote`]s and lock it for update in this transaction
313    async fn get_mint_quote_by_request(
314        &mut self,
315        request: &str,
316    ) -> Result<Option<Acquired<MintMintQuote>>, Self::Err>;
317
318    /// Get all [`MintMintQuote`]s
319    async fn get_mint_quote_by_request_lookup_id(
320        &mut self,
321        request_lookup_id: &PaymentIdentifier,
322    ) -> Result<Option<Acquired<MintMintQuote>>, Self::Err>;
323}
324
325/// Mint Quote Database trait
326#[async_trait]
327pub trait QuotesDatabase {
328    /// Mint Quotes Database Error
329    type Err: Into<Error> + From<Error>;
330
331    /// Get [`MintMintQuote`]
332    async fn get_mint_quote(&self, quote_id: &QuoteId) -> Result<Option<MintMintQuote>, Self::Err>;
333
334    /// Get all [`MintMintQuote`]s
335    async fn get_mint_quote_by_request(
336        &self,
337        request: &str,
338    ) -> Result<Option<MintMintQuote>, Self::Err>;
339    /// Get all [`MintMintQuote`]s
340    async fn get_mint_quote_by_request_lookup_id(
341        &self,
342        request_lookup_id: &PaymentIdentifier,
343    ) -> Result<Option<MintMintQuote>, Self::Err>;
344    /// Get Mint Quotes
345    async fn get_mint_quotes(&self) -> Result<Vec<MintMintQuote>, Self::Err>;
346    /// Get [`mint::MeltQuote`]
347    async fn get_melt_quote(
348        &self,
349        quote_id: &QuoteId,
350    ) -> Result<Option<mint::MeltQuote>, Self::Err>;
351    /// Get all [`mint::MeltQuote`]s
352    async fn get_melt_quotes(&self) -> Result<Vec<mint::MeltQuote>, Self::Err>;
353}
354
355/// Mint Proof Transaction trait
356#[async_trait]
357pub trait ProofsTransaction {
358    /// Mint Proof Database Error
359    type Err: Into<Error> + From<Error>;
360
361    /// Add  [`Proofs`]
362    ///
363    /// Adds proofs to the database. The database should error if the proof already exits, with a
364    /// `AttemptUpdateSpentProof` if the proof is already spent or a `Duplicate` error otherwise.
365    async fn add_proofs(
366        &mut self,
367        proof: Proofs,
368        quote_id: Option<QuoteId>,
369        operation: &Operation,
370    ) -> Result<Acquired<ProofsWithState>, Self::Err>;
371
372    /// Updates the proofs to the given state in the database.
373    ///
374    /// Also updates the `state` field on the [`ProofsWithState`] wrapper to reflect
375    /// the new state after the database update succeeds.
376    async fn update_proofs_state(
377        &mut self,
378        proofs: &mut Acquired<ProofsWithState>,
379        new_state: State,
380    ) -> Result<(), Self::Err>;
381
382    /// get proofs states
383    async fn get_proofs(
384        &mut self,
385        ys: &[PublicKey],
386    ) -> Result<Acquired<ProofsWithState>, Self::Err>;
387
388    /// Remove [`Proofs`]
389    async fn remove_proofs(
390        &mut self,
391        ys: &[PublicKey],
392        quote_id: Option<QuoteId>,
393    ) -> Result<(), Self::Err>;
394
395    /// Get ys by quote id
396    async fn get_proof_ys_by_quote_id(
397        &mut self,
398        quote_id: &QuoteId,
399    ) -> Result<Vec<PublicKey>, Self::Err>;
400
401    /// Get proof ys by operation id
402    async fn get_proof_ys_by_operation_id(
403        &mut self,
404        operation_id: &uuid::Uuid,
405    ) -> Result<Vec<PublicKey>, Self::Err>;
406}
407
408/// Mint Proof Database trait
409#[async_trait]
410pub trait ProofsDatabase {
411    /// Mint Proof Database Error
412    type Err: Into<Error> + From<Error>;
413
414    /// Get [`Proofs`] by ys
415    async fn get_proofs_by_ys(&self, ys: &[PublicKey]) -> Result<Vec<Option<Proof>>, Self::Err>;
416    /// Get ys by quote id
417    async fn get_proof_ys_by_quote_id(
418        &self,
419        quote_id: &QuoteId,
420    ) -> Result<Vec<PublicKey>, Self::Err>;
421    /// Get [`Proofs`] state
422    async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err>;
423
424    /// Get [`Proofs`] by state
425    async fn get_proofs_by_keyset_id(
426        &self,
427        keyset_id: &Id,
428    ) -> Result<(Proofs, Vec<Option<State>>), Self::Err>;
429
430    /// Get total proofs redeemed by keyset id
431    async fn get_total_redeemed(&self) -> Result<HashMap<Id, Amount>, Self::Err>;
432
433    /// Get proof ys by operation id
434    async fn get_proof_ys_by_operation_id(
435        &self,
436        operation_id: &uuid::Uuid,
437    ) -> Result<Vec<PublicKey>, Self::Err>;
438}
439
440#[async_trait]
441/// Mint Signatures Transaction trait
442pub trait SignaturesTransaction {
443    /// Mint Signature Database Error
444    type Err: Into<Error> + From<Error>;
445
446    /// Add [`BlindSignature`]
447    async fn add_blind_signatures(
448        &mut self,
449        blinded_messages: &[PublicKey],
450        blind_signatures: &[BlindSignature],
451        quote_id: Option<QuoteId>,
452    ) -> Result<(), Self::Err>;
453
454    /// Get [`BlindSignature`]s
455    async fn get_blind_signatures(
456        &mut self,
457        blinded_messages: &[PublicKey],
458    ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
459}
460
461#[async_trait]
462/// Mint Signatures Database trait
463pub trait SignaturesDatabase {
464    /// Mint Signature Database Error
465    type Err: Into<Error> + From<Error>;
466
467    /// Get [`BlindSignature`]s
468    async fn get_blind_signatures(
469        &self,
470        blinded_messages: &[PublicKey],
471    ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
472
473    /// Get [`BlindSignature`]s for keyset_id
474    async fn get_blind_signatures_for_keyset(
475        &self,
476        keyset_id: &Id,
477    ) -> Result<Vec<BlindSignature>, Self::Err>;
478
479    /// Get [`BlindSignature`]s for quote
480    async fn get_blind_signatures_for_quote(
481        &self,
482        quote_id: &QuoteId,
483    ) -> Result<Vec<BlindSignature>, Self::Err>;
484
485    /// Get total amount issued by keyset id
486    async fn get_total_issued(&self) -> Result<HashMap<Id, Amount>, Self::Err>;
487
488    /// Get blinded secrets (B values) by operation id
489    async fn get_blinded_secrets_by_operation_id(
490        &self,
491        operation_id: &uuid::Uuid,
492    ) -> Result<Vec<PublicKey>, Self::Err>;
493}
494
495#[async_trait]
496/// Saga Transaction trait
497pub trait SagaTransaction {
498    /// Saga Database Error
499    type Err: Into<Error> + From<Error>;
500
501    /// Get saga by operation_id
502    async fn get_saga(
503        &mut self,
504        operation_id: &uuid::Uuid,
505    ) -> Result<Option<mint::Saga>, Self::Err>;
506
507    /// Add saga
508    async fn add_saga(&mut self, saga: &mint::Saga) -> Result<(), Self::Err>;
509
510    /// Update saga state (only updates state and updated_at fields)
511    async fn update_saga(
512        &mut self,
513        operation_id: &uuid::Uuid,
514        new_state: mint::SagaStateEnum,
515    ) -> Result<(), Self::Err>;
516
517    /// Delete saga
518    async fn delete_saga(&mut self, operation_id: &uuid::Uuid) -> Result<(), Self::Err>;
519}
520
521#[async_trait]
522/// Saga Database trait
523pub trait SagaDatabase {
524    /// Saga Database Error
525    type Err: Into<Error> + From<Error>;
526
527    /// Get all incomplete sagas for a given operation kind
528    async fn get_incomplete_sagas(
529        &self,
530        operation_kind: mint::OperationKind,
531    ) -> Result<Vec<mint::Saga>, Self::Err>;
532}
533
534#[async_trait]
535/// Completed Operations Transaction trait
536pub trait CompletedOperationsTransaction {
537    /// Completed Operations Database Error
538    type Err: Into<Error> + From<Error>;
539
540    /// Add completed operation
541    async fn add_completed_operation(
542        &mut self,
543        operation: &mint::Operation,
544        fee_by_keyset: &std::collections::HashMap<crate::nuts::Id, crate::Amount>,
545    ) -> Result<(), Self::Err>;
546}
547
548#[async_trait]
549/// Completed Operations Database trait
550pub trait CompletedOperationsDatabase {
551    /// Completed Operations Database Error
552    type Err: Into<Error> + From<Error>;
553
554    /// Get completed operation by operation_id
555    async fn get_completed_operation(
556        &self,
557        operation_id: &uuid::Uuid,
558    ) -> Result<Option<mint::Operation>, Self::Err>;
559
560    /// Get completed operations by operation kind
561    async fn get_completed_operations_by_kind(
562        &self,
563        operation_kind: mint::OperationKind,
564    ) -> Result<Vec<mint::Operation>, Self::Err>;
565
566    /// Get all completed operations
567    async fn get_completed_operations(&self) -> Result<Vec<mint::Operation>, Self::Err>;
568}
569
570/// Base database writer
571pub trait Transaction<Error>:
572    DbTransactionFinalizer<Err = Error>
573    + QuotesTransaction<Err = Error>
574    + SignaturesTransaction<Err = Error>
575    + ProofsTransaction<Err = Error>
576    + KVStoreTransaction<Error>
577    + SagaTransaction<Err = Error>
578    + CompletedOperationsTransaction<Err = Error>
579{
580}
581
582/// Mint Database trait
583#[async_trait]
584pub trait Database<Error>:
585    KVStoreDatabase<Err = Error>
586    + QuotesDatabase<Err = Error>
587    + ProofsDatabase<Err = Error>
588    + SignaturesDatabase<Err = Error>
589    + SagaDatabase<Err = Error>
590    + CompletedOperationsDatabase<Err = Error>
591{
592    /// Begins a transaction
593    async fn begin_transaction(&self) -> Result<Box<dyn Transaction<Error> + Send + Sync>, Error>;
594}
595
596/// Type alias for Mint Database
597pub type DynMintDatabase = std::sync::Arc<dyn Database<Error> + Send + Sync>;
598
599/// Type alias for Mint Transaction
600pub type DynMintTransaction = Box<dyn Transaction<Error> + Send + Sync>;