Browse Source

Add implicit rollback.

The proof_writer implicit rollback on Drop is too slow for our tests causing
race conditions with Postgres.

This commit enhances the rollback logic and makes it explicit
Cesar Rodas 11 tháng trước cách đây
mục cha
commit
7a97f3105b
2 tập tin đã thay đổi với 18 bổ sung2 xóa
  1. 1 0
      crates/cdk/src/mint/melt.rs
  2. 17 2
      crates/cdk/src/mint/proof_writer.rs

+ 1 - 0
crates/cdk/src/mint/melt.rs

@@ -662,6 +662,7 @@ impl Mint {
                             "Lightning payment for quote {} failed.",
                             melt_request.quote()
                         );
+                        proof_writer.rollback().await?;
                         return Err(Error::PaymentFailed);
                     }
                     MeltQuoteState::Pending => {

+ 17 - 2
crates/cdk/src/mint/proof_writer.rs

@@ -143,13 +143,28 @@ impl ProofWriter {
     }
 
     /// Rollback all changes in this ProofWriter consuming it.
-    pub async fn rollback(mut self, tx: &mut Tx<'_, '_>) -> Result<(), Error> {
+    pub async fn rollback(mut self) -> Result<(), Error> {
+        let db = if let Some(db) = self.db.take() {
+            db
+        } else {
+            return Ok(());
+        };
+        let mut tx = db.begin_transaction().await?;
         let (ys, original_states) = if let Some(proofs) = self.proof_original_states.take() {
             proofs.into_iter().unzip::<_, _, Vec<_>, Vec<_>>()
         } else {
             return Ok(());
         };
-        reset_proofs_to_original_state(tx, &ys, original_states).await?;
+
+        tracing::info!(
+            "Rollback proofs to their original states {:?} {:?}",
+            ys,
+            original_states
+        );
+
+        reset_proofs_to_original_state(&mut tx, &ys, original_states).await?;
+        tx.commit().await?;
+
         Ok(())
     }
 }