@@ -4121,20 +4121,12 @@ impl<SP: Deref> Channel<SP> where
4121
4121
Ok(self.get_announcement_sigs(node_signer, chain_hash, user_config, best_block.height, logger))
4122
4122
}
4123
4123
4124
- pub fn update_add_htlc<F, FE: Deref, L: Deref>(
4125
- &mut self, msg: &msgs::UpdateAddHTLC, mut pending_forward_status: PendingHTLCStatus,
4126
- create_pending_htlc_status: F, fee_estimator: &LowerBoundedFeeEstimator<FE>, logger: &L
4127
- ) -> Result<(), ChannelError>
4128
- where F: for<'a> Fn(&'a Self, PendingHTLCStatus, u16) -> PendingHTLCStatus,
4129
- FE::Target: FeeEstimator, L::Target: Logger,
4130
- {
4124
+ pub fn update_add_htlc(
4125
+ &mut self, msg: &msgs::UpdateAddHTLC, pending_forward_status: PendingHTLCStatus,
4126
+ ) -> Result<(), ChannelError> {
4131
4127
if !matches!(self.context.channel_state, ChannelState::ChannelReady(_)) {
4132
4128
return Err(ChannelError::Close("Got add HTLC message when channel was not in an operational state".to_owned()));
4133
4129
}
4134
- // We can't accept HTLCs sent after we've sent a shutdown.
4135
- if self.context.channel_state.is_local_shutdown_sent() {
4136
- pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x4000|8);
4137
- }
4138
4130
// If the remote has sent a shutdown prior to adding this HTLC, then they are in violation of the spec.
4139
4131
if self.context.channel_state.is_remote_shutdown_sent() {
4140
4132
return Err(ChannelError::Close("Got add HTLC message when channel was not in an operational state".to_owned()));
@@ -4153,7 +4145,6 @@ impl<SP: Deref> Channel<SP> where
4153
4145
}
4154
4146
4155
4147
let inbound_stats = self.context.get_inbound_pending_htlc_stats(None);
4156
- let outbound_stats = self.context.get_outbound_pending_htlc_stats(None);
4157
4148
if inbound_stats.pending_htlcs + 1 > self.context.holder_max_accepted_htlcs as u32 {
4158
4149
return Err(ChannelError::Close(format!("Remote tried to push more than our max accepted HTLCs ({})", self.context.holder_max_accepted_htlcs)));
4159
4150
}
@@ -4182,34 +4173,6 @@ impl<SP: Deref> Channel<SP> where
4182
4173
}
4183
4174
}
4184
4175
4185
- let max_dust_htlc_exposure_msat = self.context.get_max_dust_htlc_exposure_msat(fee_estimator);
4186
- let (htlc_timeout_dust_limit, htlc_success_dust_limit) = if self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
4187
- (0, 0)
4188
- } else {
4189
- let dust_buffer_feerate = self.context.get_dust_buffer_feerate(None) as u64;
4190
- (dust_buffer_feerate * htlc_timeout_tx_weight(self.context.get_channel_type()) / 1000,
4191
- dust_buffer_feerate * htlc_success_tx_weight(self.context.get_channel_type()) / 1000)
4192
- };
4193
- let exposure_dust_limit_timeout_sats = htlc_timeout_dust_limit + self.context.counterparty_dust_limit_satoshis;
4194
- if msg.amount_msat / 1000 < exposure_dust_limit_timeout_sats {
4195
- 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;
4196
- if on_counterparty_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
4197
- log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx",
4198
- on_counterparty_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
4199
- pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x1000|7);
4200
- }
4201
- }
4202
-
4203
- let exposure_dust_limit_success_sats = htlc_success_dust_limit + self.context.holder_dust_limit_satoshis;
4204
- if msg.amount_msat / 1000 < exposure_dust_limit_success_sats {
4205
- 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;
4206
- if on_holder_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
4207
- log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx",
4208
- on_holder_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
4209
- pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x1000|7);
4210
- }
4211
- }
4212
-
4213
4176
let pending_value_to_self_msat =
4214
4177
self.context.value_to_self_msat + inbound_stats.pending_htlcs_value_msat - removed_outbound_total_msat;
4215
4178
let pending_remote_value_msat =
@@ -4243,23 +4206,7 @@ impl<SP: Deref> Channel<SP> where
4243
4206
} else {
4244
4207
0
4245
4208
};
4246
- if !self.context.is_outbound() {
4247
- // `Some(())` is for the fee spike buffer we keep for the remote. This deviates from
4248
- // the spec because the fee spike buffer requirement doesn't exist on the receiver's
4249
- // side, only on the sender's. Note that with anchor outputs we are no longer as
4250
- // sensitive to fee spikes, so we need to account for them.
4251
- let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
4252
- let mut remote_fee_cost_incl_stuck_buffer_msat = self.context.next_remote_commit_tx_fee_msat(htlc_candidate, Some(()));
4253
- if !self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
4254
- remote_fee_cost_incl_stuck_buffer_msat *= FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
4255
- }
4256
- 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 {
4257
- // Note that if the pending_forward_status is not updated here, then it's because we're already failing
4258
- // the HTLC, i.e. its status is already set to failing.
4259
- log_info!(logger, "Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", &self.context.channel_id());
4260
- pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x1000|7);
4261
- }
4262
- } else {
4209
+ if self.context.is_outbound() {
4263
4210
// Check that they won't violate our local required channel reserve by adding this HTLC.
4264
4211
let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
4265
4212
let local_commit_tx_fee_msat = self.context.next_local_commit_tx_fee_msat(htlc_candidate, None);
@@ -6148,6 +6095,86 @@ impl<SP: Deref> Channel<SP> where
6148
6095
})
6149
6096
}
6150
6097
6098
+ pub fn can_accept_incoming_htlc<F: Deref, L: Deref>(
6099
+ &self, msg: &msgs::UpdateAddHTLC, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: L
6100
+ ) -> Result<(), (&'static str, u16)>
6101
+ where
6102
+ F::Target: FeeEstimator,
6103
+ L::Target: Logger
6104
+ {
6105
+ if self.context.channel_state.is_local_shutdown_sent() {
6106
+ return Err(("Shutdown was already sent", 0x4000|8))
6107
+ }
6108
+
6109
+ let inbound_stats = self.context.get_inbound_pending_htlc_stats(None);
6110
+ let outbound_stats = self.context.get_outbound_pending_htlc_stats(None);
6111
+ let max_dust_htlc_exposure_msat = self.context.get_max_dust_htlc_exposure_msat(fee_estimator);
6112
+ let (htlc_timeout_dust_limit, htlc_success_dust_limit) = if self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
6113
+ (0, 0)
6114
+ } else {
6115
+ let dust_buffer_feerate = self.context.get_dust_buffer_feerate(None) as u64;
6116
+ (dust_buffer_feerate * htlc_timeout_tx_weight(self.context.get_channel_type()) / 1000,
6117
+ dust_buffer_feerate * htlc_success_tx_weight(self.context.get_channel_type()) / 1000)
6118
+ };
6119
+ let exposure_dust_limit_timeout_sats = htlc_timeout_dust_limit + self.context.counterparty_dust_limit_satoshis;
6120
+ if msg.amount_msat / 1000 < exposure_dust_limit_timeout_sats {
6121
+ 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;
6122
+ if on_counterparty_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
6123
+ log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx",
6124
+ on_counterparty_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
6125
+ return Err(("Exceeded our dust exposure limit on counterparty commitment tx", 0x1000|7))
6126
+ }
6127
+ }
6128
+
6129
+ let exposure_dust_limit_success_sats = htlc_success_dust_limit + self.context.holder_dust_limit_satoshis;
6130
+ if msg.amount_msat / 1000 < exposure_dust_limit_success_sats {
6131
+ 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;
6132
+ if on_holder_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
6133
+ log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx",
6134
+ on_holder_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
6135
+ return Err(("Exceeded our dust exposure limit on holder commitment tx", 0x1000|7))
6136
+ }
6137
+ }
6138
+
6139
+ let anchor_outputs_value_msat = if self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
6140
+ ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000
6141
+ } else {
6142
+ 0
6143
+ };
6144
+
6145
+ let mut removed_outbound_total_msat = 0;
6146
+ for ref htlc in self.context.pending_outbound_htlcs.iter() {
6147
+ if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) = htlc.state {
6148
+ removed_outbound_total_msat += htlc.amount_msat;
6149
+ } else if let OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) = htlc.state {
6150
+ removed_outbound_total_msat += htlc.amount_msat;
6151
+ }
6152
+ }
6153
+
6154
+ let pending_value_to_self_msat =
6155
+ self.context.value_to_self_msat + inbound_stats.pending_htlcs_value_msat - removed_outbound_total_msat;
6156
+ let pending_remote_value_msat =
6157
+ self.context.channel_value_satoshis * 1000 - pending_value_to_self_msat;
6158
+
6159
+ if !self.context.is_outbound() {
6160
+ // `Some(())` is for the fee spike buffer we keep for the remote. This deviates from
6161
+ // the spec because the fee spike buffer requirement doesn't exist on the receiver's
6162
+ // side, only on the sender's. Note that with anchor outputs we are no longer as
6163
+ // sensitive to fee spikes, so we need to account for them.
6164
+ let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
6165
+ let mut remote_fee_cost_incl_stuck_buffer_msat = self.context.next_remote_commit_tx_fee_msat(htlc_candidate, Some(()));
6166
+ if !self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
6167
+ remote_fee_cost_incl_stuck_buffer_msat *= FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
6168
+ }
6169
+ 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 {
6170
+ log_info!(logger, "Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", &self.context.channel_id());
6171
+ return Err(("Fee spike buffer violation", 0x1000|7));
6172
+ }
6173
+ }
6174
+
6175
+ Ok(())
6176
+ }
6177
+
6151
6178
pub fn get_cur_holder_commitment_transaction_number(&self) -> u64 {
6152
6179
self.context.cur_holder_commitment_transaction_number + 1
6153
6180
}
0 commit comments