Skip to content

Commit 41ffe61

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 1261386 commit 41ffe61

File tree

4 files changed

+165
-163
lines changed

4 files changed

+165
-163
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use bitcoin::secp256k1::{ecdsa::Signature, Message, Secp256k1};
4141
use bitcoin::secp256k1::{PublicKey, Scalar, SecretKey};
4242
use bitcoin::{secp256k1, Sequence, Witness};
4343

44+
use super::channel::second_stage_tx_fees_sat;
4445
use super::channel_keys::{
4546
DelayedPaymentBasepoint, DelayedPaymentKey, HtlcBasepoint, HtlcKey, RevocationBasepoint,
4647
RevocationKey,
@@ -239,13 +240,13 @@ pub(crate) fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, channel_t
239240
pub(crate) fn commit_and_htlc_tx_fees_sat(feerate_per_kw: u32, num_accepted_htlcs: usize, num_offered_htlcs: usize, channel_type_features: &ChannelTypeFeatures) -> u64 {
240241
let num_htlcs = num_accepted_htlcs + num_offered_htlcs;
241242
let commit_tx_fees_sat = commit_tx_fee_sat(feerate_per_kw, num_htlcs, channel_type_features);
242-
let htlc_tx_fees_sat = if !channel_type_features.supports_anchors_zero_fee_htlc_tx() {
243-
num_accepted_htlcs as u64 * htlc_success_tx_weight(channel_type_features) * feerate_per_kw as u64 / 1000
244-
+ num_offered_htlcs as u64 * htlc_timeout_tx_weight(channel_type_features) * feerate_per_kw as u64 / 1000
245-
} else {
246-
0
247-
};
248-
commit_tx_fees_sat + htlc_tx_fees_sat
243+
let (htlc_success_tx_fee_sat, htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
244+
channel_type_features, feerate_per_kw,
245+
);
246+
247+
commit_tx_fees_sat
248+
+ num_accepted_htlcs as u64 * htlc_success_tx_fee_sat
249+
+ num_offered_htlcs as u64 * htlc_timeout_tx_fee_sat
249250
}
250251

251252
/// Returns a fee estimate for the commitment transaction depending on channel type.
@@ -826,16 +827,17 @@ pub(crate) fn build_htlc_input(commitment_txid: &Txid, htlc: &HTLCOutputInCommit
826827
pub(crate) fn build_htlc_output(
827828
feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, channel_type_features: &ChannelTypeFeatures, broadcaster_delayed_payment_key: &DelayedPaymentKey, revocation_key: &RevocationKey
828829
) -> TxOut {
829-
let weight = if htlc.offered {
830-
htlc_timeout_tx_weight(channel_type_features)
831-
} else {
832-
htlc_success_tx_weight(channel_type_features)
833-
};
834-
let output_value = if channel_type_features.supports_anchors_zero_fee_htlc_tx() {
835-
htlc.to_bitcoin_amount()
836-
} else {
837-
let total_fee = Amount::from_sat(feerate_per_kw as u64 * weight / 1000);
838-
htlc.to_bitcoin_amount() - total_fee
830+
let (htlc_success_tx_fee_sat, htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
831+
channel_type_features, feerate_per_kw,
832+
);
833+
834+
let output_value = {
835+
let total_fee = if htlc.offered {
836+
htlc_timeout_tx_fee_sat
837+
} else {
838+
htlc_success_tx_fee_sat
839+
};
840+
htlc.to_bitcoin_amount() - Amount::from_sat(total_fee)
839841
};
840842

841843
TxOut {

0 commit comments

Comments
 (0)