Skip to content

Commit 0bebd49

Browse files
Set UpdateAddHTLC::hold_htlc for offline payees
As part of supporting sending payments as an often-offline sender, the sender needs to be able to set a flag in their update_add_htlc message indicating that the HTLC should be held until receipt of a release_held_htlc onion message from the often-offline payment recipient. The prior commits laid groundwork to finally set the flag here in this commit. See-also <lightning/bolts#989>
1 parent 16c262d commit 0bebd49

File tree

3 files changed

+79
-38
lines changed

3 files changed

+79
-38
lines changed

lightning/src/ln/channel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9066,7 +9066,7 @@ where
90669066
onion_routing_packet: (**onion_packet).clone(),
90679067
skimmed_fee_msat: htlc.skimmed_fee_msat,
90689068
blinding_point: htlc.blinding_point,
9069-
hold_htlc: None, // Will be set by the async sender when support is added
9069+
hold_htlc: htlc.source.hold_htlc_at_next_hop().then(|| ()),
90709070
});
90719071
}
90729072
}

lightning/src/ln/channelmanager.rs

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,13 @@ impl HTLCSource {
842842
_ => None,
843843
}
844844
}
845+
846+
pub(crate) fn hold_htlc_at_next_hop(&self) -> bool {
847+
match self {
848+
Self::OutboundRoute { hold_htlc, .. } => hold_htlc.is_some(),
849+
_ => false,
850+
}
851+
}
845852
}
846853

847854
/// This enum is used to specify which error data to send to peers when failing back an HTLC
@@ -4930,6 +4937,7 @@ where
49304937
invoice_request: None,
49314938
bolt12_invoice: None,
49324939
session_priv_bytes,
4940+
hold_htlc_at_next_hop: false,
49334941
})
49344942
}
49354943

@@ -4945,6 +4953,7 @@ where
49454953
invoice_request,
49464954
bolt12_invoice,
49474955
session_priv_bytes,
4956+
hold_htlc_at_next_hop,
49484957
} = args;
49494958
// The top-level caller should hold the total_consistency_lock read lock.
49504959
debug_assert!(self.total_consistency_lock.try_write().is_err());
@@ -5026,7 +5035,7 @@ where
50265035
first_hop_htlc_msat: htlc_msat,
50275036
payment_id,
50285037
bolt12_invoice: bolt12_invoice.cloned(),
5029-
hold_htlc: None,
5038+
hold_htlc: hold_htlc_at_next_hop.then(|| ()),
50305039
};
50315040
let send_res = chan.send_htlc_and_commit(
50325041
htlc_msat,
@@ -5412,19 +5421,35 @@ where
54125421
},
54135422
};
54145423

5415-
let enqueue_held_htlc_available_res = self.flow.enqueue_held_htlc_available(
5416-
invoice,
5417-
payment_id,
5418-
self.get_peers_for_blinded_path(),
5419-
);
5420-
if enqueue_held_htlc_available_res.is_err() {
5421-
self.abandon_payment_with_reason(
5424+
// If the call to `Self::hold_htlc_channels` succeeded, then we are a private node and can
5425+
// hold the HTLCs for this payment at our next-hop channel counterparty until the recipient
5426+
// comes online. This allows us to go offline after locking in the HTLCs.
5427+
if let Ok(channels) = hold_htlc_channels_res {
5428+
if let Err(e) =
5429+
self.send_payment_for_static_invoice_no_persist(payment_id, channels)
5430+
{
5431+
log_trace!(
5432+
self.logger,
5433+
"Failed to send held HTLC with payment id {}: {:?}",
5434+
payment_id,
5435+
e
5436+
);
5437+
}
5438+
} else {
5439+
let enqueue_held_htlc_available_res = self.flow.enqueue_held_htlc_available(
5440+
invoice,
54225441
payment_id,
5423-
PaymentFailureReason::BlindedPathCreationFailed,
5442+
self.get_peers_for_blinded_path(),
54245443
);
5425-
res = Err(Bolt12PaymentError::BlindedPathCreationFailed);
5426-
return NotifyOption::DoPersist;
5427-
};
5444+
if enqueue_held_htlc_available_res.is_err() {
5445+
self.abandon_payment_with_reason(
5446+
payment_id,
5447+
PaymentFailureReason::BlindedPathCreationFailed,
5448+
);
5449+
res = Err(Bolt12PaymentError::BlindedPathCreationFailed);
5450+
return NotifyOption::DoPersist;
5451+
};
5452+
}
54285453

54295454
NotifyOption::DoPersist
54305455
});
@@ -5469,26 +5494,15 @@ where
54695494
}
54705495
}
54715496

5497+
/// If we want the HTLCs for this payment to be held at the next-hop channel counterparty, use
5498+
/// [`Self::hold_htlc_channels`] and pass the resulting channels in here.
54725499
fn send_payment_for_static_invoice(
5473-
&self, payment_id: PaymentId,
5500+
&self, payment_id: PaymentId, first_hops: Vec<ChannelDetails>,
54745501
) -> Result<(), Bolt12PaymentError> {
5475-
let best_block_height = self.best_block.read().unwrap().height;
54765502
let mut res = Ok(());
54775503
PersistenceNotifierGuard::optionally_notify(self, || {
5478-
let outbound_pmts_res = self.pending_outbound_payments.send_payment_for_static_invoice(
5479-
payment_id,
5480-
&self.router,
5481-
self.list_usable_channels(),
5482-
|| self.compute_inflight_htlcs(),
5483-
&self.entropy_source,
5484-
&self.node_signer,
5485-
&self,
5486-
&self.secp_ctx,
5487-
best_block_height,
5488-
&self.logger,
5489-
&self.pending_events,
5490-
|args| self.send_payment_along_path(args),
5491-
);
5504+
let outbound_pmts_res =
5505+
self.send_payment_for_static_invoice_no_persist(payment_id, first_hops);
54925506
match outbound_pmts_res {
54935507
Err(Bolt12PaymentError::UnexpectedInvoice)
54945508
| Err(Bolt12PaymentError::DuplicateInvoice) => {
@@ -5504,6 +5518,27 @@ where
55045518
res
55055519
}
55065520

5521+
/// Useful if the caller is already triggering a persist of the `ChannelManager`.
5522+
fn send_payment_for_static_invoice_no_persist(
5523+
&self, payment_id: PaymentId, first_hops: Vec<ChannelDetails>,
5524+
) -> Result<(), Bolt12PaymentError> {
5525+
let best_block_height = self.best_block.read().unwrap().height;
5526+
self.pending_outbound_payments.send_payment_for_static_invoice(
5527+
payment_id,
5528+
&self.router,
5529+
first_hops,
5530+
|| self.compute_inflight_htlcs(),
5531+
&self.entropy_source,
5532+
&self.node_signer,
5533+
&self,
5534+
&self.secp_ctx,
5535+
best_block_height,
5536+
&self.logger,
5537+
&self.pending_events,
5538+
|args| self.send_payment_along_path(args),
5539+
)
5540+
}
5541+
55075542
/// If we are holding an HTLC on behalf of an often-offline sender, this method allows us to
55085543
/// create a path for the sender to use as the reply path when they send the recipient a
55095544
/// [`HeldHtlcAvailable`] onion message, so the recipient's [`ReleaseHeldHtlc`] response will be
@@ -14781,7 +14816,9 @@ where
1478114816
fn handle_release_held_htlc(&self, _message: ReleaseHeldHtlc, context: AsyncPaymentsContext) {
1478214817
match context {
1478314818
AsyncPaymentsContext::OutboundPayment { payment_id } => {
14784-
if let Err(e) = self.send_payment_for_static_invoice(payment_id) {
14819+
if let Err(e) =
14820+
self.send_payment_for_static_invoice(payment_id, self.list_usable_channels())
14821+
{
1478514822
log_trace!(
1478614823
self.logger,
1478714824
"Failed to release held HTLC with payment id {}: {:?}",

lightning/src/ln/outbound_payment.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ pub(super) struct SendAlongPathArgs<'a> {
832832
pub invoice_request: Option<&'a InvoiceRequest>,
833833
pub bolt12_invoice: Option<&'a PaidBolt12Invoice>,
834834
pub session_priv_bytes: [u8; 32],
835+
pub hold_htlc_at_next_hop: bool,
835836
}
836837

837838
pub(super) struct OutboundPayments {
@@ -1064,26 +1065,29 @@ impl OutboundPayments {
10641065

10651066
let payment_params = Some(route_params.payment_params.clone());
10661067
let mut outbounds = self.pending_outbound_payments.lock().unwrap();
1067-
let onion_session_privs = match outbounds.entry(payment_id) {
1068+
let (onion_session_privs, hold_htlcs_at_next_hop) = match outbounds.entry(payment_id) {
10681069
hash_map::Entry::Occupied(entry) => match entry.get() {
10691070
PendingOutboundPayment::InvoiceReceived { .. } => {
10701071
let (retryable_payment, onion_session_privs) = Self::create_pending_payment(
10711072
payment_hash, recipient_onion.clone(), keysend_preimage, None, Some(bolt12_invoice.clone()), &route,
10721073
Some(retry_strategy), payment_params, entropy_source, best_block_height,
10731074
);
10741075
*entry.into_mut() = retryable_payment;
1075-
onion_session_privs
1076+
(onion_session_privs, false)
10761077
},
10771078
PendingOutboundPayment::StaticInvoiceReceived { .. } => {
1078-
let invreq = if let PendingOutboundPayment::StaticInvoiceReceived { invoice_request, .. } = entry.remove() {
1079-
invoice_request
1080-
} else { unreachable!() };
1079+
let (invreq, hold_htlcs_at_next_hop) =
1080+
if let PendingOutboundPayment::StaticInvoiceReceived {
1081+
invoice_request, hold_htlcs_at_next_hop, ..
1082+
} = entry.remove() {
1083+
(invoice_request, hold_htlcs_at_next_hop)
1084+
} else { unreachable!() };
10811085
let (retryable_payment, onion_session_privs) = Self::create_pending_payment(
10821086
payment_hash, recipient_onion.clone(), keysend_preimage, Some(invreq), Some(bolt12_invoice.clone()), &route,
10831087
Some(retry_strategy), payment_params, entropy_source, best_block_height
10841088
);
10851089
outbounds.insert(payment_id, retryable_payment);
1086-
onion_session_privs
1090+
(onion_session_privs, hold_htlcs_at_next_hop)
10871091
},
10881092
_ => return Err(Bolt12PaymentError::DuplicateInvoice),
10891093
},
@@ -1093,7 +1097,7 @@ impl OutboundPayments {
10931097

10941098
let result = self.pay_route_internal(
10951099
&route, payment_hash, &recipient_onion, keysend_preimage, invoice_request, Some(&bolt12_invoice), payment_id,
1096-
Some(route_params.final_value_msat), &onion_session_privs, false, node_signer,
1100+
Some(route_params.final_value_msat), &onion_session_privs, hold_htlcs_at_next_hop, node_signer,
10971101
best_block_height, &send_payment_along_path
10981102
);
10991103
log_info!(
@@ -2130,7 +2134,7 @@ impl OutboundPayments {
21302134
let path_res = send_payment_along_path(SendAlongPathArgs {
21312135
path: &path, payment_hash: &payment_hash, recipient_onion, total_value,
21322136
cur_height, payment_id, keysend_preimage: &keysend_preimage, invoice_request,
2133-
bolt12_invoice,
2137+
bolt12_invoice, hold_htlc_at_next_hop: hold_htlcs_at_next_hop,
21342138
session_priv_bytes: *session_priv_bytes
21352139
});
21362140
results.push(path_res);

0 commit comments

Comments
 (0)