@@ -25,7 +25,9 @@ use bitcoin::hashes::ripemd160::Hash as Ripemd160;
2525use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
2626use 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+ } ;
2931use crate :: chain:: package:: WEIGHT_REVOKED_OUTPUT ;
3032use crate :: ln:: msgs:: DecodeError ;
3133use 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]
237255pub ( 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
806838pub ( 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