Skip to content

Commit 4ec0bb9

Browse files
Refresh served static invoices on each timer tick
As an async recipient, we need to interactively build offers and corresponding static invoices, the latter of which an always-online node will serve to payers on our behalf. We want the static invoice server to always have the freshest possible invoice available, so on each timer tick for every offer that is either currently in use or pending confirmation, send them a new invoice.
1 parent e20e52c commit 4ec0bb9

File tree

4 files changed

+128
-7
lines changed

4 files changed

+128
-7
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5142,7 +5142,17 @@ where
51425142
#[cfg(async_payments)]
51435143
fn check_refresh_async_receive_offers(&self, timer_tick_occurred: bool) {
51445144
let peers = self.get_peers_for_blinded_path();
5145-
match self.flow.check_refresh_async_receive_offers(peers, timer_tick_occurred) {
5145+
let channels = self.list_usable_channels();
5146+
let entropy = &*self.entropy_source;
5147+
let router = &*self.router;
5148+
let refresh_res = self.flow.check_refresh_async_receive_offers(
5149+
peers,
5150+
channels,
5151+
entropy,
5152+
router,
5153+
timer_tick_occurred,
5154+
);
5155+
match refresh_res {
51465156
Err(()) => {
51475157
log_error!(
51485158
self.logger,

lightning/src/offers/async_receive_offer_cache.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,30 @@ impl AsyncReceiveOfferCache {
364364
self.offer_paths_request_attempts = 0;
365365
}
366366

367+
/// Returns an iterator over the list of cached offers where we need to send an updated invoice to
368+
/// the static invoice server.
369+
pub(super) fn offers_needing_invoice_refresh(
370+
&self,
371+
) -> impl Iterator<Item = (&Offer, Nonce, u8, &Responder)> {
372+
// For any offers which are either in use or pending confirmation by the server, we should send
373+
// them a fresh invoice on each timer tick.
374+
self.offers_with_idx().filter_map(|(idx, offer)| {
375+
let needs_invoice_update =
376+
offer.status == OfferStatus::Used || offer.status == OfferStatus::Pending;
377+
if needs_invoice_update {
378+
let offer_slot = idx.try_into().unwrap_or(u8::MAX);
379+
Some((
380+
&offer.offer,
381+
offer.offer_nonce,
382+
offer_slot,
383+
&offer.update_static_invoice_path,
384+
))
385+
} else {
386+
None
387+
}
388+
})
389+
}
390+
367391
/// Should be called when we receive a [`StaticInvoicePersisted`] message from the static invoice
368392
/// server, which indicates that a new offer was persisted by the server and they are ready to
369393
/// serve the corresponding static invoice to payers on our behalf.

lightning/src/offers/flow.rs

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,8 +1131,9 @@ where
11311131
core::mem::take(&mut self.pending_dns_onion_messages.lock().unwrap())
11321132
}
11331133

1134-
/// Sends out [`OfferPathsRequest`] onion messages if we are an often-offline recipient and are
1135-
/// configured to interactively build offers and static invoices with a static invoice server.
1134+
/// Sends out [`OfferPathsRequest`] and [`ServeStaticInvoice`] onion messages if we are an
1135+
/// often-offline recipient and are configured to interactively build offers and static invoices
1136+
/// with a static invoice server.
11361137
///
11371138
/// # Usage
11381139
///
@@ -1144,9 +1145,14 @@ where
11441145
///
11451146
/// Errors if we failed to create blinded reply paths when sending an [`OfferPathsRequest`] message.
11461147
#[cfg(async_payments)]
1147-
pub(crate) fn check_refresh_async_receive_offers(
1148-
&self, peers: Vec<MessageForwardNode>, timer_tick_occurred: bool,
1149-
) -> Result<(), ()> {
1148+
pub(crate) fn check_refresh_async_receive_offers<ES: Deref, R: Deref>(
1149+
&self, peers: Vec<MessageForwardNode>, usable_channels: Vec<ChannelDetails>, entropy: ES,
1150+
router: R, timer_tick_occurred: bool,
1151+
) -> Result<(), ()>
1152+
where
1153+
ES::Target: EntropySource,
1154+
R::Target: Router,
1155+
{
11501156
// Terminate early if this node does not intend to receive async payments.
11511157
if self.paths_to_static_invoice_server.lock().unwrap().is_empty() {
11521158
return Ok(());
@@ -1168,7 +1174,7 @@ where
11681174
path_absolute_expiry: duration_since_epoch
11691175
.saturating_add(TEMP_REPLY_PATH_RELATIVE_EXPIRY),
11701176
});
1171-
let reply_paths = match self.create_blinded_paths(peers, context) {
1177+
let reply_paths = match self.create_blinded_paths(peers.clone(), context) {
11721178
Ok(paths) => paths,
11731179
Err(()) => {
11741180
return Err(());
@@ -1189,9 +1195,84 @@ where
11891195
);
11901196
}
11911197

1198+
if timer_tick_occurred {
1199+
self.check_refresh_static_invoices(peers, usable_channels, entropy, router);
1200+
}
1201+
11921202
Ok(())
11931203
}
11941204

1205+
/// Enqueue onion messages that will used to request invoice refresh from the static invoice
1206+
/// server, based on the offers provided by the cache.
1207+
#[cfg(async_payments)]
1208+
fn check_refresh_static_invoices<ES: Deref, R: Deref>(
1209+
&self, peers: Vec<MessageForwardNode>, usable_channels: Vec<ChannelDetails>, entropy: ES,
1210+
router: R,
1211+
) where
1212+
ES::Target: EntropySource,
1213+
R::Target: Router,
1214+
{
1215+
let duration_since_epoch = self.duration_since_epoch();
1216+
1217+
let mut serve_static_invoice_msgs = Vec::new();
1218+
{
1219+
let cache = self.async_receive_offer_cache.lock().unwrap();
1220+
for offer_and_metadata in cache.offers_needing_invoice_refresh() {
1221+
let (offer, offer_nonce, slot_number, update_static_invoice_path) =
1222+
offer_and_metadata;
1223+
1224+
let (invoice, forward_invreq_path) = match self.create_static_invoice_for_server(
1225+
offer,
1226+
offer_nonce,
1227+
peers.clone(),
1228+
usable_channels.clone(),
1229+
&*entropy,
1230+
&*router,
1231+
) {
1232+
Ok((invoice, path)) => (invoice, path),
1233+
Err(()) => continue,
1234+
};
1235+
1236+
let reply_path_context = {
1237+
let path_absolute_expiry =
1238+
duration_since_epoch.saturating_add(TEMP_REPLY_PATH_RELATIVE_EXPIRY);
1239+
MessageContext::AsyncPayments(AsyncPaymentsContext::StaticInvoicePersisted {
1240+
path_absolute_expiry,
1241+
offer_slot: slot_number,
1242+
})
1243+
};
1244+
1245+
let serve_invoice_message = ServeStaticInvoice {
1246+
invoice,
1247+
forward_invoice_request_path: forward_invreq_path,
1248+
invoice_slot: slot_number,
1249+
};
1250+
serve_static_invoice_msgs.push((
1251+
serve_invoice_message,
1252+
update_static_invoice_path.clone(),
1253+
reply_path_context,
1254+
));
1255+
}
1256+
}
1257+
1258+
// Enqueue the new serve_static_invoice messages in a separate loop to avoid holding the offer
1259+
// cache lock and the pending_async_payments_messages lock at the same time.
1260+
for (serve_invoice_msg, serve_invoice_path, reply_path_ctx) in serve_static_invoice_msgs {
1261+
let reply_paths = match self.create_blinded_paths(peers.clone(), reply_path_ctx) {
1262+
Ok(paths) => paths,
1263+
Err(()) => continue,
1264+
};
1265+
1266+
let message = AsyncPaymentsMessage::ServeStaticInvoice(serve_invoice_msg);
1267+
enqueue_onion_message_with_reply_paths(
1268+
message,
1269+
&[serve_invoice_path.into_blinded_path()],
1270+
reply_paths,
1271+
&mut self.pending_async_payments_messages.lock().unwrap(),
1272+
);
1273+
}
1274+
}
1275+
11951276
/// Handles an incoming [`OfferPaths`] message from the static invoice server, sending out
11961277
/// [`ServeStaticInvoice`] onion messages in response if we've built a new async receive offer and
11971278
/// need the corresponding [`StaticInvoice`] to be persisted by the static invoice server.

lightning/src/onion_message/messenger.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,12 @@ impl Responder {
432432
context: Some(context),
433433
}
434434
}
435+
436+
/// Converts a [`Responder`] into its inner [`BlindedMessagePath`].
437+
#[cfg(async_payments)]
438+
pub(crate) fn into_blinded_path(self) -> BlindedMessagePath {
439+
self.reply_path
440+
}
435441
}
436442

437443
/// Instructions for how and where to send the response to an onion message.

0 commit comments

Comments
 (0)