Skip to content

Commit

Permalink
Move pre-funded-channel immediate shutdown logic to the right place
Browse files Browse the repository at this point in the history
Because a `Funded` `Channel` cannot possibly be pre-funding, the
logic in `ChannelManager::close_channel_internal` to handle
pre-funding channels is in the wrong place.

Rather than being handled inside the `Funded` branch, it should be
in an `else` following it, handling either of the two
`ChannelPhases` outside of `Funded`.

Sadly, because of a previous control flow management `loop {}`, the
existing code will infinite loop, which is fixed here.
  • Loading branch information
TheBlueMatt committed Nov 29, 2023
1 parent 3a67c81 commit 73b20e2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
25 changes: 9 additions & 16 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2603,7 +2603,8 @@ where

let mut failed_htlcs: Vec<(HTLCSource, PaymentHash)>;
let mut shutdown_result = None;
loop {

{
let per_peer_state = self.per_peer_state.read().unwrap();

let peer_state_mutex = per_peer_state.get(counterparty_node_id)
Expand All @@ -2614,10 +2615,11 @@ where

match peer_state.channel_by_id.entry(channel_id.clone()) {
hash_map::Entry::Occupied(mut chan_phase_entry) => {
let unbroadcasted_batch_funding_txid =
chan_phase_entry.get().context().unbroadcasted_batch_funding_txid();
if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
let funding_txo_opt = chan.context.get_funding_txo();
let their_features = &peer_state.latest_features;
let unbroadcasted_batch_funding_txid = chan.context.unbroadcasted_batch_funding_txid();
let (shutdown_msg, mut monitor_update_opt, htlcs) =
chan.get_shutdown(&self.signer_provider, their_features, target_feerate_sats_per_1000_weight, override_shutdown_script)?;
failed_htlcs = htlcs;
Expand All @@ -2637,21 +2639,12 @@ where
if let Some(monitor_update) = monitor_update_opt.take() {
handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
peer_state_lock, peer_state, per_peer_state, chan);
break;
}

if chan.is_shutdown() {
if let ChannelPhase::Funded(chan) = remove_channel_phase!(self, chan_phase_entry) {
if let Ok(channel_update) = self.get_channel_update_for_broadcast(&chan) {
peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
msg: channel_update
});
}
self.issue_channel_close_events(&chan.context, ClosureReason::HolderForceClosed);
shutdown_result = Some((None, Vec::new(), unbroadcasted_batch_funding_txid));
}
}
break;
} else {
self.issue_channel_close_events(chan_phase_entry.get().context(), ClosureReason::HolderForceClosed);
failed_htlcs = Vec::new();
remove_channel_phase!(self, chan_phase_entry);
shutdown_result = Some((None, Vec::new(), unbroadcasted_batch_funding_txid));
}
},
hash_map::Entry::Vacant(_) => {
Expand Down
15 changes: 15 additions & 0 deletions lightning/src/ln/shutdown_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,21 @@ fn shutdown_on_unfunded_channel() {
check_closed_event!(nodes[0], 1, ClosureReason::CounterpartyCoopClosedUnfundedChannel, [nodes[1].node.get_our_node_id()], 1_000_000);
}

#[test]
fn close_on_unfunded_channel() {
// Test the user asking us to close prior to funding generation
let chanmon_cfgs = create_chanmon_cfgs(2);
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);

let chan_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1_000_000, 100_000, 0, None).unwrap();
let open_chan = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());

nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 1_000_000);
}

#[test]
fn expect_channel_shutdown_state_with_force_closure() {
// Test sending a shutdown prior to channel_ready after funding generation
Expand Down

0 comments on commit 73b20e2

Please sign in to comment.