Skip to content

Commit b330f78

Browse files
committed
Force close pending channels in internal_shutdown
1 parent af19599 commit b330f78

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
@@ -5402,38 +5402,48 @@ where
54025402
})?;
54035403
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
54045404
let peer_state = &mut *peer_state_lock;
5405-
match peer_state.channel_by_id.entry(msg.channel_id.clone()) {
5406-
hash_map::Entry::Occupied(mut chan_entry) => {
5405+
if let hash_map::Entry::Occupied(chan_entry) = peer_state.outbound_v1_channel_by_id.entry(msg.channel_id.clone()) {
5406+
log_error!(self.logger, "Force-closing channel {}", log_bytes!(&msg.channel_id[..]));
5407+
self.issue_channel_close_events(&chan_entry.get().context, ClosureReason::HolderForceClosed);
5408+
let mut chan = remove_channel!(self, chan_entry);
5409+
self.finish_force_close_channel(chan.context.force_shutdown(false));
5410+
return Ok(());
5411+
} else if let hash_map::Entry::Occupied(chan_entry) = peer_state.inbound_v1_channel_by_id.entry(msg.channel_id.clone()) {
5412+
log_error!(self.logger, "Force-closing channel {}", log_bytes!(&msg.channel_id[..]));
5413+
self.issue_channel_close_events(&chan_entry.get().context, ClosureReason::HolderForceClosed);
5414+
let mut chan = remove_channel!(self, chan_entry);
5415+
self.finish_force_close_channel(chan.context.force_shutdown(false));
5416+
return Ok(());
5417+
} else if let hash_map::Entry::Occupied(mut chan_entry) = peer_state.channel_by_id.entry(msg.channel_id.clone()) {
5418+
if !chan_entry.get().received_shutdown() {
5419+
log_info!(self.logger, "Received a shutdown message from our counterparty for channel {}{}.",
5420+
log_bytes!(msg.channel_id),
5421+
if chan_entry.get().sent_shutdown() { " after we initiated shutdown" } else { "" });
5422+
}
54075423

5408-
if !chan_entry.get().received_shutdown() {
5409-
log_info!(self.logger, "Received a shutdown message from our counterparty for channel {}{}.",
5410-
log_bytes!(msg.channel_id),
5411-
if chan_entry.get().sent_shutdown() { " after we initiated shutdown" } else { "" });
5412-
}
5424+
let funding_txo_opt = chan_entry.get().context.get_funding_txo();
5425+
let (shutdown, monitor_update_opt, htlcs) = try_chan_entry!(self,
5426+
chan_entry.get_mut().shutdown(&self.signer_provider, &peer_state.latest_features, &msg), chan_entry);
5427+
dropped_htlcs = htlcs;
54135428

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

5429-
// Update the monitor with the shutdown script if necessary.
5430-
if let Some(monitor_update) = monitor_update_opt {
5431-
break handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
5432-
peer_state_lock, peer_state, per_peer_state, chan_entry).map(|_| ());
5433-
}
5434-
break Ok(());
5435-
},
5436-
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))
5439+
// Update the monitor with the shutdown script if necessary.
5440+
if let Some(monitor_update) = monitor_update_opt {
5441+
break handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
5442+
peer_state_lock, peer_state, per_peer_state, chan_entry).map(|_| ());
5443+
}
5444+
break Ok(());
5445+
} else {
5446+
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))
54375447
}
54385448
};
54395449
for htlc_source in dropped_htlcs.drain(..) {

0 commit comments

Comments
 (0)