Skip to content

Commit eae2bb1

Browse files
authored
Merge pull request #3884 from carlaKC/3789-fees-and-dust
Update fee and dust handling for zero fee channels
2 parents 033eda2 + 54559b3 commit eae2bb1

File tree

8 files changed

+405
-257
lines changed

8 files changed

+405
-257
lines changed

lightning/src/chain/package.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ pub(crate) fn verify_channel_type_features(channel_type_features: &Option<Channe
9999
supported_feature_set.set_scid_privacy_required();
100100
supported_feature_set.set_zero_conf_required();
101101

102+
#[cfg(test)]
103+
supported_feature_set.set_anchor_zero_fee_commitments_required();
104+
102105
// allow the passing of an additional necessary permitted flag
103106
if let Some(additional_permitted_features) = additional_permitted_features {
104107
supported_feature_set |= additional_permitted_features;

lightning/src/ln/chan_utils.rs

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use bitcoin::hashes::ripemd160::Hash as Ripemd160;
2525
use bitcoin::hashes::sha256::Hash as Sha256;
2626
use bitcoin::hashes::{Hash, HashEngine};
2727

28-
use crate::chain::chaininterface::fee_for_weight;
28+
use crate::chain::chaininterface::{
29+
fee_for_weight, ConfirmationTarget, FeeEstimator, LowerBoundedFeeEstimator,
30+
};
2931
use crate::chain::package::WEIGHT_REVOKED_OUTPUT;
3032
use crate::ln::msgs::DecodeError;
3133
use crate::sign::EntropySource;
@@ -233,15 +235,45 @@ pub(crate) fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, channel_t
233235
/ 1000
234236
}
235237

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+
|| channel_type.supports_anchor_zero_fee_commitments()
244+
{
245+
(0, 0)
246+
} else {
247+
(
248+
feerate_sat_per_1000_weight as u64 * htlc_success_tx_weight(channel_type) / 1000,
249+
feerate_sat_per_1000_weight as u64 * htlc_timeout_tx_weight(channel_type) / 1000,
250+
)
251+
}
252+
}
253+
236254
#[rustfmt::skip]
237255
pub(crate) fn htlc_tx_fees_sat(feerate_per_kw: u32, num_accepted_htlcs: usize, num_offered_htlcs: usize, channel_type_features: &ChannelTypeFeatures) -> u64 {
238-
let htlc_tx_fees_sat = if !channel_type_features.supports_anchors_zero_fee_htlc_tx() {
239-
num_accepted_htlcs as u64 * htlc_success_tx_weight(channel_type_features) * feerate_per_kw as u64 / 1000
240-
+ num_offered_htlcs as u64 * htlc_timeout_tx_weight(channel_type_features) * feerate_per_kw as u64 / 1000
241-
} else {
256+
let (htlc_success_tx_fee_sat, htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
257+
channel_type_features, feerate_per_kw,
258+
);
259+
260+
num_accepted_htlcs as u64 * htlc_success_tx_fee_sat + num_offered_htlcs as u64 * htlc_timeout_tx_fee_sat
261+
}
262+
263+
/// Returns a fee estimate for the commitment transaction depending on channel type.
264+
pub(super) fn commitment_sat_per_1000_weight_for_type<F: Deref>(
265+
fee_estimator: &LowerBoundedFeeEstimator<F>, channel_type: &ChannelTypeFeatures,
266+
) -> u32
267+
where
268+
F::Target: FeeEstimator,
269+
{
270+
if channel_type.supports_anchor_zero_fee_commitments() {
242271
0
243-
};
244-
htlc_tx_fees_sat
272+
} else if channel_type.supports_anchors_zero_fee_htlc_tx() {
273+
fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::AnchorChannelFee)
274+
} else {
275+
fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee)
276+
}
245277
}
246278

247279
// Various functions for key derivation and transaction creation for use within channels. Primarily
@@ -806,16 +838,17 @@ pub(crate) fn build_htlc_input(commitment_txid: &Txid, htlc: &HTLCOutputInCommit
806838
pub(crate) fn build_htlc_output(
807839
feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, channel_type_features: &ChannelTypeFeatures, broadcaster_delayed_payment_key: &DelayedPaymentKey, revocation_key: &RevocationKey
808840
) -> TxOut {
809-
let weight = if htlc.offered {
810-
htlc_timeout_tx_weight(channel_type_features)
811-
} else {
812-
htlc_success_tx_weight(channel_type_features)
813-
};
814-
let output_value = if channel_type_features.supports_anchors_zero_fee_htlc_tx() && !channel_type_features.supports_anchors_nonzero_fee_htlc_tx() {
815-
htlc.to_bitcoin_amount()
816-
} else {
817-
let total_fee = Amount::from_sat(feerate_per_kw as u64 * weight / 1000);
818-
htlc.to_bitcoin_amount() - total_fee
841+
let (htlc_success_tx_fee_sat, htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
842+
channel_type_features, feerate_per_kw,
843+
);
844+
845+
let output_value = {
846+
let total_fee = if htlc.offered {
847+
htlc_timeout_tx_fee_sat
848+
} else {
849+
htlc_success_tx_fee_sat
850+
};
851+
htlc.to_bitcoin_amount() - Amount::from_sat(total_fee)
819852
};
820853

821854
TxOut {

0 commit comments

Comments
 (0)