Skip to content

Commit c22f122

Browse files
Mark offer as ready on StaticInvoicePersisted onion message
As an async recipient, we need to interactively build a static invoice that an always-online node will serve on our behalf. Once this invoice is built and persisted by the static invoice server, they will send us a confirmation onion message. At this time, mark the corresponding pending offer as ready to receive async payments.
1 parent dfd9db3 commit c22f122

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13076,6 +13076,13 @@ where
1307613076
fn handle_static_invoice_persisted(
1307713077
&self, _message: StaticInvoicePersisted, _context: AsyncPaymentsContext,
1307813078
) {
13079+
#[cfg(async_payments)]
13080+
{
13081+
let should_persist = self.flow.handle_static_invoice_persisted(_context);
13082+
if should_persist {
13083+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
13084+
}
13085+
}
1307913086
}
1308013087

1308113088
fn handle_held_htlc_available(

lightning/src/offers/async_receive_offer_cache.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,24 @@ impl AsyncReceiveOfferCache {
316316
self.offer_paths_request_attempts = 0;
317317
self.last_offer_paths_request_timestamp = Duration::from_secs(0);
318318
}
319+
320+
/// Should be called when we receive a [`StaticInvoicePersisted`] message from the static invoice
321+
/// server, which indicates that a new offer was persisted by the server and they are ready to
322+
/// serve the corresponding static invoice to payers on our behalf.
323+
///
324+
/// Returns a bool indicating whether an offer was added/updated and re-persistence of the cache
325+
/// is needed.
326+
pub(super) fn static_invoice_persisted(
327+
&mut self, offer_slot: u8, duration_since_epoch: Duration,
328+
) -> bool {
329+
if let Some(Some(ref mut offer)) = self.offers.get_mut(offer_slot as usize) {
330+
offer.status =
331+
OfferStatus::Ready { invoice_confirmed_persisted_at: duration_since_epoch };
332+
return true;
333+
}
334+
335+
false
336+
}
319337
}
320338

321339
impl Writeable for AsyncReceiveOfferCache {

lightning/src/offers/flow.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,28 @@ where
13401340
Ok((invoice, forward_invoice_request_path))
13411341
}
13421342

1343+
/// Handles an incoming [`StaticInvoicePersisted`] onion message from the static invoice server.
1344+
/// Returns a bool indicating whether the async receive offer cache needs to be re-persisted.
1345+
///
1346+
/// [`StaticInvoicePersisted`]: crate::onion_message::async_payments::StaticInvoicePersisted
1347+
#[cfg(async_payments)]
1348+
pub(crate) fn handle_static_invoice_persisted(&self, context: AsyncPaymentsContext) -> bool {
1349+
let duration_since_epoch = self.duration_since_epoch();
1350+
1351+
let confirmed_offer_slot = match context {
1352+
AsyncPaymentsContext::StaticInvoicePersisted { path_absolute_expiry, offer_slot } => {
1353+
if duration_since_epoch > path_absolute_expiry {
1354+
return false;
1355+
}
1356+
offer_slot
1357+
},
1358+
_ => return false,
1359+
};
1360+
1361+
let mut cache = self.async_receive_offer_cache.lock().unwrap();
1362+
cache.static_invoice_persisted(confirmed_offer_slot, duration_since_epoch)
1363+
}
1364+
13431365
/// Get the `AsyncReceiveOfferCache` for persistence.
13441366
pub(crate) fn writeable_async_receive_offer_cache(&self) -> impl Writeable + '_ {
13451367
&self.async_receive_offer_cache

0 commit comments

Comments
 (0)