Skip to content

Commit ab542c7

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 28dc289 commit ab542c7

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
@@ -9076,7 +9076,7 @@ where
90769076
onion_routing_packet: (**onion_packet).clone(),
90779077
skimmed_fee_msat: htlc.skimmed_fee_msat,
90789078
blinding_point: htlc.blinding_point,
9079-
hold_htlc: None, // Will be set by the async sender when support is added
9079+
hold_htlc: htlc.source.hold_htlc_at_next_hop().then(|| ()),
90809080
});
90819081
}
90829082
}

lightning/src/ln/channelmanager.rs

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,13 @@ impl HTLCSource {
832832
_ => None,
833833
}
834834
}
835+
836+
pub(crate) fn hold_htlc_at_next_hop(&self) -> bool {
837+
match self {
838+
Self::OutboundRoute { hold_htlc, .. } => hold_htlc.is_some(),
839+
_ => false,
840+
}
841+
}
835842
}
836843

837844
/// This enum is used to specify which error data to send to peers when failing back an HTLC
@@ -4920,6 +4927,7 @@ where
49204927
invoice_request: None,
49214928
bolt12_invoice: None,
49224929
session_priv_bytes,
4930+
hold_htlc_at_next_hop: false,
49234931
})
49244932
}
49254933

@@ -4935,6 +4943,7 @@ where
49354943
invoice_request,
49364944
bolt12_invoice,
49374945
session_priv_bytes,
4946+
hold_htlc_at_next_hop,
49384947
} = args;
49394948
// The top-level caller should hold the total_consistency_lock read lock.
49404949
debug_assert!(self.total_consistency_lock.try_write().is_err());
@@ -5016,7 +5025,7 @@ where
50165025
first_hop_htlc_msat: htlc_msat,
50175026
payment_id,
50185027
bolt12_invoice: bolt12_invoice.cloned(),
5019-
hold_htlc: None,
5028+
hold_htlc: hold_htlc_at_next_hop.then(|| ()),
50205029
};
50215030
let send_res = chan.send_htlc_and_commit(
50225031
htlc_msat,
@@ -5402,19 +5411,35 @@ where
54025411
},
54035412
};
54045413

5405-
let enqueue_held_htlc_available_res = self.flow.enqueue_held_htlc_available(
5406-
invoice,
5407-
payment_id,
5408-
self.get_peers_for_blinded_path(),
5409-
);
5410-
if enqueue_held_htlc_available_res.is_err() {
5411-
self.abandon_payment_with_reason(
5414+
// If the call to `Self::hold_htlc_channels` succeeded, then we are a private node and can
5415+
// hold the HTLCs for this payment at our next-hop channel counterparty until the recipient
5416+
// comes online. This allows us to go offline after locking in the HTLCs.
5417+
if let Ok(channels) = hold_htlc_channels_res {
5418+
if let Err(e) =
5419+
self.send_payment_for_static_invoice_no_persist(payment_id, channels)
5420+
{
5421+
log_trace!(
5422+
self.logger,
5423+
"Failed to send held HTLC with payment id {}: {:?}",
5424+
payment_id,
5425+
e
5426+
);
5427+
}
5428+
} else {
5429+
let enqueue_held_htlc_available_res = self.flow.enqueue_held_htlc_available(
5430+
invoice,
54125431
payment_id,
5413-
PaymentFailureReason::BlindedPathCreationFailed,
5432+
self.get_peers_for_blinded_path(),
54145433
);
5415-
res = Err(Bolt12PaymentError::BlindedPathCreationFailed);
5416-
return NotifyOption::DoPersist;
5417-
};
5434+
if enqueue_held_htlc_available_res.is_err() {
5435+
self.abandon_payment_with_reason(
5436+
payment_id,
5437+
PaymentFailureReason::BlindedPathCreationFailed,
5438+
);
5439+
res = Err(Bolt12PaymentError::BlindedPathCreationFailed);
5440+
return NotifyOption::DoPersist;
5441+
};
5442+
}
54185443

54195444
NotifyOption::DoPersist
54205445
});
@@ -5459,26 +5484,15 @@ where
54595484
}
54605485
}
54615486

5487+
/// If we want the HTLCs for this payment to be held at the next-hop channel counterparty, use
5488+
/// [`Self::hold_htlc_channels`] and pass the resulting channels in here.
54625489
fn send_payment_for_static_invoice(
5463-
&self, payment_id: PaymentId,
5490+
&self, payment_id: PaymentId, first_hops: Vec<ChannelDetails>,
54645491
) -> Result<(), Bolt12PaymentError> {
5465-
let best_block_height = self.best_block.read().unwrap().height;
54665492
let mut res = Ok(());
54675493
PersistenceNotifierGuard::optionally_notify(self, || {
5468-
let outbound_pmts_res = self.pending_outbound_payments.send_payment_for_static_invoice(
5469-
payment_id,
5470-
&self.router,
5471-
self.list_usable_channels(),
5472-
|| self.compute_inflight_htlcs(),
5473-
&self.entropy_source,
5474-
&self.node_signer,
5475-
&self,
5476-
&self.secp_ctx,
5477-
best_block_height,
5478-
&self.logger,
5479-
&self.pending_events,
5480-
|args| self.send_payment_along_path(args),
5481-
);
5494+
let outbound_pmts_res =
5495+
self.send_payment_for_static_invoice_no_persist(payment_id, first_hops);
54825496
match outbound_pmts_res {
54835497
Err(Bolt12PaymentError::UnexpectedInvoice)
54845498
| Err(Bolt12PaymentError::DuplicateInvoice) => {
@@ -5494,6 +5508,27 @@ where
54945508
res
54955509
}
54965510

5511+
/// Useful if the caller is already triggering a persist of the `ChannelManager`.
5512+
fn send_payment_for_static_invoice_no_persist(
5513+
&self, payment_id: PaymentId, first_hops: Vec<ChannelDetails>,
5514+
) -> Result<(), Bolt12PaymentError> {
5515+
let best_block_height = self.best_block.read().unwrap().height;
5516+
self.pending_outbound_payments.send_payment_for_static_invoice(
5517+
payment_id,
5518+
&self.router,
5519+
first_hops,
5520+
|| self.compute_inflight_htlcs(),
5521+
&self.entropy_source,
5522+
&self.node_signer,
5523+
&self,
5524+
&self.secp_ctx,
5525+
best_block_height,
5526+
&self.logger,
5527+
&self.pending_events,
5528+
|args| self.send_payment_along_path(args),
5529+
)
5530+
}
5531+
54975532
/// If we are holding an HTLC on behalf of an often-offline sender, this method allows us to
54985533
/// create a path for the sender to use as the reply path when they send the recipient a
54995534
/// [`HeldHtlcAvailable`] onion message, so the recipient's [`ReleaseHeldHtlc`] response will be
@@ -14742,7 +14777,9 @@ where
1474214777
fn handle_release_held_htlc(&self, _message: ReleaseHeldHtlc, context: AsyncPaymentsContext) {
1474314778
match context {
1474414779
AsyncPaymentsContext::OutboundPayment { payment_id } => {
14745-
if let Err(e) = self.send_payment_for_static_invoice(payment_id) {
14780+
if let Err(e) =
14781+
self.send_payment_for_static_invoice(payment_id, self.list_usable_channels())
14782+
{
1474614783
log_trace!(
1474714784
self.logger,
1474814785
"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)