diff --git a/crates/cdk-sql-common/src/mint/auth/mod.rs b/crates/cdk-sql-common/src/mint/auth/mod.rs index f9d1351a..c2840aed 100644 --- a/crates/cdk-sql-common/src/mint/auth/mod.rs +++ b/crates/cdk-sql-common/src/mint/auth/mod.rs @@ -144,7 +144,7 @@ where y: &PublicKey, proofs_state: State, ) -> Result, 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>, 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()) diff --git a/crates/cdk-sql-common/src/mint/mod.rs b/crates/cdk-sql-common/src/mint/mod.rs index b6d6a41c..fe82c42d 100644 --- a/crates/cdk-sql-common/src/mint/mod.rs +++ b/crates/cdk-sql-common/src/mint/mod.rs @@ -82,6 +82,9 @@ async fn get_current_states( 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) diff --git a/crates/cdk/src/mint/subscription/on_subscription.rs b/crates/cdk/src/mint/subscription/on_subscription.rs index efb3598e..4d57976f 100644 --- a/crates/cdk/src/mint/subscription/on_subscription.rs +++ b/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| x.into()) - .collect::>() - }) - .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| x.into()) - .collect::>() - }) - .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| x.into()) + .collect::>() + }) + .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 !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| x.into()) + .collect::>() + }) + .map_err(|e| e.to_string())?, + ); + } + + 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) }