Skip to content

Commit 662a0c2

Browse files
committed
Force close pending channels in internal_shutdown
1 parent c4dc4d1 commit 662a0c2

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
@@ -5496,38 +5496,48 @@ where
54965496
})?;
54975497
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
54985498
let peer_state = &mut *peer_state_lock;
5499-
match peer_state.channel_by_id.entry(msg.channel_id.clone()) {
5500-
hash_map::Entry::Occupied(mut chan_entry) => {
5499+
if let hash_map::Entry::Occupied(chan_entry) = peer_state.outbound_v1_channel_by_id.entry(msg.channel_id.clone()) {
5500+
log_error!(self.logger, "Force-closing channel {}", log_bytes!(&msg.channel_id[..]));
5501+
self.issue_channel_close_events(&chan_entry.get().context, ClosureReason::HolderForceClosed);
5502+
let mut chan = remove_channel!(self, chan_entry);
5503+
self.finish_force_close_channel(chan.context.force_shutdown(false));
5504+
return Ok(());
5505+
} else if let hash_map::Entry::Occupied(chan_entry) = peer_state.inbound_v1_channel_by_id.entry(msg.channel_id.clone()) {
5506+
log_error!(self.logger, "Force-closing channel {}", log_bytes!(&msg.channel_id[..]));
5507+
self.issue_channel_close_events(&chan_entry.get().context, ClosureReason::HolderForceClosed);
5508+
let mut chan = remove_channel!(self, chan_entry);
5509+
self.finish_force_close_channel(chan.context.force_shutdown(false));
5510+
return Ok(());
5511+
} else if let hash_map::Entry::Occupied(mut chan_entry) = peer_state.channel_by_id.entry(msg.channel_id.clone()) {
5512+
if !chan_entry.get().received_shutdown() {
5513+
log_info!(self.logger, "Received a shutdown message from our counterparty for channel {}{}.",
5514+
log_bytes!(msg.channel_id),
5515+
if chan_entry.get().sent_shutdown() { " after we initiated shutdown" } else { "" });
5516+
}
55015517

5502-
if !chan_entry.get().received_shutdown() {
5503-
log_info!(self.logger, "Received a shutdown message from our counterparty for channel {}{}.",
5504-
log_bytes!(msg.channel_id),
5505-
if chan_entry.get().sent_shutdown() { " after we initiated shutdown" } else { "" });
5506-
}
5518+
let funding_txo_opt = chan_entry.get().context.get_funding_txo();
5519+
let (shutdown, monitor_update_opt, htlcs) = try_chan_entry!(self,
5520+
chan_entry.get_mut().shutdown(&self.signer_provider, &peer_state.latest_features, &msg), chan_entry);
5521+
dropped_htlcs = htlcs;
55075522

5508-
let funding_txo_opt = chan_entry.get().context.get_funding_txo();
5509-
let (shutdown, monitor_update_opt, htlcs) = try_chan_entry!(self,
5510-
chan_entry.get_mut().shutdown(&self.signer_provider, &peer_state.latest_features, &msg), chan_entry);
5511-
dropped_htlcs = htlcs;
5512-
5513-
if let Some(msg) = shutdown {
5514-
// We can send the `shutdown` message before updating the `ChannelMonitor`
5515-
// here as we don't need the monitor update to complete until we send a
5516-
// `shutdown_signed`, which we'll delay if we're pending a monitor update.
5517-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
5518-
node_id: *counterparty_node_id,
5519-
msg,
5520-
});
5521-
}
5523+
if let Some(msg) = shutdown {
5524+
// We can send the `shutdown` message before updating the `ChannelMonitor`
5525+
// here as we don't need the monitor update to complete until we send a
5526+
// `shutdown_signed`, which we'll delay if we're pending a monitor update.
5527+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
5528+
node_id: *counterparty_node_id,
5529+
msg,
5530+
});
5531+
}
55225532

5523-
// Update the monitor with the shutdown script if necessary.
5524-
if let Some(monitor_update) = monitor_update_opt {
5525-
break handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
5526-
peer_state_lock, peer_state, per_peer_state, chan_entry).map(|_| ());
5527-
}
5528-
break Ok(());
5529-
},
5530-
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))
5533+
// Update the monitor with the shutdown script if necessary.
5534+
if let Some(monitor_update) = monitor_update_opt {
5535+
break handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
5536+
peer_state_lock, peer_state, per_peer_state, chan_entry).map(|_| ());
5537+
}
5538+
break Ok(());
5539+
} else {
5540+
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))
55315541
}
55325542
};
55335543
for htlc_source in dropped_htlcs.drain(..) {

0 commit comments

Comments
 (0)