diff --git a/crates/cdk-mintd/src/main.rs b/crates/cdk-mintd/src/main.rs index d658a92e5..3e82b6c23 100644 --- a/crates/cdk-mintd/src/main.rs +++ b/crates/cdk-mintd/src/main.rs @@ -261,9 +261,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 @@ -341,42 +339,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 58c5a206d..8d47378f0 100644 --- a/crates/cdk/src/mint/mod.rs +++ b/crates/cdk/src/mint/mod.rs @@ -32,6 +32,7 @@ mod keysets; mod melt; mod mint_18; 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 000000000..7d3d920ba --- /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(()) + } +}