Skip to content

Commit 7f69689

Browse files
committed
f - Expose an async / Future-based async receive offer API
Replace the blocking `await_async_receive_offer(Duration)` with an `async fn await_async_receive_offer(&self) -> Result<Offer, ()>` and a new `get_async_receive_offer_ready_future() -> Future` helper. Async callers can `.await` the offer directly; synchronous callers can fetch the underlying [`Future`] and call `wait_timeout` on it, which lets them combine with whatever timing primitive their runtime provides. Co-Authored-By: HAL 9000
1 parent 2d7e835 commit 7f69689

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

lightning/src/ln/channelmanager.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5884,21 +5884,36 @@ impl<
58845884
})
58855885
}
58865886

5887-
/// Waits for an async receive offer to become ready after the interactive static-invoice
5888-
/// protocol completes.
5887+
/// Returns once an async receive offer is ready after the interactive static-invoice
5888+
/// protocol completes, or immediately if one is already available.
5889+
///
5890+
/// Callers that need a timeout can combine this future with their runtime's timeout
5891+
/// primitive. Synchronous callers should instead fetch the underlying [`Future`] via
5892+
/// [`Self::get_async_receive_offer_ready_future`] and call [`Future::wait_timeout`] on it.
5893+
///
5894+
/// [`Future`]: crate::util::wakers::Future
5895+
/// [`Future::wait_timeout`]: crate::util::wakers::Future::wait_timeout
58895896
#[cfg(feature = "std")]
5890-
pub fn await_async_receive_offer(&self, max_wait: Duration) -> Result<Offer, ()> {
5897+
pub async fn await_async_receive_offer(&self) -> Result<Offer, ()> {
58915898
if let Ok(offer) = self.get_async_receive_offer() {
58925899
return Ok(offer);
58935900
}
58945901

5895-
if !self.flow.wait_for_async_receive_offer_ready(max_wait) {
5896-
return Err(());
5897-
}
5898-
5902+
self.flow.get_async_receive_offer_ready_future().await;
58995903
self.get_async_receive_offer()
59005904
}
59015905

5906+
/// Returns a [`Future`] that completes when an async receive offer is ready.
5907+
///
5908+
/// See [`OffersMessageFlow::get_async_receive_offer_ready_future`] for details.
5909+
///
5910+
/// [`Future`]: crate::util::wakers::Future
5911+
/// [`OffersMessageFlow::get_async_receive_offer_ready_future`]: crate::offers::flow::OffersMessageFlow::get_async_receive_offer_ready_future
5912+
#[cfg(feature = "std")]
5913+
pub fn get_async_receive_offer_ready_future(&self) -> crate::util::wakers::Future {
5914+
self.flow.get_async_receive_offer_ready_future()
5915+
}
5916+
59025917
/// Should be called after handling an [`Event::PersistStaticInvoice`], where the `Responder`
59035918
/// comes from [`Event::PersistStaticInvoice::invoice_persisted_path`].
59045919
pub fn static_invoice_persisted(&self, invoice_persisted_path: Responder) {

lightning/src/offers/flow.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ use crate::types::payment::{PaymentHash, PaymentSecret};
6262
use crate::util::logger::Logger;
6363
use crate::util::ser::Writeable;
6464
#[cfg(feature = "std")]
65+
use crate::util::wakers::Future;
66+
#[cfg(feature = "std")]
6567
use crate::util::wakers::Notifier;
6668

6769
/// A BOLT12 offers code and flow utility provider, which facilitates
@@ -1719,9 +1721,15 @@ impl<MR: MessageRouter, L: Logger> OffersMessageFlow<MR, L> {
17191721
updated
17201722
}
17211723

1724+
/// Returns a [`Future`] that completes when an async receive offer is ready, i.e., after the
1725+
/// interactive static-invoice protocol completes.
1726+
///
1727+
/// Callers can either `.await` the returned [`Future`] in an async context or call
1728+
/// [`Future::wait_timeout`] from a synchronous context. After it completes, use
1729+
/// [`Self::get_async_receive_offer`] to retrieve the offer.
17221730
#[cfg(feature = "std")]
1723-
pub(crate) fn wait_for_async_receive_offer_ready(&self, max_wait: Duration) -> bool {
1724-
self.async_receive_offer_ready_notifier.get_future().wait_timeout(max_wait)
1731+
pub fn get_async_receive_offer_ready_future(&self) -> Future {
1732+
self.async_receive_offer_ready_notifier.get_future()
17251733
}
17261734

17271735
/// Get the encoded [`AsyncReceiveOfferCache`] for persistence.

0 commit comments

Comments
 (0)