@@ -724,7 +724,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
724
724
725
725
cur_holder_commitment_transaction_number: u64,
726
726
cur_counterparty_commitment_transaction_number: u64,
727
- value_to_self_msat: u64, // Excluding all pending_htlcs, excluding fees
727
+ value_to_self_msat: u64, // Excluding all pending_htlcs, fees, and anchor outputs
728
728
pending_inbound_htlcs: Vec<InboundHTLCOutput>,
729
729
pending_outbound_htlcs: Vec<OutboundHTLCOutput>,
730
730
holding_cell_htlc_updates: Vec<HTLCUpdateAwaitingACK>,
@@ -1673,6 +1673,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1673
1673
1674
1674
let mut available_capacity_msat = outbound_capacity_msat;
1675
1675
1676
+ let anchor_outputs_value_msat = if context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
1677
+ ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000
1678
+ } else {
1679
+ 0
1680
+ };
1676
1681
if context.is_outbound() {
1677
1682
// We should mind channel commit tx fee when computing how much of the available capacity
1678
1683
// can be used in the next htlc. Mirrors the logic in send_htlc.
@@ -1687,14 +1692,19 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1687
1692
}
1688
1693
1689
1694
let htlc_above_dust = HTLCCandidate::new(real_dust_limit_timeout_sat * 1000, HTLCInitiator::LocalOffered);
1690
- let max_reserved_commit_tx_fee_msat = FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE * context.next_local_commit_tx_fee_msat(htlc_above_dust, Some(()));
1695
+ let mut max_reserved_commit_tx_fee_msat = context.next_local_commit_tx_fee_msat(htlc_above_dust, Some(()));
1691
1696
let htlc_dust = HTLCCandidate::new(real_dust_limit_timeout_sat * 1000 - 1, HTLCInitiator::LocalOffered);
1692
- let min_reserved_commit_tx_fee_msat = FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE * context.next_local_commit_tx_fee_msat(htlc_dust, Some(()));
1697
+ let mut min_reserved_commit_tx_fee_msat = context.next_local_commit_tx_fee_msat(htlc_dust, Some(()));
1698
+ if !context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
1699
+ max_reserved_commit_tx_fee_msat *= FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
1700
+ min_reserved_commit_tx_fee_msat *= FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
1701
+ }
1693
1702
1694
1703
// We will first subtract the fee as if we were above-dust. Then, if the resulting
1695
1704
// value ends up being below dust, we have this fee available again. In that case,
1696
1705
// match the value to right-below-dust.
1697
- let mut capacity_minus_commitment_fee_msat: i64 = (available_capacity_msat as i64) - (max_reserved_commit_tx_fee_msat as i64);
1706
+ let mut capacity_minus_commitment_fee_msat: i64 = available_capacity_msat as i64 -
1707
+ max_reserved_commit_tx_fee_msat as i64 - anchor_outputs_value_msat as i64;
1698
1708
if capacity_minus_commitment_fee_msat < (real_dust_limit_timeout_sat as i64) * 1000 {
1699
1709
let one_htlc_difference_msat = max_reserved_commit_tx_fee_msat - min_reserved_commit_tx_fee_msat;
1700
1710
debug_assert!(one_htlc_difference_msat != 0);
@@ -1719,7 +1729,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1719
1729
let remote_balance_msat = (context.channel_value_satoshis * 1000 - context.value_to_self_msat)
1720
1730
.saturating_sub(inbound_stats.pending_htlcs_value_msat);
1721
1731
1722
- if remote_balance_msat < max_reserved_commit_tx_fee_msat + holder_selected_chan_reserve_msat {
1732
+ if remote_balance_msat < max_reserved_commit_tx_fee_msat + holder_selected_chan_reserve_msat + anchor_outputs_value_msat {
1723
1733
// If another HTLC's fee would reduce the remote's balance below the reserve limit
1724
1734
// we've selected for them, we can only send dust HTLCs.
1725
1735
available_capacity_msat = cmp::min(available_capacity_msat, real_dust_limit_success_sat * 1000 - 1);
@@ -2119,7 +2129,7 @@ fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, channel_type_feature
2119
2129
2120
2130
// Get the fee cost in MSATS of a commitment tx with a given number of HTLC outputs.
2121
2131
// Note that num_htlcs should not include dust HTLCs.
2122
- fn commit_tx_fee_msat(feerate_per_kw: u32, num_htlcs: usize, channel_type_features: &ChannelTypeFeatures) -> u64 {
2132
+ pub(crate) fn commit_tx_fee_msat(feerate_per_kw: u32, num_htlcs: usize, channel_type_features: &ChannelTypeFeatures) -> u64 {
2123
2133
// Note that we need to divide before multiplying to round properly,
2124
2134
// since the lowest denomination of bitcoin on-chain is the satoshi.
2125
2135
(commitment_tx_base_weight(channel_type_features) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate_per_kw as u64 / 1000 * 1000
@@ -2766,6 +2776,7 @@ impl<SP: Deref> Channel<SP> where
2766
2776
if inbound_stats.pending_htlcs_value_msat + msg.amount_msat > self.context.holder_max_htlc_value_in_flight_msat {
2767
2777
return Err(ChannelError::Close(format!("Remote HTLC add would put them over our max HTLC value ({})", self.context.holder_max_htlc_value_in_flight_msat)));
2768
2778
}
2779
+
2769
2780
// Check holder_selected_channel_reserve_satoshis (we're getting paid, so they have to at least meet
2770
2781
// the reserve_satoshis we told them to always have as direct payment so that they lose
2771
2782
// something if we punish them for broadcasting an old state).
@@ -2825,30 +2836,40 @@ impl<SP: Deref> Channel<SP> where
2825
2836
2826
2837
// Check that the remote can afford to pay for this HTLC on-chain at the current
2827
2838
// feerate_per_kw, while maintaining their channel reserve (as required by the spec).
2828
- let remote_commit_tx_fee_msat = if self.context.is_outbound() { 0 } else {
2829
- let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
2830
- self.context.next_remote_commit_tx_fee_msat(htlc_candidate, None) // Don't include the extra fee spike buffer HTLC in calculations
2831
- };
2832
- if pending_remote_value_msat - msg.amount_msat < remote_commit_tx_fee_msat {
2833
- return Err(ChannelError::Close("Remote HTLC add would not leave enough to pay for fees".to_owned()));
2834
- };
2835
-
2836
- if pending_remote_value_msat - msg.amount_msat - remote_commit_tx_fee_msat < self.context.holder_selected_channel_reserve_satoshis * 1000 {
2837
- return Err(ChannelError::Close("Remote HTLC add would put them under remote reserve value".to_owned()));
2839
+ {
2840
+ let remote_commit_tx_fee_msat = if self.context.is_outbound() { 0 } else {
2841
+ let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
2842
+ self.context.next_remote_commit_tx_fee_msat(htlc_candidate, None) // Don't include the extra fee spike buffer HTLC in calculations
2843
+ };
2844
+ let anchor_outputs_value_msat = if !self.context.is_outbound() && self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
2845
+ ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000
2846
+ } else {
2847
+ 0
2848
+ };
2849
+ if pending_remote_value_msat.saturating_sub(msg.amount_msat).saturating_sub(anchor_outputs_value_msat) < remote_commit_tx_fee_msat {
2850
+ return Err(ChannelError::Close("Remote HTLC add would not leave enough to pay for fees".to_owned()));
2851
+ };
2852
+ if pending_remote_value_msat.saturating_sub(msg.amount_msat).saturating_sub(remote_commit_tx_fee_msat).saturating_sub(anchor_outputs_value_msat) < self.context.holder_selected_channel_reserve_satoshis * 1000 {
2853
+ return Err(ChannelError::Close("Remote HTLC add would put them under remote reserve value".to_owned()));
2854
+ }
2838
2855
}
2839
2856
2857
+ let anchor_outputs_value_msat = if self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
2858
+ ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000
2859
+ } else {
2860
+ 0
2861
+ };
2840
2862
if !self.context.is_outbound() {
2841
- // `2 *` and `Some(())` is for the fee spike buffer we keep for the remote. This deviates from
2842
- // the spec because in the spec, the fee spike buffer requirement doesn't exist on the
2843
- // receiver's side, only on the sender's.
2844
- // Note that when we eventually remove support for fee updates and switch to anchor output
2845
- // fees, we will drop the `2 *`, since we no longer be as sensitive to fee spikes. But, keep
2846
- // the extra htlc when calculating the next remote commitment transaction fee as we should
2847
- // still be able to afford adding this HTLC plus one more future HTLC, regardless of being
2848
- // sensitive to fee spikes.
2863
+ // `Some(())` is for the fee spike buffer we keep for the remote. This deviates from
2864
+ // the spec because the fee spike buffer requirement doesn't exist on the receiver's
2865
+ // side, only on the sender's. Note that with anchor outputs we are no longer as
2866
+ // sensitive to fee spikes, so we need to account for them.
2849
2867
let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
2850
- let remote_fee_cost_incl_stuck_buffer_msat = 2 * self.context.next_remote_commit_tx_fee_msat(htlc_candidate, Some(()));
2851
- if pending_remote_value_msat - msg.amount_msat - self.context.holder_selected_channel_reserve_satoshis * 1000 < remote_fee_cost_incl_stuck_buffer_msat {
2868
+ let mut remote_fee_cost_incl_stuck_buffer_msat = self.context.next_remote_commit_tx_fee_msat(htlc_candidate, Some(()));
2869
+ if !self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
2870
+ remote_fee_cost_incl_stuck_buffer_msat *= FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
2871
+ }
2872
+ if pending_remote_value_msat.saturating_sub(msg.amount_msat).saturating_sub(self.context.holder_selected_channel_reserve_satoshis * 1000).saturating_sub(anchor_outputs_value_msat) < remote_fee_cost_incl_stuck_buffer_msat {
2852
2873
// Note that if the pending_forward_status is not updated here, then it's because we're already failing
2853
2874
// the HTLC, i.e. its status is already set to failing.
2854
2875
log_info!(logger, "Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", &self.context.channel_id());
@@ -2858,7 +2879,7 @@ impl<SP: Deref> Channel<SP> where
2858
2879
// Check that they won't violate our local required channel reserve by adding this HTLC.
2859
2880
let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
2860
2881
let local_commit_tx_fee_msat = self.context.next_local_commit_tx_fee_msat(htlc_candidate, None);
2861
- if self.context.value_to_self_msat < self.context.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 + local_commit_tx_fee_msat {
2882
+ if self.context.value_to_self_msat < self.context.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 + local_commit_tx_fee_msat + anchor_outputs_value_msat {
2862
2883
return Err(ChannelError::Close("Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value".to_owned()));
2863
2884
}
2864
2885
}
@@ -5721,16 +5742,16 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
5721
5742
let channel_type = Self::get_initial_channel_type(&config, their_features);
5722
5743
debug_assert!(channel_type.is_subset(&channelmanager::provided_channel_type_features(&config)));
5723
5744
5724
- let commitment_conf_target = if channel_type.supports_anchors_zero_fee_htlc_tx() {
5725
- ConfirmationTarget::AnchorChannelFee
5745
+ let ( commitment_conf_target, anchor_outputs_value_msat) = if channel_type.supports_anchors_zero_fee_htlc_tx() {
5746
+ ( ConfirmationTarget::AnchorChannelFee, ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000)
5726
5747
} else {
5727
- ConfirmationTarget::NonAnchorChannelFee
5748
+ ( ConfirmationTarget::NonAnchorChannelFee, 0)
5728
5749
};
5729
5750
let commitment_feerate = fee_estimator.bounded_sat_per_1000_weight(commitment_conf_target);
5730
5751
5731
5752
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
5732
5753
let commitment_tx_fee = commit_tx_fee_msat(commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
5733
- if value_to_self_msat < commitment_tx_fee {
5754
+ if value_to_self_msat.saturating_sub(anchor_outputs_value_msat) < commitment_tx_fee {
5734
5755
return Err(APIError::APIMisuseError{ err: format!("Funding amount ({}) can't even pay fee for initial commitment transaction fee of {}.", value_to_self_msat / 1000, commitment_tx_fee / 1000) });
5735
5756
}
5736
5757
@@ -6347,13 +6368,18 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6347
6368
6348
6369
// check if the funder's amount for the initial commitment tx is sufficient
6349
6370
// for full fee payment plus a few HTLCs to ensure the channel will be useful.
6371
+ let anchor_outputs_value = if channel_type.supports_anchors_zero_fee_htlc_tx() {
6372
+ ANCHOR_OUTPUT_VALUE_SATOSHI * 2
6373
+ } else {
6374
+ 0
6375
+ };
6350
6376
let funders_amount_msat = msg.funding_satoshis * 1000 - msg.push_msat;
6351
6377
let commitment_tx_fee = commit_tx_fee_msat(msg.feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT, &channel_type) / 1000;
6352
- if funders_amount_msat / 1000 < commitment_tx_fee {
6353
- return Err(ChannelError::Close(format!("Funding amount ({} sats) can't even pay fee for initial commitment transaction fee of {} sats.", funders_amount_msat / 1000, commitment_tx_fee)));
6378
+ if ( funders_amount_msat / 1000).saturating_sub(anchor_outputs_value) < commitment_tx_fee {
6379
+ return Err(ChannelError::Close(format!("Funding amount ({} sats) can't even pay fee for initial commitment transaction fee of {} sats.", ( funders_amount_msat / 1000).saturating_sub(anchor_outputs_value) , commitment_tx_fee)));
6354
6380
}
6355
6381
6356
- let to_remote_satoshis = funders_amount_msat / 1000 - commitment_tx_fee;
6382
+ let to_remote_satoshis = funders_amount_msat / 1000 - commitment_tx_fee - anchor_outputs_value ;
6357
6383
// While it's reasonable for us to not meet the channel reserve initially (if they don't
6358
6384
// want to push much to us), our counterparty should always have more than our reserve.
6359
6385
if to_remote_satoshis < holder_selected_channel_reserve_satoshis {
0 commit comments