Skip to content

Commit f17f712

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 ebf220a commit f17f712

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::{OutboundPayments, PaymentAttempts, PendingOutb
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};
@@ -8437,6 +8437,43 @@ where
84378437
#[cfg(c_bindings)]
84388438
create_refund_builder!(self, RefundMaybeWithDerivedMetadataBuilder);
84398439

8440+
fn create_invoice_request_messages(&self, invoice_request: InvoiceRequest, reply_path: BlindedPath)
8441+
-> Result<Vec<PendingOnionMessage<OffersMessage>>, Bolt12SemanticError> {
8442+
let paths = invoice_request.paths();
8443+
let signing_pubkey = invoice_request.signing_pubkey();
8444+
8445+
if paths.is_empty() && signing_pubkey.is_none() {
8446+
debug_assert!(false);
8447+
return Err(Bolt12SemanticError::MissingSigningPubkey);
8448+
}
8449+
8450+
// Send as many invoice requests as there are paths in the offer (with an upper bound).
8451+
// Using only one path could result in a failure if the path no longer exists. But only
8452+
// one invoice for a given payment id will be paid, even if more than one is received.
8453+
const REQUEST_LIMIT: usize = 10;
8454+
8455+
let messages = if !paths.is_empty() {
8456+
paths.into_iter()
8457+
.take(REQUEST_LIMIT)
8458+
.map(|path| {
8459+
new_pending_onion_message(
8460+
OffersMessage::InvoiceRequest(invoice_request.clone()),
8461+
Destination::BlindedPath(path.clone()),
8462+
Some(reply_path.clone()),
8463+
)
8464+
})
8465+
.collect()
8466+
} else {
8467+
vec![new_pending_onion_message(
8468+
OffersMessage::InvoiceRequest(invoice_request.clone()),
8469+
Destination::Node(signing_pubkey.unwrap()),
8470+
Some(reply_path.clone()),
8471+
)]
8472+
};
8473+
8474+
Ok(messages)
8475+
}
8476+
84408477
/// Pays for an [`Offer`] using the given parameters by creating an [`InvoiceRequest`] and
84418478
/// enqueuing it to be sent via an onion message. [`ChannelManager`] will pay the actual
84428479
/// [`Bolt12Invoice`] once it is received.
@@ -8529,31 +8566,7 @@ where
85298566
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
85308567

85318568
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
8532-
if !offer.paths().is_empty() {
8533-
// Send as many invoice requests as there are paths in the offer (with an upper bound).
8534-
// Using only one path could result in a failure if the path no longer exists. But only
8535-
// one invoice for a given payment id will be paid, even if more than one is received.
8536-
const REQUEST_LIMIT: usize = 10;
8537-
for path in offer.paths().into_iter().take(REQUEST_LIMIT) {
8538-
let message = new_pending_onion_message(
8539-
OffersMessage::InvoiceRequest(invoice_request.clone()),
8540-
Destination::BlindedPath(path.clone()),
8541-
Some(reply_path.clone()),
8542-
);
8543-
pending_offers_messages.push(message);
8544-
}
8545-
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
8546-
let message = new_pending_onion_message(
8547-
OffersMessage::InvoiceRequest(invoice_request),
8548-
Destination::Node(signing_pubkey),
8549-
Some(reply_path),
8550-
);
8551-
pending_offers_messages.push(message);
8552-
} else {
8553-
debug_assert!(false);
8554-
return Err(Bolt12SemanticError::MissingSigningPubkey);
8555-
}
8556-
8569+
pending_offers_messages.extend(self.create_invoice_request_messages(invoice_request, reply_path)?);
85578570
self.pending_outbound_payments.awaiting_invoice_flag.store(true, Ordering::SeqCst);
85588571

85598572
Ok(())

0 commit comments

Comments
 (0)