feat: mint check into cdk

This commit is contained in:
thesimplekid
2024-11-18 08:59:44 +00:00
parent 4e7f55e5b0
commit 682e0058de
3 changed files with 45 additions and 39 deletions

View File

@@ -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<Mint>,
ln: Arc<dyn MintLightning<Err = cdk_lightning::Error> + 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(&quote.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<Mint>,
ln_backends: &HashMap<LnKey, Arc<dyn MintLightning<Err = cdk_lightning::Error> + Send + Sync>>,

View File

@@ -29,6 +29,7 @@ mod info;
mod keysets;
mod melt;
mod mint_nut04;
mod start_up_check;
mod swap;
pub mod types;

View File

@@ -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(&quote.id, state)
.await?;
}
}
Err(err) => {
tracing::warn!("Could not check state of pending invoice: {}", lookup_id);
tracing::error!("{}", err);
}
}
}
}
Ok(())
}
}