@@ -2449,61 +2449,63 @@ where
2449
2449
2450
2450
let mut failed_htlcs: Vec<(HTLCSource, PaymentHash)>;
2451
2451
loop {
2452
- {
2453
- let per_peer_state = self.per_peer_state.read().unwrap();
2452
+ let per_peer_state = self.per_peer_state.read().unwrap();
2454
2453
2455
- let peer_state_mutex = per_peer_state.get(counterparty_node_id)
2456
- .ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
2454
+ let peer_state_mutex = per_peer_state.get(counterparty_node_id)
2455
+ .ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
2457
2456
2458
- let mut peer_state_lock = peer_state_mutex.lock().unwrap();
2459
- let peer_state = &mut *peer_state_lock;
2457
+ let mut peer_state_lock = peer_state_mutex.lock().unwrap();
2458
+ let peer_state = &mut *peer_state_lock;
2460
2459
2461
- match peer_state.channel_by_id.entry(channel_id.clone()) {
2462
- hash_map::Entry::Occupied(mut chan_phase_entry) => {
2463
- if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
2464
- let funding_txo_opt = chan.context.get_funding_txo();
2465
- let their_features = &peer_state.latest_features;
2466
- let (shutdown_msg, mut monitor_update_opt, htlcs) =
2467
- chan.get_shutdown(&self.signer_provider, their_features, target_feerate_sats_per_1000_weight, override_shutdown_script)?;
2468
- failed_htlcs = htlcs;
2460
+ match peer_state.channel_by_id.entry(channel_id.clone()) {
2461
+ hash_map::Entry::Occupied(mut chan_phase_entry) => {
2462
+ if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
2463
+ let funding_txo_opt = chan.context.get_funding_txo();
2464
+ let their_features = &peer_state.latest_features;
2465
+ let (shutdown_msg, mut monitor_update_opt, htlcs) =
2466
+ chan.get_shutdown(&self.signer_provider, their_features, target_feerate_sats_per_1000_weight, override_shutdown_script)?;
2467
+ failed_htlcs = htlcs;
2468
+
2469
+ // We can send the `shutdown` message before updating the `ChannelMonitor`
2470
+ // here as we don't need the monitor update to complete until we send a
2471
+ // `shutdown_signed`, which we'll delay if we're pending a monitor update.
2472
+ peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
2473
+ node_id: *counterparty_node_id,
2474
+ msg: shutdown_msg,
2475
+ });
2469
2476
2470
- // We can send the `shutdown` message before updating the `ChannelMonitor`
2471
- // here as we don't need the monitor update to complete until we send a
2472
- // `shutdown_signed`, which we'll delay if we're pending a monitor update.
2473
- peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
2474
- node_id: *counterparty_node_id,
2475
- msg: shutdown_msg,
2476
- });
2477
+ debug_assert!(monitor_update_opt.is_none() || !chan.is_shutdown(),
2478
+ "Initializing shutdown shouldn't both generate a monitor update and fail the channel");
2477
2479
2478
- // Update the monitor with the shutdown script if necessary.
2479
- if let Some(monitor_update) = monitor_update_opt.take() {
2480
- handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
2481
- peer_state_lock, peer_state, per_peer_state, chan_phase_entry);
2482
- break;
2483
- }
2480
+ // Update the monitor with the shutdown script if necessary.
2481
+ if let Some(monitor_update) = monitor_update_opt.take() {
2482
+ handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
2483
+ peer_state_lock, peer_state, per_peer_state, chan_phase_entry);
2484
+ break;
2485
+ }
2484
2486
2485
- if chan.is_shutdown() {
2486
- if let ChannelPhase::Funded(chan) = remove_channel_phase!(self, chan_phase_entry) {
2487
- if let Ok(channel_update) = self.get_channel_update_for_broadcast(&chan) {
2488
- peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
2489
- msg: channel_update
2490
- });
2491
- }
2492
- self.issue_channel_close_events(&chan.context, ClosureReason::HolderForceClosed);
2487
+ if chan.is_shutdown() {
2488
+ if let ChannelPhase::Funded(chan) = remove_channel_phase!(self, chan_phase_entry) {
2489
+ if let Ok(channel_update) = self.get_channel_update_for_broadcast(&chan) {
2490
+ peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
2491
+ msg: channel_update
2492
+ });
2493
2493
}
2494
+ self.issue_channel_close_events(&chan.context, ClosureReason::HolderForceClosed);
2494
2495
}
2495
- break;
2496
2496
}
2497
- },
2498
- hash_map::Entry::Vacant(_) => (),
2499
- }
2497
+ break;
2498
+ }
2499
+ },
2500
+ hash_map::Entry::Vacant(_) => {
2501
+ // If we reach this point, it means that the channel_id either refers to an unfunded channel or
2502
+ // it does not exist for this peer. Either way, we can attempt to force-close it.
2503
+ //
2504
+ // An appropriate error will be returned for non-existence of the channel if that's the case.
2505
+ return self.force_close_channel_with_peer(&channel_id, counterparty_node_id, None, false).map(|_| ())
2506
+ },
2500
2507
}
2501
- // If we reach this point, it means that the channel_id either refers to an unfunded channel or
2502
- // it does not exist for this peer. Either way, we can attempt to force-close it.
2503
- //
2504
- // An appropriate error will be returned for non-existence of the channel if that's the case.
2505
- return self.force_close_channel_with_peer(&channel_id, counterparty_node_id, None, false).map(|_| ())
2506
- };
2508
+ }
2507
2509
2508
2510
for htlc_source in failed_htlcs.drain(..) {
2509
2511
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
@@ -5814,7 +5816,7 @@ where
5814
5816
5815
5817
fn internal_shutdown(&self, counterparty_node_id: &PublicKey, msg: &msgs::Shutdown) -> Result<(), MsgHandleErrInternal> {
5816
5818
let mut dropped_htlcs: Vec<(HTLCSource, PaymentHash)>;
5817
- let result: Result<(), _> = loop {
5819
+ {
5818
5820
let per_peer_state = self.per_peer_state.read().unwrap();
5819
5821
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
5820
5822
.ok_or_else(|| {
@@ -5852,7 +5854,6 @@ where
5852
5854
handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
5853
5855
peer_state_lock, peer_state, per_peer_state, chan_phase_entry);
5854
5856
}
5855
- break Ok(());
5856
5857
},
5857
5858
ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::UnfundedOutboundV1(_) => {
5858
5859
let context = phase.context_mut();
@@ -5866,14 +5867,14 @@ where
5866
5867
} else {
5867
5868
return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
5868
5869
}
5869
- };
5870
+ }
5870
5871
for htlc_source in dropped_htlcs.drain(..) {
5871
5872
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id: msg.channel_id };
5872
5873
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
5873
5874
self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
5874
5875
}
5875
5876
5876
- result
5877
+ Ok(())
5877
5878
}
5878
5879
5879
5880
fn internal_closing_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::ClosingSigned) -> Result<(), MsgHandleErrInternal> {
@@ -6247,7 +6248,7 @@ where
6247
6248
}
6248
6249
6249
6250
fn internal_revoke_and_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<(), MsgHandleErrInternal> {
6250
- let ( htlcs_to_fail, res) = {
6251
+ let htlcs_to_fail = {
6251
6252
let per_peer_state = self.per_peer_state.read().unwrap();
6252
6253
let mut peer_state_lock = per_peer_state.get(counterparty_node_id)
6253
6254
.ok_or_else(|| {
@@ -6272,7 +6273,7 @@ where
6272
6273
handle_new_monitor_update!(self, funding_txo, monitor_update,
6273
6274
peer_state_lock, peer_state, per_peer_state, chan_phase_entry);
6274
6275
}
6275
- ( htlcs_to_fail, Ok(()))
6276
+ htlcs_to_fail
6276
6277
} else {
6277
6278
return try_chan_phase_entry!(self, Err(ChannelError::Close(
6278
6279
"Got a revoke_and_ack message for an unfunded channel!".into())), chan_phase_entry);
@@ -6282,7 +6283,7 @@ where
6282
6283
}
6283
6284
};
6284
6285
self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id, counterparty_node_id);
6285
- res
6286
+ Ok(())
6286
6287
}
6287
6288
6288
6289
fn internal_update_fee(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFee) -> Result<(), MsgHandleErrInternal> {
0 commit comments