Skip to content

Commit 5cd2b7d

Browse files
Add setter for paths to static invoice server
In future commits, as part of being an async recipient, we will interactively build offers and static invoices with an always-online node that will serve static invoices on our behalf. Here we add a setter for the blinded paths that we as an async recipient will use to contact the static invoice server to get paths to put in our offers.
1 parent f38f015 commit 5cd2b7d

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10776,6 +10776,16 @@ where
1077610776
)
1077710777
}
1077810778

10779+
/// Sets the [`BlindedMessagePath`]s that we will use as an async recipient to interactively build
10780+
/// [`Offer`]s with a static invoice server, so the server can serve [`StaticInvoice`]s to payers
10781+
/// on our behalf when we're offline.
10782+
#[cfg(async_payments)]
10783+
pub fn set_paths_to_static_invoice_server(
10784+
&self, paths_to_static_invoice_server: Vec<BlindedMessagePath>,
10785+
) -> Result<(), ()> {
10786+
self.flow.set_paths_to_static_invoice_server(paths_to_static_invoice_server)
10787+
}
10788+
1077910789
/// Pays for an [`Offer`] using the given parameters by creating an [`InvoiceRequest`] and
1078010790
/// enqueuing it to be sent via an onion message. [`ChannelManager`] will pay the actual
1078110791
/// [`Bolt12Invoice`] once it is received.

lightning/src/offers/async_receive_offer_cache.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::util::ser::{Readable, Writeable, Writer};
2323
use core::time::Duration;
2424

2525
/// The status of this offer in the cache.
26+
#[derive(Clone)]
2627
enum OfferStatus {
2728
/// This offer has been returned to the user from the cache, so it needs to be stored until it
2829
/// expires and its invoice needs to be kept updated.
@@ -42,6 +43,7 @@ enum OfferStatus {
4243
Pending,
4344
}
4445

46+
#[derive(Clone)]
4547
struct AsyncReceiveOffer {
4648
offer: Offer,
4749
/// Whether this offer is used, ready for use, or pending invoice persistence with the static
@@ -140,8 +142,35 @@ impl AsyncReceiveOfferCache {
140142
pub(super) fn paths_to_static_invoice_server(&self) -> Vec<BlindedMessagePath> {
141143
self.paths_to_static_invoice_server.clone()
142144
}
145+
146+
/// Sets the [`BlindedMessagePath`]s that we will use as an async recipient to interactively build
147+
/// [`Offer`]s with a static invoice server, so the server can serve [`StaticInvoice`]s to payers
148+
/// on our behalf when we're offline.
149+
///
150+
/// [`StaticInvoice`]: crate::offers::static_invoice::StaticInvoice
151+
#[cfg(async_payments)]
152+
pub fn set_paths_to_static_invoice_server(
153+
&mut self, paths_to_static_invoice_server: Vec<BlindedMessagePath>,
154+
) -> Result<(), ()> {
155+
if paths_to_static_invoice_server.is_empty() {
156+
return Err(());
157+
}
158+
159+
self.paths_to_static_invoice_server = paths_to_static_invoice_server;
160+
if self.offers.is_empty() {
161+
// See `AsyncReceiveOfferCache::offers`.
162+
self.offers = vec![None; MAX_CACHED_OFFERS_TARGET];
163+
}
164+
Ok(())
165+
}
143166
}
144167

168+
// The target number of offers we want to have cached at any given time, to mitigate too much
169+
// reuse of the same offer while also limiting the amount of space our offers take up on the
170+
// server's end.
171+
#[cfg(async_payments)]
172+
const MAX_CACHED_OFFERS_TARGET: usize = 10;
173+
145174
impl Writeable for AsyncReceiveOfferCache {
146175
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
147176
write_tlv_fields!(w, {

lightning/src/offers/flow.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,24 @@ where
158158
self
159159
}
160160

161+
/// Sets the [`BlindedMessagePath`]s that we will use as an async recipient to interactively build
162+
/// [`Offer`]s with a static invoice server, so the server can serve [`StaticInvoice`]s to payers
163+
/// on our behalf when we're offline.
164+
#[cfg(async_payments)]
165+
pub(crate) fn set_paths_to_static_invoice_server(
166+
&self, paths_to_static_invoice_server: Vec<BlindedMessagePath>,
167+
) -> Result<(), ()> {
168+
// Store the paths in the async receive cache so they are persisted with the cache, but also
169+
// store them in-memory in the `OffersMessageFlow` so the flow has access to them when building
170+
// onion messages to send to the static invoice server, without introducing undesirable lock
171+
// dependencies with the cache.
172+
*self.paths_to_static_invoice_server.lock().unwrap() =
173+
paths_to_static_invoice_server.clone();
174+
175+
let mut cache = self.async_receive_offer_cache.lock().unwrap();
176+
cache.set_paths_to_static_invoice_server(paths_to_static_invoice_server)
177+
}
178+
161179
/// Gets the node_id held by this [`OffersMessageFlow`]`
162180
fn get_our_node_id(&self) -> PublicKey {
163181
self.our_network_pubkey

0 commit comments

Comments
 (0)