Browse Source

Read the latest mint quote status in a transaction to avoid race conditions

Fixes #1162
Cesar Rodas 1 month ago
parent
commit
e552643d0b
1 changed files with 14 additions and 0 deletions
  1. 14 0
      crates/cdk/src/mint/ln.rs

+ 14 - 0
crates/cdk/src/mint/ln.rs

@@ -45,6 +45,20 @@ impl Mint {
 
 
         let mut tx = self.localstore.begin_transaction().await?;
         let mut tx = self.localstore.begin_transaction().await?;
 
 
+        if quote.payment_method == PaymentMethod::Bolt11 {
+            // reload the quote, as it state may have changed
+            *quote = tx
+                .get_mint_quote(&quote.id)
+                .await?
+                .ok_or(Error::UnknownQuote)?;
+
+            let current_state = quote.state();
+
+            if current_state == MintQuoteState::Issued || current_state == MintQuoteState::Paid {
+                return Ok(());
+            }
+        }
+
         for payment in ln_status {
         for payment in ln_status {
             if !quote.payment_ids().contains(&&payment.payment_id)
             if !quote.payment_ids().contains(&&payment.payment_id)
                 && payment.payment_amount > Amount::ZERO
                 && payment.payment_amount > Amount::ZERO