Skip to content

Commit ccf710d

Browse files
authored
Merge pull request #2809 from TheBlueMatt/2023-12-closing-event-cleanup-fixes
Clean Up Funding Error Handling and shutdown
2 parents f352d03 + 7bc2a14 commit ccf710d

File tree

4 files changed

+227
-116
lines changed

4 files changed

+227
-116
lines changed

lightning/src/ln/channel.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ pub(super) struct ReestablishResponses {
814814
/// The result of a shutdown that should be handled.
815815
#[must_use]
816816
pub(crate) struct ShutdownResult {
817+
pub(crate) closure_reason: ClosureReason,
817818
/// A channel monitor update to apply.
818819
pub(crate) monitor_update: Option<(PublicKey, OutPoint, ChannelMonitorUpdate)>,
819820
/// A list of dropped outbound HTLCs that can safely be failed backwards immediately.
@@ -822,7 +823,10 @@ pub(crate) struct ShutdownResult {
822823
/// propagated to the remainder of the batch.
823824
pub(crate) unbroadcasted_batch_funding_txid: Option<Txid>,
824825
pub(crate) channel_id: ChannelId,
826+
pub(crate) user_channel_id: u128,
827+
pub(crate) channel_capacity_satoshis: u64,
825828
pub(crate) counterparty_node_id: PublicKey,
829+
pub(crate) unbroadcasted_funding_tx: Option<Transaction>,
826830
}
827831

828832
/// If the majority of the channels funds are to the fundee and the initiator holds only just
@@ -2311,15 +2315,17 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23112315
res
23122316
}
23132317

2314-
fn if_unbroadcasted_funding<F, O>(&self, f: F) -> Option<O>
2315-
where F: Fn() -> Option<O> {
2318+
fn if_unbroadcasted_funding<F, O>(&self, f: F) -> Option<O> where F: Fn() -> Option<O> {
23162319
match self.channel_state {
23172320
ChannelState::FundingNegotiated => f(),
2318-
ChannelState::AwaitingChannelReady(flags) => if flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH) {
2319-
f()
2320-
} else {
2321-
None
2322-
},
2321+
ChannelState::AwaitingChannelReady(flags) =>
2322+
if flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH) ||
2323+
flags.is_set(FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS.into())
2324+
{
2325+
f()
2326+
} else {
2327+
None
2328+
},
23232329
_ => None,
23242330
}
23252331
}
@@ -2354,7 +2360,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23542360
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
23552361
/// Also returns the list of payment_hashes for channels which we can safely fail backwards
23562362
/// immediately (others we will have to allow to time out).
2357-
pub fn force_shutdown(&mut self, should_broadcast: bool) -> ShutdownResult {
2363+
pub fn force_shutdown(&mut self, should_broadcast: bool, closure_reason: ClosureReason) -> ShutdownResult {
23582364
// Note that we MUST only generate a monitor update that indicates force-closure - we're
23592365
// called during initialization prior to the chain_monitor in the encompassing ChannelManager
23602366
// being fully configured in some cases. Thus, its likely any monitor events we generate will
@@ -2395,15 +2401,20 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23952401
} else { None }
23962402
} else { None };
23972403
let unbroadcasted_batch_funding_txid = self.unbroadcasted_batch_funding_txid();
2404+
let unbroadcasted_funding_tx = self.unbroadcasted_funding();
23982405

23992406
self.channel_state = ChannelState::ShutdownComplete;
24002407
self.update_time_counter += 1;
24012408
ShutdownResult {
2409+
closure_reason,
24022410
monitor_update,
24032411
dropped_outbound_htlcs,
24042412
unbroadcasted_batch_funding_txid,
24052413
channel_id: self.channel_id,
2414+
user_channel_id: self.user_id,
2415+
channel_capacity_satoshis: self.channel_value_satoshis,
24062416
counterparty_node_id: self.counterparty_node_id,
2417+
unbroadcasted_funding_tx,
24072418
}
24082419
}
24092420

@@ -4931,11 +4942,15 @@ impl<SP: Deref> Channel<SP> where
49314942
if let Some((last_fee, sig)) = self.context.last_sent_closing_fee {
49324943
if last_fee == msg.fee_satoshis {
49334944
let shutdown_result = ShutdownResult {
4945+
closure_reason: ClosureReason::CooperativeClosure,
49344946
monitor_update: None,
49354947
dropped_outbound_htlcs: Vec::new(),
49364948
unbroadcasted_batch_funding_txid: self.context.unbroadcasted_batch_funding_txid(),
49374949
channel_id: self.context.channel_id,
4950+
user_channel_id: self.context.user_id,
4951+
channel_capacity_satoshis: self.context.channel_value_satoshis,
49384952
counterparty_node_id: self.context.counterparty_node_id,
4953+
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
49394954
};
49404955
let tx = self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig);
49414956
self.context.channel_state = ChannelState::ShutdownComplete;
@@ -4961,11 +4976,15 @@ impl<SP: Deref> Channel<SP> where
49614976
.map_err(|_| ChannelError::Close("External signer refused to sign closing transaction".to_owned()))?;
49624977
let (signed_tx, shutdown_result) = if $new_fee == msg.fee_satoshis {
49634978
let shutdown_result = ShutdownResult {
4979+
closure_reason: ClosureReason::CooperativeClosure,
49644980
monitor_update: None,
49654981
dropped_outbound_htlcs: Vec::new(),
49664982
unbroadcasted_batch_funding_txid: self.context.unbroadcasted_batch_funding_txid(),
49674983
channel_id: self.context.channel_id,
4984+
user_channel_id: self.context.user_id,
4985+
channel_capacity_satoshis: self.context.channel_value_satoshis,
49684986
counterparty_node_id: self.context.counterparty_node_id,
4987+
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
49694988
};
49704989
self.context.channel_state = ChannelState::ShutdownComplete;
49714990
self.context.update_time_counter += 1;

0 commit comments

Comments
 (0)