Skip to content

Commit c9ca69e

Browse files
committed
Force close pending channels in internal_shutdown
1 parent ecb44f4 commit c9ca69e

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

lightning/src/ln/channelmanager.rs

+40-30
Original file line numberDiff line numberDiff line change
@@ -5518,38 +5518,48 @@ where
55185518
})?;
55195519
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
55205520
let peer_state = &mut *peer_state_lock;
5521-
match peer_state.channel_by_id.entry(msg.channel_id.clone()) {
5522-
hash_map::Entry::Occupied(mut chan_entry) => {
5523-
5524-
if !chan_entry.get().received_shutdown() {
5525-
log_info!(self.logger, "Received a shutdown message from our counterparty for channel {}{}.",
5526-
log_bytes!(msg.channel_id),
5527-
if chan_entry.get().sent_shutdown() { " after we initiated shutdown" } else { "" });
5528-
}
5529-
5530-
let funding_txo_opt = chan_entry.get().context.get_funding_txo();
5531-
let (shutdown, monitor_update_opt, htlcs) = try_chan_entry!(self,
5532-
chan_entry.get_mut().shutdown(&self.signer_provider, &peer_state.latest_features, &msg), chan_entry);
5533-
dropped_htlcs = htlcs;
5521+
if let hash_map::Entry::Occupied(chan_entry) = peer_state.outbound_v1_channel_by_id.entry(msg.channel_id.clone()) {
5522+
log_error!(self.logger, "Force-closing channel {}", log_bytes!(&msg.channel_id[..]));
5523+
self.issue_channel_close_events(&chan_entry.get().context, ClosureReason::HolderForceClosed);
5524+
let mut chan = remove_channel!(self, chan_entry);
5525+
self.finish_force_close_channel(chan.context.force_shutdown(false));
5526+
return Ok(());
5527+
} else if let hash_map::Entry::Occupied(chan_entry) = peer_state.inbound_v1_channel_by_id.entry(msg.channel_id.clone()) {
5528+
log_error!(self.logger, "Force-closing channel {}", log_bytes!(&msg.channel_id[..]));
5529+
self.issue_channel_close_events(&chan_entry.get().context, ClosureReason::HolderForceClosed);
5530+
let mut chan = remove_channel!(self, chan_entry);
5531+
self.finish_force_close_channel(chan.context.force_shutdown(false));
5532+
return Ok(());
5533+
} else if let hash_map::Entry::Occupied(mut chan_entry) = peer_state.channel_by_id.entry(msg.channel_id.clone()) {
5534+
if !chan_entry.get().received_shutdown() {
5535+
log_info!(self.logger, "Received a shutdown message from our counterparty for channel {}{}.",
5536+
log_bytes!(msg.channel_id),
5537+
if chan_entry.get().sent_shutdown() { " after we initiated shutdown" } else { "" });
5538+
}
55345539

5535-
if let Some(msg) = shutdown {
5536-
// We can send the `shutdown` message before updating the `ChannelMonitor`
5537-
// here as we don't need the monitor update to complete until we send a
5538-
// `shutdown_signed`, which we'll delay if we're pending a monitor update.
5539-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
5540-
node_id: *counterparty_node_id,
5541-
msg,
5542-
});
5543-
}
5540+
let funding_txo_opt = chan_entry.get().context.get_funding_txo();
5541+
let (shutdown, monitor_update_opt, htlcs) = try_chan_entry!(self,
5542+
chan_entry.get_mut().shutdown(&self.signer_provider, &peer_state.latest_features, &msg), chan_entry);
5543+
dropped_htlcs = htlcs;
5544+
5545+
if let Some(msg) = shutdown {
5546+
// We can send the `shutdown` message before updating the `ChannelMonitor`
5547+
// here as we don't need the monitor update to complete until we send a
5548+
// `shutdown_signed`, which we'll delay if we're pending a monitor update.
5549+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
5550+
node_id: *counterparty_node_id,
5551+
msg,
5552+
});
5553+
}
55445554

5545-
// Update the monitor with the shutdown script if necessary.
5546-
if let Some(monitor_update) = monitor_update_opt {
5547-
break handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
5548-
peer_state_lock, peer_state, per_peer_state, chan_entry).map(|_| ());
5549-
}
5550-
break Ok(());
5551-
},
5552-
hash_map::Entry::Vacant(_) => 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))
5555+
// Update the monitor with the shutdown script if necessary.
5556+
if let Some(monitor_update) = monitor_update_opt {
5557+
break handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
5558+
peer_state_lock, peer_state, per_peer_state, chan_entry).map(|_| ());
5559+
}
5560+
break Ok(());
5561+
} else {
5562+
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))
55535563
}
55545564
};
55555565
for htlc_source in dropped_htlcs.drain(..) {

0 commit comments

Comments
 (0)