Skip to content

Commit 8d78577

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 bcba634 commit 8d78577

File tree

1 file changed

+53
-52
lines changed

1 file changed

+53
-52
lines changed

lightning/src/ln/channelmanager.rs

+53-52
Original file line numberDiff line numberDiff line change
@@ -2449,61 +2449,63 @@ where
24492449

24502450
let mut failed_htlcs: Vec<(HTLCSource, PaymentHash)>;
24512451
loop {
2452-
{
2453-
let per_peer_state = self.per_peer_state.read().unwrap();
2452+
let per_peer_state = self.per_peer_state.read().unwrap();
24542453

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

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

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

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

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

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+
});
24932493
}
2494+
self.issue_channel_close_events(&chan.context, ClosureReason::HolderForceClosed);
24942495
}
2495-
break;
24962496
}
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+
},
25002507
}
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+
}
25072509

25082510
for htlc_source in failed_htlcs.drain(..) {
25092511
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
@@ -5814,7 +5816,7 @@ where
58145816

58155817
fn internal_shutdown(&self, counterparty_node_id: &PublicKey, msg: &msgs::Shutdown) -> Result<(), MsgHandleErrInternal> {
58165818
let mut dropped_htlcs: Vec<(HTLCSource, PaymentHash)>;
5817-
let result: Result<(), _> = loop {
5819+
{
58185820
let per_peer_state = self.per_peer_state.read().unwrap();
58195821
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
58205822
.ok_or_else(|| {
@@ -5852,7 +5854,6 @@ where
58525854
handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
58535855
peer_state_lock, peer_state, per_peer_state, chan_phase_entry);
58545856
}
5855-
break Ok(());
58565857
},
58575858
ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::UnfundedOutboundV1(_) => {
58585859
let context = phase.context_mut();
@@ -5866,14 +5867,14 @@ where
58665867
} else {
58675868
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))
58685869
}
5869-
};
5870+
}
58705871
for htlc_source in dropped_htlcs.drain(..) {
58715872
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id: msg.channel_id };
58725873
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
58735874
self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
58745875
}
58755876

5876-
result
5877+
Ok(())
58775878
}
58785879

58795880
fn internal_closing_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::ClosingSigned) -> Result<(), MsgHandleErrInternal> {
@@ -6247,7 +6248,7 @@ where
62476248
}
62486249

62496250
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 = {
62516252
let per_peer_state = self.per_peer_state.read().unwrap();
62526253
let mut peer_state_lock = per_peer_state.get(counterparty_node_id)
62536254
.ok_or_else(|| {
@@ -6272,7 +6273,7 @@ where
62726273
handle_new_monitor_update!(self, funding_txo, monitor_update,
62736274
peer_state_lock, peer_state, per_peer_state, chan_phase_entry);
62746275
}
6275-
(htlcs_to_fail, Ok(()))
6276+
htlcs_to_fail
62766277
} else {
62776278
return try_chan_phase_entry!(self, Err(ChannelError::Close(
62786279
"Got a revoke_and_ack message for an unfunded channel!".into())), chan_phase_entry);
@@ -6282,7 +6283,7 @@ where
62826283
}
62836284
};
62846285
self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id, counterparty_node_id);
6285-
res
6286+
Ok(())
62866287
}
62876288

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

0 commit comments

Comments
 (0)