Skip to content

Commit d03ef2b

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 669a459 commit d03ef2b

File tree

1 file changed

+62
-42
lines changed

1 file changed

+62
-42
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8376,8 +8376,10 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
83768376
let entropy = &*$self.entropy_source;
83778377
let secp_ctx = &$self.secp_ctx;
83788378

8379-
let path = $self.create_blinded_path_using_absolute_expiry(absolute_expiry)
8379+
let path = $self.create_blinded_paths_using_absolute_expiry(absolute_expiry)
8380+
.and_then(|paths| paths.into_iter().next().ok_or(()))
83808381
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
8382+
83818383
let builder = OfferBuilder::deriving_signing_pubkey(
83828384
node_id, expanded_key, entropy, secp_ctx
83838385
)
@@ -8448,8 +8450,10 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
84488450
let entropy = &*$self.entropy_source;
84498451
let secp_ctx = &$self.secp_ctx;
84508452

8451-
let path = $self.create_blinded_path_using_absolute_expiry(Some(absolute_expiry))
8453+
let path = $self.create_blinded_paths_using_absolute_expiry(Some(absolute_expiry))
8454+
.and_then(|paths| paths.into_iter().next().ok_or(()))
84528455
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
8456+
84538457
let builder = RefundBuilder::deriving_payer_id(
84548458
node_id, expanded_key, entropy, secp_ctx, amount_msats, payment_id
84558459
)?
@@ -8470,6 +8474,12 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
84708474
}
84718475
} }
84728476

8477+
/// Defines the maximum number of [`OffersMessage`] to be sent along different reply paths.
8478+
/// Sending multiple requests increases the chances of successful delivery in case some
8479+
/// paths are unavailable. However, only one invoice for a given [`PaymentId`] will be paid,
8480+
/// even if multiple invoices are received.
8481+
const OFFERS_MESSAGE_REQUEST_LIMIT: usize = 10;
8482+
84738483
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref> ChannelManager<M, T, ES, NS, SP, F, R, L>
84748484
where
84758485
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
@@ -8571,7 +8581,7 @@ where
85718581
Some(payer_note) => builder.payer_note(payer_note),
85728582
};
85738583
let invoice_request = builder.build_and_sign()?;
8574-
let reply_path = self.create_blinded_path().map_err(|_| Bolt12SemanticError::MissingPaths)?;
8584+
let reply_paths = self.create_blinded_paths().map_err(|_| Bolt12SemanticError::MissingPaths)?;
85758585

85768586
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
85778587

@@ -8584,25 +8594,27 @@ where
85848594

85858595
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
85868596
if !offer.paths().is_empty() {
8587-
// Send as many invoice requests as there are paths in the offer (with an upper bound).
8588-
// Using only one path could result in a failure if the path no longer exists. But only
8589-
// one invoice for a given payment id will be paid, even if more than one is received.
8590-
const REQUEST_LIMIT: usize = 10;
8591-
for path in offer.paths().into_iter().take(REQUEST_LIMIT) {
8597+
reply_paths
8598+
.iter()
8599+
.flat_map(|reply_path| offer.paths().iter().map(move |path| (path, reply_path)))
8600+
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
8601+
.for_each(|(path, reply_path)| {
8602+
let message = new_pending_onion_message(
8603+
OffersMessage::InvoiceRequest(invoice_request.clone()),
8604+
Destination::BlindedPath(path.clone()),
8605+
Some(reply_path.clone()),
8606+
);
8607+
pending_offers_messages.push(message);
8608+
});
8609+
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
8610+
for reply_path in reply_paths {
85928611
let message = new_pending_onion_message(
85938612
OffersMessage::InvoiceRequest(invoice_request.clone()),
8594-
Destination::BlindedPath(path.clone()),
8595-
Some(reply_path.clone()),
8613+
Destination::Node(signing_pubkey),
8614+
Some(reply_path),
85968615
);
85978616
pending_offers_messages.push(message);
85988617
}
8599-
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
8600-
let message = new_pending_onion_message(
8601-
OffersMessage::InvoiceRequest(invoice_request),
8602-
Destination::Node(signing_pubkey),
8603-
Some(reply_path),
8604-
);
8605-
pending_offers_messages.push(message);
86068618
} else {
86078619
debug_assert!(false);
86088620
return Err(Bolt12SemanticError::MissingSigningPubkey);
@@ -8671,26 +8683,32 @@ where
86718683
)?;
86728684
let builder: InvoiceBuilder<DerivedSigningPubkey> = builder.into();
86738685
let invoice = builder.allow_mpp().build_and_sign(secp_ctx)?;
8674-
let reply_path = self.create_blinded_path()
8686+
let reply_paths = self.create_blinded_paths()
86758687
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
86768688

86778689
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
86788690
if refund.paths().is_empty() {
8679-
let message = new_pending_onion_message(
8680-
OffersMessage::Invoice(invoice.clone()),
8681-
Destination::Node(refund.payer_id()),
8682-
Some(reply_path),
8683-
);
8684-
pending_offers_messages.push(message);
8685-
} else {
8686-
for path in refund.paths() {
8691+
for reply_path in reply_paths {
86878692
let message = new_pending_onion_message(
86888693
OffersMessage::Invoice(invoice.clone()),
8689-
Destination::BlindedPath(path.clone()),
8690-
Some(reply_path.clone()),
8694+
Destination::Node(refund.payer_id()),
8695+
Some(reply_path),
86918696
);
86928697
pending_offers_messages.push(message);
86938698
}
8699+
} else {
8700+
reply_paths
8701+
.iter()
8702+
.flat_map(|reply_path| refund.paths().iter().map(move |path| (path, reply_path)))
8703+
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
8704+
.for_each(|(path, reply_path)| {
8705+
let message = new_pending_onion_message(
8706+
OffersMessage::Invoice(invoice.clone()),
8707+
Destination::BlindedPath(path.clone()),
8708+
Some(reply_path.clone()),
8709+
);
8710+
pending_offers_messages.push(message);
8711+
});
86948712
}
86958713

86968714
Ok(invoice)
@@ -8797,22 +8815,22 @@ where
87978815
inbound_payment::get_payment_preimage(payment_hash, payment_secret, &self.inbound_payment_key)
87988816
}
87998817

8800-
/// Creates a blinded path by delegating to [`MessageRouter`] based on the path's intended
8801-
/// lifetime.
8818+
/// Creates a collection of blinded paths by delegating to [`MessageRouter`] based on
8819+
/// the path's intended lifetime.
88028820
///
88038821
/// Whether or not the path is compact depends on whether the path is short-lived or long-lived,
88048822
/// respectively, based on the given `absolute_expiry` as seconds since the Unix epoch. See
88058823
/// [`MAX_SHORT_LIVED_RELATIVE_EXPIRY`].
8806-
fn create_blinded_path_using_absolute_expiry(
8824+
fn create_blinded_paths_using_absolute_expiry(
88078825
&self, absolute_expiry: Option<Duration>
8808-
) -> Result<BlindedPath, ()> {
8826+
) -> Result<Vec<BlindedPath>, ()> {
88098827
let now = self.duration_since_epoch();
88108828
let max_short_lived_absolute_expiry = now.saturating_add(MAX_SHORT_LIVED_RELATIVE_EXPIRY);
88118829

88128830
if absolute_expiry.unwrap_or(Duration::MAX) <= max_short_lived_absolute_expiry {
8813-
self.create_compact_blinded_path()
8831+
self.create_compact_blinded_paths()
88148832
} else {
8815-
self.create_blinded_path()
8833+
self.create_blinded_paths()
88168834
}
88178835
}
88188836

@@ -8829,10 +8847,11 @@ where
88298847
now
88308848
}
88318849

8832-
/// Creates a blinded path by delegating to [`MessageRouter::create_blinded_paths`].
8850+
/// Creates a collection of blinded paths by delegating to
8851+
/// [`MessageRouter::create_blinded_paths`].
88338852
///
8834-
/// Errors if the `MessageRouter` errors or returns an empty `Vec`.
8835-
fn create_blinded_path(&self) -> Result<BlindedPath, ()> {
8853+
/// Errors if the `MessageRouter` errors.
8854+
fn create_blinded_paths(&self) -> Result<Vec<BlindedPath>, ()> {
88368855
let recipient = self.get_our_node_id();
88378856
let secp_ctx = &self.secp_ctx;
88388857

@@ -8846,13 +8865,14 @@ where
88468865

88478866
self.router
88488867
.create_blinded_paths(recipient, peers, secp_ctx)
8849-
.and_then(|paths| paths.into_iter().next().ok_or(()))
8868+
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
88508869
}
88518870

8852-
/// Creates a blinded path by delegating to [`MessageRouter::create_compact_blinded_paths`].
8871+
/// Creates a collection of blinded paths by delegating to
8872+
/// [`MessageRouter::create_compact_blinded_paths`].
88538873
///
8854-
/// Errors if the `MessageRouter` errors or returns an empty `Vec`.
8855-
fn create_compact_blinded_path(&self) -> Result<BlindedPath, ()> {
8874+
/// Errors if the `MessageRouter` errors.
8875+
fn create_compact_blinded_paths(&self) -> Result<Vec<BlindedPath>, ()> {
88568876
let recipient = self.get_our_node_id();
88578877
let secp_ctx = &self.secp_ctx;
88588878

@@ -8873,7 +8893,7 @@ where
88738893

88748894
self.router
88758895
.create_compact_blinded_paths(recipient, peers, secp_ctx)
8876-
.and_then(|paths| paths.into_iter().next().ok_or(()))
8896+
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
88778897
}
88788898

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

0 commit comments

Comments
 (0)