Skip to content

Commit

Permalink
Remove create_blinded_path_using_absolute_expiry
Browse files Browse the repository at this point in the history
1. This function was initially introduced in commit
   `8012c2b213127372bdad150cdc354920d971087d` to allow using
   compact or full-length blinded paths based on the lifetime of
   Offers and Refunds.
2. With the introduction of `BlindedPathParams`, users can now
   explicitly specify the type of blinded path to be used, rendering
   this functionality redundant.
3. As a result, the corresponding `create_blinded_path` variant is removed.
4. The params parameter is introduced as an option, that allows the user
   to create Offers and Refund without BlindedPath if needed.
  • Loading branch information
shaavan committed Aug 19, 2024
1 parent 318b72e commit c078155
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 106 deletions.
79 changes: 35 additions & 44 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1704,12 +1704,16 @@ where
/// # use lightning::events::{Event, EventsProvider, PaymentPurpose};
/// # use lightning::ln::channelmanager::AChannelManager;
/// # use lightning::offers::parse::Bolt12SemanticError;
/// # use lightning::onion_message::messenger::BlindedPathParams;
/// #
/// # fn example<T: AChannelManager>(channel_manager: T) -> Result<(), Bolt12SemanticError> {
/// # let channel_manager = channel_manager.get_cm();
/// # let absolute_expiry = None;
/// # let params = BlindedPathParams {
/// # paths: 0,
/// # is_compact: false,
/// # };
/// let offer = channel_manager
/// .create_offer_builder(absolute_expiry)?
/// .create_offer_builder(Some(params))?
/// # ;
/// # // Needed for compiling for c_bindings
/// # let builder: lightning::offers::offer::OfferBuilder<_, _> = offer.into();
Expand Down Expand Up @@ -1807,16 +1811,21 @@ where
/// # use lightning::events::{Event, EventsProvider};
/// # use lightning::ln::channelmanager::{AChannelManager, PaymentId, RecentPaymentDetails, Retry};
/// # use lightning::offers::parse::Bolt12SemanticError;
/// # use lightning::onion_message::messenger::BlindedPathParams;
/// #
/// # fn example<T: AChannelManager>(
/// # channel_manager: T, amount_msats: u64, absolute_expiry: Duration, retry: Retry,
/// # max_total_routing_fee_msat: Option<u64>
/// # ) -> Result<(), Bolt12SemanticError> {
/// # let channel_manager = channel_manager.get_cm();
/// let payment_id = PaymentId([42; 32]);
/// # let params = BlindedPathParams {
/// # paths: 0,
/// # is_compact: false,
/// # };
/// # let payment_id = PaymentId([42; 32]);
/// let refund = channel_manager
/// .create_refund_builder(
/// amount_msats, absolute_expiry, payment_id, retry, max_total_routing_fee_msat
/// Some(params), amount_msats, absolute_expiry, payment_id, retry, max_total_routing_fee_msat
/// )?
/// # ;
/// # // Needed for compiling for c_bindings
Expand Down Expand Up @@ -8808,7 +8817,7 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
/// [`Offer`]: crate::offers::offer::Offer
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
pub fn create_offer_builder(
&$self, absolute_expiry: Option<Duration>
&$self, params: Option<BlindedPathParams>,
) -> Result<$builder, Bolt12SemanticError> {
let node_id = $self.get_our_node_id();
let expanded_key = &$self.inbound_payment_key;
Expand All @@ -8817,17 +8826,15 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {

let nonce = Nonce::from_entropy_source(entropy);
let context = OffersContext::InvoiceRequest { nonce };
let path = $self.create_blinded_paths_using_absolute_expiry(context, absolute_expiry)
let mut builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
.chain_hash($self.chain_hash);
if let Some(params) = params {
let path = $self.create_blinded_paths(params, context)
.and_then(|paths| paths.into_iter().next().ok_or(()))
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
let builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
.chain_hash($self.chain_hash)
.path(path);

let builder = match absolute_expiry {
None => builder,
Some(absolute_expiry) => builder.absolute_expiry(absolute_expiry),
};

builder = builder.path(path);
}

Ok(builder.into())
}
Expand Down Expand Up @@ -8880,7 +8887,8 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
/// [`Bolt12Invoice::payment_paths`]: crate::offers::invoice::Bolt12Invoice::payment_paths
/// [Avoiding Duplicate Payments]: #avoiding-duplicate-payments
pub fn create_refund_builder(
&$self, amount_msats: u64, absolute_expiry: Duration, payment_id: PaymentId,
&$self, params: Option<BlindedPathParams>, amount_msats: u64,
absolute_expiry: Duration, payment_id: PaymentId,
retry_strategy: Retry, max_total_routing_fee_msat: Option<u64>
) -> Result<$builder, Bolt12SemanticError> {
let node_id = $self.get_our_node_id();
Expand All @@ -8891,15 +8899,19 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
let nonce = Nonce::from_entropy_source(entropy);
let context = OffersContext::OutboundPayment { payment_id, nonce, hmac: None };

let path = $self.create_blinded_paths_using_absolute_expiry(context, Some(absolute_expiry))
.and_then(|paths| paths.into_iter().next().ok_or(()))
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
let builder = RefundBuilder::deriving_payer_id(
let mut builder = RefundBuilder::deriving_payer_id(
node_id, expanded_key, nonce, secp_ctx, amount_msats, payment_id
)?
.chain_hash($self.chain_hash)
.absolute_expiry(absolute_expiry)
.path(path);
.absolute_expiry(absolute_expiry);

if let Some(params) = params {
let path = $self.create_blinded_paths(params, context)
.and_then(|paths| paths.into_iter().next().ok_or(()))
.map_err(|_| Bolt12SemanticError::MissingPaths)?;

builder = builder.path(path);
};

let _persistence_guard = PersistenceNotifierGuard::notify_on_drop($self);

Expand Down Expand Up @@ -9273,29 +9285,7 @@ where
inbound_payment::get_payment_preimage(payment_hash, payment_secret, &self.inbound_payment_key)
}

/// Creates a collection of blinded paths by delegating to [`MessageRouter`] based on
/// the path's intended lifetime.
///
/// Whether or not the path is compact depends on whether the path is short-lived or long-lived,
/// respectively, based on the given `absolute_expiry` as seconds since the Unix epoch. See
/// [`MAX_SHORT_LIVED_RELATIVE_EXPIRY`].
fn create_blinded_paths_using_absolute_expiry(
&self, context: OffersContext, absolute_expiry: Option<Duration>,
) -> Result<Vec<BlindedMessagePath>, ()> {
let now = self.duration_since_epoch();
let max_short_lived_absolute_expiry = now.saturating_add(MAX_SHORT_LIVED_RELATIVE_EXPIRY);

if absolute_expiry.unwrap_or(Duration::MAX) <= max_short_lived_absolute_expiry {
self.create_compact_blinded_paths(context)
} else {
let params = BlindedPathParams {
paths: PATHS_PLACEHOLDER,
is_compact: false
};
self.create_blinded_paths(params, context)
}
}

#[cfg(test)]
pub(super) fn duration_since_epoch(&self) -> Duration {
#[cfg(not(feature = "std"))]
let now = Duration::from_secs(
Expand Down Expand Up @@ -9334,6 +9324,7 @@ where
/// [`MessageRouter::create_compact_blinded_paths`].
///
/// Errors if the `MessageRouter` errors.
#[allow(unused)]
fn create_compact_blinded_paths(&self, context: OffersContext) -> Result<Vec<BlindedMessagePath>, ()> {
let recipient = self.get_our_node_id();
let secp_ctx = &self.secp_ctx;
Expand Down
7 changes: 6 additions & 1 deletion lightning/src/ln/max_payment_path_len_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::ln::onion_utils;
use crate::ln::onion_utils::MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY;
use crate::ln::outbound_payment::{RecipientOnionFields, Retry, RetryableSendFailure};
use crate::offers::invoice::BlindedPayInfo;
use crate::onion_message::messenger::{BlindedPathParams, PATHS_PLACEHOLDER};
use crate::prelude::*;
use crate::routing::router::{DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, PaymentParameters, RouteParameters};
use crate::util::errors::APIError;
Expand Down Expand Up @@ -378,7 +379,11 @@ fn bolt12_invoice_too_large_blinded_paths() {
)
)]);

let offer = nodes[1].node.create_offer_builder(None).unwrap().build().unwrap();
let params = BlindedPathParams {
paths: PATHS_PLACEHOLDER,
is_compact: false,
};
let offer = nodes[1].node.create_offer_builder(Some(params)).unwrap().build().unwrap();
let payment_id = PaymentId([1; 32]);
nodes[0].node.pay_for_offer(&offer, None, Some(5000), None, payment_id, Retry::Attempts(0), None).unwrap();
let invreq_om = nodes[0].onion_messenger.next_onion_message_for_peer(nodes[1].node.get_our_node_id()).unwrap();
Expand Down
Loading

0 comments on commit c078155

Please sign in to comment.