@@ -987,9 +987,8 @@ struct CommitmentData<'a> {
987
987
/// A struct gathering stats on a commitment transaction, either local or remote.
988
988
struct CommitmentStats {
989
989
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
993
992
}
994
993
995
994
/// 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 {
3773
3772
if update_fee {
3774
3773
debug_assert!(!funding.is_outbound());
3775
3774
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 {
3777
3776
return Err(ChannelError::close("Funding remote cannot afford proposed new fee".to_owned()));
3778
3777
}
3779
3778
}
@@ -3933,11 +3932,23 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3933
3932
let total_fee_sat = commit_tx_fee_sat(feerate_per_kw, non_dust_htlc_count, &funding.channel_transaction_parameters.channel_type_features);
3934
3933
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 };
3935
3934
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
+
3936
3948
CommitmentStats {
3937
3949
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,
3941
3952
}
3942
3953
}
3943
3954
@@ -3964,9 +3975,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3964
3975
let stats = self.build_commitment_stats(funding, local, generated_by_local);
3965
3976
let CommitmentStats {
3966
3977
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
3970
3980
} = stats;
3971
3981
3972
3982
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 {
4037
4047
// cover the total fee and the anchors.
4038
4048
4039
4049
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)
4041
4051
} 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))
4043
4053
};
4044
4054
4045
4055
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
6667
6677
&self.holder_commitment_point.current_point(), true, true, logger,
6668
6678
);
6669
6679
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;
6671
6681
if holder_balance_msat < buffer_fee_msat + self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
6672
6682
//TODO: auto-close after a number of failures?
6673
6683
log_debug!(logger, "Cannot afford to send new feerate at {}", feerate_per_kw);
0 commit comments