@@ -1254,11 +1254,11 @@ impl<Signer: Sign> Channel<Signer> {
12541254 broadcaster_max_commitment_tx_output. 1 = cmp:: max ( broadcaster_max_commitment_tx_output. 1 , value_to_remote_msat as u64 ) ;
12551255 }
12561256
1257- let total_fee = feerate_per_kw as u64 * ( COMMITMENT_TX_BASE_WEIGHT + ( included_non_dust_htlcs. len ( ) as u64 ) * COMMITMENT_TX_WEIGHT_PER_HTLC ) / 1000 ;
1257+ let total_fee_sat = Channel :: < Signer > :: commit_tx_fee_sat ( feerate_per_kw, included_non_dust_htlcs. len ( ) ) ;
12581258 let ( value_to_self, value_to_remote) = if self . is_outbound ( ) {
1259- ( value_to_self_msat / 1000 - total_fee as i64 , value_to_remote_msat / 1000 )
1259+ ( value_to_self_msat / 1000 - total_fee_sat as i64 , value_to_remote_msat / 1000 )
12601260 } else {
1261- ( value_to_self_msat / 1000 , value_to_remote_msat / 1000 - total_fee as i64 )
1261+ ( value_to_self_msat / 1000 , value_to_remote_msat / 1000 - total_fee_sat as i64 )
12621262 } ;
12631263
12641264 let mut value_to_a = if local { value_to_self } else { value_to_remote } ;
@@ -2066,14 +2066,21 @@ impl<Signer: Sign> Channel<Signer> {
20662066 self . counterparty_selected_channel_reserve_satoshis )
20672067 }
20682068
2069- // Get the fee cost of a commitment tx with a given number of HTLC outputs.
2069+ // Get the fee cost in MSATS of a commitment tx with a given number of HTLC outputs.
20702070 // Note that num_htlcs should not include dust HTLCs.
20712071 fn commit_tx_fee_msat ( & self , num_htlcs : usize ) -> u64 {
20722072 // Note that we need to divide before multiplying to round properly,
20732073 // since the lowest denomination of bitcoin on-chain is the satoshi.
20742074 ( COMMITMENT_TX_BASE_WEIGHT + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC ) * self . feerate_per_kw as u64 / 1000 * 1000
20752075 }
20762076
2077+ // Get the fee cost in SATS of a commitment tx with a given number of HTLC outputs.
2078+ // Note that num_htlcs should not include dust HTLCs.
2079+ #[ inline]
2080+ fn commit_tx_fee_sat ( feerate_per_kw : u32 , num_htlcs : usize ) -> u64 {
2081+ feerate_per_kw as u64 * ( COMMITMENT_TX_BASE_WEIGHT + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC ) / 1000
2082+ }
2083+
20772084 // Get the commitment tx fee for the local's (i.e. our) next commitment transaction based on the
20782085 // number of pending HTLCs that are on track to be in our next commitment tx, plus an additional
20792086 // HTLC if `fee_spike_buffer_htlc` is Some, plus a new HTLC given by `new_htlc_amount`. Dust HTLCs
@@ -2497,10 +2504,10 @@ impl<Signer: Sign> Channel<Signer> {
24972504 update_state == FeeUpdateState :: RemoteAnnounced
24982505 } else { false } ;
24992506 if update_fee { debug_assert ! ( !self . is_outbound( ) ) ; }
2500- let total_fee = feerate_per_kw as u64 * ( COMMITMENT_TX_BASE_WEIGHT + ( num_htlcs as u64 ) * COMMITMENT_TX_WEIGHT_PER_HTLC ) / 1000 ;
2507+ let total_fee_sat = Channel :: < Signer > :: commit_tx_fee_sat ( feerate_per_kw, num_htlcs) ;
25012508 if update_fee {
25022509 let counterparty_reserve_we_require = Channel :: < Signer > :: get_holder_selected_channel_reserve_satoshis ( self . channel_value_satoshis ) ;
2503- if self . channel_value_satoshis - self . value_to_self_msat / 1000 < total_fee + counterparty_reserve_we_require {
2510+ if self . channel_value_satoshis - self . value_to_self_msat / 1000 < total_fee_sat + counterparty_reserve_we_require {
25042511 return Err ( ( None , ChannelError :: Close ( "Funding remote cannot afford proposed new fee" . to_owned ( ) ) ) ) ;
25052512 }
25062513 }
@@ -2516,7 +2523,7 @@ impl<Signer: Sign> Channel<Signer> {
25162523 && info. next_holder_htlc_id == self . next_holder_htlc_id
25172524 && info. next_counterparty_htlc_id == self . next_counterparty_htlc_id
25182525 && info. feerate == self . feerate_per_kw {
2519- assert_eq ! ( total_fee , info. fee / 1000 ) ;
2526+ assert_eq ! ( total_fee_sat , info. fee / 1000 ) ;
25202527 }
25212528 }
25222529 }
0 commit comments