Skip to content

Commit 56013be

Browse files
committed
Expand create_blinded_path Functionality for Enhanced Path Diversification
- Previously, the `create_blinded_path` function was limited to returning a single `BlindedPath`, which restricted the usage of `blinded_paths`. - This commit extends the `create_blinded_path` function to return the entire blinded path vector generated by the `MessageRouter`'s `create_blinded_paths`. - The updated functionality is integrated across the codebase, enabling the sending of Offers Response messages, such as `InvoiceRequest` (in `pay_for_offer`) and `Invoice` (in `request_refund_payment`), utilizing multiple reply paths.
1 parent 2701bc5 commit 56013be

File tree

2 files changed

+58
-32
lines changed

2 files changed

+58
-32
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8270,7 +8270,11 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
82708270
let entropy = &*$self.entropy_source;
82718271
let secp_ctx = &$self.secp_ctx;
82728272

8273-
let path = $self.create_blinded_path().map_err(|_| Bolt12SemanticError::MissingPaths)?;
8273+
// TODO: Introduce a parameter to allow adding more paths to the created Offer.
8274+
let path = $self.create_blinded_path()
8275+
.and_then(|paths| paths.into_iter().next().ok_or(()))
8276+
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
8277+
82748278
let builder = OfferBuilder::deriving_signing_pubkey(
82758279
node_id, expanded_key, entropy, secp_ctx
82768280
)
@@ -8337,7 +8341,11 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
83378341
let entropy = &*$self.entropy_source;
83388342
let secp_ctx = &$self.secp_ctx;
83398343

8340-
let path = $self.create_blinded_path().map_err(|_| Bolt12SemanticError::MissingPaths)?;
8344+
// TODO: Introduce a parameter to allow adding more paths to the created Refund.
8345+
let path = $self.create_blinded_path()
8346+
.and_then(|paths| paths.into_iter().next().ok_or(()))
8347+
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
8348+
83418349
let builder = RefundBuilder::deriving_payer_id(
83428350
node_id, expanded_key, entropy, secp_ctx, amount_msats, payment_id
83438351
)?
@@ -8460,7 +8468,7 @@ where
84608468
Some(payer_note) => builder.payer_note(payer_note),
84618469
};
84628470
let invoice_request = builder.build_and_sign()?;
8463-
let reply_path = self.create_blinded_path().map_err(|_| Bolt12SemanticError::MissingPaths)?;
8471+
let reply_paths = self.create_blinded_path().map_err(|_| Bolt12SemanticError::MissingPaths)?;
84648472

84658473
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
84668474

@@ -8473,25 +8481,32 @@ where
84738481

84748482
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
84758483
if !offer.paths().is_empty() {
8476-
// Send as many invoice requests as there are paths in the offer (with an upper bound).
8477-
// Using only one path could result in a failure if the path no longer exists. But only
8478-
// one invoice for a given payment id will be paid, even if more than one is received.
8484+
// Send as many invoice requests as there are paths in the offer using as many different
8485+
// reply_paths possible (with an upper bound).
8486+
// Using only one path could result in a failure if the path no longer exists.
8487+
// But only one invoice for a given payment ID will be paid, even if more than one is received.
84798488
const REQUEST_LIMIT: usize = 10;
8480-
for path in offer.paths().into_iter().take(REQUEST_LIMIT) {
8489+
reply_paths
8490+
.iter()
8491+
.flat_map(|reply_path| offer.paths().iter().map(move |path| (path, reply_path)))
8492+
.take(REQUEST_LIMIT)
8493+
.for_each(|(path, reply_path)| {
8494+
let message = new_pending_onion_message(
8495+
OffersMessage::InvoiceRequest(invoice_request.clone()),
8496+
Destination::BlindedPath(path.clone()),
8497+
Some(reply_path.clone()),
8498+
);
8499+
pending_offers_messages.push(message);
8500+
});
8501+
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
8502+
for reply_path in reply_paths {
84818503
let message = new_pending_onion_message(
84828504
OffersMessage::InvoiceRequest(invoice_request.clone()),
8483-
Destination::BlindedPath(path.clone()),
8484-
Some(reply_path.clone()),
8505+
Destination::Node(signing_pubkey),
8506+
Some(reply_path),
84858507
);
84868508
pending_offers_messages.push(message);
84878509
}
8488-
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
8489-
let message = new_pending_onion_message(
8490-
OffersMessage::InvoiceRequest(invoice_request),
8491-
Destination::Node(signing_pubkey),
8492-
Some(reply_path),
8493-
);
8494-
pending_offers_messages.push(message);
84958510
} else {
84968511
debug_assert!(false);
84978512
return Err(Bolt12SemanticError::MissingSigningPubkey);
@@ -8560,26 +8575,37 @@ where
85608575
)?;
85618576
let builder: InvoiceBuilder<DerivedSigningPubkey> = builder.into();
85628577
let invoice = builder.allow_mpp().build_and_sign(secp_ctx)?;
8563-
let reply_path = self.create_blinded_path()
8578+
let reply_paths = self.create_blinded_path()
85648579
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
85658580

85668581
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
85678582
if refund.paths().is_empty() {
8568-
let message = new_pending_onion_message(
8569-
OffersMessage::Invoice(invoice.clone()),
8570-
Destination::Node(refund.payer_id()),
8571-
Some(reply_path),
8572-
);
8573-
pending_offers_messages.push(message);
8574-
} else {
8575-
for path in refund.paths() {
8583+
for reply_path in reply_paths {
85768584
let message = new_pending_onion_message(
85778585
OffersMessage::Invoice(invoice.clone()),
8578-
Destination::BlindedPath(path.clone()),
8579-
Some(reply_path.clone()),
8586+
Destination::Node(refund.payer_id()),
8587+
Some(reply_path),
85808588
);
85818589
pending_offers_messages.push(message);
85828590
}
8591+
} else {
8592+
// Send as many BOLT12Invoices as there are paths in the refund using as many different
8593+
// reply_paths possible (with an upper bound).
8594+
// Using only one path could result in a failure if the path no longer exists.
8595+
// But only one invoice for a given payment ID will be paid, even if more than one is received.
8596+
const REQUEST_LIMIT: usize = 10;
8597+
reply_paths
8598+
.iter()
8599+
.flat_map(|reply_path| refund.paths().iter().map(move |path| (path, reply_path)))
8600+
.take(REQUEST_LIMIT)
8601+
.for_each(|(path, reply_path)| {
8602+
let message = new_pending_onion_message(
8603+
OffersMessage::Invoice(invoice.clone()),
8604+
Destination::BlindedPath(path.clone()),
8605+
Some(reply_path.clone()),
8606+
);
8607+
pending_offers_messages.push(message);
8608+
});
85838609
}
85848610

85858611
Ok(invoice)
@@ -8689,7 +8715,7 @@ where
86898715
/// Creates a blinded path by delegating to [`MessageRouter::create_blinded_paths`].
86908716
///
86918717
/// Errors if the `MessageRouter` errors or returns an empty `Vec`.
8692-
fn create_blinded_path(&self) -> Result<BlindedPath, ()> {
8718+
fn create_blinded_path(&self) -> Result<Vec<BlindedPath>, ()> {
86938719
let recipient = self.get_our_node_id();
86948720
let secp_ctx = &self.secp_ctx;
86958721

@@ -8709,7 +8735,6 @@ where
87098735

87108736
self.router
87118737
.create_blinded_paths(recipient, peers, secp_ctx)
8712-
.and_then(|paths| paths.into_iter().next().ok_or(()))
87138738
}
87148739

87158740
/// Creates multi-hop blinded payment paths for the given `amount_msats` by delegating to

lightning/src/onion_message/messenger.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ where
10721072
.map_err(|_| SendError::PathNotFound)
10731073
}
10741074

1075-
fn create_blinded_path(&self) -> Result<BlindedPath, SendError> {
1075+
fn create_blinded_path(&self) -> Result<Vec<BlindedPath>, SendError> {
10761076
let recipient = self.node_signer
10771077
.get_node_id(Recipient::Node)
10781078
.map_err(|_| SendError::GetNodeIdFailed)?;
@@ -1089,7 +1089,6 @@ where
10891089

10901090
self.message_router
10911091
.create_blinded_paths(recipient, peers, secp_ctx)
1092-
.and_then(|paths| paths.into_iter().next().ok_or(()))
10931092
.map_err(|_| SendError::PathNotFound)
10941093
}
10951094

@@ -1185,7 +1184,9 @@ where
11851184

11861185
let message_type = response.message.msg_type();
11871186
let reply_path = if create_reply_path {
1188-
match self.create_blinded_path() {
1187+
match self.create_blinded_path().and_then(
1188+
|paths| paths.into_iter().next().ok_or(SendError::PathNotFound)
1189+
) {
11891190
Ok(reply_path) => Some(reply_path),
11901191
Err(err) => {
11911192
log_trace!(

0 commit comments

Comments
 (0)