Skip to content

Commit 1dff554

Browse files
committed
Introduce retry_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 7fd7364 commit 1dff554

File tree

2 files changed

+62
-24
lines changed

2 files changed

+62
-24
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ use crate::util::string::UntrustedString;
7676
use crate::util::ser::{BigSize, FixedLengthReader, Readable, ReadableArgs, MaybeReadable, Writeable, Writer, VecWriter};
7777
use crate::util::logger::{Level, Logger, WithContext};
7878
use crate::util::errors::APIError;
79+
use super::onion_utils::construct_invoice_request_message;
7980

8081
#[cfg(not(c_bindings))]
8182
use {
@@ -6043,6 +6044,27 @@ where
60436044
});
60446045
}
60456046

6047+
/// Performs actions that should happen roughly once every 5 seconds.
6048+
///
6049+
/// Currently, this includes retrying the sending of [`InvoiceRequest`] messages using newly
6050+
/// generated `reply_path` for payments that are still awaiting their corresponding
6051+
/// [`Bolt12Invoice`].
6052+
///
6053+
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
6054+
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
6055+
fn retry_invoice_request_messages(&self) -> Result<(), Bolt12SemanticError> {
6056+
let invoice_requests = self.pending_outbound_payments.get_invoice_request_awaiting_invoice();
6057+
if invoice_requests.is_empty() { return Ok(()); }
6058+
if let Ok(reply_path) = self.create_blinded_path() {
6059+
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
6060+
6061+
for invoice_request in invoice_requests {
6062+
pending_offers_messages.extend(construct_invoice_request_message(invoice_request, reply_path.clone())?);
6063+
}
6064+
}
6065+
Ok(())
6066+
}
6067+
60466068
/// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
60476069
/// after a PaymentClaimable event, failing the HTLC back to its origin and freeing resources
60486070
/// along the path (including in our own channel on which we received it).
@@ -8777,30 +8799,7 @@ where
87778799
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
87788800

87798801
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
8780-
if !offer.paths().is_empty() {
8781-
// Send as many invoice requests as there are paths in the offer (with an upper bound).
8782-
// Using only one path could result in a failure if the path no longer exists. But only
8783-
// one invoice for a given payment id will be paid, even if more than one is received.
8784-
const REQUEST_LIMIT: usize = 10;
8785-
for path in offer.paths().into_iter().take(REQUEST_LIMIT) {
8786-
let message = new_pending_onion_message(
8787-
OffersMessage::InvoiceRequest(invoice_request.clone()),
8788-
Destination::BlindedPath(path.clone()),
8789-
Some(reply_path.clone()),
8790-
);
8791-
pending_offers_messages.push(message);
8792-
}
8793-
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
8794-
let message = new_pending_onion_message(
8795-
OffersMessage::InvoiceRequest(invoice_request),
8796-
Destination::Node(signing_pubkey),
8797-
Some(reply_path),
8798-
);
8799-
pending_offers_messages.push(message);
8800-
} else {
8801-
debug_assert!(false);
8802-
return Err(Bolt12SemanticError::MissingSigningPubkey);
8803-
}
8802+
pending_offers_messages.extend(construct_invoice_request_message(invoice_request, reply_path)?);
88048803

88058804
Ok(())
88068805
}

lightning/src/ln/onion_utils.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
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::types::{PaymentHash, PaymentPreimage};
1516
use crate::ln::wire::Encode;
17+
use crate::offers::invoice_request::InvoiceRequest;
18+
use crate::offers::parse::Bolt12SemanticError;
19+
use crate::onion_message::messenger::{
20+
new_pending_onion_message, Destination, PendingOnionMessage,
21+
};
22+
use crate::onion_message::offers::OffersMessage;
1623
use crate::routing::gossip::NetworkUpdate;
1724
use crate::routing::router::{BlindedTail, Path, RouteHop};
1825
use crate::sign::NodeSigner;
@@ -1235,6 +1242,38 @@ fn decode_next_hop<T, R: ReadableArgs<T>, N: NextPacketBytes>(
12351242
}
12361243
}
12371244

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

0 commit comments

Comments
 (0)