@@ -3093,7 +3093,7 @@ where
3093
3093
3094
3094
fn can_forward_htlc_to_outgoing_channel(
3095
3095
&self, chan: &mut Channel<SP>, msg: &msgs::UpdateAddHTLC, next_packet: &NextPacketDetails
3096
- ) -> Result<Option<msgs::ChannelUpdate> , (&'static str, u16, Option<msgs::ChannelUpdate>)> {
3096
+ ) -> Result<() , (&'static str, u16, Option<msgs::ChannelUpdate>)> {
3097
3097
if !chan.context.should_announce() && !self.default_configuration.accept_forwards_to_priv_channels {
3098
3098
// Note that the behavior here should be identical to the above block - we
3099
3099
// should NOT reveal the existence or non-existence of a private channel if
@@ -3106,7 +3106,6 @@ where
3106
3106
// we don't have the channel here.
3107
3107
return Err(("Refusing to forward over real channel SCID as our counterparty requested.", 0x4000 | 10, None));
3108
3108
}
3109
- let chan_update_opt = self.get_channel_update_for_onion(next_packet.outgoing_scid, chan).ok();
3110
3109
3111
3110
// Note that we could technically not return an error yet here and just hope
3112
3111
// that the connection is reestablished or monitor updated by the time we get
@@ -3117,20 +3116,85 @@ where
3117
3116
// If the channel_update we're going to return is disabled (i.e. the
3118
3117
// peer has been disabled for some time), return `channel_disabled`,
3119
3118
// otherwise return `temporary_channel_failure`.
3119
+ let chan_update_opt = self.get_channel_update_for_onion(next_packet.outgoing_scid, chan).ok();
3120
3120
if chan_update_opt.as_ref().map(|u| u.contents.flags & 2 == 2).unwrap_or(false) {
3121
3121
return Err(("Forwarding channel has been disconnected for some time.", 0x1000 | 20, chan_update_opt));
3122
3122
} else {
3123
3123
return Err(("Forwarding channel is not in a ready state.", 0x1000 | 7, chan_update_opt));
3124
3124
}
3125
3125
}
3126
3126
if next_packet.outgoing_amt_msat < chan.context.get_counterparty_htlc_minimum_msat() { // amount_below_minimum
3127
+ let chan_update_opt = self.get_channel_update_for_onion(next_packet.outgoing_scid, chan).ok();
3127
3128
return Err(("HTLC amount was below the htlc_minimum_msat", 0x1000 | 11, chan_update_opt));
3128
3129
}
3129
3130
if let Err((err, code)) = chan.htlc_satisfies_config(msg, next_packet.outgoing_amt_msat, next_packet.outgoing_cltv_value) {
3131
+ let chan_update_opt = self.get_channel_update_for_onion(next_packet.outgoing_scid, chan).ok();
3130
3132
return Err((err, code, chan_update_opt));
3131
3133
}
3132
3134
3133
- Ok(chan_update_opt)
3135
+ Ok(())
3136
+ }
3137
+
3138
+ fn do_channel_callback<X, C: Fn(&mut Channel<SP>) -> X>(
3139
+ &self, scid: u64, callback: C,
3140
+ ) -> Option<X> {
3141
+ let (counterparty_node_id, channel_id) = match self.short_to_chan_info.read().unwrap().get(&scid).cloned() {
3142
+ None => return None,
3143
+ Some((cp_id, id)) => (cp_id, id),
3144
+ };
3145
+ let per_peer_state = self.per_peer_state.read().unwrap();
3146
+ let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
3147
+ if peer_state_mutex_opt.is_none() {
3148
+ return None;
3149
+ }
3150
+ let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
3151
+ let peer_state = &mut *peer_state_lock;
3152
+ match peer_state.channel_by_id.get_mut(&channel_id).map(
3153
+ |chan_phase| if let ChannelPhase::Funded(chan) = chan_phase { Some(chan) } else { None }
3154
+ ).flatten() {
3155
+ None => None,
3156
+ Some(chan) => Some(callback(chan)),
3157
+ }
3158
+ }
3159
+
3160
+ fn can_forward_htlc(
3161
+ &self, msg: &msgs::UpdateAddHTLC, next_packet_details: &NextPacketDetails
3162
+ ) -> Result<(), (&'static str, u16, Option<msgs::ChannelUpdate>)> {
3163
+ match self.do_channel_callback(next_packet_details.outgoing_scid, |chan: &mut Channel<SP>| {
3164
+ self.can_forward_htlc_to_outgoing_channel(chan, msg, next_packet_details)
3165
+ }) {
3166
+ Some(Ok(())) => {},
3167
+ Some(Err(e)) => return Err(e),
3168
+ None => {
3169
+ // If we couldn't find the channel info for the sicd, it may be a phantom or
3170
+ // intercept forward.
3171
+ if (self.default_configuration.accept_intercept_htlcs &&
3172
+ fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, next_packet_details.outgoing_scid, &self.chain_hash)) ||
3173
+ fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, next_packet_details.outgoing_scid, &self.chain_hash)
3174
+ {} else {
3175
+ return Err(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
3176
+ }
3177
+ }
3178
+ }
3179
+
3180
+ let cur_height = self.best_block.read().unwrap().height + 1;
3181
+ if let Err((err_msg, err_code)) = check_incoming_htlc_cltv(
3182
+ cur_height, next_packet_details.outgoing_cltv_value, msg.cltv_expiry
3183
+ ) {
3184
+ let chan_update_opt = self.do_channel_callback(next_packet_details.outgoing_scid, |chan: &mut Channel<SP>| {
3185
+ self.get_channel_update_for_onion(next_packet_details.outgoing_scid, chan).ok()
3186
+ }).flatten();
3187
+ if err_code & 0x1000 != 0 && chan_update_opt.is_none() {
3188
+ // We really should set `incorrect_cltv_expiry` here but as we're not
3189
+ // forwarding over a real channel we can't generate a channel_update
3190
+ // for it. Instead we just return a generic temporary_node_failure.
3191
+ return Err((err_msg, 0x2000 | 2, None));
3192
+ }
3193
+ let chan_update_opt = if err_code & 0x1000 != 0 { chan_update_opt } else { None };
3194
+ return Err((err_msg, err_code, chan_update_opt));
3195
+ }
3196
+
3197
+ Ok(())
3134
3198
}
3135
3199
3136
3200
fn htlc_failure_from_update_add_err(
@@ -3205,71 +3269,14 @@ where
3205
3269
3206
3270
// Perform outbound checks here instead of in [`Self::construct_pending_htlc_info`] because we
3207
3271
// can't hold the outbound peer state lock at the same time as the inbound peer state lock.
3208
- if let Some((err, code, chan_update)) = loop {
3209
- let id_option = self.short_to_chan_info.read().unwrap().get(&next_packet_details.outgoing_scid).cloned();
3210
- let forwarding_chan_info_opt = match id_option {
3211
- None => { // unknown_next_peer
3212
- // Note that this is likely a timing oracle for detecting whether an scid is a
3213
- // phantom or an intercept.
3214
- if (self.default_configuration.accept_intercept_htlcs &&
3215
- fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, next_packet_details.outgoing_scid, &self.chain_hash)) ||
3216
- fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, next_packet_details.outgoing_scid, &self.chain_hash)
3217
- {
3218
- None
3219
- } else {
3220
- break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
3221
- }
3222
- },
3223
- Some((cp_id, id)) => Some((cp_id.clone(), id.clone())),
3224
- };
3225
- let chan_update_opt = if let Some((counterparty_node_id, forwarding_id)) = forwarding_chan_info_opt {
3226
- let per_peer_state = self.per_peer_state.read().unwrap();
3227
- let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
3228
- if peer_state_mutex_opt.is_none() {
3229
- break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
3230
- }
3231
- let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
3232
- let peer_state = &mut *peer_state_lock;
3233
- let chan = match peer_state.channel_by_id.get_mut(&forwarding_id).map(
3234
- |chan_phase| if let ChannelPhase::Funded(chan) = chan_phase { Some(chan) } else { None }
3235
- ).flatten() {
3236
- None => {
3237
- // Channel was removed. The short_to_chan_info and channel_by_id maps
3238
- // have no consistency guarantees.
3239
- break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
3240
- },
3241
- Some(chan) => chan
3242
- };
3243
- match self.can_forward_htlc_to_outgoing_channel(chan, msg, &next_packet_details) {
3244
- Ok(chan_update_opt) => chan_update_opt,
3245
- Err(e) => break Some(e),
3246
- }
3247
- } else {
3248
- None
3249
- };
3250
-
3251
- let cur_height = self.best_block.read().unwrap().height + 1;
3252
-
3253
- if let Err((err_msg, code)) = check_incoming_htlc_cltv(
3254
- cur_height, next_packet_details.outgoing_cltv_value, msg.cltv_expiry
3255
- ) {
3256
- if code & 0x1000 != 0 && chan_update_opt.is_none() {
3257
- // We really should set `incorrect_cltv_expiry` here but as we're not
3258
- // forwarding over a real channel we can't generate a channel_update
3259
- // for it. Instead we just return a generic temporary_node_failure.
3260
- break Some((err_msg, 0x2000 | 2, None))
3261
- }
3262
- let chan_update_opt = if code & 0x1000 != 0 { chan_update_opt } else { None };
3263
- break Some((err_msg, code, chan_update_opt));
3264
- }
3272
+ let _ = self.can_forward_htlc(&msg, &next_packet_details).map_err(|e| {
3273
+ let (err_msg, err_code, chan_update_opt) = e;
3274
+ self.htlc_failure_from_update_add_err(
3275
+ msg, counterparty_node_id, err_msg, err_code, chan_update_opt,
3276
+ next_hop.is_intro_node_blinded_forward(), &shared_secret
3277
+ )
3278
+ })?;
3265
3279
3266
- break None;
3267
- }
3268
- {
3269
- return Err(self.htlc_failure_from_update_add_err(
3270
- msg, counterparty_node_id, err, code, chan_update, next_hop.is_intro_node_blinded_forward(), &shared_secret
3271
- ));
3272
- }
3273
3280
Ok((next_hop, shared_secret, Some(next_packet_details.next_packet_pubkey)))
3274
3281
}
3275
3282
0 commit comments