Skip to content

Commit ee817d5

Browse files
committed
lightning: introduce LocalHTLCFailureReason to expand B04 error codes
Introduce LocalHTLCFailureReason enum which provides additional information about local HTLC failures that are otherwise erased by BOLT04 error codes. The HTLCFailureDetails enum is introduced for two reasons: - To prompt code to consider whether a LocalHTLCFailureReason is needed - To avoid needing to enumerate every BOLT04 error code, when some aren't interesting to the end user
1 parent 4c43a5b commit ee817d5

File tree

4 files changed

+235
-97
lines changed

4 files changed

+235
-97
lines changed

lightning/src/ln/channel.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use crate::ln::chan_utils::{
5050
#[cfg(splicing)]
5151
use crate::ln::chan_utils::FUNDING_TRANSACTION_WITNESS_WEIGHT;
5252
use crate::ln::chan_utils;
53-
use crate::ln::onion_utils::HTLCFailReason;
53+
use crate::ln::onion_utils::{HTLCFailReason, LocalHTLCFailureReason, HTLCFailureDetails};
5454
use crate::chain::BestBlock;
5555
use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, LowerBoundedFeeEstimator, fee_for_weight};
5656
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS};
@@ -7588,20 +7588,20 @@ impl<SP: Deref> FundedChannel<SP> where
75887588

75897589
fn internal_htlc_satisfies_config(
75907590
&self, htlc: &msgs::UpdateAddHTLC, amt_to_forward: u64, outgoing_cltv_value: u32, config: &ChannelConfig,
7591-
) -> Result<(), (&'static str, u16)> {
7591+
) -> Result<(), (&'static str, LocalHTLCFailureReason)> {
75927592
let fee = amt_to_forward.checked_mul(config.forwarding_fee_proportional_millionths as u64)
75937593
.and_then(|prop_fee| (prop_fee / 1000000).checked_add(config.forwarding_fee_base_msat as u64));
75947594
if fee.is_none() || htlc.amount_msat < fee.unwrap() ||
75957595
(htlc.amount_msat - fee.unwrap()) < amt_to_forward {
75967596
return Err((
75977597
"Prior hop has deviated from specified fees parameters or origin node has obsolete ones",
7598-
0x1000 | 12, // fee_insufficient
7598+
LocalHTLCFailureReason::FeeInsufficient,
75997599
));
76007600
}
76017601
if (htlc.cltv_expiry as u64) < outgoing_cltv_value as u64 + config.cltv_expiry_delta as u64 {
76027602
return Err((
76037603
"Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta",
7604-
0x1000 | 13, // incorrect_cltv_expiry
7604+
LocalHTLCFailureReason::IncorrectCLTVDetla,
76057605
));
76067606
}
76077607
Ok(())
@@ -7612,7 +7612,7 @@ impl<SP: Deref> FundedChannel<SP> where
76127612
/// unsuccessful, falls back to the previous one if one exists.
76137613
pub fn htlc_satisfies_config(
76147614
&self, htlc: &msgs::UpdateAddHTLC, amt_to_forward: u64, outgoing_cltv_value: u32,
7615-
) -> Result<(), (&'static str, u16)> {
7615+
) -> Result<(), (&'static str, LocalHTLCFailureReason)> {
76167616
self.internal_htlc_satisfies_config(&htlc, amt_to_forward, outgoing_cltv_value, &self.context.config())
76177617
.or_else(|err| {
76187618
if let Some(prev_config) = self.context.prev_config() {
@@ -7627,13 +7627,16 @@ impl<SP: Deref> FundedChannel<SP> where
76277627
/// this function determines whether to fail the HTLC, or forward / claim it.
76287628
pub fn can_accept_incoming_htlc<F: Deref, L: Deref>(
76297629
&self, msg: &msgs::UpdateAddHTLC, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: L
7630-
) -> Result<(), (&'static str, u16)>
7630+
) -> Result<(), (&'static str, HTLCFailureDetails)>
76317631
where
76327632
F::Target: FeeEstimator,
76337633
L::Target: Logger
76347634
{
76357635
if self.context.channel_state.is_local_shutdown_sent() {
7636-
return Err(("Shutdown was already sent", 0x4000|8))
7636+
return Err((
7637+
"Shutdown was already sent",
7638+
LocalHTLCFailureReason::ShutdownSent.into(),
7639+
))
76377640
}
76387641

76397642
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
@@ -7644,7 +7647,10 @@ impl<SP: Deref> FundedChannel<SP> where
76447647
// Note that the total dust exposure includes both the dust HTLCs and the excess mining fees of the counterparty commitment transaction
76457648
log_info!(logger, "Cannot accept value that would put our total dust exposure at {} over the limit {} on counterparty commitment tx",
76467649
on_counterparty_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
7647-
return Err(("Exceeded our total dust exposure limit on counterparty commitment tx", 0x1000|7))
7650+
return Err((
7651+
"Exceeded our total dust exposure limit on counterparty commitment tx.",
7652+
LocalHTLCFailureReason::DustLimitReached { holder_commitment: false }.into(),
7653+
))
76487654
}
76497655
let htlc_success_dust_limit = if self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
76507656
0
@@ -7658,7 +7664,10 @@ impl<SP: Deref> FundedChannel<SP> where
76587664
if on_holder_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
76597665
log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx",
76607666
on_holder_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
7661-
return Err(("Exceeded our dust exposure limit on holder commitment tx", 0x1000|7))
7667+
return Err((
7668+
"Exceeded our total dust exposure limit on holder commitment tx.",
7669+
LocalHTLCFailureReason::DustLimitReached { holder_commitment: true }.into(),
7670+
))
76627671
}
76637672
}
76647673

@@ -7696,7 +7705,10 @@ impl<SP: Deref> FundedChannel<SP> where
76967705
}
76977706
if pending_remote_value_msat.saturating_sub(self.funding.holder_selected_channel_reserve_satoshis * 1000).saturating_sub(anchor_outputs_value_msat) < remote_fee_cost_incl_stuck_buffer_msat {
76987707
log_info!(logger, "Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", &self.context.channel_id());
7699-
return Err(("Fee spike buffer violation", 0x1000|7));
7708+
return Err((
7709+
"Fee spike buffer violation.",
7710+
LocalHTLCFailureReason::FeeSpikeBuffer.into(),
7711+
));
77007712
}
77017713
}
77027714

0 commit comments

Comments
 (0)