Skip to content

Commit 3dd3a1c

Browse files
committed
Add create_invoice_request_messages function
- Extract code responsible for creating InvoiceRequest messages into a separate function. - This function will be reused in the subsequent commit.
1 parent 88abc24 commit 3dd3a1c

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use crate::ln::outbound_payment::{Bolt12PaymentError, OutboundPayments, PaymentA
6262
use crate::ln::wire::Encode;
6363
use crate::offers::invoice::{BlindedPayInfo, Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder, UnsignedBolt12Invoice};
6464
use crate::offers::invoice_error::InvoiceError;
65-
use crate::offers::invoice_request::{DerivedPayerId, InvoiceRequestBuilder};
65+
use crate::offers::invoice_request::{DerivedPayerId, InvoiceRequest, InvoiceRequestBuilder};
6666
use crate::offers::offer::{Offer, OfferBuilder};
6767
use crate::offers::parse::Bolt12SemanticError;
6868
use crate::offers::refund::{Refund, RefundBuilder};
@@ -8400,6 +8400,43 @@ where
84008400
#[cfg(c_bindings)]
84018401
create_refund_builder!(self, RefundMaybeWithDerivedMetadataBuilder);
84028402

8403+
fn create_invoice_request_messages(&self, invoice_request: InvoiceRequest, reply_path: BlindedPath)
8404+
-> Result<Vec<PendingOnionMessage<OffersMessage>>, Bolt12SemanticError> {
8405+
let paths = invoice_request.paths();
8406+
let signing_pubkey = invoice_request.signing_pubkey();
8407+
8408+
if paths.is_empty() && signing_pubkey.is_none() {
8409+
debug_assert!(false);
8410+
return Err(Bolt12SemanticError::MissingSigningPubkey);
8411+
}
8412+
8413+
// Send as many invoice requests as there are paths in the offer (with an upper bound).
8414+
// Using only one path could result in a failure if the path no longer exists. But only
8415+
// one invoice for a given payment id will be paid, even if more than one is received.
8416+
const REQUEST_LIMIT: usize = 10;
8417+
8418+
let messages = if !paths.is_empty() {
8419+
paths.into_iter()
8420+
.take(REQUEST_LIMIT)
8421+
.map(|path| {
8422+
new_pending_onion_message(
8423+
OffersMessage::InvoiceRequest(invoice_request.clone()),
8424+
Destination::BlindedPath(path.clone()),
8425+
Some(reply_path.clone()),
8426+
)
8427+
})
8428+
.collect()
8429+
} else {
8430+
vec![new_pending_onion_message(
8431+
OffersMessage::InvoiceRequest(invoice_request.clone()),
8432+
Destination::Node(signing_pubkey.unwrap()),
8433+
Some(reply_path.clone()),
8434+
)]
8435+
};
8436+
8437+
Ok(messages)
8438+
}
8439+
84038440
/// Pays for an [`Offer`] using the given parameters by creating an [`InvoiceRequest`] and
84048441
/// enqueuing it to be sent via an onion message. [`ChannelManager`] will pay the actual
84058442
/// [`Bolt12Invoice`] once it is received.
@@ -8492,31 +8529,7 @@ where
84928529
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
84938530

84948531
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
8495-
if !offer.paths().is_empty() {
8496-
// Send as many invoice requests as there are paths in the offer (with an upper bound).
8497-
// Using only one path could result in a failure if the path no longer exists. But only
8498-
// one invoice for a given payment id will be paid, even if more than one is received.
8499-
const REQUEST_LIMIT: usize = 10;
8500-
for path in offer.paths().into_iter().take(REQUEST_LIMIT) {
8501-
let message = new_pending_onion_message(
8502-
OffersMessage::InvoiceRequest(invoice_request.clone()),
8503-
Destination::BlindedPath(path.clone()),
8504-
Some(reply_path.clone()),
8505-
);
8506-
pending_offers_messages.push(message);
8507-
}
8508-
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
8509-
let message = new_pending_onion_message(
8510-
OffersMessage::InvoiceRequest(invoice_request),
8511-
Destination::Node(signing_pubkey),
8512-
Some(reply_path),
8513-
);
8514-
pending_offers_messages.push(message);
8515-
} else {
8516-
debug_assert!(false);
8517-
return Err(Bolt12SemanticError::MissingSigningPubkey);
8518-
}
8519-
8532+
pending_offers_messages.extend(self.create_invoice_request_messages(invoice_request, reply_path)?);
85208533
self.pending_outbound_payments.awaiting_invoice_flag.store(true, Ordering::SeqCst);
85218534

85228535
Ok(())

0 commit comments

Comments
 (0)