@@ -2520,61 +2520,63 @@ where
2520
2520
2521
2521
let mut failed_htlcs: Vec<(HTLCSource, PaymentHash)>;
2522
2522
loop {
2523
- {
2524
- let per_peer_state = self.per_peer_state.read().unwrap();
2523
+ let per_peer_state = self.per_peer_state.read().unwrap();
2525
2524
2526
- let peer_state_mutex = per_peer_state.get(counterparty_node_id)
2527
- .ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
2525
+ let peer_state_mutex = per_peer_state.get(counterparty_node_id)
2526
+ .ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
2528
2527
2529
- let mut peer_state_lock = peer_state_mutex.lock().unwrap();
2530
- let peer_state = &mut *peer_state_lock;
2528
+ let mut peer_state_lock = peer_state_mutex.lock().unwrap();
2529
+ let peer_state = &mut *peer_state_lock;
2531
2530
2532
- match peer_state.channel_by_id.entry(channel_id.clone()) {
2533
- hash_map::Entry::Occupied(mut chan_phase_entry) => {
2534
- if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
2535
- let funding_txo_opt = chan.context.get_funding_txo();
2536
- let their_features = &peer_state.latest_features;
2537
- let (shutdown_msg, mut monitor_update_opt, htlcs) =
2538
- chan.get_shutdown(&self.signer_provider, their_features, target_feerate_sats_per_1000_weight, override_shutdown_script)?;
2539
- failed_htlcs = htlcs;
2531
+ match peer_state.channel_by_id.entry(channel_id.clone()) {
2532
+ hash_map::Entry::Occupied(mut chan_phase_entry) => {
2533
+ if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
2534
+ let funding_txo_opt = chan.context.get_funding_txo();
2535
+ let their_features = &peer_state.latest_features;
2536
+ let (shutdown_msg, mut monitor_update_opt, htlcs) =
2537
+ chan.get_shutdown(&self.signer_provider, their_features, target_feerate_sats_per_1000_weight, override_shutdown_script)?;
2538
+ failed_htlcs = htlcs;
2539
+
2540
+ // We can send the `shutdown` message before updating the `ChannelMonitor`
2541
+ // here as we don't need the monitor update to complete until we send a
2542
+ // `shutdown_signed`, which we'll delay if we're pending a monitor update.
2543
+ peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
2544
+ node_id: *counterparty_node_id,
2545
+ msg: shutdown_msg,
2546
+ });
2540
2547
2541
- // We can send the `shutdown` message before updating the `ChannelMonitor`
2542
- // here as we don't need the monitor update to complete until we send a
2543
- // `shutdown_signed`, which we'll delay if we're pending a monitor update.
2544
- peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
2545
- node_id: *counterparty_node_id,
2546
- msg: shutdown_msg,
2547
- });
2548
+ debug_assert!(monitor_update_opt.is_none() || !chan.is_shutdown(),
2549
+ "We can't both complete shutdown and generate a monitor update");
2548
2550
2549
- // Update the monitor with the shutdown script if necessary.
2550
- if let Some(monitor_update) = monitor_update_opt.take() {
2551
- handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
2552
- peer_state_lock, peer_state, per_peer_state, chan_phase_entry);
2553
- break;
2554
- }
2551
+ // Update the monitor with the shutdown script if necessary.
2552
+ if let Some(monitor_update) = monitor_update_opt.take() {
2553
+ handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
2554
+ peer_state_lock, peer_state, per_peer_state, chan_phase_entry);
2555
+ break;
2556
+ }
2555
2557
2556
- if chan.is_shutdown() {
2557
- if let ChannelPhase::Funded(chan) = remove_channel_phase!(self, chan_phase_entry) {
2558
- if let Ok(channel_update) = self.get_channel_update_for_broadcast(&chan) {
2559
- peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
2560
- msg: channel_update
2561
- });
2562
- }
2563
- self.issue_channel_close_events(&chan.context, ClosureReason::HolderForceClosed);
2558
+ if chan.is_shutdown() {
2559
+ if let ChannelPhase::Funded(chan) = remove_channel_phase!(self, chan_phase_entry) {
2560
+ if let Ok(channel_update) = self.get_channel_update_for_broadcast(&chan) {
2561
+ peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
2562
+ msg: channel_update
2563
+ });
2564
2564
}
2565
+ self.issue_channel_close_events(&chan.context, ClosureReason::HolderForceClosed);
2565
2566
}
2566
- break;
2567
2567
}
2568
- },
2569
- hash_map::Entry::Vacant(_) => (),
2570
- }
2568
+ break;
2569
+ }
2570
+ },
2571
+ hash_map::Entry::Vacant(_) => {
2572
+ // If we reach this point, it means that the channel_id either refers to an unfunded channel or
2573
+ // it does not exist for this peer. Either way, we can attempt to force-close it.
2574
+ //
2575
+ // An appropriate error will be returned for non-existence of the channel if that's the case.
2576
+ return self.force_close_channel_with_peer(&channel_id, counterparty_node_id, None, false).map(|_| ())
2577
+ },
2571
2578
}
2572
- // If we reach this point, it means that the channel_id either refers to an unfunded channel or
2573
- // it does not exist for this peer. Either way, we can attempt to force-close it.
2574
- //
2575
- // An appropriate error will be returned for non-existence of the channel if that's the case.
2576
- return self.force_close_channel_with_peer(&channel_id, counterparty_node_id, None, false).map(|_| ())
2577
- };
2579
+ }
2578
2580
2579
2581
for htlc_source in failed_htlcs.drain(..) {
2580
2582
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
@@ -6016,7 +6018,7 @@ where
6016
6018
6017
6019
fn internal_shutdown(&self, counterparty_node_id: &PublicKey, msg: &msgs::Shutdown) -> Result<(), MsgHandleErrInternal> {
6018
6020
let mut dropped_htlcs: Vec<(HTLCSource, PaymentHash)>;
6019
- let result: Result<(), _> = loop {
6021
+ {
6020
6022
let per_peer_state = self.per_peer_state.read().unwrap();
6021
6023
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6022
6024
.ok_or_else(|| {
@@ -6054,7 +6056,6 @@ where
6054
6056
handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
6055
6057
peer_state_lock, peer_state, per_peer_state, chan_phase_entry);
6056
6058
}
6057
- break Ok(());
6058
6059
},
6059
6060
ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::UnfundedOutboundV1(_) => {
6060
6061
let context = phase.context_mut();
@@ -6068,14 +6069,14 @@ where
6068
6069
} else {
6069
6070
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))
6070
6071
}
6071
- };
6072
+ }
6072
6073
for htlc_source in dropped_htlcs.drain(..) {
6073
6074
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id: msg.channel_id };
6074
6075
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
6075
6076
self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
6076
6077
}
6077
6078
6078
- result
6079
+ Ok(())
6079
6080
}
6080
6081
6081
6082
fn internal_closing_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::ClosingSigned) -> Result<(), MsgHandleErrInternal> {
@@ -6456,7 +6457,7 @@ where
6456
6457
}
6457
6458
6458
6459
fn internal_revoke_and_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<(), MsgHandleErrInternal> {
6459
- let ( htlcs_to_fail, res) = {
6460
+ let htlcs_to_fail = {
6460
6461
let per_peer_state = self.per_peer_state.read().unwrap();
6461
6462
let mut peer_state_lock = per_peer_state.get(counterparty_node_id)
6462
6463
.ok_or_else(|| {
@@ -6481,7 +6482,7 @@ where
6481
6482
handle_new_monitor_update!(self, funding_txo, monitor_update,
6482
6483
peer_state_lock, peer_state, per_peer_state, chan_phase_entry);
6483
6484
}
6484
- ( htlcs_to_fail, Ok(()))
6485
+ htlcs_to_fail
6485
6486
} else {
6486
6487
return try_chan_phase_entry!(self, Err(ChannelError::Close(
6487
6488
"Got a revoke_and_ack message for an unfunded channel!".into())), chan_phase_entry);
@@ -6491,7 +6492,7 @@ where
6491
6492
}
6492
6493
};
6493
6494
self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id, counterparty_node_id);
6494
- res
6495
+ Ok(())
6495
6496
}
6496
6497
6497
6498
fn internal_update_fee(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFee) -> Result<(), MsgHandleErrInternal> {
0 commit comments