Browse Source

Fixed bug to avoid empty calls `get_proofs_states`

Cesar Rodas 2 tháng trước cách đây
mục cha
commit
2e1099adbe

+ 4 - 1
crates/cdk-sql-common/src/mint/auth/mod.rs

@@ -144,7 +144,7 @@ where
         y: &PublicKey,
         proofs_state: State,
     ) -> Result<Option<State>, Self::Err> {
-        let current_state = query(r#"SELECT state FROM proof WHERE y = :y"#)?
+        let current_state = query(r#"SELECT state FROM proof WHERE y = :y FOR UPDATE"#)?
             .bind("y", y.to_bytes().to_vec())
             .pluck(&self.inner)
             .await?
@@ -320,6 +320,9 @@ where
     }
 
     async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err> {
+        if ys.is_empty() {
+            return Ok(vec![]);
+        }
         let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
         let mut current_states = query(r#"SELECT y, state FROM proof WHERE y IN (:ys)"#)?
             .bind_vec("ys", ys.iter().map(|y| y.to_bytes().to_vec()).collect())

+ 3 - 0
crates/cdk-sql-common/src/mint/mod.rs

@@ -82,6 +82,9 @@ async fn get_current_states<C>(
 where
     C: DatabaseExecutor + Send + Sync,
 {
+    if ys.is_empty() {
+        return Ok(Default::default());
+    }
     query(r#"SELECT y, state FROM proof WHERE y IN (:ys)"#)?
         .bind_vec("ys", ys.iter().map(|y| y.to_bytes().to_vec()).collect())
         .fetch_all(conn)

+ 41 - 34
crates/cdk/src/mint/subscription/on_subscription.rs

@@ -57,41 +57,48 @@ impl OnNewSubscription for OnSubscription {
             }
         }
 
-        to_return.extend(
-            futures::future::try_join_all(melt_queries)
-                .await
-                .map(|quotes| {
-                    quotes
-                        .into_iter()
-                        .filter_map(|quote| quote.map(|x| x.into()))
-                        .map(|x: MeltQuoteBolt11Response<Uuid>| x.into())
-                        .collect::<Vec<_>>()
-                })
-                .map_err(|e| e.to_string())?,
-        );
-        to_return.extend(
-            futures::future::try_join_all(mint_queries)
-                .await
-                .map(|quotes| {
-                    quotes
-                        .into_iter()
-                        .filter_map(|quote| quote.map(|x| x.into()))
-                        .map(|x: MintQuoteBolt11Response<Uuid>| x.into())
-                        .collect::<Vec<_>>()
-                })
-                .map_err(|e| e.to_string())?,
-        );
+        if !melt_queries.is_empty() {
+            to_return.extend(
+                futures::future::try_join_all(melt_queries)
+                    .await
+                    .map(|quotes| {
+                        quotes
+                            .into_iter()
+                            .filter_map(|quote| quote.map(|x| x.into()))
+                            .map(|x: MeltQuoteBolt11Response<Uuid>| x.into())
+                            .collect::<Vec<_>>()
+                    })
+                    .map_err(|e| e.to_string())?,
+            );
+        }
+
+        if !mint_queries.is_empty() {
+            to_return.extend(
+                futures::future::try_join_all(mint_queries)
+                    .await
+                    .map(|quotes| {
+                        quotes
+                            .into_iter()
+                            .filter_map(|quote| quote.map(|x| x.into()))
+                            .map(|x: MintQuoteBolt11Response<Uuid>| x.into())
+                            .collect::<Vec<_>>()
+                    })
+                    .map_err(|e| e.to_string())?,
+            );
+        }
 
-        to_return.extend(
-            datastore
-                .get_proofs_states(public_keys.as_slice())
-                .await
-                .map_err(|e| e.to_string())?
-                .into_iter()
-                .enumerate()
-                .filter_map(|(idx, state)| state.map(|state| (public_keys[idx], state).into()))
-                .map(|state: ProofState| state.into()),
-        );
+        if !public_keys.is_empty() {
+            to_return.extend(
+                datastore
+                    .get_proofs_states(public_keys.as_slice())
+                    .await
+                    .map_err(|e| e.to_string())?
+                    .into_iter()
+                    .enumerate()
+                    .filter_map(|(idx, state)| state.map(|state| (public_keys[idx], state).into()))
+                    .map(|state: ProofState| state.into()),
+            );
+        }
 
         Ok(to_return)
     }