Browse Source

Update tests to take advantage of rollback instead of pending proofs

Cesar Rodas 4 weeks ago
parent
commit
b0386373d5

+ 27 - 10
crates/cdk-integration-tests/tests/fake_wallet.rs

@@ -28,7 +28,6 @@ use cdk::wallet::types::TransactionDirection;
 use cdk::wallet::{HttpClient, MintConnector, Wallet};
 use cdk::StreamExt;
 use cdk_fake_wallet::{create_fake_invoice, FakeInvoiceDescription};
-use cdk_integration_tests::attempt_to_swap_pending;
 use cdk_sqlite::wallet::memory;
 
 const MINT_URL: &str = "http://127.0.0.1:8086";
@@ -70,7 +69,15 @@ async fn test_fake_tokens_pending() {
 
     assert!(melt.is_err());
 
-    attempt_to_swap_pending(&wallet).await.unwrap();
+    assert!(
+        wallet
+            .localstore
+            .get_proofs(None, None, Some(vec![State::Pending]), None)
+            .await
+            .expect("no an error")
+            .is_empty(),
+        "Database rollback removes all pending proofs"
+    );
 }
 
 /// Tests that if the pay error fails and the check returns unknown or failed,
@@ -175,13 +182,15 @@ async fn test_fake_melt_payment_fail_and_check() {
     let melt = wallet.melt(&melt_quote.id).await;
     assert!(melt.is_err());
 
-    let pending = wallet
-        .localstore
-        .get_proofs(None, None, Some(vec![State::Pending]), None)
-        .await
-        .unwrap();
-
-    assert!(!pending.is_empty());
+    assert!(
+        wallet
+            .localstore
+            .get_proofs(None, None, Some(vec![State::Pending]), None)
+            .await
+            .expect("no an error")
+            .is_empty(),
+        "Database rollback removes all pending proofs"
+    );
 }
 
 /// Tests that when the ln backend returns a failed status but does not error,
@@ -346,7 +355,15 @@ async fn test_fake_melt_payment_err_paid() {
     let melt = wallet.melt(&melt_quote.id).await;
     assert!(melt.is_err());
 
-    attempt_to_swap_pending(&wallet).await.unwrap();
+    assert!(
+        wallet
+            .localstore
+            .get_proofs(None, None, Some(vec![State::Pending]), None)
+            .await
+            .expect("no an error")
+            .is_empty(),
+        "Database rollback removes all pending proofs"
+    );
 }
 
 /// Tests that change outputs in a melt quote are correctly handled

+ 1 - 2
crates/cdk-integration-tests/tests/integration_tests_pure.rs

@@ -778,10 +778,9 @@ async fn test_mint_change_with_fee_melt() {
         .await
         .unwrap();
     let w = wallet_alice
-        .melt_proofs_with_metadata_with_tx(&mut tx, &melt_quote.id, proofs, HashMap::new())
+        .melt_proofs_with_metadata_with_tx(tx, &melt_quote.id, proofs, HashMap::new())
         .await
         .unwrap();
-    tx.commit().await.unwrap();
 
     assert_eq!(w.change.unwrap().total_amount().unwrap(), 97.into());
 }

+ 13 - 9
crates/cdk/src/wallet/melt/melt_bolt11.rs

@@ -138,11 +138,14 @@ impl Wallet {
         proofs: Proofs,
         metadata: HashMap<String, String>,
     ) -> Result<Melted, Error> {
-        let mut tx = self.localstore.begin_db_transaction().await?;
         let result = self
-            .melt_proofs_with_metadata_with_tx(&mut tx, quote_id, proofs, metadata)
+            .melt_proofs_with_metadata_with_tx(
+                self.localstore.begin_db_transaction().await?,
+                quote_id,
+                proofs,
+                metadata,
+            )
             .await?;
-        tx.commit().await?;
         Ok(result)
     }
 
@@ -150,7 +153,7 @@ impl Wallet {
     #[instrument(skip(self, tx, proofs))]
     pub async fn melt_proofs_with_metadata_with_tx(
         &self,
-        tx: &mut Tx<'_, '_>,
+        mut tx: Tx<'_, '_>,
         quote_id: &str,
         proofs: Proofs,
         metadata: HashMap<String, String>,
@@ -160,7 +163,7 @@ impl Wallet {
             .await?
             .ok_or(Error::UnknownQuote)?;
 
-        let active_keyset_id = self.fetch_active_keyset(Some(tx)).await?.id;
+        let active_keyset_id = self.fetch_active_keyset(Some(&mut tx)).await?.id;
 
         let active_keys = tx
             .get_keys(&active_keyset_id)
@@ -226,7 +229,8 @@ impl Wallet {
                 tracing::error!("Could not melt: {}", err);
                 tracing::info!("Checking status of input proofs.");
 
-                self.reclaim_unspent_with_tx(tx, proofs).await?;
+                self.reclaim_unspent_with_tx(&mut tx, proofs).await?;
+                tx.commit().await?;
 
                 return Err(err);
             }
@@ -311,6 +315,8 @@ impl Wallet {
         })
         .await?;
 
+        tx.commit().await?;
+
         Ok(melted)
     }
 
@@ -432,11 +438,9 @@ impl Wallet {
         }
 
         let melted = self
-            .melt_proofs_with_metadata_with_tx(&mut tx, quote_id, input_proofs, metadata)
+            .melt_proofs_with_metadata_with_tx(tx, quote_id, input_proofs, metadata)
             .await?;
 
-        tx.commit().await?;
-
         Ok(melted)
     }
 }