@@ -1130,10 +1130,12 @@ struct CommitmentData<'a> {
11301130
11311131/// A struct gathering stats on a commitment transaction, either local or remote.
11321132struct CommitmentStats {
1133- total_fee_sat: u64, // the total fee included in the transaction
1134- total_anchors_sat: u64, // the sum of the anchors' amounts
1135- local_balance_before_fee_anchors_msat: u64, // local balance before fees and anchors *not* considering dust limits
1136- remote_balance_before_fee_anchors_msat: u64, // remote balance before fees and anchors *not* considering dust limits
1133+ /// The total fee included in the commitment transaction
1134+ commit_tx_fee_sat: u64,
1135+ /// The local balance before fees *not* considering dust limits
1136+ local_balance_before_fee_msat: u64,
1137+ /// The remote balance before fees *not* considering dust limits
1138+ remote_balance_before_fee_msat: u64,
11371139}
11381140
11391141/// Used when calculating whether we or the remote can afford an additional HTLC.
@@ -4235,7 +4237,7 @@ where
42354237 if update_fee {
42364238 debug_assert!(!funding.is_outbound());
42374239 let counterparty_reserve_we_require_msat = funding.holder_selected_channel_reserve_satoshis * 1000;
4238- if commitment_data.stats.remote_balance_before_fee_anchors_msat < commitment_data.stats.total_fee_sat * 1000 + commitment_data.stats.total_anchors_sat * 1000 + counterparty_reserve_we_require_msat {
4240+ if commitment_data.stats.remote_balance_before_fee_msat < commitment_data.stats.commit_tx_fee_sat * 1000 + counterparty_reserve_we_require_msat {
42394241 return Err(ChannelError::close("Funding remote cannot afford proposed new fee".to_owned()));
42404242 }
42414243 }
@@ -4251,7 +4253,7 @@ where
42514253 && info.next_holder_htlc_id == self.next_holder_htlc_id
42524254 && info.next_counterparty_htlc_id == self.next_counterparty_htlc_id
42534255 && info.feerate == self.feerate_per_kw {
4254- assert_eq!(commitment_data.stats.total_fee_sat , info.fee / 1000);
4256+ assert_eq!(commitment_data.stats.commit_tx_fee_sat , info.fee / 1000);
42554257 }
42564258 }
42574259 }
@@ -4327,8 +4329,8 @@ where
43274329 &holder_commitment_point.current_point(), true, true, logger,
43284330 );
43294331 let buffer_fee_msat = commit_tx_fee_sat(feerate_per_kw, commitment_data.tx.nondust_htlcs().len() + htlc_stats.on_holder_tx_outbound_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize, funding.get_channel_type()) * 1000;
4330- let holder_balance_msat = commitment_data.stats.local_balance_before_fee_anchors_msat - htlc_stats.outbound_holding_cell_msat;
4331- if holder_balance_msat < buffer_fee_msat + commitment_data.stats.total_anchors_sat * 1000 + funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
4332+ let holder_balance_msat = commitment_data.stats.local_balance_before_fee_msat - htlc_stats.outbound_holding_cell_msat;
4333+ if holder_balance_msat < buffer_fee_msat + funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
43324334 //TODO: auto-close after a number of failures?
43334335 log_debug!(logger, "Cannot afford to send new feerate at {}", feerate_per_kw);
43344336 return false;
@@ -4506,14 +4508,26 @@ where
45064508 broadcaster_max_commitment_tx_output.1 = cmp::max(broadcaster_max_commitment_tx_output.1, value_to_remote_msat);
45074509 }
45084510
4509- let total_fee_sat = commit_tx_fee_sat(feerate_per_kw, non_dust_htlc_count, &funding.channel_transaction_parameters.channel_type_features);
4511+ let commit_tx_fee_sat = commit_tx_fee_sat(feerate_per_kw, non_dust_htlc_count, &funding.channel_transaction_parameters.channel_type_features);
45104512 let total_anchors_sat = if funding.channel_transaction_parameters.channel_type_features.supports_anchors_zero_fee_htlc_tx() { ANCHOR_OUTPUT_VALUE_SATOSHI * 2 } else { 0 };
45114513
4514+ // We MUST use saturating subs here, as the funder's balance is not guaranteed to be greater
4515+ // than or equal to `total_anchors_sat`.
4516+ //
4517+ // This is because when the remote party sends an `update_fee` message, we build the new
4518+ // commitment transaction *before* checking whether the remote party's balance is enough to
4519+ // cover the total anchor sum.
4520+
4521+ if funding.is_outbound() {
4522+ value_to_self_msat = value_to_self_msat.saturating_sub(total_anchors_sat * 1000);
4523+ } else {
4524+ value_to_remote_msat = value_to_remote_msat.saturating_sub(total_anchors_sat * 1000);
4525+ }
4526+
45124527 CommitmentStats {
4513- total_fee_sat,
4514- total_anchors_sat,
4515- local_balance_before_fee_anchors_msat: value_to_self_msat,
4516- remote_balance_before_fee_anchors_msat: value_to_remote_msat,
4528+ commit_tx_fee_sat,
4529+ local_balance_before_fee_msat: value_to_self_msat,
4530+ remote_balance_before_fee_msat: value_to_remote_msat,
45174531 }
45184532 }
45194533
@@ -4540,10 +4554,9 @@ where
45404554
45414555 let stats = self.build_commitment_stats(funding, local, generated_by_local);
45424556 let CommitmentStats {
4543- total_fee_sat,
4544- total_anchors_sat,
4545- local_balance_before_fee_anchors_msat,
4546- remote_balance_before_fee_anchors_msat
4557+ commit_tx_fee_sat,
4558+ local_balance_before_fee_msat,
4559+ remote_balance_before_fee_msat
45474560 } = stats;
45484561
45494562 let num_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len();
@@ -4607,16 +4620,16 @@ where
46074620 };
46084621
46094622 // We MUST use saturating subs here, as the funder's balance is not guaranteed to be greater
4610- // than or equal to the sum of `total_fee_sat` and `total_anchors_sat `.
4623+ // than or equal to `commit_tx_fee_sat `.
46114624 //
46124625 // This is because when the remote party sends an `update_fee` message, we build the new
46134626 // commitment transaction *before* checking whether the remote party's balance is enough to
4614- // cover the total fee and the anchors .
4627+ // cover the total fee.
46154628
46164629 let (value_to_self, value_to_remote) = if funding.is_outbound() {
4617- ((local_balance_before_fee_anchors_msat / 1000).saturating_sub(total_anchors_sat).saturating_sub(total_fee_sat), remote_balance_before_fee_anchors_msat / 1000)
4630+ ((local_balance_before_fee_msat / 1000).saturating_sub(commit_tx_fee_sat), remote_balance_before_fee_msat / 1000)
46184631 } else {
4619- (local_balance_before_fee_anchors_msat / 1000, (remote_balance_before_fee_anchors_msat / 1000).saturating_sub(total_anchors_sat).saturating_sub(total_fee_sat ))
4632+ (local_balance_before_fee_msat / 1000, (remote_balance_before_fee_msat / 1000).saturating_sub(commit_tx_fee_sat ))
46204633 };
46214634
46224635 let mut to_broadcaster_value_sat = if local { value_to_self } else { value_to_remote };
0 commit comments