瀏覽代碼

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 月之前
父節點
當前提交
7a97f3105b
共有 2 個文件被更改,包括 18 次插入2 次删除
  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(())
     }
 }