Skip to content

Commit 46b387a

Browse files
committed
Update create_refund_builder to use create_blinded_paths
Aligned the `create_refund_builder` logic with `create_offer_builder` by updating it to use `create_blinded_paths`. Now that custom routers can be passed via `create_refund_builder_using_router`, the default builder can consistently generate full-length blinded paths without branching logic. This simplifies the default behavior while keeping flexibility available for custom use cases.
1 parent 2d2f3a2 commit 46b387a

File tree

2 files changed

+101
-94
lines changed

2 files changed

+101
-94
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 99 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -8095,7 +8095,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80958095

80968096
let per_peer_state = self.per_peer_state.read().unwrap();
80978097
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
8098-
.ok_or_else(|| {
8098+
.ok_or_else(|| {
80998099
debug_assert!(false);
81008100
MsgHandleErrInternal::send_err_msg_no_close(
81018101
format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id),
@@ -10172,6 +10172,28 @@ impl Default for Bolt11InvoiceParameters {
1017210172
}
1017310173

1017410174
macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
10175+
fn create_offer_builder_intern<PF>(&$self, make_path: PF) -> Result<$builder, Bolt12SemanticError>
10176+
where
10177+
PF: FnOnce(PublicKey, MessageContext, &secp256k1::Secp256k1<secp256k1::All>) -> Result<Option<BlindedMessagePath>, Bolt12SemanticError>,
10178+
{
10179+
let node_id = $self.get_our_node_id();
10180+
let expanded_key = &$self.inbound_payment_key;
10181+
let entropy = &*$self.entropy_source;
10182+
let secp_ctx = &$self.secp_ctx;
10183+
10184+
let nonce = Nonce::from_entropy_source(entropy);
10185+
let context = MessageContext::Offers(OffersContext::InvoiceRequest { nonce });
10186+
10187+
let mut builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
10188+
.chain_hash($self.chain_hash);
10189+
10190+
if let Some(path) = make_path(node_id, context, secp_ctx)? {
10191+
builder = builder.path(path)
10192+
}
10193+
10194+
Ok(builder.into())
10195+
}
10196+
1017510197
/// Creates an [`OfferBuilder`] such that the [`Offer`] it builds is recognized by the
1017610198
/// [`ChannelManager`] when handling [`InvoiceRequest`] messages for the offer. The offer's
1017710199
/// expiration will be `absolute_expiry` if `Some`, otherwise it will not expire.
@@ -10196,21 +10218,12 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
1019610218
/// [`Offer`]: crate::offers::offer::Offer
1019710219
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
1019810220
pub fn create_offer_builder(&$self) -> Result<$builder, Bolt12SemanticError> {
10199-
let node_id = $self.get_our_node_id();
10200-
let expanded_key = &$self.inbound_payment_key;
10201-
let entropy = &*$self.entropy_source;
10202-
let secp_ctx = &$self.secp_ctx;
10203-
10204-
let nonce = Nonce::from_entropy_source(entropy);
10205-
let context = MessageContext::Offers(OffersContext::InvoiceRequest { nonce });
10206-
let path = $self.create_blinded_paths(context)
10207-
.and_then(|paths| paths.into_iter().next().ok_or(()))
10208-
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
10209-
let builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
10210-
.chain_hash($self.chain_hash)
10211-
.path(path);
10212-
10213-
Ok(builder.into())
10221+
$self.create_offer_builder_intern(|_, context, _| {
10222+
$self.create_blinded_paths(context)
10223+
.and_then(|paths| paths.into_iter().next().ok_or(()))
10224+
.map(Some)
10225+
.map_err(|_| Bolt12SemanticError::MissingPaths)
10226+
})
1021410227
}
1021510228

1021610229
/// Creates an [`OfferBuilder`] such that the [`Offer`] it builds is recognized by the
@@ -10239,32 +10252,63 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
1023910252
&$self,
1024010253
router: ME,
1024110254
) -> Result<$builder, Bolt12SemanticError> {
10255+
$self.create_offer_builder_intern(|node_id, context, secp_ctx| {
10256+
let peers = $self.get_peers_for_blinded_path();
10257+
router.create_blinded_paths(node_id, context, peers, secp_ctx)
10258+
.map(|paths| paths.into_iter().next())
10259+
.map_err(|_| Bolt12SemanticError::MissingPaths)
10260+
})
10261+
}
10262+
} }
10263+
10264+
macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
10265+
fn create_refund_builder_intern<PF>(
10266+
&$self,
10267+
make_path: PF,
10268+
amount_msats: u64,
10269+
absolute_expiry: Duration,
10270+
payment_id: PaymentId,
10271+
retry_strategy: Retry,
10272+
route_params_config: RouteParametersConfig
10273+
) -> Result<$builder, Bolt12SemanticError>
10274+
where
10275+
PF: FnOnce(PublicKey, MessageContext, &secp256k1::Secp256k1<secp256k1::All>) -> Result<Option<BlindedMessagePath>, Bolt12SemanticError>,
10276+
{
1024210277
let node_id = $self.get_our_node_id();
1024310278
let expanded_key = &$self.inbound_payment_key;
1024410279
let entropy = &*$self.entropy_source;
1024510280
let secp_ctx = &$self.secp_ctx;
1024610281

1024710282
let nonce = Nonce::from_entropy_source(entropy);
10248-
let context = MessageContext::Offers(OffersContext::InvoiceRequest { nonce });
10283+
let context = MessageContext::Offers(
10284+
OffersContext::OutboundPayment { payment_id, nonce, hmac: None }
10285+
);
1024910286

10250-
let peers = $self.get_peers_for_blinded_path();
10287+
// Create the base builder with common properties
10288+
let mut builder = RefundBuilder::deriving_signing_pubkey(
10289+
node_id, expanded_key, nonce, secp_ctx, amount_msats, payment_id
10290+
)?
10291+
.chain_hash($self.chain_hash)
10292+
.absolute_expiry(absolute_expiry);
1025110293

10252-
let path = router.create_blinded_paths(node_id, context, peers, secp_ctx)
10253-
.map_err(|_| Bolt12SemanticError::MissingPaths)?
10254-
.into_iter().next();
10294+
// Add path if one is provided by the path creator
10295+
if let Some(path) = make_path(node_id, context, secp_ctx)? {
10296+
builder = builder.path(path);
10297+
}
1025510298

10256-
let mut builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
10257-
.chain_hash($self.chain_hash);
10299+
// Handle persistence and payment tracking
10300+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop($self);
1025810301

10259-
if let Some(path) = path {
10260-
builder = builder.path(path)
10261-
}
10302+
let expiration = StaleExpiration::AbsoluteTimeout(absolute_expiry);
10303+
$self.pending_outbound_payments
10304+
.add_new_awaiting_invoice(
10305+
payment_id, expiration, retry_strategy, route_params_config, None,
10306+
)
10307+
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
1026210308

1026310309
Ok(builder.into())
1026410310
}
10265-
} }
1026610311

10267-
macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
1026810312
/// Creates a [`RefundBuilder`] such that the [`Refund`] it builds is recognized by the
1026910313
/// [`ChannelManager`] when handling [`Bolt12Invoice`] messages for the refund.
1027010314
///
@@ -10287,8 +10331,7 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
1028710331
///
1028810332
/// # Privacy
1028910333
///
10290-
/// Uses [`MessageRouter`] to construct a [`BlindedMessagePath`] for the refund based on the given
10291-
/// `absolute_expiry` according to [`MAX_SHORT_LIVED_RELATIVE_EXPIRY`]. See those docs for
10334+
/// Uses [`MessageRouter`] to construct a [`BlindedMessagePath`] for the refund. See those docs for
1029210335
/// privacy implications as well as those of the parameterized [`Router`], which implements
1029310336
/// [`MessageRouter`].
1029410337
///
@@ -10314,34 +10357,19 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
1031410357
&$self, amount_msats: u64, absolute_expiry: Duration, payment_id: PaymentId,
1031510358
retry_strategy: Retry, route_params_config: RouteParametersConfig
1031610359
) -> Result<$builder, Bolt12SemanticError> {
10317-
let node_id = $self.get_our_node_id();
10318-
let expanded_key = &$self.inbound_payment_key;
10319-
let entropy = &*$self.entropy_source;
10320-
let secp_ctx = &$self.secp_ctx;
10321-
10322-
let nonce = Nonce::from_entropy_source(entropy);
10323-
let context = OffersContext::OutboundPayment { payment_id, nonce, hmac: None };
10324-
let path = $self.create_blinded_paths_using_absolute_expiry(context, Some(absolute_expiry))
10325-
.and_then(|paths| paths.into_iter().next().ok_or(()))
10326-
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
10327-
10328-
let builder = RefundBuilder::deriving_signing_pubkey(
10329-
node_id, expanded_key, nonce, secp_ctx, amount_msats, payment_id
10330-
)?
10331-
.chain_hash($self.chain_hash)
10332-
.absolute_expiry(absolute_expiry)
10333-
.path(path);
10334-
10335-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop($self);
10336-
10337-
let expiration = StaleExpiration::AbsoluteTimeout(absolute_expiry);
10338-
$self.pending_outbound_payments
10339-
.add_new_awaiting_invoice(
10340-
payment_id, expiration, retry_strategy, route_params_config, None,
10341-
)
10342-
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
10343-
10344-
Ok(builder.into())
10360+
$self.create_refund_builder_intern(
10361+
|_, context, _| {
10362+
$self.create_blinded_paths(context)
10363+
.and_then(|paths| paths.into_iter().next().ok_or(()))
10364+
.map(Some)
10365+
.map_err(|_| Bolt12SemanticError::MissingPaths)
10366+
},
10367+
amount_msats,
10368+
absolute_expiry,
10369+
payment_id,
10370+
retry_strategy,
10371+
route_params_config
10372+
)
1034510373
}
1034610374

1034710375
/// Creates a [`RefundBuilder`] such that the [`Refund`] it builds is recognized by the
@@ -10392,41 +10420,19 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
1039210420
&$self, router: ME, amount_msats: u64, absolute_expiry: Duration, payment_id: PaymentId,
1039310421
retry_strategy: Retry, route_params_config: RouteParametersConfig
1039410422
) -> Result<$builder, Bolt12SemanticError> {
10395-
let node_id = $self.get_our_node_id();
10396-
let expanded_key = &$self.inbound_payment_key;
10397-
let entropy = &*$self.entropy_source;
10398-
let secp_ctx = &$self.secp_ctx;
10399-
10400-
let nonce = Nonce::from_entropy_source(entropy);
10401-
let context = MessageContext::Offers(
10402-
OffersContext::OutboundPayment { payment_id, nonce, hmac: None }
10403-
);
10404-
10405-
let peers = $self.get_peers_for_blinded_path();
10406-
let path = router.create_blinded_paths(node_id, context, peers, secp_ctx)
10407-
.map_err(|_| Bolt12SemanticError::MissingPaths)?
10408-
.into_iter().next();
10409-
10410-
let mut builder = RefundBuilder::deriving_signing_pubkey(
10411-
node_id, expanded_key, nonce, secp_ctx, amount_msats, payment_id
10412-
)?
10413-
.chain_hash($self.chain_hash)
10414-
.absolute_expiry(absolute_expiry);
10415-
10416-
if let Some(path) = path {
10417-
builder = builder.path(path)
10418-
}
10419-
10420-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop($self);
10421-
10422-
let expiration = StaleExpiration::AbsoluteTimeout(absolute_expiry);
10423-
$self.pending_outbound_payments
10424-
.add_new_awaiting_invoice(
10425-
payment_id, expiration, retry_strategy, route_params_config, None,
10426-
)
10427-
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
10428-
10429-
Ok(builder.into())
10423+
$self.create_refund_builder_intern(
10424+
|node_id, context, secp_ctx| {
10425+
let peers = $self.get_peers_for_blinded_path();
10426+
router.create_blinded_paths(node_id, context, peers, secp_ctx)
10427+
.map(|paths| paths.into_iter().next())
10428+
.map_err(|_| Bolt12SemanticError::MissingPaths)
10429+
},
10430+
amount_msats,
10431+
absolute_expiry,
10432+
payment_id,
10433+
retry_strategy,
10434+
route_params_config
10435+
)
1043010436
}
1043110437
} }
1043210438

lightning/src/ln/offers_tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,11 @@ fn creates_short_lived_refund() {
454454
let bob = &nodes[1];
455455
let bob_id = bob.node.get_our_node_id();
456456

457+
let router = CompactMessageRouter::new(alice.network_graph, alice.node.entropy_source);
457458
let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
458459
let payment_id = PaymentId([1; 32]);
459460
let refund = bob.node
460-
.create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), RouteParametersConfig::default())
461+
.create_refund_builder_using_router(router, 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), RouteParametersConfig::default())
461462
.unwrap()
462463
.build().unwrap();
463464
assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));

0 commit comments

Comments
 (0)