Skip to content

Commit 86ffea1

Browse files
committed
Remove extra sum of tx fee dust on the counterparty tx dust exposure
Previously, `get_pending_htlc_stats` did not account for the inbound htlc because `can_accept_incoming_htlc` was called before the htlc was irrevocably committed. But after commit d8d9dc7, `can_accept_incoming_htlc` is called only when the htlc is irrevocably committed, hence `get_pending_htlc_stats` does account for the inbound htlc. Nonetheless, in the case of a non-dust htlc, our calculation of the counterparty tx dust exposure still assumed that `get_pending_htlc_stats` did not account for the inbound htlc, causing us to add the dust exposure due to that inbound htlc twice. This commit removes this extra sum.
1 parent 6cf270d commit 86ffea1

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

lightning/src/ln/channel.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7336,6 +7336,8 @@ impl<SP: Deref> FundedChannel<SP> where
73367336
})
73377337
}
73387338

7339+
/// When this function is called, the HTLC is already irrevocably committed to the channel;
7340+
/// this function determines whether to fail the HTLC, or forward / claim it.
73397341
pub fn can_accept_incoming_htlc<F: Deref, L: Deref>(
73407342
&self, msg: &msgs::UpdateAddHTLC, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: L
73417343
) -> Result<(), (&'static str, u16)>
@@ -7350,33 +7352,19 @@ impl<SP: Deref> FundedChannel<SP> where
73507352
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
73517353
let htlc_stats = self.context.get_pending_htlc_stats(None, dust_exposure_limiting_feerate);
73527354
let max_dust_htlc_exposure_msat = self.context.get_max_dust_htlc_exposure_msat(dust_exposure_limiting_feerate);
7353-
let (htlc_timeout_dust_limit, htlc_success_dust_limit) = if self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
7354-
(0, 0)
7355+
let on_counterparty_tx_dust_htlc_exposure_msat = htlc_stats.on_counterparty_tx_dust_exposure_msat;
7356+
if on_counterparty_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
7357+
// Note that the total dust exposure includes both the dust HTLCs and the excess mining fees of the counterparty commitment transaction
7358+
log_info!(logger, "Cannot accept value that would put our total dust exposure at {} over the limit {} on counterparty commitment tx",
7359+
on_counterparty_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
7360+
return Err(("Exceeded our total dust exposure limit on counterparty commitment tx", 0x1000|7))
7361+
}
7362+
let htlc_success_dust_limit = if self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
7363+
0
73557364
} else {
73567365
let dust_buffer_feerate = self.context.get_dust_buffer_feerate(None) as u64;
7357-
(dust_buffer_feerate * htlc_timeout_tx_weight(self.context.get_channel_type()) / 1000,
7358-
dust_buffer_feerate * htlc_success_tx_weight(self.context.get_channel_type()) / 1000)
7366+
dust_buffer_feerate * htlc_success_tx_weight(self.context.get_channel_type()) / 1000
73597367
};
7360-
let exposure_dust_limit_timeout_sats = htlc_timeout_dust_limit + self.context.counterparty_dust_limit_satoshis;
7361-
if msg.amount_msat / 1000 < exposure_dust_limit_timeout_sats {
7362-
let on_counterparty_tx_dust_htlc_exposure_msat = htlc_stats.on_counterparty_tx_dust_exposure_msat;
7363-
if on_counterparty_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
7364-
log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx",
7365-
on_counterparty_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
7366-
return Err(("Exceeded our dust exposure limit on counterparty commitment tx", 0x1000|7))
7367-
}
7368-
} else {
7369-
let htlc_dust_exposure_msat =
7370-
per_outbound_htlc_counterparty_commit_tx_fee_msat(self.context.feerate_per_kw, &self.context.channel_type);
7371-
let counterparty_tx_dust_exposure =
7372-
htlc_stats.on_counterparty_tx_dust_exposure_msat.saturating_add(htlc_dust_exposure_msat);
7373-
if counterparty_tx_dust_exposure > max_dust_htlc_exposure_msat {
7374-
log_info!(logger, "Cannot accept value that would put our exposure to tx fee dust at {} over the limit {} on counterparty commitment tx",
7375-
counterparty_tx_dust_exposure, max_dust_htlc_exposure_msat);
7376-
return Err(("Exceeded our tx fee dust exposure limit on counterparty commitment tx", 0x1000|7))
7377-
}
7378-
}
7379-
73807368
let exposure_dust_limit_success_sats = htlc_success_dust_limit + self.context.holder_dust_limit_satoshis;
73817369
if msg.amount_msat / 1000 < exposure_dust_limit_success_sats {
73827370
let on_holder_tx_dust_htlc_exposure_msat = htlc_stats.on_holder_tx_dust_exposure_msat;

lightning/src/ln/functional_tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10378,13 +10378,13 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e
1037810378
} else { 0 };
1037910379
let initial_feerate = if apply_excess_fee { 253 * 2 } else { 253 };
1038010380
let expected_dust_buffer_feerate = initial_feerate + 2530;
10381-
let mut commitment_tx_cost = commit_tx_fee_msat(initial_feerate - 253, nondust_htlc_count_in_limit, &ChannelTypeFeatures::empty());
10382-
commitment_tx_cost +=
10381+
let mut commitment_tx_cost_msat = commit_tx_fee_msat(initial_feerate - 253, nondust_htlc_count_in_limit, &ChannelTypeFeatures::empty());
10382+
commitment_tx_cost_msat +=
1038310383
if on_holder_tx {
1038410384
htlc_success_tx_weight(&ChannelTypeFeatures::empty())
1038510385
} else {
1038610386
htlc_timeout_tx_weight(&ChannelTypeFeatures::empty())
10387-
} * (initial_feerate as u64 - 253) / 1000 * nondust_htlc_count_in_limit;
10387+
} * (initial_feerate as u64 - 253) * nondust_htlc_count_in_limit;
1038810388
{
1038910389
let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1039010390
*feerate_lock = initial_feerate;
@@ -10393,8 +10393,8 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e
1039310393
// Default test fee estimator rate is 253 sat/kw, so we set the multiplier to 5_000_000 / 253
1039410394
// to get roughly the same initial value as the default setting when this test was
1039510395
// originally written.
10396-
MaxDustHTLCExposure::FeeRateMultiplier((5_000_000 + commitment_tx_cost) / 253)
10397-
} else { MaxDustHTLCExposure::FixedLimitMsat(5_000_000 + commitment_tx_cost) };
10396+
MaxDustHTLCExposure::FeeRateMultiplier((5_000_000 + commitment_tx_cost_msat) / 253)
10397+
} else { MaxDustHTLCExposure::FixedLimitMsat(5_000_000 + commitment_tx_cost_msat) };
1039810398
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1039910399
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config), None]);
1040010400
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
@@ -10538,8 +10538,8 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e
1053810538
} else {
1053910539
// Outbound dust balance: 5200 sats
1054010540
nodes[0].logger.assert_log("lightning::ln::channel",
10541-
format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx",
10542-
dust_htlc_on_counterparty_tx_msat * dust_htlc_on_counterparty_tx + commitment_tx_cost + 4,
10541+
format!("Cannot accept value that would put our total dust exposure at {} over the limit {} on counterparty commitment tx",
10542+
dust_htlc_on_counterparty_tx_msat * dust_htlc_on_counterparty_tx + commitment_tx_cost_msat + 4,
1054310543
max_dust_htlc_exposure_msat), 1);
1054410544
}
1054510545
} else if exposure_breach_event == ExposureEvent::AtUpdateFeeOutbound {

0 commit comments

Comments
 (0)