cdk 0.16.0-rc.0

Core Cashu Development Kit library implementing the Cashu protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! Check used at mint start up
//!
//! These checks are need in the case the mint was offline and the lightning node was node.
//! These ensure that the status of the mint or melt quote matches in the mint db and on the node.

use std::str::FromStr;

use cdk_common::mint::{OperationKind, Saga};
use cdk_common::QuoteId;

use super::{Error, Mint};
use crate::mint::swap::swap_saga::compensation::{CompensatingAction, RemoveSwapSetup};
use crate::mint::{MeltQuote, MeltQuoteState};
use crate::types::PaymentProcessorKey;

impl Mint {
    /// Get incomplete melt saga by quote_id
    async fn get_melt_saga_by_quote_id(&self, quote_id: &str) -> Result<Option<Saga>, Error> {
        let incomplete_sagas = self
            .localstore
            .get_incomplete_sagas(OperationKind::Melt)
            .await?;

        for saga in incomplete_sagas {
            if let Some(ref qid) = saga.quote_id {
                if qid == quote_id {
                    return Ok(Some(saga));
                }
            }
        }
        Ok(None)
    }

    /// Checks the payment status of a melt quote with the LN backend
    ///
    /// This is a helper function used by saga recovery to determine whether to
    /// finalize or compensate an incomplete melt operation.
    ///
    /// # Returns
    ///
    /// - `Ok(MakePaymentResponse)`: Payment status successfully retrieved from backend
    /// - `Err(Error)`: Failed to check payment status (backend unavailable, no lookup_id, etc.)
    async fn check_melt_payment_status(
        &self,
        quote: &MeltQuote,
    ) -> Result<crate::cdk_payment::MakePaymentResponse, Error> {
        let ln_key = PaymentProcessorKey {
            unit: quote.unit.clone(),
            method: quote.payment_method.clone(),
        };

        let ln_backend = self.payment_processors.get(&ln_key).ok_or_else(|| {
            tracing::warn!("No backend for ln key: {:?}", ln_key);
            Error::UnsupportedUnit
        })?;

        let lookup_id = quote.request_lookup_id.as_ref().ok_or_else(|| {
            tracing::warn!(
                "No lookup_id for melt quote {}, cannot check payment status",
                quote.id
            );
            Error::Internal
        })?;

        // Check payment status with LN backend
        let pay_invoice_response =
            ln_backend
                .check_outgoing_payment(lookup_id)
                .await
                .map_err(|err| {
                    tracing::error!(
                        "Failed to check payment status for quote {}: {}",
                        quote.id,
                        err
                    );
                    Error::Internal
                })?;

        tracing::info!(
            "Payment status for melt quote {}: {}",
            quote.id,
            pay_invoice_response.status
        );

        Ok(pay_invoice_response)
    }

    /// Finalizes a paid melt quote during startup check
    ///
    /// Uses shared finalization logic from melt::shared module
    async fn finalize_paid_melt_quote(
        &self,
        quote: &MeltQuote,
        total_spent: cdk_common::Amount<cdk_common::CurrencyUnit>,
        payment_preimage: Option<String>,
        payment_lookup_id: &cdk_common::payment::PaymentIdentifier,
    ) -> Result<(), Error> {
        tracing::info!("Finalizing paid melt quote {} during startup", quote.id);

        // Use shared finalization
        super::melt::shared::finalize_melt_quote(
            self,
            &self.localstore,
            &self.pubsub_manager,
            quote,
            total_spent,
            payment_preimage,
            payment_lookup_id,
        )
        .await?;

        tracing::info!(
            "Successfully finalized melt quote {} during startup check",
            quote.id
        );

        Ok(())
    }

    /// Checks all persisted sagas for swap operations and compensates
    /// incomplete ones by removing both proofs and blinded messages.
    pub async fn recover_from_incomplete_sagas(&self) -> Result<(), Error> {
        let incomplete_sagas = self
            .localstore
            .get_incomplete_sagas(OperationKind::Swap)
            .await?;

        if incomplete_sagas.is_empty() {
            tracing::info!("No incomplete swap sagas found to recover.");
            return Ok(());
        }

        let total_sagas = incomplete_sagas.len();
        tracing::info!("Found {} incomplete swap sagas to recover.", total_sagas);

        for saga in incomplete_sagas {
            tracing::info!(
                "Recovering saga {} in state '{}' (created: {}, updated: {})",
                saga.operation_id,
                saga.state.state(),
                saga.created_at,
                saga.updated_at
            );

            // Look up input_ys and blinded_secrets from the proof and blind_signature tables
            let input_ys = self
                .localstore
                .get_proof_ys_by_operation_id(&saga.operation_id)
                .await?;
            let blinded_secrets = self
                .localstore
                .get_blinded_secrets_by_operation_id(&saga.operation_id)
                .await?;

            // Use the same compensation logic as in-process failures
            // Saga deletion is included in the compensation transaction
            let compensation = RemoveSwapSetup {
                blinded_secrets,
                input_ys,
                operation_id: saga.operation_id,
            };

            // Execute compensation (includes saga deletion)
            if let Err(e) = compensation
                .execute(&self.localstore, &self.pubsub_manager)
                .await
            {
                tracing::error!(
                    "Failed to compensate saga {}: {}. Continuing...",
                    saga.operation_id,
                    e
                );
                continue;
            }

            tracing::info!("Successfully recovered saga {}", saga.operation_id);
        }

        tracing::info!(
            "Successfully recovered {} incomplete swap sagas.",
            total_sagas
        );

        Ok(())
    }

    /// Recover from incomplete melt sagas
    ///
    /// Checks all persisted sagas for melt operations and determines whether to:
    /// - **Finalize**: If payment was confirmed as PAID on LN backend
    /// - **Compensate**: If payment was confirmed as UNPAID/FAILED or never sent
    /// - **Skip**: If payment is PENDING/UNKNOWN (leave for check_pending_melt_quotes)
    ///
    /// This recovery handles SetupComplete state which means:
    /// - Proofs were reserved (marked as PENDING)
    /// - Change outputs were added
    /// - Payment may or may not have been sent
    ///
    /// # Critical Bug Fix
    ///
    /// Previously, this function always compensated (rolled back) incomplete sagas without
    /// checking if the payment actually succeeded on the LN backend. This could cause the
    /// mint to lose funds if:
    /// 1. Payment succeeded on LN backend
    /// 2. Mint crashed before finalize() committed
    /// 3. Recovery compensated (returned proofs) instead of finalizing
    ///
    /// Now we check the LN backend payment status before deciding whether to compensate or finalize.
    pub async fn recover_from_incomplete_melt_sagas(&self) -> Result<(), Error> {
        let incomplete_sagas = self
            .localstore
            .get_incomplete_sagas(OperationKind::Melt)
            .await?;

        if incomplete_sagas.is_empty() {
            tracing::info!("No incomplete melt sagas found to recover.");
            return Ok(());
        }

        let total_sagas = incomplete_sagas.len();
        tracing::info!("Found {} incomplete melt sagas to recover.", total_sagas);

        for saga in incomplete_sagas {
            tracing::info!(
                "Recovering melt saga {} in state '{}' (created: {}, updated: {})",
                saga.operation_id,
                saga.state.state(),
                saga.created_at,
                saga.updated_at
            );

            // Look up input_ys and blinded_secrets from the proof and blind_signature tables
            let input_ys = self
                .localstore
                .get_proof_ys_by_operation_id(&saga.operation_id)
                .await?;
            let blinded_secrets = self
                .localstore
                .get_blinded_secrets_by_operation_id(&saga.operation_id)
                .await?;

            // Get quote_id from saga (new field added for efficient lookup)
            let quote_id = match saga.quote_id {
                Some(ref qid) => qid.clone(),
                None => {
                    tracing::warn!(
                        "Saga {} has no quote_id (old saga format) - attempting fallback lookup",
                        saga.operation_id
                    );

                    // Fallback: Find quote by matching input_ys (for backward compatibility)
                    let melt_quotes = match self.localstore.get_melt_quotes().await {
                        Ok(quotes) => quotes,
                        Err(e) => {
                            tracing::error!(
                                "Failed to get melt quotes for saga {}: {}",
                                saga.operation_id,
                                e
                            );
                            continue;
                        }
                    };

                    let mut quote_id_found = None;
                    for quote in melt_quotes {
                        let mut tx = self.localstore.begin_transaction().await?;
                        let proof_ys = tx.get_proof_ys_by_quote_id(&quote.id).await?;
                        tx.rollback().await?;

                        if !input_ys.is_empty()
                            && !proof_ys.is_empty()
                            && input_ys.iter().any(|y| proof_ys.contains(y))
                        {
                            quote_id_found = Some(quote.id.clone());
                            break;
                        }
                    }

                    match quote_id_found {
                        Some(qid) => qid.to_string(),
                        None => {
                            tracing::warn!(
                                "Could not find quote_id for saga {} - may have been cleaned up already. Deleting orphaned saga.",
                                saga.operation_id
                            );

                            let mut delete_tx = self.localstore.begin_transaction().await?;
                            if let Err(e) = delete_tx.delete_saga(&saga.operation_id).await {
                                tracing::error!(
                                    "Failed to delete orphaned saga {}: {}",
                                    saga.operation_id,
                                    e
                                );
                                delete_tx.rollback().await?;
                            } else {
                                delete_tx.commit().await?;
                            }
                            continue;
                        }
                    }
                }
            };

            // Get the quote from database
            let quote_id_parsed = match QuoteId::from_str(&quote_id) {
                Ok(id) => id,
                Err(e) => {
                    tracing::error!(
                        "Failed to parse quote_id '{}' for saga {}: {:?}. Skipping saga.",
                        quote_id,
                        saga.operation_id,
                        e
                    );
                    continue;
                }
            };

            let mut quote = match self.localstore.get_melt_quote(&quote_id_parsed).await {
                Ok(Some(q)) => q,
                Ok(None) => {
                    tracing::warn!(
                        "Quote {} for saga {} not found - may have been cleaned up. Deleting orphaned saga.",
                        quote_id,
                        saga.operation_id
                    );

                    let mut delete_tx = self.localstore.begin_transaction().await?;
                    if let Err(e) = delete_tx.delete_saga(&saga.operation_id).await {
                        tracing::error!(
                            "Failed to delete orphaned saga {}: {}",
                            saga.operation_id,
                            e
                        );
                        delete_tx.rollback().await?;
                    } else {
                        delete_tx.commit().await?;
                    }
                    continue;
                }
                Err(e) => {
                    tracing::error!(
                        "Failed to get quote {} for saga {}: {}. Skipping saga.",
                        quote_id,
                        saga.operation_id,
                        e
                    );
                    continue;
                }
            };

            // Check saga state to determine if payment was attempted
            // SetupComplete means setup transaction committed but payment NOT yet attempted
            // PaymentAttempted means payment was attempted - must check LN backend
            let should_compensate = match &saga.state {
                cdk_common::mint::SagaStateEnum::Melt(state) => {
                    match state {
                        cdk_common::mint::MeltSagaState::SetupComplete => {
                            // Setup complete but payment never attempted - always compensate
                            tracing::info!(
                                "Saga {} in SetupComplete state - payment never attempted, will compensate",
                                saga.operation_id
                            );
                            true
                        }
                        cdk_common::mint::MeltSagaState::Finalizing => {
                            // TX1 committed (proofs Spent, quote Paid) - finalize change + cleanup
                            tracing::info!(
                                "Saga {} in Finalizing state - TX1 completed, will finalize change and cleanup",
                                saga.operation_id
                            );

                            let total_spent = quote.amount();
                            let payment_lookup_id =
                                quote.request_lookup_id.clone().unwrap_or_else(|| {
                                    cdk_common::payment::PaymentIdentifier::CustomId(
                                        quote.id.to_string(),
                                    )
                                });

                            if let Err(err) = self
                                .finalize_paid_melt_quote(
                                    &quote,
                                    total_spent,
                                    None,
                                    &payment_lookup_id,
                                )
                                .await
                            {
                                tracing::error!(
                                    "Failed to finalize Finalizing saga {}: {}. Will retry.",
                                    saga.operation_id,
                                    err
                                );
                                continue;
                            }

                            // Delete saga after successful finalization
                            let mut tx = self.localstore.begin_transaction().await?;
                            if let Err(e) = tx.delete_saga(&saga.operation_id).await {
                                tracing::error!(
                                    "Failed to delete saga {}: {}. Will retry on next recovery cycle.",
                                    saga.operation_id,
                                    e
                                );
                                tx.rollback().await?;
                                continue;
                            }
                            tx.commit().await?;
                            tracing::info!(
                                "Successfully recovered Finalizing saga {}",
                                saga.operation_id
                            );
                            continue;
                        }
                        cdk_common::mint::MeltSagaState::PaymentAttempted => {
                            // Payment was attempted - check for internal settlement first, then LN backend
                            tracing::info!(
                                "Saga {} in PaymentAttempted state - checking for internal or external payment",
                                saga.operation_id
                            );

                            // Check if this was an internal settlement by looking for a mint quote
                            // that was paid by this melt quote
                            let is_internal_settlement = match self
                                .localstore
                                .get_mint_quote_by_request(&quote.request.to_string())
                                .await
                            {
                                Ok(Some(mint_quote)) => {
                                    // Check if this mint quote was paid by our melt quote
                                    let melt_quote_id_str = quote.id.to_string();
                                    mint_quote.payment_ids().contains(&&melt_quote_id_str)
                                }
                                Ok(None) => false,
                                Err(e) => {
                                    tracing::warn!(
                                        "Error checking for internal settlement for saga {}: {}",
                                        saga.operation_id,
                                        e
                                    );
                                    false
                                }
                            };

                            if is_internal_settlement {
                                // Internal settlement was completed - finalize directly
                                tracing::info!(
                                    "Saga {} was internal settlement - will finalize directly",
                                    saga.operation_id
                                );

                                // Get payment info for finalization
                                let total_spent = quote.amount();
                                let payment_lookup_id =
                                    quote.request_lookup_id.clone().unwrap_or_else(|| {
                                        cdk_common::payment::PaymentIdentifier::CustomId(
                                            quote.id.to_string(),
                                        )
                                    });

                                if let Err(err) = self
                                    .finalize_paid_melt_quote(
                                        &quote,
                                        total_spent,
                                        None, // No preimage for internal settlement
                                        &payment_lookup_id,
                                    )
                                    .await
                                {
                                    tracing::error!(
                                        "Failed to finalize internal settlement saga {}: {}. Will retry on next recovery cycle.",
                                        saga.operation_id,
                                        err
                                    );
                                    continue;
                                }

                                // Delete saga after successful finalization
                                let mut tx = self.localstore.begin_transaction().await?;
                                if let Err(e) = tx.delete_saga(&saga.operation_id).await {
                                    tracing::error!(
                                        "Failed to delete saga {}: {}. Will retry on next recovery cycle.",
                                        saga.operation_id,
                                        e
                                    );
                                    tx.rollback().await?;
                                    continue;
                                }
                                tx.commit().await?;
                                tracing::info!(
                                    "Successfully recovered and finalized internal settlement saga {}",
                                    saga.operation_id
                                );

                                continue; // Skip to next saga
                            }

                            false // Will check LN payment status below
                        }
                    }
                }
                _ => {
                    continue; // Skip non-melt sagas
                }
            };

            let should_compensate = if should_compensate {
                true
            } else if quote.request_lookup_id.is_none() {
                // Fallback: No request_lookup_id means payment likely never sent
                tracing::info!(
                    "Saga {} for quote {} has no request_lookup_id - payment never sent, will compensate",
                    saga.operation_id,
                    quote_id
                );
                true
            } else {
                // Payment was attempted - check LN backend status
                tracing::info!(
                    "Saga {} for quote {} has request_lookup_id - checking payment status with LN backend",
                    saga.operation_id,
                    quote_id
                );

                match self.check_melt_payment_status(&quote).await {
                    Ok(payment_response) => {
                        match payment_response.status {
                            MeltQuoteState::Paid => {
                                if let Err(err) = super::saga_recovery::process_melt_saga_outcome(
                                    &saga,
                                    &mut quote,
                                    &payment_response,
                                    &self.localstore,
                                    &self.pubsub_manager,
                                    self,
                                )
                                .await
                                {
                                    tracing::error!(
                                        "Failed to process paid melt saga {}: {}. Will retry on next recovery cycle.",
                                        saga.operation_id,
                                        err
                                    );
                                    continue;
                                }
                                continue; // Saga handled
                            }
                            MeltQuoteState::Unpaid | MeltQuoteState::Failed => {
                                if let Err(err) = super::saga_recovery::process_melt_saga_outcome(
                                    &saga,
                                    &mut quote,
                                    &payment_response,
                                    &self.localstore,
                                    &self.pubsub_manager,
                                    self,
                                )
                                .await
                                {
                                    tracing::error!(
                                        "Failed to process failed melt saga {}: {}. Will retry on next recovery cycle.",
                                        saga.operation_id,
                                        err
                                    );
                                    continue;
                                }
                                continue; // Saga handled
                            }
                            MeltQuoteState::Pending | MeltQuoteState::Unknown => {
                                // Payment still pending - skip for check_pending_melt_quotes
                                tracing::info!(
                                    "Saga {} for quote {} - payment {} on LN backend, skipping",
                                    saga.operation_id,
                                    quote_id,
                                    payment_response.status
                                );
                                continue; // Skip this saga
                            }
                        }
                    }
                    Err(err) => {
                        // LN backend unavailable - skip this saga, will retry on next recovery cycle
                        tracing::warn!(
                            "Failed to check payment status for saga {} quote {}: {}. Skipping for now, will retry on next recovery cycle.",
                            saga.operation_id,
                            quote_id,
                            err
                        );
                        continue; // Skip this saga
                    }
                }
            };

            // Compensate if needed
            if should_compensate {
                tracing::info!(
                    "Compensating melt saga {} (removing {} proofs, {} change outputs)",
                    saga.operation_id,
                    input_ys.len(),
                    blinded_secrets.len()
                );

                if let Err(err) = super::melt::shared::rollback_melt_quote(
                    &self.localstore,
                    &self.pubsub_manager,
                    &quote_id_parsed,
                    &input_ys,
                    &blinded_secrets,
                    &saga.operation_id,
                )
                .await
                {
                    tracing::error!(
                        "Failed to rollback melt quote {} for saga {}: {}",
                        quote_id_parsed,
                        saga.operation_id,
                        err
                    );
                }
            }
        }

        tracing::info!(
            "Successfully recovered {} incomplete melt sagas.",
            total_sagas
        );

        Ok(())
    }

    /// Handle pending melt quote by resuming the saga
    pub(crate) async fn handle_pending_melt_quote(
        &self,
        quote: &mut MeltQuote,
    ) -> Result<(), Error> {
        if quote.state != MeltQuoteState::Pending {
            return Ok(());
        }

        let saga = match self
            .get_melt_saga_by_quote_id(&quote.id.to_string())
            .await?
        {
            Some(saga) => saga,
            None => {
                tracing::warn!(
                    "No saga found for pending melt quote {}, cannot resume",
                    quote.id
                );
                return Ok(());
            }
        };

        let payment_response = self.check_melt_payment_status(quote).await?;

        super::saga_recovery::process_melt_saga_outcome(
            &saga,
            quote,
            &payment_response,
            &self.localstore,
            &self.pubsub_manager,
            self,
        )
        .await?;

        Ok(())
    }
}