From 6de73df5180f3f00a3486f0a8d82044265b27809 Mon Sep 17 00:00:00 2001 From: Dzejkop Date: Mon, 11 Dec 2023 13:26:53 +0100 Subject: [PATCH] Fix --- src/db.rs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/db.rs b/src/db.rs index df84750..f4262f3 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1025,32 +1025,28 @@ impl Database { } pub async fn purge_unsent_txs(&self, relayer_id: &str) -> eyre::Result<()> { - let unsent_txs = self.get_unsent_txs().await?; - - let unsent_tx_ids: Vec<_> = unsent_txs - .into_iter() - .filter(|tx| tx.relayer_id == relayer_id) - .map(|tx| tx.id) - .collect(); - - sqlx::query( + let (nonce,): (i64,) = sqlx::query_as( r#" - DELETE FROM transactions - WHERE id = ANY($1::TEXT[]) + UPDATE relayers + SET nonce = current_nonce + WHERE id = $1 + RETURNING nonce "#, ) - .bind(&unsent_tx_ids) - .execute(&self.pool) + .bind(relayer_id) + .fetch_one(&self.pool) .await?; sqlx::query( r#" - UPDATE relayers - SET nonce = current_nonce - WHERE id = $1 + DELETE FROM transactions + WHERE nonce > $1 + AND id NOT IN ( + SELECT tx_id FROM sent_transactions + ) "#, ) - .bind(relayer_id) + .bind(nonce) .execute(&self.pool) .await?;