Skip to content

Update fee and dust handling for zero fee channels #3884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
3 changes: 3 additions & 0 deletions lightning/src/chain/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ pub(crate) fn verify_channel_type_features(channel_type_features: &Option<Channe
supported_feature_set.set_scid_privacy_required();
supported_feature_set.set_zero_conf_required();

#[cfg(test)]
supported_feature_set.set_anchor_zero_fee_commitments_required();

// allow the passing of an additional necessary permitted flag
if let Some(additional_permitted_features) = additional_permitted_features {
supported_feature_set |= additional_permitted_features;
Expand Down
54 changes: 37 additions & 17 deletions lightning/src/ln/chan_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use bitcoin::hashes::ripemd160::Hash as Ripemd160;
use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hashes::{Hash, HashEngine};

use crate::chain::chaininterface::fee_for_weight;
use crate::chain::chaininterface::{
fee_for_weight, ConfirmationTarget, FeeEstimator, LowerBoundedFeeEstimator,
};
use crate::chain::package::WEIGHT_REVOKED_OUTPUT;
use crate::ln::msgs::DecodeError;
use crate::sign::EntropySource;
Expand All @@ -39,6 +41,7 @@ use bitcoin::secp256k1::{ecdsa::Signature, Message, Secp256k1};
use bitcoin::secp256k1::{PublicKey, Scalar, SecretKey};
use bitcoin::{secp256k1, Sequence, Witness};

use super::channel::second_stage_tx_fees_sat;
use super::channel_keys::{
DelayedPaymentBasepoint, DelayedPaymentKey, HtlcBasepoint, HtlcKey, RevocationBasepoint,
RevocationKey,
Expand Down Expand Up @@ -237,13 +240,29 @@ pub(crate) fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, channel_t
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 {
let num_htlcs = num_accepted_htlcs + num_offered_htlcs;
let commit_tx_fees_sat = commit_tx_fee_sat(feerate_per_kw, num_htlcs, channel_type_features);
let htlc_tx_fees_sat = if !channel_type_features.supports_anchors_zero_fee_htlc_tx() {
num_accepted_htlcs as u64 * htlc_success_tx_weight(channel_type_features) * feerate_per_kw as u64 / 1000
+ num_offered_htlcs as u64 * htlc_timeout_tx_weight(channel_type_features) * feerate_per_kw as u64 / 1000
} else {
let (htlc_success_tx_fee_sat, htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
channel_type_features, feerate_per_kw,
);

commit_tx_fees_sat
+ num_accepted_htlcs as u64 * htlc_success_tx_fee_sat
+ num_offered_htlcs as u64 * htlc_timeout_tx_fee_sat
}

/// Returns a fee estimate for the commitment transaction depending on channel type.
pub(super) fn commitment_sat_per_1000_weight_for_type<'a, F: Deref>(
fee_estimator: &'a LowerBoundedFeeEstimator<F>, channel_type: &ChannelTypeFeatures,
) -> u32
where
F::Target: FeeEstimator,
{
if channel_type.supports_anchor_zero_fee_commitments() {
0
};
commit_tx_fees_sat + htlc_tx_fees_sat
} else if channel_type.supports_anchors_zero_fee_htlc_tx() {
fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::AnchorChannelFee)
} else {
fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee)
}
}

// Various functions for key derivation and transaction creation for use within channels. Primarily
Expand Down Expand Up @@ -808,16 +827,17 @@ pub(crate) fn build_htlc_input(commitment_txid: &Txid, htlc: &HTLCOutputInCommit
pub(crate) fn build_htlc_output(
feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, channel_type_features: &ChannelTypeFeatures, broadcaster_delayed_payment_key: &DelayedPaymentKey, revocation_key: &RevocationKey
) -> TxOut {
let weight = if htlc.offered {
htlc_timeout_tx_weight(channel_type_features)
} else {
htlc_success_tx_weight(channel_type_features)
};
let output_value = if channel_type_features.supports_anchors_zero_fee_htlc_tx() && !channel_type_features.supports_anchors_nonzero_fee_htlc_tx() {
htlc.to_bitcoin_amount()
} else {
let total_fee = Amount::from_sat(feerate_per_kw as u64 * weight / 1000);
htlc.to_bitcoin_amount() - total_fee
let (htlc_success_tx_fee_sat, htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
channel_type_features, feerate_per_kw,
);

let output_value = {
let total_fee = if htlc.offered {
htlc_timeout_tx_fee_sat
} else {
htlc_success_tx_fee_sat
};
htlc.to_bitcoin_amount() - Amount::from_sat(total_fee)
};

TxOut {
Expand Down
Loading
Loading