Skip to content

Commit 3bb0586

Browse files
committed
ln/refactor: move success/timeout dust limits into helper
This commit pulls calculation of second stage fees into a helper function. A side effect of this refactor is that it fixes a rounding issue in commit_and_htlc_tx_fees_sat. Previously, rounding down of feerate would happen after multiplying by the number of HTLCs. Now the feerate will be rounded down before multiplying by the number of HTLCs. This wasn't a serious issue - it would just cause us very slightly over estimate our dust exposure at certain feerates that needed rounding. A hard-coded value in test_nondust_htlc_excess_fees_are_dust is updated to account for this rounding change.
1 parent dbffbcb commit 3bb0586

File tree

4 files changed

+151
-157
lines changed

4 files changed

+151
-157
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,27 @@ pub(crate) fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, channel_t
235235
/ 1000
236236
}
237237

238+
/// Returns the fees for success and timeout second stage HTLC transactions.
239+
pub(super) fn second_stage_tx_fees_sat(
240+
channel_type: &ChannelTypeFeatures, feerate_sat_per_1000_weight: u32,
241+
) -> (u64, u64) {
242+
if channel_type.supports_anchors_zero_fee_htlc_tx() {
243+
(0, 0)
244+
} else {
245+
(
246+
feerate_sat_per_1000_weight as u64 * htlc_success_tx_weight(channel_type) / 1000,
247+
feerate_sat_per_1000_weight as u64 * htlc_timeout_tx_weight(channel_type) / 1000,
248+
)
249+
}
250+
}
251+
238252
#[rustfmt::skip]
239253
pub(crate) fn htlc_tx_fees_sat(feerate_per_kw: u32, num_accepted_htlcs: usize, num_offered_htlcs: usize, channel_type_features: &ChannelTypeFeatures) -> u64 {
240-
let htlc_tx_fees_sat = if !channel_type_features.supports_anchors_zero_fee_htlc_tx() {
241-
num_accepted_htlcs as u64 * htlc_success_tx_weight(channel_type_features) * feerate_per_kw as u64 / 1000
242-
+ num_offered_htlcs as u64 * htlc_timeout_tx_weight(channel_type_features) * feerate_per_kw as u64 / 1000
243-
} else {
244-
0
245-
};
246-
htlc_tx_fees_sat
254+
let (htlc_success_tx_fee_sat, htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
255+
channel_type_features, feerate_per_kw,
256+
);
257+
258+
num_accepted_htlcs as u64 * htlc_success_tx_fee_sat + num_offered_htlcs as u64 * htlc_timeout_tx_fee_sat
247259
}
248260

249261
/// Returns a fee estimate for the commitment transaction depending on channel type.
@@ -824,16 +836,17 @@ pub(crate) fn build_htlc_input(commitment_txid: &Txid, htlc: &HTLCOutputInCommit
824836
pub(crate) fn build_htlc_output(
825837
feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, channel_type_features: &ChannelTypeFeatures, broadcaster_delayed_payment_key: &DelayedPaymentKey, revocation_key: &RevocationKey
826838
) -> TxOut {
827-
let weight = if htlc.offered {
828-
htlc_timeout_tx_weight(channel_type_features)
829-
} else {
830-
htlc_success_tx_weight(channel_type_features)
831-
};
832-
let output_value = if channel_type_features.supports_anchors_zero_fee_htlc_tx() {
833-
htlc.to_bitcoin_amount()
834-
} else {
835-
let total_fee = Amount::from_sat(feerate_per_kw as u64 * weight / 1000);
836-
htlc.to_bitcoin_amount() - total_fee
839+
let (htlc_success_tx_fee_sat, htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
840+
channel_type_features, feerate_per_kw,
841+
);
842+
843+
let output_value = {
844+
let total_fee = if htlc.offered {
845+
htlc_timeout_tx_fee_sat
846+
} else {
847+
htlc_success_tx_fee_sat
848+
};
849+
htlc.to_bitcoin_amount() - Amount::from_sat(total_fee)
837850
};
838851

839852
TxOut {

0 commit comments

Comments
 (0)