@@ -1130,10 +1130,12 @@ struct CommitmentData<'a> {
1130
1130
1131
1131
/// A struct gathering stats on a commitment transaction, either local or remote.
1132
1132
struct 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,
1137
1139
}
1138
1140
1139
1141
/// Used when calculating whether we or the remote can afford an additional HTLC.
@@ -4235,7 +4237,7 @@ where
4235
4237
if update_fee {
4236
4238
debug_assert!(!funding.is_outbound());
4237
4239
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 {
4239
4241
return Err(ChannelError::close("Funding remote cannot afford proposed new fee".to_owned()));
4240
4242
}
4241
4243
}
@@ -4251,7 +4253,7 @@ where
4251
4253
&& info.next_holder_htlc_id == self.next_holder_htlc_id
4252
4254
&& info.next_counterparty_htlc_id == self.next_counterparty_htlc_id
4253
4255
&& 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);
4255
4257
}
4256
4258
}
4257
4259
}
@@ -4327,8 +4329,8 @@ where
4327
4329
&holder_commitment_point.current_point(), true, true, logger,
4328
4330
);
4329
4331
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 {
4332
4334
//TODO: auto-close after a number of failures?
4333
4335
log_debug!(logger, "Cannot afford to send new feerate at {}", feerate_per_kw);
4334
4336
return false;
@@ -4506,14 +4508,26 @@ where
4506
4508
broadcaster_max_commitment_tx_output.1 = cmp::max(broadcaster_max_commitment_tx_output.1, value_to_remote_msat);
4507
4509
}
4508
4510
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);
4510
4512
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 };
4511
4513
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
+
4512
4527
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,
4517
4531
}
4518
4532
}
4519
4533
@@ -4540,10 +4554,9 @@ where
4540
4554
4541
4555
let stats = self.build_commitment_stats(funding, local, generated_by_local);
4542
4556
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
4547
4560
} = stats;
4548
4561
4549
4562
let num_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len();
@@ -4607,16 +4620,16 @@ where
4607
4620
};
4608
4621
4609
4622
// 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 `.
4611
4624
//
4612
4625
// This is because when the remote party sends an `update_fee` message, we build the new
4613
4626
// 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.
4615
4628
4616
4629
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)
4618
4631
} 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 ))
4620
4633
};
4621
4634
4622
4635
let mut to_broadcaster_value_sat = if local { value_to_self } else { value_to_remote };
0 commit comments