Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move start up check logic to cdk::Mint #369

Open
thesimplekid opened this issue Sep 27, 2024 · 0 comments
Open

Move start up check logic to cdk::Mint #369

thesimplekid opened this issue Sep 27, 2024 · 0 comments
Assignees
Labels

Comments

@thesimplekid
Copy link
Collaborator

/// 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::trace!("There are {} pending mint quotes.", pending_quotes.len());
let mut unpaid_quotes = mint.get_unpaid_mint_quotes().await?;
tracing::trace!("There are {} unpaid mint quotes.", unpaid_quotes.len());
unpaid_quotes.append(&mut pending_quotes);
for quote in unpaid_quotes {
tracing::trace!("Checking status of mint quote: {}", quote.id);
let lookup_id = quote.request_lookup_id;
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?;
}
}
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>>,
) -> Result<()> {
let melt_quotes = mint.localstore.get_melt_quotes().await?;
let pending_quotes: Vec<MeltQuote> = melt_quotes
.into_iter()
.filter(|q| q.state == MeltQuoteState::Pending || q.state == MeltQuoteState::Unknown)
.collect();
for pending_quote in pending_quotes {
let melt_request_ln_key = mint.localstore.get_melt_request(&pending_quote.id).await?;
let (melt_request, ln_key) = match melt_request_ln_key {
None => (
None,
LnKey {
unit: pending_quote.unit,
method: PaymentMethod::Bolt11,
},
),
Some((melt_request, ln_key)) => (Some(melt_request), ln_key),
};
let ln_backend = match ln_backends.get(&ln_key) {
Some(ln_backend) => ln_backend,
None => {
tracing::warn!("No backend for ln key: {:?}", ln_key);
continue;
}
};
let pay_invoice_response = ln_backend
.check_outgoing_payment(&pending_quote.request_lookup_id)
.await?;
match melt_request {
Some(melt_request) => {
match pay_invoice_response.status {
MeltQuoteState::Paid => {
if let Err(err) = mint
.process_melt_request(
&melt_request,
pay_invoice_response.payment_preimage,
pay_invoice_response.total_spent,
)
.await
{
tracing::error!(
"Could not process melt request for pending quote: {}",
melt_request.quote
);
tracing::error!("{}", err);
}
}
MeltQuoteState::Unpaid | MeltQuoteState::Unknown | MeltQuoteState::Failed => {
// Payment has not been made we want to unset
tracing::info!("Lightning payment for quote {} failed.", pending_quote.id);
if let Err(err) = mint.process_unpaid_melt(&melt_request).await {
tracing::error!("Could not reset melt quote state: {}", err);
}
}
MeltQuoteState::Pending => {
tracing::warn!(
"LN payment pending, proofs are stuck as pending for quote: {}",
melt_request.quote
);
// Quote is still pending we do not want to do anything
// continue to check next quote
}
}
}
None => {
tracing::warn!(
"There is no stored melt request for pending melt quote: {}",
pending_quote.id
);
mint.localstore
.update_melt_quote_state(&pending_quote.id, pay_invoice_response.status)
.await?;
}
};
}
Ok(())
}

@thesimplekid thesimplekid self-assigned this Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant