Skip to content

Commit ee5b8c7

Browse files
committed
Move DiscardFunding generation into finish_close_channel
Currently the channel shutdown sequence has a number of steps which all the shutdown callsites have to call. Because many shutdown cases are rare error cases, its relatively easy to miss a call and leave users without `Event`s or miss some important cleanup. One of those steps, calling `issue_channel_close_events`, is rather easy to remove, as it only generates two events, which can simply be moved to another shutdown step. Here we move the first of the two events, `DiscardFunding`, into `finish_force_close_channel`.
1 parent 396c36b commit ee5b8c7

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

lightning/src/ln/channel.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ pub(crate) struct ShutdownResult {
823823
pub(crate) unbroadcasted_batch_funding_txid: Option<Txid>,
824824
pub(crate) channel_id: ChannelId,
825825
pub(crate) counterparty_node_id: PublicKey,
826+
pub(crate) unbroadcasted_funding_tx: Option<Transaction>,
826827
}
827828

828829
/// If the majority of the channels funds are to the fundee and the initiator holds only just
@@ -2402,6 +2403,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24022403
} else { None }
24032404
} else { None };
24042405
let unbroadcasted_batch_funding_txid = self.unbroadcasted_batch_funding_txid();
2406+
let unbroadcasted_funding_tx = self.unbroadcasted_funding();
24052407

24062408
self.channel_state = ChannelState::ShutdownComplete;
24072409
self.update_time_counter += 1;
@@ -2411,6 +2413,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24112413
unbroadcasted_batch_funding_txid,
24122414
channel_id: self.channel_id,
24132415
counterparty_node_id: self.counterparty_node_id,
2416+
unbroadcasted_funding_tx,
24142417
}
24152418
}
24162419

@@ -4943,6 +4946,7 @@ impl<SP: Deref> Channel<SP> where
49434946
unbroadcasted_batch_funding_txid: self.context.unbroadcasted_batch_funding_txid(),
49444947
channel_id: self.context.channel_id,
49454948
counterparty_node_id: self.context.counterparty_node_id,
4949+
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
49464950
};
49474951
let tx = self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig);
49484952
self.context.channel_state = ChannelState::ShutdownComplete;
@@ -4973,6 +4977,7 @@ impl<SP: Deref> Channel<SP> where
49734977
unbroadcasted_batch_funding_txid: self.context.unbroadcasted_batch_funding_txid(),
49744978
channel_id: self.context.channel_id,
49754979
counterparty_node_id: self.context.counterparty_node_id,
4980+
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
49764981
};
49774982
self.context.channel_state = ChannelState::ShutdownComplete;
49784983
self.context.update_time_counter += 1;

lightning/src/ln/channelmanager.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,14 +2704,6 @@ where
27042704
/// Helper function that issues the channel close events
27052705
fn issue_channel_close_events(&self, context: &ChannelContext<SP>, closure_reason: ClosureReason) {
27062706
let mut pending_events_lock = self.pending_events.lock().unwrap();
2707-
match context.unbroadcasted_funding() {
2708-
Some(transaction) => {
2709-
pending_events_lock.push_back((events::Event::DiscardFunding {
2710-
channel_id: context.channel_id(), transaction
2711-
}, None));
2712-
},
2713-
None => {},
2714-
}
27152707
pending_events_lock.push_back((events::Event::ChannelClosed {
27162708
channel_id: context.channel_id(),
27172709
user_channel_id: context.get_user_id(),
@@ -2897,6 +2889,15 @@ where
28972889
"Closing a batch where all channels have completed initial monitor update",
28982890
);
28992891
}
2892+
2893+
{
2894+
let mut pending_events = self.pending_events.lock().unwrap();
2895+
if let Some(transaction) = shutdown_res.unbroadcasted_funding_tx {
2896+
pending_events.push_back((events::Event::DiscardFunding {
2897+
channel_id: shutdown_res.channel_id, transaction
2898+
}, None));
2899+
}
2900+
}
29002901
for shutdown_result in shutdown_results.drain(..) {
29012902
self.finish_close_channel(shutdown_result);
29022903
}

0 commit comments

Comments
 (0)