From 2434857870f6f9f10a577c412c6a09a62069cad9 Mon Sep 17 00:00:00 2001 From: Richard Holzeis Date: Fri, 31 May 2024 16:13:32 +0200 Subject: [PATCH] chore: Set hodl invoice to `Settled` on lnd event It's better to update our state only if the same state has been applied to lnd. --- coordinator/src/db/hodl_invoice.rs | 17 +++++++++++++++-- coordinator/src/node/invoice.rs | 18 ++++++++++++++++++ coordinator/src/trade/mod.rs | 3 +-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/coordinator/src/db/hodl_invoice.rs b/coordinator/src/db/hodl_invoice.rs index 8f82edabe..ef1046a3e 100644 --- a/coordinator/src/db/hodl_invoice.rs +++ b/coordinator/src/db/hodl_invoice.rs @@ -69,6 +69,19 @@ pub fn get_r_hash_by_order_id(conn: &mut PgConnection, order_id: Uuid) -> QueryR .get_result(conn) } +/// Returns the pre image of the hodl invoice associated with the order id +/// If the hodl invoice can not be found a [`Not Found`] error is returned +/// If the hodl invoice is found the pre_image is optional, as it might have not yet been set. +pub fn get_pre_image_by_order_id( + conn: &mut PgConnection, + order_id: Uuid, +) -> QueryResult> { + hodl_invoices::table + .filter(hodl_invoices::order_id.eq(order_id)) + .select(hodl_invoices::pre_image) + .get_result(conn) +} + pub fn update_hodl_invoice_to_accepted( conn: &mut PgConnection, hash: &str, @@ -91,10 +104,10 @@ pub fn update_hodl_invoice_to_accepted( pub fn update_hodl_invoice_to_settled( conn: &mut PgConnection, - order_id: Uuid, + r_hash: String, ) -> QueryResult> { diesel::update(hodl_invoices::table) - .filter(hodl_invoices::order_id.eq(order_id)) + .filter(hodl_invoices::r_hash.eq(r_hash)) .set(( hodl_invoices::updated_at.eq(OffsetDateTime::now_utc()), hodl_invoices::invoice_state.eq(InvoiceState::Settled), diff --git a/coordinator/src/node/invoice.rs b/coordinator/src/node/invoice.rs index b20262228..2b404504d 100644 --- a/coordinator/src/node/invoice.rs +++ b/coordinator/src/node/invoice.rs @@ -33,6 +33,24 @@ pub fn spawn_invoice_watch( } InvoiceState::Settled => { tracing::info!(%trader_pubkey, r_hash, "Accepted hodl invoice has been settled."); + if let Err(e) = spawn_blocking({ + let r_hash = r_hash.clone(); + move || { + let mut conn = pool.get()?; + db::hodl_invoice::update_hodl_invoice_to_settled( + &mut conn, r_hash, + )?; + anyhow::Ok(()) + } + }) + .await + .expect("task to finish") + { + tracing::error!( + r_hash, + "Failed to set hodl invoice to failed. Error: {e:#}" + ); + } break; } InvoiceState::Canceled => { diff --git a/coordinator/src/trade/mod.rs b/coordinator/src/trade/mod.rs index c563c6724..ee817c922 100644 --- a/coordinator/src/trade/mod.rs +++ b/coordinator/src/trade/mod.rs @@ -225,8 +225,7 @@ impl TradeExecutor { let pool = self.node.pool.clone(); move || { let mut conn = pool.get()?; - let pre_image = - db::hodl_invoice::update_hodl_invoice_to_settled(&mut conn, order_id)?; + let pre_image = db::hodl_invoice::get_pre_image_by_order_id(&mut conn, order_id)?; anyhow::Ok(pre_image) }