@@ -987,9 +987,8 @@ struct CommitmentData<'a> {
987987/// A struct gathering stats on a commitment transaction, either local or remote.
988988struct CommitmentStats {
989989 total_fee_sat: u64, // the total fee included in the transaction
990- total_anchors_sat: u64, // the sum of the anchors' amounts
991- local_balance_before_fee_anchors_msat: u64, // local balance before fees and anchors *not* considering dust limits
992- remote_balance_before_fee_anchors_msat: u64, // remote balance before fees and anchors *not* considering dust limits
990+ local_balance_before_fee_msat: u64, // local balance before fees *not* considering dust limits
991+ remote_balance_before_fee_msat: u64, // remote balance before fees *not* considering dust limits
993992}
994993
995994/// Used when calculating whether we or the remote can afford an additional HTLC.
@@ -3773,7 +3772,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
37733772 if update_fee {
37743773 debug_assert!(!funding.is_outbound());
37753774 let counterparty_reserve_we_require_msat = funding.holder_selected_channel_reserve_satoshis * 1000;
3776- if commitment_data.stats.remote_balance_before_fee_anchors_msat < commitment_data.stats.total_fee_sat * 1000 + counterparty_reserve_we_require_msat {
3775+ if commitment_data.stats.remote_balance_before_fee_msat < commitment_data.stats.total_fee_sat * 1000 + counterparty_reserve_we_require_msat {
37773776 return Err(ChannelError::close("Funding remote cannot afford proposed new fee".to_owned()));
37783777 }
37793778 }
@@ -3933,11 +3932,23 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
39333932 let total_fee_sat = commit_tx_fee_sat(feerate_per_kw, non_dust_htlc_count, &funding.channel_transaction_parameters.channel_type_features);
39343933 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 };
39353934
3935+ // We MUST use saturating subs here, as the funder's balance is not guaranteed to be greater
3936+ // than or equal to `total_anchors_sat`.
3937+ //
3938+ // This is because when the remote party sends an `update_fee` message, we build the new
3939+ // commitment transaction *before* checking whether the remote party's balance is enough to
3940+ // cover the total anchor sum.
3941+
3942+ if funding.is_outbound() {
3943+ value_to_self_msat = value_to_self_msat.saturating_sub(total_anchors_sat * 1000);
3944+ } else {
3945+ value_to_remote_msat = value_to_remote_msat.saturating_sub(total_anchors_sat * 1000);
3946+ }
3947+
39363948 CommitmentStats {
39373949 total_fee_sat,
3938- total_anchors_sat,
3939- local_balance_before_fee_anchors_msat: value_to_self_msat,
3940- remote_balance_before_fee_anchors_msat: value_to_remote_msat,
3950+ local_balance_before_fee_msat: value_to_self_msat,
3951+ remote_balance_before_fee_msat: value_to_remote_msat,
39413952 }
39423953 }
39433954
@@ -3964,9 +3975,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
39643975 let stats = self.build_commitment_stats(funding, local, generated_by_local);
39653976 let CommitmentStats {
39663977 total_fee_sat,
3967- total_anchors_sat,
3968- local_balance_before_fee_anchors_msat,
3969- remote_balance_before_fee_anchors_msat
3978+ local_balance_before_fee_msat,
3979+ remote_balance_before_fee_msat
39703980 } = stats;
39713981
39723982 let num_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len();
@@ -4037,9 +4047,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
40374047 // cover the total fee and the anchors.
40384048
40394049 let (value_to_self, value_to_remote) = if funding.is_outbound() {
4040- ((local_balance_before_fee_anchors_msat / 1000).saturating_sub(total_anchors_sat).saturating_sub( total_fee_sat), remote_balance_before_fee_anchors_msat / 1000)
4050+ ((local_balance_before_fee_msat / 1000).saturating_sub(total_fee_sat), remote_balance_before_fee_msat / 1000)
40414051 } else {
4042- (local_balance_before_fee_anchors_msat / 1000, (remote_balance_before_fee_anchors_msat / 1000).saturating_sub(total_anchors_sat ).saturating_sub(total_fee_sat))
4052+ (local_balance_before_fee_msat / 1000, (remote_balance_before_fee_msat / 1000).saturating_sub(total_fee_sat))
40434053 };
40444054
40454055 let mut to_broadcaster_value_sat = if local { value_to_self } else { value_to_remote };
@@ -6667,7 +6677,7 @@ impl<SP: Deref> FundedChannel<SP> where
66676677 &self.holder_commitment_point.current_point(), true, true, logger,
66686678 );
66696679 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, self.funding.get_channel_type()) * 1000;
6670- let holder_balance_msat = commitment_data.stats.local_balance_before_fee_anchors_msat - htlc_stats.outbound_holding_cell_msat;
6680+ let holder_balance_msat = commitment_data.stats.local_balance_before_fee_msat - htlc_stats.outbound_holding_cell_msat;
66716681 if holder_balance_msat < buffer_fee_msat + self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
66726682 //TODO: auto-close after a number of failures?
66736683 log_debug!(logger, "Cannot afford to send new feerate at {}", feerate_per_kw);
0 commit comments