Browse Source

fix: publish notifications after tx commit (#1511)

* refactor: publish status update post tx commit

* merge main v2
asmo 2 weeks ago
parent
commit
c9b57d33eb
2 changed files with 17 additions and 22 deletions
  1. 9 10
      crates/cdk/src/mint/melt/melt_saga/mod.rs
  2. 8 12
      crates/cdk/src/mint/swap/swap_saga/mod.rs

+ 9 - 10
crates/cdk/src/mint/melt/melt_saga/mod.rs

@@ -254,11 +254,6 @@ impl MeltSaga<Initial> {
 
         let previous_state = quote.state;
 
-        // Publish proof state changes
-        for pk in input_ys.iter() {
-            self.pubsub.proof_state((*pk, State::Pending));
-        }
-
         if input_unit != Some(quote.unit.clone()) {
             tx.rollback().await?;
             return Err(Error::UnitMismatch);
@@ -292,9 +287,6 @@ impl MeltSaga<Initial> {
             }
         };
 
-        self.pubsub
-            .melt_quote_status(&*quote, None, None, MeltQuoteState::Pending);
-
         let inputs_fee_breakdown = self.mint.get_proofs_fee(melt_request.inputs()).await?;
         let inputs_fee = inputs_fee_breakdown.total.with_unit(quote.unit.clone());
         let fee_reserve = quote.fee_reserve();
@@ -377,6 +369,14 @@ impl MeltSaga<Initial> {
         }
 
         tx.commit().await?;
+        // Publish proof state changes
+        for pk in input_ys.iter() {
+            self.pubsub.proof_state((*pk, State::Pending));
+        }
+
+        // Publish melt quote status change AFTER transaction commits
+        self.pubsub
+            .melt_quote_status(&*quote, None, None, MeltQuoteState::Pending);
 
         // Store blinded messages for state
         let blinded_messages_vec = melt_request.outputs().clone().unwrap_or_default();
@@ -523,6 +523,7 @@ impl MeltSaga<SetupComplete> {
         mint_quote.add_payment(amount.clone(), self.state_data.quote.id.to_string(), None)?;
         tx.update_mint_quote(&mut mint_quote).await?;
 
+        tx.commit().await?;
         self.pubsub
             .mint_quote_payment(&mint_quote, mint_quote.amount_paid());
 
@@ -532,8 +533,6 @@ impl MeltSaga<SetupComplete> {
             mint_quote.id
         );
 
-        tx.commit().await?;
-
         Ok((self, SettlementDecision::Internal { amount }))
     }
 

+ 8 - 12
crates/cdk/src/mint/swap/swap_saga/mod.rs

@@ -225,11 +225,6 @@ impl<'a> SwapSaga<'a, Initial> {
             });
         }
 
-        // Publish proof state changes
-        for pk in &ys {
-            self.pubsub.proof_state((*pk, State::Pending));
-        }
-
         // Store data in saga struct (avoid duplication in state enum)
         let blinded_messages_vec = blinded_messages.to_vec();
         let blinded_secrets: Vec<PublicKey> = blinded_messages_vec
@@ -246,7 +241,10 @@ impl<'a> SwapSaga<'a, Initial> {
         }
 
         tx.commit().await?;
-
+        // Publish proof state changes
+        for pk in &ys {
+            self.pubsub.proof_state((*pk, State::Pending));
+        }
         // Register compensation (uses LIFO via push_front)
         let compensations = Arc::clone(&self.compensations);
         compensations
@@ -424,11 +422,6 @@ impl SwapSaga<'_, Signed> {
             return Err(err);
         }
 
-        // Publish proof state changes
-        for pk in &self.state_data.ys {
-            self.pubsub.proof_state((*pk, State::Spent));
-        }
-
         if let Err(err) = tx
             .add_completed_operation(
                 &self.state_data.operation,
@@ -453,7 +446,10 @@ impl SwapSaga<'_, Signed> {
         }
 
         tx.commit().await?;
-
+        // Publish proof state changes
+        for pk in &self.state_data.ys {
+            self.pubsub.proof_state((*pk, State::Spent));
+        }
         // Clear compensations - swap is complete
         self.compensations.lock().await.clear();