-
Notifications
You must be signed in to change notification settings - Fork 43
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
Yield call to send_payment after a timeout and return a pending payment #943
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,10 @@ pub enum BreezEvent { | |
}, | ||
/// Indicates that the local SDK state has just been sync-ed with the remote components | ||
Synced, | ||
/// Indicates that an outgoing payment has started | ||
PaymentStarted { | ||
details: Payment, | ||
}, | ||
/// Indicates that an outgoing payment has been completed successfully | ||
PaymentSucceed { | ||
details: Payment, | ||
|
@@ -116,6 +120,11 @@ pub enum BreezEvent { | |
details: SwapInfo, | ||
}, | ||
} | ||
impl BreezEvent { | ||
pub(crate) fn payment_started(payment: Payment) -> Self { | ||
Self::PaymentStarted { details: payment } | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq)] | ||
pub struct BackupFailedData { | ||
|
@@ -282,7 +291,7 @@ impl BreezServices { | |
/// Calling `send_payment` ensures that the payment is not already completed, if so it will result in an error. | ||
/// If the invoice doesn't specify an amount, the amount is taken from the `amount_msat` arg. | ||
pub async fn send_payment( | ||
&self, | ||
self: &Arc<BreezServices>, | ||
req: SendPaymentRequest, | ||
) -> Result<SendPaymentResponse, SendPaymentError> { | ||
self.start_node().await?; | ||
|
@@ -314,25 +323,39 @@ impl BreezServices { | |
{ | ||
Some(_) => Err(SendPaymentError::AlreadyPaid), | ||
None => { | ||
self.persist_pending_payment(&parsed_invoice, amount_msat, req.label.clone())?; | ||
let payment_res = self | ||
.node_api | ||
.send_payment( | ||
parsed_invoice.bolt11.clone(), | ||
req.amount_msat, | ||
req.label.clone(), | ||
) | ||
.map_err(Into::into) | ||
.await; | ||
let payment = self | ||
.on_payment_completed( | ||
parsed_invoice.payee_pubkey.clone(), | ||
Some(parsed_invoice), | ||
req.label, | ||
payment_res, | ||
) | ||
let pending_payment = | ||
self.persist_pending_payment(&parsed_invoice, amount_msat, req.label.clone())?; | ||
self.notify_event_listeners(BreezEvent::payment_started(pending_payment.clone())) | ||
.await?; | ||
Ok(SendPaymentResponse { payment }) | ||
|
||
let pending_timeout_sec = req.pending_timeout_sec.unwrap_or(u64::MAX); | ||
let (tx, rx) = std::sync::mpsc::channel(); | ||
let cloned = self.clone(); | ||
tokio::spawn(async move { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using an async task and a channel perhaps we can simplify by using tokio::time::timeout ?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've set this PR to draft while we work on other things, @ok300 has a proposal to improve it But regarding the tokio timeout, isn't the future cancelled if the timeout occurs? In our case the future has to complete with or without timeout There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, good point as we don't want to show error while still pending. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also we want the sync to complete in the background if pending is already returned and the Payment(Succeed/Failed) to be emitted There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added #958 to fix some issues around that and improve handling. It's using a combination of both your ideas:
|
||
let payment_res = cloned | ||
.node_api | ||
.send_payment( | ||
parsed_invoice.bolt11.clone(), | ||
req.amount_msat, | ||
req.label.clone(), | ||
) | ||
.map_err(Into::into) | ||
.await; | ||
let result = cloned | ||
.on_payment_completed( | ||
parsed_invoice.payee_pubkey.clone(), | ||
Some(parsed_invoice), | ||
req.label, | ||
payment_res, | ||
) | ||
.await; | ||
let _ = tx.send(result); | ||
}); | ||
|
||
match rx.recv_timeout(Duration::from_secs(pending_timeout_sec)) { | ||
Ok(result) => result.map(SendPaymentResponse::from_payment), | ||
Err(_) => Ok(SendPaymentResponse::from_payment(pending_payment)), | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -367,7 +390,10 @@ impl BreezServices { | |
/// is made. | ||
/// | ||
/// This method will return an [anyhow::Error] when any validation check fails. | ||
pub async fn lnurl_pay(&self, req: LnUrlPayRequest) -> Result<LnUrlPayResult, LnUrlPayError> { | ||
pub async fn lnurl_pay( | ||
self: &Arc<BreezServices>, | ||
req: LnUrlPayRequest, | ||
) -> Result<LnUrlPayResult, LnUrlPayError> { | ||
match validate_lnurl_pay( | ||
req.amount_msat, | ||
req.comment, | ||
|
@@ -382,8 +408,8 @@ impl BreezServices { | |
ValidatedCallbackResponse::EndpointSuccess { data: cb } => { | ||
let pay_req = SendPaymentRequest { | ||
bolt11: cb.pr.clone(), | ||
amount_msat: None, | ||
label: req.payment_label, | ||
..Default::default() | ||
}; | ||
let invoice = parse_invoice(cb.pr.as_str())?; | ||
|
||
|
@@ -1171,7 +1197,7 @@ impl BreezServices { | |
invoice: &LNInvoice, | ||
amount_msat: u64, | ||
label: Option<String>, | ||
) -> Result<(), SendPaymentError> { | ||
) -> Result<Payment, SendPaymentError> { | ||
self.persister.insert_or_update_payments( | ||
&[Payment { | ||
id: invoice.payment_hash.clone(), | ||
|
@@ -1218,7 +1244,12 @@ impl BreezServices { | |
attempted_error: None, | ||
}, | ||
)?; | ||
Ok(()) | ||
|
||
self.persister | ||
.get_payment_by_hash(&invoice.payment_hash)? | ||
.ok_or(SendPaymentError::Generic { | ||
err: "Payment not found".to_string(), | ||
}) | ||
} | ||
|
||
async fn on_payment_completed( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this? Why not using the payment_timeout from the config?