Skip to content

Commit f839b92

Browse files
committed
ln: surface LocalHTLCFailureReason in queue_add_htlc
1 parent 5179d8a commit f839b92

File tree

3 files changed

+62
-41
lines changed

3 files changed

+62
-41
lines changed

lightning/src/ln/channel.rs

+23-31
Original file line numberDiff line numberDiff line change
@@ -6142,22 +6142,14 @@ impl<SP: Deref> FundedChannel<SP> where
61426142
);
61436143
update_add_count += 1;
61446144
},
6145-
Err(e) => {
6146-
match e {
6147-
ChannelError::Ignore(ref msg) => {
6148-
log_info!(logger, "Failed to send HTLC with payment_hash {} due to {} in channel {}", &payment_hash, msg, &self.context.channel_id());
6149-
// If we fail to send here, then this HTLC should
6150-
// be failed backwards. Failing to send here
6151-
// indicates that this HTLC may keep being put back
6152-
// into the holding cell without ever being
6153-
// successfully forwarded/failed/fulfilled, causing
6154-
// our counterparty to eventually close on us.
6155-
htlcs_to_fail.push((source.clone(), *payment_hash));
6156-
},
6157-
_ => {
6158-
panic!("Got a non-IgnoreError action trying to send holding cell HTLC");
6159-
},
6160-
}
6145+
Err((_, msg)) => {
6146+
log_info!(logger, "Failed to send HTLC with payment_hash {} due to {} in channel {}", &payment_hash, msg, &self.context.channel_id());
6147+
// If we fail to send here, then this HTLC should be failed
6148+
// backwards. Failing to send here indicates that this HTLC may
6149+
// keep being put back into the holding cell without ever being
6150+
// successfully forwarded/failed/fulfilled, causing our
6151+
// counterparty to eventually close on us.
6152+
htlcs_to_fail.push((source.clone(), *payment_hash));
61616153
}
61626154
}
61636155
None
@@ -8819,22 +8811,19 @@ impl<SP: Deref> FundedChannel<SP> where
88198811
/// Queues up an outbound HTLC to send by placing it in the holding cell. You should call
88208812
/// [`Self::maybe_free_holding_cell_htlcs`] in order to actually generate and send the
88218813
/// commitment update.
8822-
///
8823-
/// `Err`s will only be [`ChannelError::Ignore`].
88248814
pub fn queue_add_htlc<F: Deref, L: Deref>(
88258815
&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource,
88268816
onion_routing_packet: msgs::OnionPacket, skimmed_fee_msat: Option<u64>,
88278817
blinding_point: Option<PublicKey>, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
8828-
) -> Result<(), ChannelError>
8818+
) -> Result<(), (LocalHTLCFailureReason, String)>
88298819
where F::Target: FeeEstimator, L::Target: Logger
88308820
{
88318821
self
88328822
.send_htlc(amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, true,
88338823
skimmed_fee_msat, blinding_point, fee_estimator, logger)
88348824
.map(|msg_opt| assert!(msg_opt.is_none(), "We forced holding cell?"))
88358825
.map_err(|err| {
8836-
if let ChannelError::Ignore(_) = err { /* fine */ }
8837-
else { debug_assert!(false, "Queueing cannot trigger channel failure"); }
8826+
debug_assert!(err.0.is_temporary(), "Queuing HTLC should return temporary error");
88388827
err
88398828
})
88408829
}
@@ -8854,38 +8843,40 @@ impl<SP: Deref> FundedChannel<SP> where
88548843
/// You MUST call [`Self::send_commitment_no_state_update`] prior to calling any other methods
88558844
/// on this [`FundedChannel`] if `force_holding_cell` is false.
88568845
///
8857-
/// `Err`s will only be [`ChannelError::Ignore`].
8846+
/// `Err`'s will always be temporary channel failures.
88588847
fn send_htlc<F: Deref, L: Deref>(
88598848
&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource,
88608849
onion_routing_packet: msgs::OnionPacket, mut force_holding_cell: bool,
88618850
skimmed_fee_msat: Option<u64>, blinding_point: Option<PublicKey>,
88628851
fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
8863-
) -> Result<Option<msgs::UpdateAddHTLC>, ChannelError>
8852+
) -> Result<Option<msgs::UpdateAddHTLC>, (LocalHTLCFailureReason, String)>
88648853
where F::Target: FeeEstimator, L::Target: Logger
88658854
{
88668855
if !matches!(self.context.channel_state, ChannelState::ChannelReady(_)) ||
88678856
self.context.channel_state.is_local_shutdown_sent() ||
88688857
self.context.channel_state.is_remote_shutdown_sent()
88698858
{
8870-
return Err(ChannelError::Ignore("Cannot send HTLC until channel is fully established and we haven't started shutting down".to_owned()));
8859+
return Err((LocalHTLCFailureReason::ChannelNotReady,
8860+
"Cannot send HTLC until channel is fully established and we haven't started shutting down".to_owned()));
88718861
}
88728862
let channel_total_msat = self.funding.get_value_satoshis() * 1000;
88738863
if amount_msat > channel_total_msat {
8874-
return Err(ChannelError::Ignore(format!("Cannot send amount {}, because it is more than the total value of the channel {}", amount_msat, channel_total_msat)));
8864+
return Err((LocalHTLCFailureReason::AmountExceedsCapacity,
8865+
format!("Cannot send amount {}, because it is more than the total value of the channel {}", amount_msat, channel_total_msat)));
88758866
}
88768867

88778868
if amount_msat == 0 {
8878-
return Err(ChannelError::Ignore("Cannot send 0-msat HTLC".to_owned()));
8869+
return Err((LocalHTLCFailureReason::ZeroAmount, "Cannot send 0-msat HTLC".to_owned()));
88798870
}
88808871

88818872
let available_balances = self.get_available_balances(fee_estimator);
88828873
if amount_msat < available_balances.next_outbound_htlc_minimum_msat {
8883-
return Err(ChannelError::Ignore(format!("Cannot send less than our next-HTLC minimum - {} msat",
8874+
return Err((LocalHTLCFailureReason::HTLCMinimum, format!("Cannot send less than our next-HTLC minimum - {} msat",
88848875
available_balances.next_outbound_htlc_minimum_msat)));
88858876
}
88868877

88878878
if amount_msat > available_balances.next_outbound_htlc_limit_msat {
8888-
return Err(ChannelError::Ignore(format!("Cannot send more than our next-HTLC maximum - {} msat",
8879+
return Err((LocalHTLCFailureReason::HTLCMaximum, format!("Cannot send more than our next-HTLC maximum - {} msat",
88898880
available_balances.next_outbound_htlc_limit_msat)));
88908881
}
88918882

@@ -8896,7 +8887,8 @@ impl<SP: Deref> FundedChannel<SP> where
88968887
// disconnected during the time the previous hop was doing the commitment dance we may
88978888
// end up getting here after the forwarding delay. In any case, returning an
88988889
// IgnoreError will get ChannelManager to do the right thing and fail backwards now.
8899-
return Err(ChannelError::Ignore("Cannot send an HTLC while disconnected from channel counterparty".to_owned()));
8890+
return Err((LocalHTLCFailureReason::PeerOffline,
8891+
"Cannot send an HTLC while disconnected from channel counterparty".to_owned()));
89008892
}
89018893

89028894
let need_holding_cell = !self.context.channel_state.can_generate_new_commitment();
@@ -9185,8 +9177,8 @@ impl<SP: Deref> FundedChannel<SP> where
91859177
{
91869178
let send_res = self.send_htlc(amount_msat, payment_hash, cltv_expiry, source,
91879179
onion_routing_packet, false, skimmed_fee_msat, None, fee_estimator, logger);
9188-
if let Err(e) = &send_res { if let ChannelError::Ignore(_) = e {} else { debug_assert!(false, "Sending cannot trigger channel failure"); } }
9189-
match send_res? {
9180+
// All [`LocalHTLCFailureReason`] errors are temporary, so they are [`ChannelError::Ignore`].
9181+
match send_res.map_err(|(_, msg)| ChannelError::Ignore(msg))? {
91909182
Some(_) => {
91919183
let monitor_update = self.build_commitment_no_status_check(logger);
91929184
self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());

lightning/src/ln/channelmanager.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -6102,22 +6102,17 @@ where
61026102
};
61036103
log_trace!(logger, "Forwarding HTLC from SCID {} with payment_hash {} and next hop SCID {} over {} channel {} with corresponding peer {}",
61046104
prev_short_channel_id, &payment_hash, short_chan_id, channel_description, optimal_channel.context.channel_id(), &counterparty_node_id);
6105-
if let Err(e) = optimal_channel.queue_add_htlc(outgoing_amt_msat,
6105+
if let Err((reason, msg)) = optimal_channel.queue_add_htlc(outgoing_amt_msat,
61066106
payment_hash, outgoing_cltv_value, htlc_source.clone(),
61076107
onion_packet.clone(), skimmed_fee_msat, next_blinding_point, &self.fee_estimator,
61086108
&&logger)
61096109
{
6110-
if let ChannelError::Ignore(msg) = e {
6111-
log_trace!(logger, "Failed to forward HTLC with payment_hash {} to peer {}: {}", &payment_hash, &counterparty_node_id, msg);
6112-
} else {
6113-
panic!("Stated return value requirements in send_htlc() were not met");
6114-
}
6110+
log_trace!(logger, "Failed to forward HTLC with payment_hash {} to peer {}: {}", &payment_hash, &counterparty_node_id, msg);
61156111

61166112
if let Some(chan) = peer_state.channel_by_id
61176113
.get_mut(&forward_chan_id)
61186114
.and_then(Channel::as_funded_mut)
61196115
{
6120-
let reason = LocalHTLCFailureReason::TemporaryChannelFailure;
61216116
let data = self.get_htlc_inbound_temp_fail_data(reason);
61226117
failed_forwards.push((htlc_source, payment_hash,
61236118
HTLCFailReason::reason(reason, data),

lightning/src/ln/onion_utils.rs

+37-3
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,25 @@ pub enum LocalHTLCFailureReason {
15871587
/// The HTLC was failed back because its expiry height was reached and funds were timed out
15881588
/// on chain.
15891589
OnChainTimeout,
1590+
/// The HTLC was failed because its amount is greater than the capacity of the channel.
1591+
AmountExceedsCapacity,
1592+
/// The HTLC was failed because zero amount HTLCs are not allowed.
1593+
ZeroAmount,
1594+
/// The HTLC was failed because its amount is less than the smallest HTLC that the channel
1595+
/// can currently accept.
1596+
///
1597+
/// This may occur because the HTLC is smaller than the counterparty's advertised minimum
1598+
/// accepted HTLC size, or if we have reached our maximum total dust HTLC exposure.
1599+
HTLCMinimum,
1600+
/// The HTLC was failed because its amount is more than then largest HTLC that the channel
1601+
/// can currently accept.
1602+
///
1603+
/// This may occur because the outbound channel has insufficient liquidity to forward the HTLC,
1604+
/// we have reached the counterparty's in-flight limits, or the HTLC exceeds our advertised
1605+
/// maximum accepted HTLC size.
1606+
HTLCMaximum,
1607+
/// The HTLC was failed because our remote peer is offline.
1608+
PeerOffline,
15901609
}
15911610

15921611
impl LocalHTLCFailureReason {
@@ -1602,8 +1621,13 @@ impl LocalHTLCFailureReason {
16021621
| Self::DustLimitHolder
16031622
| Self::DustLimitCounterparty
16041623
| Self::FeeSpikeBuffer
1605-
| Self::ChannelNotReady => UPDATE | 7,
1606-
Self::PermanentChannelFailure | Self::OnChainTimeout | Self::ChannelClosed => PERM | 8,
1624+
| Self::ChannelNotReady
1625+
| Self::AmountExceedsCapacity
1626+
| Self::ZeroAmount
1627+
| Self::HTLCMinimum
1628+
| Self::HTLCMaximum
1629+
| Self::PeerOffline => UPDATE | 7,
1630+
Self::PermanentChannelFailure | Self::ChannelClosed | Self::OnChainTimeout => PERM | 8,
16071631
Self::RequiredChannelFeature => PERM | 9,
16081632
Self::UnknownNextPeer
16091633
| Self::PrivateChannelForward
@@ -1730,6 +1754,11 @@ impl_writeable_tlv_based_enum!(LocalHTLCFailureReason,
17301754
(71, OutgoingCLTVTooSoon) => {},
17311755
(73, ChannelClosed) => {},
17321756
(75, OnChainTimeout) => {},
1757+
(77, AmountExceedsCapacity) => {},
1758+
(79, ZeroAmount) => {},
1759+
(81, HTLCMinimum) => {},
1760+
(83, HTLCMaximum) => {},
1761+
(85, PeerOffline) => {},
17331762
);
17341763

17351764
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
@@ -1830,7 +1859,12 @@ impl HTLCFailReason {
18301859
| LocalHTLCFailureReason::DustLimitHolder
18311860
| LocalHTLCFailureReason::DustLimitCounterparty
18321861
| LocalHTLCFailureReason::FeeSpikeBuffer
1833-
| LocalHTLCFailureReason::ChannelNotReady => {
1862+
| LocalHTLCFailureReason::ChannelNotReady
1863+
| LocalHTLCFailureReason::AmountExceedsCapacity
1864+
| LocalHTLCFailureReason::ZeroAmount
1865+
| LocalHTLCFailureReason::HTLCMinimum
1866+
| LocalHTLCFailureReason::HTLCMaximum
1867+
| LocalHTLCFailureReason::PeerOffline => {
18341868
debug_assert_eq!(
18351869
data.len() - 2,
18361870
u16::from_be_bytes(data[0..2].try_into().unwrap()) as usize

0 commit comments

Comments
 (0)