Skip to content

Commit adbc1e3

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 d6d0359 commit adbc1e3

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
@@ -2399,6 +2400,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23992400
} else { None }
24002401
} else { None };
24012402
let unbroadcasted_batch_funding_txid = self.unbroadcasted_batch_funding_txid();
2403+
let unbroadcasted_funding_tx = self.unbroadcasted_funding();
24022404

24032405
self.channel_state = ChannelState::ShutdownComplete;
24042406
self.update_time_counter += 1;
@@ -2408,6 +2410,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24082410
unbroadcasted_batch_funding_txid,
24092411
channel_id: self.channel_id,
24102412
counterparty_node_id: self.counterparty_node_id,
2413+
unbroadcasted_funding_tx,
24112414
}
24122415
}
24132416

@@ -4940,6 +4943,7 @@ impl<SP: Deref> Channel<SP> where
49404943
unbroadcasted_batch_funding_txid: self.context.unbroadcasted_batch_funding_txid(),
49414944
channel_id: self.context.channel_id,
49424945
counterparty_node_id: self.context.counterparty_node_id,
4946+
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
49434947
};
49444948
let tx = self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig);
49454949
self.context.channel_state = ChannelState::ShutdownComplete;
@@ -4970,6 +4974,7 @@ impl<SP: Deref> Channel<SP> where
49704974
unbroadcasted_batch_funding_txid: self.context.unbroadcasted_batch_funding_txid(),
49714975
channel_id: self.context.channel_id,
49724976
counterparty_node_id: self.context.counterparty_node_id,
4977+
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
49734978
};
49744979
self.context.channel_state = ChannelState::ShutdownComplete;
49754980
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)