feat: tests on lnd node

This commit is contained in:
thesimplekid
2025-01-24 14:40:10 +00:00
parent 486f98bc8b
commit 67be74abb8
7 changed files with 97 additions and 18 deletions

View File

@@ -25,6 +25,7 @@ use error::Error;
use fedimint_tonic_lnd::lnrpc::fee_limit::Limit;
use fedimint_tonic_lnd::lnrpc::payment::PaymentStatus;
use fedimint_tonic_lnd::lnrpc::FeeLimit;
use fedimint_tonic_lnd::tonic::Code;
use fedimint_tonic_lnd::Client;
use futures::{Stream, StreamExt};
use tokio::sync::Mutex;
@@ -76,6 +77,7 @@ impl Lnd {
impl MintLightning for Lnd {
type Err = cdk_lightning::Error;
#[instrument(skip_all)]
fn get_settings(&self) -> Settings {
Settings {
mpp: false,
@@ -84,14 +86,17 @@ impl MintLightning for Lnd {
}
}
#[instrument(skip_all)]
fn is_wait_invoice_active(&self) -> bool {
self.wait_invoice_is_active.load(Ordering::SeqCst)
}
#[instrument(skip_all)]
fn cancel_wait_invoice(&self) {
self.wait_invoice_cancel_token.cancel()
}
#[instrument(skip_all)]
async fn wait_any_invoice(
&self,
) -> Result<Pin<Box<dyn Stream<Item = String> + Send>>, Self::Err> {
@@ -164,6 +169,7 @@ impl MintLightning for Lnd {
.boxed())
}
#[instrument(skip_all)]
async fn get_payment_quote(
&self,
melt_quote_request: &MeltQuoteBolt11Request,
@@ -198,6 +204,23 @@ impl MintLightning for Lnd {
max_fee: Option<Amount>,
) -> Result<PayInvoiceResponse, Self::Err> {
let payment_request = melt_quote.request;
let bolt11 = Bolt11Invoice::from_str(&payment_request)?;
let pay_state = self
.check_outgoing_payment(&bolt11.payment_hash().to_string())
.await?;
match pay_state.status {
MeltQuoteState::Unpaid | MeltQuoteState::Unknown | MeltQuoteState::Failed => (),
MeltQuoteState::Paid => {
tracing::debug!("Melt attempted on invoice already paid");
return Err(Self::Err::InvoiceAlreadyPaid);
}
MeltQuoteState::Pending => {
tracing::debug!("Melt attempted on invoice already pending");
return Err(Self::Err::InvoicePaymentPending);
}
}
let amount_msat: u64 = match melt_quote.msat_to_pay {
Some(amount_msat) => amount_msat.into(),
@@ -255,6 +278,7 @@ impl MintLightning for Lnd {
})
}
#[instrument(skip(self, description))]
async fn create_invoice(
&self,
amount: Amount,
@@ -292,6 +316,7 @@ impl MintLightning for Lnd {
})
}
#[instrument(skip(self))]
async fn check_incoming_invoice_status(
&self,
request_lookup_id: &str,
@@ -324,6 +349,7 @@ impl MintLightning for Lnd {
}
}
#[instrument(skip(self))]
async fn check_outgoing_payment(
&self,
payment_hash: &str,
@@ -332,15 +358,32 @@ impl MintLightning for Lnd {
payment_hash: hex::decode(payment_hash).map_err(|_| Error::InvalidHash)?,
no_inflight_updates: true,
};
let mut payment_stream = self
let payment_response = self
.client
.lock()
.await
.router()
.track_payment_v2(track_request)
.await
.unwrap()
.into_inner();
.await;
let mut payment_stream = match payment_response {
Ok(stream) => stream.into_inner(),
Err(err) => {
let err_code = err.code();
if err_code == Code::NotFound {
return Ok(PayInvoiceResponse {
payment_lookup_id: payment_hash.to_string(),
payment_preimage: None,
status: MeltQuoteState::Unknown,
total_spent: Amount::ZERO,
unit: self.get_settings().unit,
});
} else {
return Err(cdk_lightning::Error::UnknownPaymentState);
}
}
};
while let Some(update_result) = payment_stream.next().await {
match update_result {