]> git.bitcoin.ninja Git - rust-lightning/commitdiff
DRY handling when initiating payment to BOLT 12 invoice.
authorValentine Wallace <vwallace@protonmail.com>
Thu, 29 Aug 2024 18:53:12 +0000 (14:53 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Fri, 13 Sep 2024 14:40:05 +0000 (10:40 -0400)
lightning/src/ln/channelmanager.rs

index 82f47de270080573650bcda0bd12096e4fc06d6e..0fe7bef69cf83a52b9ffdb99cd39eb4ce6f68b70 100644 (file)
@@ -10943,6 +10943,35 @@ where
                let secp_ctx = &self.secp_ctx;
                let expanded_key = &self.inbound_payment_key;
 
+               macro_rules! handle_pay_invoice_res {
+                       ($res: expr, $invoice: expr, $logger: expr) => {{
+                               let error = match $res {
+                                       Err(Bolt12PaymentError::UnknownRequiredFeatures) => {
+                                               log_trace!(
+                                                       $logger, "Invoice requires unknown features: {:?}",
+                                                       $invoice.invoice_features()
+                                               );
+                                               InvoiceError::from(Bolt12SemanticError::UnknownRequiredFeatures)
+                                       },
+                                       Err(Bolt12PaymentError::SendingFailed(e)) => {
+                                               log_trace!($logger, "Failed paying invoice: {:?}", e);
+                                               InvoiceError::from_string(format!("{:?}", e))
+                                       },
+                                       Err(Bolt12PaymentError::UnexpectedInvoice)
+                                               | Err(Bolt12PaymentError::DuplicateInvoice)
+                                               | Ok(()) => return None,
+                               };
+
+                               match responder {
+                                       Some(responder) => return Some((OffersMessage::InvoiceError(error), responder.respond())),
+                                       None => {
+                                               log_trace!($logger, "No reply path to send error: {:?}", error);
+                                               return None
+                                       },
+                               }
+                       }}
+               }
+
                match message {
                        OffersMessage::InvoiceRequest(invoice_request) => {
                                let responder = match responder {
@@ -11069,32 +11098,8 @@ where
                                        return None;
                                }
 
-                               let error = match self.send_payment_for_verified_bolt12_invoice(
-                                       &invoice, payment_id,
-                               ) {
-                                       Err(Bolt12PaymentError::UnknownRequiredFeatures) => {
-                                               log_trace!(
-                                                       logger, "Invoice requires unknown features: {:?}",
-                                                       invoice.invoice_features()
-                                               );
-                                               InvoiceError::from(Bolt12SemanticError::UnknownRequiredFeatures)
-                                       },
-                                       Err(Bolt12PaymentError::SendingFailed(e)) => {
-                                               log_trace!(logger, "Failed paying invoice: {:?}", e);
-                                               InvoiceError::from_string(format!("{:?}", e))
-                                       },
-                                       Err(Bolt12PaymentError::UnexpectedInvoice)
-                                               | Err(Bolt12PaymentError::DuplicateInvoice)
-                                               | Ok(()) => return None,
-                               };
-
-                               match responder {
-                                       Some(responder) => Some((OffersMessage::InvoiceError(error), responder.respond())),
-                                       None => {
-                                               log_trace!(logger, "No reply path to send error: {:?}", error);
-                                               None
-                                       },
-                               }
+                               let res = self.send_payment_for_verified_bolt12_invoice(&invoice, payment_id);
+                               handle_pay_invoice_res!(res, invoice, logger);
                        },
                        #[cfg(async_payments)]
                        OffersMessage::StaticInvoice(invoice) => {
@@ -11107,30 +11112,8 @@ where
                                        },
                                        _ => return None
                                };
-                               // TODO: DRY this with the above regular invoice error handling
-                               let error = match self.initiate_async_payment(&invoice, payment_id) {
-                                       Err(Bolt12PaymentError::UnknownRequiredFeatures) => {
-                                               log_trace!(
-                                                       self.logger, "Invoice requires unknown features: {:?}",
-                                                       invoice.invoice_features()
-                                               );
-                                               InvoiceError::from(Bolt12SemanticError::UnknownRequiredFeatures)
-                                       },
-                                       Err(Bolt12PaymentError::SendingFailed(e)) => {
-                                               log_trace!(self.logger, "Failed paying invoice: {:?}", e);
-                                               InvoiceError::from_string(format!("{:?}", e))
-                                       },
-                                       Err(Bolt12PaymentError::UnexpectedInvoice)
-                                               | Err(Bolt12PaymentError::DuplicateInvoice)
-                                               | Ok(()) => return None,
-                               };
-                               match responder {
-                                       Some(responder) => Some((OffersMessage::InvoiceError(error), responder.respond())),
-                                       None => {
-                                               log_trace!(self.logger, "No reply path to send error: {:?}", error);
-                                               None
-                                       },
-                               }
+                               let res = self.initiate_async_payment(&invoice, payment_id);
+                               handle_pay_invoice_res!(res, invoice, self.logger);
                        },
                        OffersMessage::InvoiceError(invoice_error) => {
                                let payment_hash = match context {