@@ -1501,6 +1501,14 @@ fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, opt_anchors: bool) -
1501
1501
feerate_per_kw as u64 * ( commitment_tx_base_weight ( opt_anchors) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC ) / 1000
1502
1502
}
1503
1503
1504
+ // Get the fee cost in MSATS of a commitment tx with a given number of HTLC outputs.
1505
+ // Note that num_htlcs should not include dust HTLCs.
1506
+ fn commit_tx_fee_msat ( feerate_per_kw : u32 , num_htlcs : usize , opt_anchors : bool ) -> u64 {
1507
+ // Note that we need to divide before multiplying to round properly,
1508
+ // since the lowest denomination of bitcoin on-chain is the satoshi.
1509
+ ( commitment_tx_base_weight ( opt_anchors) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC ) * feerate_per_kw as u64 / 1000 * 1000
1510
+ }
1511
+
1504
1512
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
1505
1513
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
1506
1514
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
@@ -1698,7 +1706,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
1698
1706
let feerate = fee_estimator. bounded_sat_per_1000_weight ( ConfirmationTarget :: Normal ) ;
1699
1707
1700
1708
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
1701
- let commitment_tx_fee = Self :: commit_tx_fee_msat ( feerate, MIN_AFFORDABLE_HTLC_COUNT , channel_type. requires_anchors_zero_fee_htlc_tx ( ) ) ;
1709
+ let commitment_tx_fee = commit_tx_fee_msat ( feerate, MIN_AFFORDABLE_HTLC_COUNT , channel_type. requires_anchors_zero_fee_htlc_tx ( ) ) ;
1702
1710
if value_to_self_msat < commitment_tx_fee {
1703
1711
return Err ( APIError :: APIMisuseError { err : format ! ( "Funding amount ({}) can't even pay fee for initial commitment transaction fee of {}." , value_to_self_msat / 1000 , commitment_tx_fee / 1000 ) } ) ;
1704
1712
}
@@ -2032,7 +2040,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2032
2040
// check if the funder's amount for the initial commitment tx is sufficient
2033
2041
// for full fee payment plus a few HTLCs to ensure the channel will be useful.
2034
2042
let funders_amount_msat = msg. funding_satoshis * 1000 - msg. push_msat ;
2035
- let commitment_tx_fee = Self :: commit_tx_fee_msat ( msg. feerate_per_kw , MIN_AFFORDABLE_HTLC_COUNT , opt_anchors) / 1000 ;
2043
+ let commitment_tx_fee = commit_tx_fee_msat ( msg. feerate_per_kw , MIN_AFFORDABLE_HTLC_COUNT , opt_anchors) / 1000 ;
2036
2044
if funders_amount_msat / 1000 < commitment_tx_fee {
2037
2045
return Err ( ChannelError :: Close ( format ! ( "Funding amount ({} sats) can't even pay fee for initial commitment transaction fee of {} sats." , funders_amount_msat / 1000 , commitment_tx_fee) ) ) ;
2038
2046
}
@@ -3157,14 +3165,6 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3157
3165
( context. holder_selected_channel_reserve_satoshis , context. counterparty_selected_channel_reserve_satoshis )
3158
3166
}
3159
3167
3160
- // Get the fee cost in MSATS of a commitment tx with a given number of HTLC outputs.
3161
- // Note that num_htlcs should not include dust HTLCs.
3162
- fn commit_tx_fee_msat ( feerate_per_kw : u32 , num_htlcs : usize , opt_anchors : bool ) -> u64 {
3163
- // Note that we need to divide before multiplying to round properly,
3164
- // since the lowest denomination of bitcoin on-chain is the satoshi.
3165
- ( commitment_tx_base_weight ( opt_anchors) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC ) * feerate_per_kw as u64 / 1000 * 1000
3166
- }
3167
-
3168
3168
/// Get the commitment tx fee for the local's (i.e. our) next commitment transaction based on the
3169
3169
/// number of pending HTLCs that are on track to be in our next commitment tx.
3170
3170
///
@@ -3242,12 +3242,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3242
3242
}
3243
3243
3244
3244
let num_htlcs = included_htlcs + addl_htlcs;
3245
- let res = Self :: commit_tx_fee_msat ( context. feerate_per_kw , num_htlcs, context. opt_anchors ( ) ) ;
3245
+ let res = commit_tx_fee_msat ( context. feerate_per_kw , num_htlcs, context. opt_anchors ( ) ) ;
3246
3246
#[ cfg( any( test, fuzzing) ) ]
3247
3247
{
3248
3248
let mut fee = res;
3249
3249
if fee_spike_buffer_htlc. is_some ( ) {
3250
- fee = Self :: commit_tx_fee_msat ( context. feerate_per_kw , num_htlcs - 1 , context. opt_anchors ( ) ) ;
3250
+ fee = commit_tx_fee_msat ( context. feerate_per_kw , num_htlcs - 1 , context. opt_anchors ( ) ) ;
3251
3251
}
3252
3252
let total_pending_htlcs = context. pending_inbound_htlcs . len ( ) + context. pending_outbound_htlcs . len ( )
3253
3253
+ context. holding_cell_htlc_updates . len ( ) ;
@@ -3333,12 +3333,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3333
3333
}
3334
3334
3335
3335
let num_htlcs = included_htlcs + addl_htlcs;
3336
- let res = Self :: commit_tx_fee_msat ( context. feerate_per_kw , num_htlcs, context. opt_anchors ( ) ) ;
3336
+ let res = commit_tx_fee_msat ( context. feerate_per_kw , num_htlcs, context. opt_anchors ( ) ) ;
3337
3337
#[ cfg( any( test, fuzzing) ) ]
3338
3338
{
3339
3339
let mut fee = res;
3340
3340
if fee_spike_buffer_htlc. is_some ( ) {
3341
- fee = Self :: commit_tx_fee_msat ( context. feerate_per_kw , num_htlcs - 1 , context. opt_anchors ( ) ) ;
3341
+ fee = commit_tx_fee_msat ( context. feerate_per_kw , num_htlcs - 1 , context. opt_anchors ( ) ) ;
3342
3342
}
3343
3343
let total_pending_htlcs = context. pending_inbound_htlcs . len ( ) + context. pending_outbound_htlcs . len ( ) ;
3344
3344
let commitment_tx_info = CommitmentTxInfoCached {
@@ -6184,7 +6184,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
6184
6184
&& info. next_holder_htlc_id == self . context . next_holder_htlc_id
6185
6185
&& info. next_counterparty_htlc_id == self . context . next_counterparty_htlc_id
6186
6186
&& info. feerate == self . context . feerate_per_kw {
6187
- let actual_fee = Self :: commit_tx_fee_msat ( self . context . feerate_per_kw , commitment_stats. num_nondust_htlcs , self . context . opt_anchors ( ) ) ;
6187
+ let actual_fee = commit_tx_fee_msat ( self . context . feerate_per_kw , commitment_stats. num_nondust_htlcs , self . context . opt_anchors ( ) ) ;
6188
6188
assert_eq ! ( actual_fee, info. fee) ;
6189
6189
}
6190
6190
}
@@ -7280,7 +7280,7 @@ mod tests {
7280
7280
use crate :: ln:: channelmanager:: { self , HTLCSource , PaymentId } ;
7281
7281
#[ cfg( anchors) ]
7282
7282
use crate :: ln:: channel:: InitFeatures ;
7283
- use crate :: ln:: channel:: { Channel , InboundHTLCOutput , OutboundHTLCOutput , InboundHTLCState , OutboundHTLCState , HTLCCandidate , HTLCInitiator } ;
7283
+ use crate :: ln:: channel:: { Channel , InboundHTLCOutput , OutboundHTLCOutput , InboundHTLCState , OutboundHTLCState , HTLCCandidate , HTLCInitiator , commit_tx_fee_msat } ;
7284
7284
use crate :: ln:: channel:: { MAX_FUNDING_SATOSHIS_NO_WUMBO , TOTAL_BITCOIN_SUPPLY_SATOSHIS , MIN_THEIR_CHAN_RESERVE_SATOSHIS } ;
7285
7285
use crate :: ln:: features:: ChannelTypeFeatures ;
7286
7286
use crate :: ln:: msgs:: { ChannelUpdate , DecodeError , UnsignedChannelUpdate , MAX_VALUE_MSAT } ;
@@ -7480,13 +7480,13 @@ mod tests {
7480
7480
// the dust limit check.
7481
7481
let htlc_candidate = HTLCCandidate :: new ( htlc_amount_msat, HTLCInitiator :: LocalOffered ) ;
7482
7482
let local_commit_tx_fee = node_a_chan. next_local_commit_tx_fee_msat ( htlc_candidate, None ) ;
7483
- let local_commit_fee_0_htlcs = Channel :: < EnforcingSigner > :: commit_tx_fee_msat ( node_a_chan. context . feerate_per_kw , 0 , node_a_chan. context . opt_anchors ( ) ) ;
7483
+ let local_commit_fee_0_htlcs = commit_tx_fee_msat ( node_a_chan. context . feerate_per_kw , 0 , node_a_chan. context . opt_anchors ( ) ) ;
7484
7484
assert_eq ! ( local_commit_tx_fee, local_commit_fee_0_htlcs) ;
7485
7485
7486
7486
// Finally, make sure that when Node A calculates the remote's commitment transaction fees, all
7487
7487
// of the HTLCs are seen to be above the dust limit.
7488
7488
node_a_chan. context . channel_transaction_parameters . is_outbound_from_holder = false ;
7489
- let remote_commit_fee_3_htlcs = Channel :: < EnforcingSigner > :: commit_tx_fee_msat ( node_a_chan. context . feerate_per_kw , 3 , node_a_chan. context . opt_anchors ( ) ) ;
7489
+ let remote_commit_fee_3_htlcs = commit_tx_fee_msat ( node_a_chan. context . feerate_per_kw , 3 , node_a_chan. context . opt_anchors ( ) ) ;
7490
7490
let htlc_candidate = HTLCCandidate :: new ( htlc_amount_msat, HTLCInitiator :: LocalOffered ) ;
7491
7491
let remote_commit_tx_fee = node_a_chan. next_remote_commit_tx_fee_msat ( htlc_candidate, None ) ;
7492
7492
assert_eq ! ( remote_commit_tx_fee, remote_commit_fee_3_htlcs) ;
@@ -7508,8 +7508,8 @@ mod tests {
7508
7508
let config = UserConfig :: default ( ) ;
7509
7509
let mut chan = Channel :: < EnforcingSigner > :: new_outbound ( & fee_est, & & keys_provider, & & keys_provider, node_id, & channelmanager:: provided_init_features ( & config) , 10000000 , 100000 , 42 , & config, 0 , 42 ) . unwrap ( ) ;
7510
7510
7511
- let commitment_tx_fee_0_htlcs = Channel :: < EnforcingSigner > :: commit_tx_fee_msat ( chan. context . feerate_per_kw , 0 , chan. context . opt_anchors ( ) ) ;
7512
- let commitment_tx_fee_1_htlc = Channel :: < EnforcingSigner > :: commit_tx_fee_msat ( chan. context . feerate_per_kw , 1 , chan. context . opt_anchors ( ) ) ;
7511
+ let commitment_tx_fee_0_htlcs = commit_tx_fee_msat ( chan. context . feerate_per_kw , 0 , chan. context . opt_anchors ( ) ) ;
7512
+ let commitment_tx_fee_1_htlc = commit_tx_fee_msat ( chan. context . feerate_per_kw , 1 , chan. context . opt_anchors ( ) ) ;
7513
7513
7514
7514
// If HTLC_SUCCESS_TX_WEIGHT and HTLC_TIMEOUT_TX_WEIGHT were swapped: then this HTLC would be
7515
7515
// counted as dust when it shouldn't be.
0 commit comments