Skip to content

Commit 007d7e3

Browse files
committed
f pull out the force-close logic into a util method
1 parent c3e82aa commit 007d7e3

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3141,6 +3141,29 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31413141
self.finish_force_close_channel(failure);
31423142
}
31433143
}
3144+
3145+
/// Handle a list of channel failures during a block_connected or block_disconnected call,
3146+
/// pushing the channel monitor update (if any) to the background events queue and removing the
3147+
/// Channel object.
3148+
fn handle_init_event_channel_failures(&self, mut failed_channels: Vec<ShutdownResult>) {
3149+
for mut failure in failed_channels.drain(..) {
3150+
// Either a commitment transactions has been confirmed on-chain or
3151+
// Channel::block_disconnected detected that the funding transaction has been
3152+
// reorganized out of the main chain.
3153+
// We cannot broadcast our latest local state via monitor update (as
3154+
// Channel::force_shutdown tries to make us do) as we may still be in initialization,
3155+
// so we track the update internally and handle it when the user next calls
3156+
// timer_chan_freshness_every_min, guaranteeing we're running normally.
3157+
if let Some((funding_txo, update)) = failure.0.take() {
3158+
assert_eq!(update.updates.len(), 1);
3159+
if let ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast } = update.updates[0] {
3160+
assert!(should_broadcast);
3161+
} else { unreachable!(); }
3162+
self.pending_background_events.lock().unwrap().push(BackgroundEvent::ClosingMonitorUpdate((funding_txo, update)));
3163+
}
3164+
self.finish_force_close_channel(failure);
3165+
}
3166+
}
31443167
}
31453168

31463169
impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> MessageSendEventsProvider for ChannelManager<Signer, M, T, K, F, L>
@@ -3296,20 +3319,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32963319
!htlcs.is_empty() // Only retain this entry if htlcs has at least one entry.
32973320
});
32983321
}
3299-
for mut failure in failed_channels.drain(..) {
3300-
// It looks like our counterparty went on-chain. We cannot broadcast our latest local
3301-
// state via monitor update (as Channel::force_shutdown tries to make us do) as we may
3302-
// still be in initialization, so we track the update internally and handle it when the
3303-
// user next calls timer_chan_freshness_every_min, guaranteeing we're running normally.
3304-
if let Some((funding_txo, update)) = failure.0.take() {
3305-
assert_eq!(update.updates.len(), 1);
3306-
if let ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast } = update.updates[0] {
3307-
assert!(should_broadcast);
3308-
} else { unreachable!(); }
3309-
self.pending_background_events.lock().unwrap().push(BackgroundEvent::ClosingMonitorUpdate((funding_txo, update)));
3310-
}
3311-
self.finish_force_close_channel(failure);
3312-
}
3322+
3323+
self.handle_init_event_channel_failures(failed_channels);
33133324

33143325
for (source, payment_hash, reason) in timed_out_htlcs.drain(..) {
33153326
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), source, &payment_hash, reason);
@@ -3362,21 +3373,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
33623373
}
33633374
});
33643375
}
3365-
for mut failure in failed_channels.drain(..) {
3366-
// Channel::block_disconnected tells us to close if the funding transaction was
3367-
// un-confirmed due to a reorg. We cannot broadcast our latest local state via monitor
3368-
// update (as Channel::force_shutdown tries to make us do) as we may still be in
3369-
// initialization, so we track the update internally and handle it when the user next
3370-
// calls timer_chan_freshness_every_min, guaranteeing we're running normally.
3371-
if let Some((funding_txo, update)) = failure.0.take() {
3372-
assert_eq!(update.updates.len(), 1);
3373-
if let ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast } = update.updates[0] {
3374-
assert!(should_broadcast);
3375-
} else { unreachable!(); }
3376-
self.pending_background_events.lock().unwrap().push(BackgroundEvent::ClosingMonitorUpdate((funding_txo, update)));
3377-
}
3378-
self.finish_force_close_channel(failure);
3379-
}
3376+
self.handle_init_event_channel_failures(failed_channels);
33803377
self.latest_block_height.fetch_sub(1, Ordering::AcqRel);
33813378
*self.last_block_hash.try_lock().expect("block_(dis)connected must not be called in parallel") = header.block_hash();
33823379
}

0 commit comments

Comments
 (0)