Skip to content

Commit 316bf5e

Browse files
committed
Force close pending channels in internal_shutdown
1 parent 3384a0d commit 316bf5e

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5397,38 +5397,48 @@ where
53975397
})?;
53985398
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
53995399
let peer_state = &mut *peer_state_lock;
5400-
match peer_state.channel_by_id.entry(msg.channel_id.clone()) {
5401-
hash_map::Entry::Occupied(mut chan_entry) => {
5400+
if let hash_map::Entry::Occupied(chan_entry) = peer_state.outbound_v1_channel_by_id.entry(msg.channel_id.clone()) {
5401+
log_error!(self.logger, "Force-closing channel {}", log_bytes!(&msg.channel_id[..]));
5402+
self.issue_channel_close_events(&chan_entry.get().context, ClosureReason::HolderForceClosed);
5403+
let mut chan = remove_channel!(self, chan_entry);
5404+
self.finish_force_close_channel(chan.context.force_shutdown(false));
5405+
return Ok(());
5406+
} else if let hash_map::Entry::Occupied(chan_entry) = peer_state.inbound_v1_channel_by_id.entry(msg.channel_id.clone()) {
5407+
log_error!(self.logger, "Force-closing channel {}", log_bytes!(&msg.channel_id[..]));
5408+
self.issue_channel_close_events(&chan_entry.get().context, ClosureReason::HolderForceClosed);
5409+
let mut chan = remove_channel!(self, chan_entry);
5410+
self.finish_force_close_channel(chan.context.force_shutdown(false));
5411+
return Ok(());
5412+
} else if let hash_map::Entry::Occupied(mut chan_entry) = peer_state.channel_by_id.entry(msg.channel_id.clone()) {
5413+
if !chan_entry.get().received_shutdown() {
5414+
log_info!(self.logger, "Received a shutdown message from our counterparty for channel {}{}.",
5415+
log_bytes!(msg.channel_id),
5416+
if chan_entry.get().sent_shutdown() { " after we initiated shutdown" } else { "" });
5417+
}
54025418

5403-
if !chan_entry.get().received_shutdown() {
5404-
log_info!(self.logger, "Received a shutdown message from our counterparty for channel {}{}.",
5405-
log_bytes!(msg.channel_id),
5406-
if chan_entry.get().sent_shutdown() { " after we initiated shutdown" } else { "" });
5407-
}
5419+
let funding_txo_opt = chan_entry.get().context.get_funding_txo();
5420+
let (shutdown, monitor_update_opt, htlcs) = try_chan_entry!(self,
5421+
chan_entry.get_mut().shutdown(&self.signer_provider, &peer_state.latest_features, &msg), chan_entry);
5422+
dropped_htlcs = htlcs;
54085423

5409-
let funding_txo_opt = chan_entry.get().context.get_funding_txo();
5410-
let (shutdown, monitor_update_opt, htlcs) = try_chan_entry!(self,
5411-
chan_entry.get_mut().shutdown(&self.signer_provider, &peer_state.latest_features, &msg), chan_entry);
5412-
dropped_htlcs = htlcs;
5413-
5414-
if let Some(msg) = shutdown {
5415-
// We can send the `shutdown` message before updating the `ChannelMonitor`
5416-
// here as we don't need the monitor update to complete until we send a
5417-
// `shutdown_signed`, which we'll delay if we're pending a monitor update.
5418-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
5419-
node_id: *counterparty_node_id,
5420-
msg,
5421-
});
5422-
}
5424+
if let Some(msg) = shutdown {
5425+
// We can send the `shutdown` message before updating the `ChannelMonitor`
5426+
// here as we don't need the monitor update to complete until we send a
5427+
// `shutdown_signed`, which we'll delay if we're pending a monitor update.
5428+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
5429+
node_id: *counterparty_node_id,
5430+
msg,
5431+
});
5432+
}
54235433

5424-
// Update the monitor with the shutdown script if necessary.
5425-
if let Some(monitor_update) = monitor_update_opt {
5426-
break handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
5427-
peer_state_lock, peer_state, per_peer_state, chan_entry).map(|_| ());
5428-
}
5429-
break Ok(());
5430-
},
5431-
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))
5434+
// Update the monitor with the shutdown script if necessary.
5435+
if let Some(monitor_update) = monitor_update_opt {
5436+
break handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
5437+
peer_state_lock, peer_state, per_peer_state, chan_entry).map(|_| ());
5438+
}
5439+
break Ok(());
5440+
} else {
5441+
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))
54325442
}
54335443
};
54345444
for htlc_source in dropped_htlcs.drain(..) {

0 commit comments

Comments
 (0)