Skip to content

Commit

Permalink
feat: mint check into cdk
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Nov 18, 2024
1 parent 4e7f55e commit 682e005
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
40 changes: 1 addition & 39 deletions crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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>>,
Expand Down
1 change: 1 addition & 0 deletions crates/cdk/src/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod info;
mod keysets;
mod melt;
mod mint_nut04;
mod start_up_check;
mod swap;
pub mod types;

Expand Down
43 changes: 43 additions & 0 deletions crates/cdk/src/mint/start_up_check.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}

0 comments on commit 682e005

Please sign in to comment.