Skip to content

Commit c2851ea

Browse files
committed
Fallback close_channel_internal to force close unfunded channels
1 parent f27515d commit c2851ea

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,48 +2380,58 @@ where
23802380

23812381
let mut failed_htlcs: Vec<(HTLCSource, PaymentHash)>;
23822382
let result: Result<(), _> = loop {
2383-
let per_peer_state = self.per_peer_state.read().unwrap();
2383+
{
2384+
let per_peer_state = self.per_peer_state.read().unwrap();
23842385

2385-
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
2386-
.ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
2386+
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
2387+
.ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
23872388

2388-
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
2389-
let peer_state = &mut *peer_state_lock;
2390-
match peer_state.channel_by_id.entry(channel_id.clone()) {
2391-
hash_map::Entry::Occupied(mut chan_entry) => {
2392-
let funding_txo_opt = chan_entry.get().context.get_funding_txo();
2393-
let their_features = &peer_state.latest_features;
2394-
let (shutdown_msg, mut monitor_update_opt, htlcs) = chan_entry.get_mut()
2395-
.get_shutdown(&self.signer_provider, their_features, target_feerate_sats_per_1000_weight, override_shutdown_script)?;
2396-
failed_htlcs = htlcs;
2397-
2398-
// We can send the `shutdown` message before updating the `ChannelMonitor`
2399-
// here as we don't need the monitor update to complete until we send a
2400-
// `shutdown_signed`, which we'll delay if we're pending a monitor update.
2401-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
2402-
node_id: *counterparty_node_id,
2403-
msg: shutdown_msg,
2404-
});
2389+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
2390+
let peer_state = &mut *peer_state_lock;
24052391

2406-
// Update the monitor with the shutdown script if necessary.
2407-
if let Some(monitor_update) = monitor_update_opt.take() {
2408-
break handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
2409-
peer_state_lock, peer_state, per_peer_state, chan_entry).map(|_| ());
2410-
}
2392+
match peer_state.channel_by_id.entry(channel_id.clone()) {
2393+
hash_map::Entry::Occupied(mut chan_entry) => {
2394+
let funding_txo_opt = chan_entry.get().context.get_funding_txo();
2395+
let their_features = &peer_state.latest_features;
2396+
let (shutdown_msg, mut monitor_update_opt, htlcs) = chan_entry.get_mut()
2397+
.get_shutdown(&self.signer_provider, their_features, target_feerate_sats_per_1000_weight, override_shutdown_script)?;
2398+
failed_htlcs = htlcs;
24112399

2412-
if chan_entry.get().is_shutdown() {
2413-
let channel = remove_channel!(self, chan_entry);
2414-
if let Ok(channel_update) = self.get_channel_update_for_broadcast(&channel) {
2415-
peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
2416-
msg: channel_update
2417-
});
2400+
// We can send the `shutdown` message before updating the `ChannelMonitor`
2401+
// here as we don't need the monitor update to complete until we send a
2402+
// `shutdown_signed`, which we'll delay if we're pending a monitor update.
2403+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
2404+
node_id: *counterparty_node_id,
2405+
msg: shutdown_msg,
2406+
});
2407+
2408+
// Update the monitor with the shutdown script if necessary.
2409+
if let Some(monitor_update) = monitor_update_opt.take() {
2410+
break handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
2411+
peer_state_lock, peer_state, per_peer_state, chan_entry).map(|_| ());
24182412
}
2419-
self.issue_channel_close_events(&channel.context, ClosureReason::HolderForceClosed);
2420-
}
2421-
break Ok(());
2422-
},
2423-
hash_map::Entry::Vacant(_) => return Err(APIError::ChannelUnavailable{err: format!("Channel with id {} not found for the passed counterparty node_id {}", log_bytes!(*channel_id), counterparty_node_id) })
2413+
2414+
if chan_entry.get().is_shutdown() {
2415+
let channel = remove_channel!(self, chan_entry);
2416+
if let Ok(channel_update) = self.get_channel_update_for_broadcast(&channel) {
2417+
peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
2418+
msg: channel_update
2419+
});
2420+
}
2421+
self.issue_channel_close_events(&channel.context, ClosureReason::HolderForceClosed);
2422+
}
2423+
break Ok(());
2424+
},
2425+
hash_map::Entry::Vacant(_) => (),
2426+
}
24242427
}
2428+
// If we reach this point, it means that the channel_id either refers to an unfunded channel or
2429+
// it does not exist for this peer. Either way, we can attempt to force-close it.
2430+
//
2431+
// An appropriate error will be returned for non-existence of the channel if that's the case.
2432+
return self.force_close_channel_with_peer(&channel_id, counterparty_node_id, None, false).map(|_| ())
2433+
// TODO(dunxen): This is still not ideal as we're doing some extra lookups.
2434+
// Fix this with https://github.com/lightningdevkit/rust-lightning/issues/2422
24252435
};
24262436

24272437
for htlc_source in failed_htlcs.drain(..) {

0 commit comments

Comments
 (0)