@@ -4105,20 +4105,12 @@ impl<SP: Deref> Channel<SP> where
4105
4105
Ok(self.get_announcement_sigs(node_signer, chain_hash, user_config, best_block.height, logger))
4106
4106
}
4107
4107
4108
- pub fn update_add_htlc<F, FE: Deref, L: Deref>(
4109
- &mut self, msg: &msgs::UpdateAddHTLC, mut pending_forward_status: PendingHTLCStatus,
4110
- create_pending_htlc_status: F, fee_estimator: &LowerBoundedFeeEstimator<FE>, logger: &L
4111
- ) -> Result<(), ChannelError>
4112
- where F: for<'a> Fn(&'a Self, PendingHTLCStatus, u16) -> PendingHTLCStatus,
4113
- FE::Target: FeeEstimator, L::Target: Logger,
4114
- {
4108
+ pub fn update_add_htlc(
4109
+ &mut self, msg: &msgs::UpdateAddHTLC, pending_forward_status: PendingHTLCStatus,
4110
+ ) -> Result<(), ChannelError> {
4115
4111
if !matches!(self.context.channel_state, ChannelState::ChannelReady(_)) {
4116
4112
return Err(ChannelError::Close("Got add HTLC message when channel was not in an operational state".to_owned()));
4117
4113
}
4118
- // We can't accept HTLCs sent after we've sent a shutdown.
4119
- if self.context.channel_state.is_local_shutdown_sent() {
4120
- pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x4000|8);
4121
- }
4122
4114
// If the remote has sent a shutdown prior to adding this HTLC, then they are in violation of the spec.
4123
4115
if self.context.channel_state.is_remote_shutdown_sent() {
4124
4116
return Err(ChannelError::Close("Got add HTLC message when channel was not in an operational state".to_owned()));
@@ -4137,7 +4129,6 @@ impl<SP: Deref> Channel<SP> where
4137
4129
}
4138
4130
4139
4131
let inbound_stats = self.context.get_inbound_pending_htlc_stats(None);
4140
- let outbound_stats = self.context.get_outbound_pending_htlc_stats(None);
4141
4132
if inbound_stats.pending_htlcs + 1 > self.context.holder_max_accepted_htlcs as u32 {
4142
4133
return Err(ChannelError::Close(format!("Remote tried to push more than our max accepted HTLCs ({})", self.context.holder_max_accepted_htlcs)));
4143
4134
}
@@ -4166,34 +4157,6 @@ impl<SP: Deref> Channel<SP> where
4166
4157
}
4167
4158
}
4168
4159
4169
- let max_dust_htlc_exposure_msat = self.context.get_max_dust_htlc_exposure_msat(fee_estimator);
4170
- let (htlc_timeout_dust_limit, htlc_success_dust_limit) = if self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
4171
- (0, 0)
4172
- } else {
4173
- let dust_buffer_feerate = self.context.get_dust_buffer_feerate(None) as u64;
4174
- (dust_buffer_feerate * htlc_timeout_tx_weight(self.context.get_channel_type()) / 1000,
4175
- dust_buffer_feerate * htlc_success_tx_weight(self.context.get_channel_type()) / 1000)
4176
- };
4177
- let exposure_dust_limit_timeout_sats = htlc_timeout_dust_limit + self.context.counterparty_dust_limit_satoshis;
4178
- if msg.amount_msat / 1000 < exposure_dust_limit_timeout_sats {
4179
- let on_counterparty_tx_dust_htlc_exposure_msat = inbound_stats.on_counterparty_tx_dust_exposure_msat + outbound_stats.on_counterparty_tx_dust_exposure_msat + msg.amount_msat;
4180
- if on_counterparty_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
4181
- log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx",
4182
- on_counterparty_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
4183
- pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x1000|7);
4184
- }
4185
- }
4186
-
4187
- let exposure_dust_limit_success_sats = htlc_success_dust_limit + self.context.holder_dust_limit_satoshis;
4188
- if msg.amount_msat / 1000 < exposure_dust_limit_success_sats {
4189
- let on_holder_tx_dust_htlc_exposure_msat = inbound_stats.on_holder_tx_dust_exposure_msat + outbound_stats.on_holder_tx_dust_exposure_msat + msg.amount_msat;
4190
- if on_holder_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
4191
- log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx",
4192
- on_holder_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
4193
- pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x1000|7);
4194
- }
4195
- }
4196
-
4197
4160
let pending_value_to_self_msat =
4198
4161
self.context.value_to_self_msat + inbound_stats.pending_htlcs_value_msat - removed_outbound_total_msat;
4199
4162
let pending_remote_value_msat =
@@ -4227,23 +4190,7 @@ impl<SP: Deref> Channel<SP> where
4227
4190
} else {
4228
4191
0
4229
4192
};
4230
- if !self.context.is_outbound() {
4231
- // `Some(())` is for the fee spike buffer we keep for the remote. This deviates from
4232
- // the spec because the fee spike buffer requirement doesn't exist on the receiver's
4233
- // side, only on the sender's. Note that with anchor outputs we are no longer as
4234
- // sensitive to fee spikes, so we need to account for them.
4235
- let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
4236
- let mut remote_fee_cost_incl_stuck_buffer_msat = self.context.next_remote_commit_tx_fee_msat(htlc_candidate, Some(()));
4237
- if !self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
4238
- remote_fee_cost_incl_stuck_buffer_msat *= FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
4239
- }
4240
- 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 {
4241
- // Note that if the pending_forward_status is not updated here, then it's because we're already failing
4242
- // the HTLC, i.e. its status is already set to failing.
4243
- log_info!(logger, "Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", &self.context.channel_id());
4244
- pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x1000|7);
4245
- }
4246
- } else {
4193
+ if self.context.is_outbound() {
4247
4194
// Check that they won't violate our local required channel reserve by adding this HTLC.
4248
4195
let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
4249
4196
let local_commit_tx_fee_msat = self.context.next_local_commit_tx_fee_msat(htlc_candidate, None);
@@ -6130,6 +6077,86 @@ impl<SP: Deref> Channel<SP> where
6130
6077
})
6131
6078
}
6132
6079
6080
+ pub fn can_accept_incoming_htlc<F: Deref, L: Deref>(
6081
+ &self, msg: &msgs::UpdateAddHTLC, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: L
6082
+ ) -> Result<(), (&'static str, u16)>
6083
+ where
6084
+ F::Target: FeeEstimator,
6085
+ L::Target: Logger
6086
+ {
6087
+ if self.context.channel_state.is_local_shutdown_sent() {
6088
+ return Err(("Shutdown was already sent", 0x4000|8))
6089
+ }
6090
+
6091
+ let inbound_stats = self.context.get_inbound_pending_htlc_stats(None);
6092
+ let outbound_stats = self.context.get_outbound_pending_htlc_stats(None);
6093
+ let max_dust_htlc_exposure_msat = self.context.get_max_dust_htlc_exposure_msat(fee_estimator);
6094
+ let (htlc_timeout_dust_limit, htlc_success_dust_limit) = if self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
6095
+ (0, 0)
6096
+ } else {
6097
+ let dust_buffer_feerate = self.context.get_dust_buffer_feerate(None) as u64;
6098
+ (dust_buffer_feerate * htlc_timeout_tx_weight(self.context.get_channel_type()) / 1000,
6099
+ dust_buffer_feerate * htlc_success_tx_weight(self.context.get_channel_type()) / 1000)
6100
+ };
6101
+ let exposure_dust_limit_timeout_sats = htlc_timeout_dust_limit + self.context.counterparty_dust_limit_satoshis;
6102
+ if msg.amount_msat / 1000 < exposure_dust_limit_timeout_sats {
6103
+ let on_counterparty_tx_dust_htlc_exposure_msat = inbound_stats.on_counterparty_tx_dust_exposure_msat + outbound_stats.on_counterparty_tx_dust_exposure_msat + msg.amount_msat;
6104
+ if on_counterparty_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
6105
+ log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx",
6106
+ on_counterparty_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
6107
+ return Err(("Exceeded our dust exposure limit on counterparty commitment tx", 0x1000|7))
6108
+ }
6109
+ }
6110
+
6111
+ let exposure_dust_limit_success_sats = htlc_success_dust_limit + self.context.holder_dust_limit_satoshis;
6112
+ if msg.amount_msat / 1000 < exposure_dust_limit_success_sats {
6113
+ let on_holder_tx_dust_htlc_exposure_msat = inbound_stats.on_holder_tx_dust_exposure_msat + outbound_stats.on_holder_tx_dust_exposure_msat + msg.amount_msat;
6114
+ if on_holder_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
6115
+ log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx",
6116
+ on_holder_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
6117
+ return Err(("Exceeded our dust exposure limit on holder commitment tx", 0x1000|7))
6118
+ }
6119
+ }
6120
+
6121
+ let anchor_outputs_value_msat = if self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
6122
+ ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000
6123
+ } else {
6124
+ 0
6125
+ };
6126
+
6127
+ let mut removed_outbound_total_msat = 0;
6128
+ for ref htlc in self.context.pending_outbound_htlcs.iter() {
6129
+ if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) = htlc.state {
6130
+ removed_outbound_total_msat += htlc.amount_msat;
6131
+ } else if let OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) = htlc.state {
6132
+ removed_outbound_total_msat += htlc.amount_msat;
6133
+ }
6134
+ }
6135
+
6136
+ let pending_value_to_self_msat =
6137
+ self.context.value_to_self_msat + inbound_stats.pending_htlcs_value_msat - removed_outbound_total_msat;
6138
+ let pending_remote_value_msat =
6139
+ self.context.channel_value_satoshis * 1000 - pending_value_to_self_msat;
6140
+
6141
+ if !self.context.is_outbound() {
6142
+ // `Some(())` is for the fee spike buffer we keep for the remote. This deviates from
6143
+ // the spec because the fee spike buffer requirement doesn't exist on the receiver's
6144
+ // side, only on the sender's. Note that with anchor outputs we are no longer as
6145
+ // sensitive to fee spikes, so we need to account for them.
6146
+ let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
6147
+ let mut remote_fee_cost_incl_stuck_buffer_msat = self.context.next_remote_commit_tx_fee_msat(htlc_candidate, Some(()));
6148
+ if !self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
6149
+ remote_fee_cost_incl_stuck_buffer_msat *= FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
6150
+ }
6151
+ 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 {
6152
+ log_info!(logger, "Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", &self.context.channel_id());
6153
+ return Err(("Fee spike buffer violation", 0x1000|7));
6154
+ }
6155
+ }
6156
+
6157
+ Ok(())
6158
+ }
6159
+
6133
6160
pub fn get_cur_holder_commitment_transaction_number(&self) -> u64 {
6134
6161
self.context.cur_holder_commitment_transaction_number + 1
6135
6162
}
0 commit comments