From 682e0058de3f7c11540e45e3be6b5a5133a36e3d Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Mon, 18 Nov 2024 08:59:44 +0000 Subject: [PATCH] feat: mint check into cdk --- crates/cdk-mintd/src/main.rs | 40 +------------------------ crates/cdk/src/mint/mod.rs | 1 + crates/cdk/src/mint/start_up_check.rs | 43 +++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 39 deletions(-) create mode 100644 crates/cdk/src/mint/start_up_check.rs diff --git a/crates/cdk-mintd/src/main.rs b/crates/cdk-mintd/src/main.rs index 554c4ff8..57da5bda 100644 --- a/crates/cdk-mintd/src/main.rs +++ b/crates/cdk-mintd/src/main.rs @@ -270,9 +270,7 @@ async fn main() -> anyhow::Result<()> { // In the event that the mint server is down but the ln node is not // it is possible that a mint quote was paid but the mint has not been updated // this will check and update the mint state of those quotes - for ln in ln_backends.values() { - check_pending_mint_quotes(Arc::clone(&mint), Arc::clone(ln)).await?; - } + mint.check_pending_mint_quotes().await?; // Checks the status of all pending melt quotes // Pending melt quotes where the payment has gone through inputs are burnt @@ -346,42 +344,6 @@ async fn main() -> anyhow::Result<()> { Ok(()) } -/// Used on mint start up to check status of all pending mint quotes -async fn check_pending_mint_quotes( - mint: Arc, - ln: Arc + Send + Sync>, -) -> Result<()> { - let mut pending_quotes = mint.get_pending_mint_quotes().await?; - tracing::info!("There are {} pending mint quotes.", pending_quotes.len()); - let mut unpaid_quotes = mint.get_unpaid_mint_quotes().await?; - tracing::info!("There are {} unpaid mint quotes.", unpaid_quotes.len()); - - unpaid_quotes.append(&mut pending_quotes); - - for quote in unpaid_quotes { - tracing::debug!("Checking status of mint quote: {}", quote.id); - let lookup_id = quote.request_lookup_id.as_str(); - match ln.check_incoming_invoice_status(lookup_id).await { - Ok(state) => { - if state != quote.state { - tracing::trace!("Mint quote status changed: {}", quote.id); - mint.localstore - .update_mint_quote_state("e.id, state) - .await?; - mint.pubsub_manager.mint_quote_bolt11_status(quote, state); - } - } - - Err(err) => { - tracing::warn!("Could not check state of pending invoice: {}", lookup_id); - tracing::error!("{}", err); - } - } - } - - Ok(()) -} - async fn check_pending_melt_quotes( mint: Arc, ln_backends: &HashMap + Send + Sync>>, diff --git a/crates/cdk/src/mint/mod.rs b/crates/cdk/src/mint/mod.rs index 360ff266..3a1fc751 100644 --- a/crates/cdk/src/mint/mod.rs +++ b/crates/cdk/src/mint/mod.rs @@ -29,6 +29,7 @@ mod info; mod keysets; mod melt; mod mint_nut04; +mod start_up_check; mod swap; pub mod types; diff --git a/crates/cdk/src/mint/start_up_check.rs b/crates/cdk/src/mint/start_up_check.rs new file mode 100644 index 00000000..7d3d920b --- /dev/null +++ b/crates/cdk/src/mint/start_up_check.rs @@ -0,0 +1,43 @@ +//! Check used at mint start up +//! +//! These checks are need in the case the mint was offline and the lightning node was node. +//! These ensure that the status of the mint or melt quote matches in the mint db and on the node. + +use super::{Error, Mint}; + +impl Mint { + /// Check the status of all pending mint quotes in the mint db + /// with all the lighting backends. This check that any payments + /// received while the mint was offline are accounted for, and the wallet can mint associated ecash + pub async fn check_pending_mint_quotes(&self) -> Result<(), Error> { + let mut pending_quotes = self.get_pending_mint_quotes().await?; + tracing::info!("There are {} pending mint quotes.", pending_quotes.len()); + let mut unpaid_quotes = self.get_unpaid_mint_quotes().await?; + tracing::info!("There are {} unpaid mint quotes.", unpaid_quotes.len()); + + unpaid_quotes.append(&mut pending_quotes); + + for ln in self.ln.values() { + for quote in unpaid_quotes.iter() { + tracing::debug!("Checking status of mint quote: {}", quote.id); + let lookup_id = quote.request_lookup_id.as_str(); + match ln.check_incoming_invoice_status(lookup_id).await { + Ok(state) => { + if state != quote.state { + tracing::trace!("Mint quote status changed: {}", quote.id); + self.localstore + .update_mint_quote_state("e.id, state) + .await?; + } + } + + Err(err) => { + tracing::warn!("Could not check state of pending invoice: {}", lookup_id); + tracing::error!("{}", err); + } + } + } + } + Ok(()) + } +}