Skip to content

Commit 5177311

Browse files
committed
f: Do not round HTLCStats::on_counterparty_tx_dust_exposure
All the other dust calculations in `get_pending_htlc_stats` are done with msat precision, so use msat precision when calculating the excess fees of the counterparty transaction.
1 parent b63b336 commit 5177311

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,23 @@ pub(crate) fn commitment_tx_base_weight(channel_type_features: &ChannelTypeFeatu
189189

190190
/// Get the fee cost of a commitment tx with a given number of HTLC outputs.
191191
/// Note that num_htlcs should not include dust HTLCs.
192+
/// If no rounding and msat precision is required, use `commit_tx_fee_msat`.
192193
pub(crate) fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, channel_type_features: &ChannelTypeFeatures) -> u64 {
193194
feerate_per_kw as u64 *
194195
(commitment_tx_base_weight(channel_type_features) +
195196
num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC)
196197
/ 1000
197198
}
198199

200+
/// Get the fee cost of a commitment tx with a given number of HTLC outputs.
201+
/// Note that num_htlcs should not include dust HTLCs.
202+
/// This version of the function does not round, and returns the number in msat precision. See also `commit_tx_fee_sat`.
203+
pub(crate) fn commit_tx_fee_msat(feerate_per_kw: u32, num_htlcs: usize, channel_type_features: &ChannelTypeFeatures) -> u64 {
204+
feerate_per_kw as u64 *
205+
(commitment_tx_base_weight(channel_type_features) +
206+
num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC)
207+
}
208+
199209
pub(crate) fn per_outbound_htlc_counterparty_commit_tx_fee_msat(feerate_per_kw: u32, channel_type_features: &ChannelTypeFeatures) -> u64 {
200210
// We avoid rounding here to make the returned value consistent with the slope (commit_tx_fee_sat/num_htlcs)
201211
let commitment_tx_fee_msat = COMMITMENT_TX_WEIGHT_PER_HTLC * feerate_per_kw as u64;

lightning/src/ln/channel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use crate::ln::chan_utils::{
4646
HolderCommitmentTransaction, ChannelTransactionParameters,
4747
CounterpartyChannelTransactionParameters, MAX_HTLCS,
4848
get_commitment_transaction_number_obscure_factor,
49-
ClosingTransaction, commit_tx_fee_sat, per_outbound_htlc_counterparty_commit_tx_fee_msat,
49+
ClosingTransaction, commit_tx_fee_sat, per_outbound_htlc_counterparty_commit_tx_fee_msat, commit_tx_fee_msat,
5050
};
5151
use crate::ln::chan_utils;
5252
use crate::ln::onion_utils::HTLCFailReason;
@@ -3665,7 +3665,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
36653665
let on_counterparty_tx_nondust_htlcs =
36663666
on_counterparty_tx_accepted_nondust_htlcs + on_counterparty_tx_offered_nondust_htlcs;
36673667
on_counterparty_tx_dust_exposure_msat +=
3668-
commit_tx_fee_sat(excess_feerate, on_counterparty_tx_nondust_htlcs, &self.channel_type) * 1000;
3668+
commit_tx_fee_msat(excess_feerate, on_counterparty_tx_nondust_htlcs, &self.channel_type);
36693669
if !self.channel_type.supports_anchors_zero_fee_htlc_tx() {
36703670
on_counterparty_tx_dust_exposure_msat +=
36713671
on_counterparty_tx_accepted_nondust_htlcs as u64 * htlc_success_tx_weight(&self.channel_type)

lightning/src/ln/functional_tests.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10300,7 +10300,7 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e
1030010300
} else { 0 };
1030110301
let initial_feerate = if apply_excess_fee { 253 * 2 } else { 253 };
1030210302
let expected_dust_buffer_feerate = initial_feerate + 2530;
10303-
let mut commitment_tx_cost = commit_tx_fee_msat(initial_feerate - 253, nondust_htlc_count_in_limit, &ChannelTypeFeatures::empty());
10303+
let mut commitment_tx_cost = chan_utils::commit_tx_fee_msat(initial_feerate - 253, nondust_htlc_count_in_limit as usize, &ChannelTypeFeatures::empty());
1030410304
commitment_tx_cost +=
1030510305
if on_holder_tx {
1030610306
htlc_success_tx_weight(&ChannelTypeFeatures::empty())
@@ -10646,14 +10646,14 @@ fn do_test_nondust_htlc_fees_dust_exposure_delta(features: ChannelTypeFeatures)
1064610646
}
1064710647

1064810648
// Set `expected_dust_exposure_msat` to match the calculation in `FundedChannel::can_accept_incoming_htlc`
10649-
// only_static_remote_key: 500_000 + 22 * (724 + 172) / 1000 * 1000 + 22 * 663 = 533_586
10650-
// anchors_zero_htlc_fee: 500_000 + 22 * (1_124 + 172) / 1000 * 1000 = 528_000
10651-
let mut expected_dust_exposure_msat = BASE_DUST_EXPOSURE_MSAT + EXCESS_FEERATE * (commitment_tx_base_weight(&features) + COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000 * 1000;
10649+
// only_static_remote_key: 500_000 + 22 * (724 + 172) + 22 * 663 = 534_298
10650+
// anchors_zero_htlc_fee: 500_000 + 22 * (1_124 + 172) = 528_512
10651+
let mut expected_dust_exposure_msat = BASE_DUST_EXPOSURE_MSAT + EXCESS_FEERATE * (commitment_tx_base_weight(&features) + COMMITMENT_TX_WEIGHT_PER_HTLC);
1065210652
if features == ChannelTypeFeatures::only_static_remote_key() {
1065310653
expected_dust_exposure_msat += EXCESS_FEERATE * htlc_timeout_tx_weight(&features);
10654-
assert_eq!(expected_dust_exposure_msat, 533_586);
10654+
assert_eq!(expected_dust_exposure_msat, 534_298);
1065510655
} else {
10656-
assert_eq!(expected_dust_exposure_msat, 528_000);
10656+
assert_eq!(expected_dust_exposure_msat, 528_512);
1065710657
}
1065810658

1065910659
let mut default_config = test_default_channel_config();
@@ -10749,15 +10749,13 @@ fn do_test_nondust_htlc_fees_dust_exposure_delta(features: ChannelTypeFeatures)
1074910749
assert_eq!(nodes[0].node.list_channels()[0].pending_outbound_htlcs.len(), DUST_HTLC_COUNT);
1075010750
assert_eq!(nodes[1].node.list_channels()[0].pending_inbound_htlcs.len(), DUST_HTLC_COUNT);
1075110751

10752-
// Set `expected_dust_exposure_msat` to match the calculation in `ChannelContext::get_available_balances`
10753-
// only_static_remote_key: 500_000 + 22 * 724 / 1000 * 1000 + 22 * 172 + 22 * 703 = 534_250
10754-
// anchors_zero_htlc_fee: 500_000 + 22 * 1124 / 1000 * 1000 + 22 * 172 = 527_784
10755-
let mut expected_dust_exposure_msat = BASE_DUST_EXPOSURE_MSAT + EXCESS_FEERATE * commitment_tx_base_weight(&features) / 1000 * 1000 + EXCESS_FEERATE * COMMITMENT_TX_WEIGHT_PER_HTLC;
10752+
// The `expected_dust_exposure_msat` for the outbound htlc changes in the non-anchor case, as the htlc success and timeout transactions have different weights
10753+
// only_static_remote_key: 500_000 + 22 * 724 + 22 * 172 + 22 * 703 = 535_178
1075610754
if features == ChannelTypeFeatures::only_static_remote_key() {
10757-
expected_dust_exposure_msat += EXCESS_FEERATE * htlc_success_tx_weight(&features);
10758-
assert_eq!(expected_dust_exposure_msat, 534_250);
10755+
expected_dust_exposure_msat = BASE_DUST_EXPOSURE_MSAT + EXCESS_FEERATE * commitment_tx_base_weight(&features) + EXCESS_FEERATE * COMMITMENT_TX_WEIGHT_PER_HTLC + EXCESS_FEERATE * htlc_success_tx_weight(&features);
10756+
assert_eq!(expected_dust_exposure_msat, 535_178);
1075910757
} else {
10760-
assert_eq!(expected_dust_exposure_msat, 527_784);
10758+
assert_eq!(expected_dust_exposure_msat, 528_512);
1076110759
}
1076210760

1076310761
// Set node 1's max dust htlc exposure to 1msat below `expected_dust_exposure_msat`

0 commit comments

Comments
 (0)