Merge pull request #943 from crodas/fix/avoid-empty-proofs-query

Fixed bug to avoid empty calls `get_proofs_states`
This commit is contained in:
thesimplekid
2025-08-08 11:01:58 +03:00
committed by GitHub
3 changed files with 48 additions and 35 deletions

View File

@@ -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())

View File

@@ -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)

View File

@@ -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())?,
);
}
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<Uuid>| x.into())
.collect::<Vec<_>>()
})
.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)
}