@@ -25,7 +25,9 @@ use bitcoin::hashes::ripemd160::Hash as Ripemd160;
25
25
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
26
26
use bitcoin:: hashes:: { Hash , HashEngine } ;
27
27
28
- use crate :: chain:: chaininterface:: fee_for_weight;
28
+ use crate :: chain:: chaininterface:: {
29
+ fee_for_weight, ConfirmationTarget , FeeEstimator , LowerBoundedFeeEstimator ,
30
+ } ;
29
31
use crate :: chain:: package:: WEIGHT_REVOKED_OUTPUT ;
30
32
use crate :: ln:: msgs:: DecodeError ;
31
33
use crate :: sign:: EntropySource ;
@@ -233,15 +235,45 @@ pub(crate) fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, channel_t
233
235
/ 1000
234
236
}
235
237
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
+
236
254
#[ rustfmt:: skip]
237
255
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 ( ) {
242
271
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
+ }
245
277
}
246
278
247
279
// 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
806
838
pub ( crate ) fn build_htlc_output (
807
839
feerate_per_kw : u32 , contest_delay : u16 , htlc : & HTLCOutputInCommitment , channel_type_features : & ChannelTypeFeatures , broadcaster_delayed_payment_key : & DelayedPaymentKey , revocation_key : & RevocationKey
808
840
) -> 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)
819
852
} ;
820
853
821
854
TxOut {
0 commit comments