Skip to content

Commit 7631b44

Browse files
committed
Clean up code flow in ChannelManager
In the previous commit various dead code was removed. Here we finish that cleanup by removing uneccessary indentation and syntax.
1 parent 32e21ed commit 7631b44

File tree

1 file changed

+53
-52
lines changed

1 file changed

+53
-52
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,61 +2520,63 @@ where
25202520

25212521
let mut failed_htlcs: Vec<(HTLCSource, PaymentHash)>;
25222522
loop {
2523-
{
2524-
let per_peer_state = self.per_peer_state.read().unwrap();
2523+
let per_peer_state = self.per_peer_state.read().unwrap();
25252524

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) })?;
25282527

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;
25312530

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+
});
25402547

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");
25482550

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+
}
25552557

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+
});
25642564
}
2565+
self.issue_channel_close_events(&chan.context, ClosureReason::HolderForceClosed);
25652566
}
2566-
break;
25672567
}
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+
},
25712578
}
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+
}
25782580

25792581
for htlc_source in failed_htlcs.drain(..) {
25802582
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
@@ -6016,7 +6018,7 @@ where
60166018

60176019
fn internal_shutdown(&self, counterparty_node_id: &PublicKey, msg: &msgs::Shutdown) -> Result<(), MsgHandleErrInternal> {
60186020
let mut dropped_htlcs: Vec<(HTLCSource, PaymentHash)>;
6019-
let result: Result<(), _> = loop {
6021+
{
60206022
let per_peer_state = self.per_peer_state.read().unwrap();
60216023
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
60226024
.ok_or_else(|| {
@@ -6054,7 +6056,6 @@ where
60546056
handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
60556057
peer_state_lock, peer_state, per_peer_state, chan_phase_entry);
60566058
}
6057-
break Ok(());
60586059
},
60596060
ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::UnfundedOutboundV1(_) => {
60606061
let context = phase.context_mut();
@@ -6068,14 +6069,14 @@ where
60686069
} else {
60696070
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))
60706071
}
6071-
};
6072+
}
60726073
for htlc_source in dropped_htlcs.drain(..) {
60736074
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id: msg.channel_id };
60746075
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
60756076
self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
60766077
}
60776078

6078-
result
6079+
Ok(())
60796080
}
60806081

60816082
fn internal_closing_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::ClosingSigned) -> Result<(), MsgHandleErrInternal> {
@@ -6456,7 +6457,7 @@ where
64566457
}
64576458

64586459
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 = {
64606461
let per_peer_state = self.per_peer_state.read().unwrap();
64616462
let mut peer_state_lock = per_peer_state.get(counterparty_node_id)
64626463
.ok_or_else(|| {
@@ -6481,7 +6482,7 @@ where
64816482
handle_new_monitor_update!(self, funding_txo, monitor_update,
64826483
peer_state_lock, peer_state, per_peer_state, chan_phase_entry);
64836484
}
6484-
(htlcs_to_fail, Ok(()))
6485+
htlcs_to_fail
64856486
} else {
64866487
return try_chan_phase_entry!(self, Err(ChannelError::Close(
64876488
"Got a revoke_and_ack message for an unfunded channel!".into())), chan_phase_entry);
@@ -6491,7 +6492,7 @@ where
64916492
}
64926493
};
64936494
self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id, counterparty_node_id);
6494-
res
6495+
Ok(())
64956496
}
64966497

64976498
fn internal_update_fee(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFee) -> Result<(), MsgHandleErrInternal> {

0 commit comments

Comments
 (0)