Skip to content

Commit 12af26d

Browse files
committed
Introduce request_invoice_request_messages function
We need to retry InvoiceRequest messages in a smaller time duration than timer_tick_occurred. This function provides the base for doing the retry, by getting the InvoiceRequest for PendingOutboundPayments and using a new reply_path to create and enqueue InvoiceRequest messages.
1 parent 970a790 commit 12af26d

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ use crate::util::string::UntrustedString;
7575
use crate::util::ser::{BigSize, FixedLengthReader, Readable, ReadableArgs, MaybeReadable, Writeable, Writer, VecWriter};
7676
use crate::util::logger::{Level, Logger, WithContext};
7777
use crate::util::errors::APIError;
78+
use super::onion_utils::construct_invoice_request_message;
79+
7880
#[cfg(not(c_bindings))]
7981
use {
8082
crate::offers::offer::DerivedMetadata,
@@ -6030,6 +6032,28 @@ where
60306032
});
60316033
}
60326034

6035+
/// Performs actions that should happen roughly once every 5 seconds.
6036+
///
6037+
/// Currently, this includes retrying the sending of [`InvoiceRequest`] messages using newly
6038+
/// generated `reply_path` for payments that are still awaiting their corresponding
6039+
/// [`Bolt12Invoice`].
6040+
///
6041+
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
6042+
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
6043+
fn retry_invoice_request_messages(&self) {
6044+
let invoice_requests = self.pending_outbound_payments.get_invoice_request_awaiting_invoice();
6045+
6046+
if invoice_requests.is_empty() { return; }
6047+
6048+
if let Ok(reply_path) = self.create_blinded_path() {
6049+
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
6050+
6051+
for invoice_request in invoice_requests {
6052+
pending_offers_messages.extend(construct_invoice_request_message(invoice_request, reply_path.clone()));
6053+
}
6054+
}
6055+
}
6056+
60336057
/// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
60346058
/// after a PaymentClaimable event, failing the HTLC back to its origin and freeing resources
60356059
/// along the path (including in our own channel on which we received it).
@@ -8765,27 +8789,7 @@ where
87658789
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
87668790

87678791
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
8768-
if offer.paths().is_empty() {
8769-
let message = new_pending_onion_message(
8770-
OffersMessage::InvoiceRequest(invoice_request),
8771-
Destination::Node(offer.signing_pubkey()),
8772-
Some(reply_path),
8773-
);
8774-
pending_offers_messages.push(message);
8775-
} else {
8776-
// Send as many invoice requests as there are paths in the offer (with an upper bound).
8777-
// Using only one path could result in a failure if the path no longer exists. But only
8778-
// one invoice for a given payment id will be paid, even if more than one is received.
8779-
const REQUEST_LIMIT: usize = 10;
8780-
for path in offer.paths().into_iter().take(REQUEST_LIMIT) {
8781-
let message = new_pending_onion_message(
8782-
OffersMessage::InvoiceRequest(invoice_request.clone()),
8783-
Destination::BlindedPath(path.clone()),
8784-
Some(reply_path.clone()),
8785-
);
8786-
pending_offers_messages.push(message);
8787-
}
8788-
}
8792+
pending_offers_messages.extend(construct_invoice_request_message(invoice_request, reply_path));
87898793

87908794
Ok(())
87918795
}

lightning/src/ln/onion_utils.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10+
use crate::blinded_path::BlindedPath;
1011
use crate::crypto::chacha20::ChaCha20;
1112
use crate::crypto::streams::ChaChaReader;
1213
use crate::ln::channelmanager::{HTLCSource, RecipientOnionFields};
1314
use crate::ln::msgs;
1415
use crate::ln::wire::Encode;
1516
use crate::ln::{PaymentHash, PaymentPreimage};
17+
use crate::offers::invoice_request::InvoiceRequest;
18+
use crate::onion_message::messenger::{
19+
new_pending_onion_message, Destination, PendingOnionMessage,
20+
};
21+
use crate::onion_message::offers::OffersMessage;
1622
use crate::routing::gossip::NetworkUpdate;
1723
use crate::routing::router::{BlindedTail, Path, RouteHop};
1824
use crate::sign::NodeSigner;
@@ -1235,6 +1241,34 @@ fn decode_next_hop<T, R: ReadableArgs<T>, N: NextPacketBytes>(
12351241
}
12361242
}
12371243

1244+
pub fn construct_invoice_request_message(
1245+
invoice_request: InvoiceRequest, reply_path: BlindedPath,
1246+
) -> Vec<PendingOnionMessage<OffersMessage>> {
1247+
let mut messages = vec![];
1248+
if invoice_request.paths().is_empty() {
1249+
let message = new_pending_onion_message(
1250+
OffersMessage::InvoiceRequest(invoice_request.clone()),
1251+
Destination::Node(invoice_request.signing_pubkey()),
1252+
Some(reply_path),
1253+
);
1254+
messages.push(message);
1255+
} else {
1256+
// Send as many invoice requests as there are paths in the offer (with an upper bound).
1257+
// Using only one path could result in a failure if the path no longer exists. But only
1258+
// one invoice for a given payment id will be paid, even if more than one is received.
1259+
const REQUEST_LIMIT: usize = 10;
1260+
for path in invoice_request.paths().into_iter().take(REQUEST_LIMIT) {
1261+
let message = new_pending_onion_message(
1262+
OffersMessage::InvoiceRequest(invoice_request.clone()),
1263+
Destination::BlindedPath(path.clone()),
1264+
Some(reply_path.clone()),
1265+
);
1266+
messages.push(message);
1267+
}
1268+
}
1269+
messages
1270+
}
1271+
12381272
#[cfg(test)]
12391273
mod tests {
12401274
use crate::io;

0 commit comments

Comments
 (0)