Skip to content

Commit a21a226

Browse files
committed
f re-DRY force-shutdown logic
1 parent 1a119fb commit a21a226

File tree

1 file changed

+24
-70
lines changed

1 file changed

+24
-70
lines changed

lightning/src/ln/channel.rs

Lines changed: 24 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5338,73 +5338,8 @@ where
53385338
self.unbroadcasted_funding_txid(funding).filter(|_| self.is_batch_funding())
53395339
}
53405340

5341-
/// Shuts down this channel (no more calls into this Channel may be made afterwards except
5342-
/// those explicitly stated to be allowed after shutdown, e.g. some simple getters).
5343-
///
5344-
/// Only allowed for channels which never been used (i.e. have never broadcasted their funding
5345-
/// transaction).
5346-
fn abandon_unfunded_chan(
5347-
&mut self, funding: &FundingScope, mut closure_reason: ClosureReason,
5348-
) -> ShutdownResult {
5349-
assert!(!matches!(self.channel_state, ChannelState::ShutdownComplete));
5350-
assert!(!self.is_funding_broadcast());
5351-
5352-
let unbroadcasted_batch_funding_txid = self.unbroadcasted_batch_funding_txid(funding);
5353-
let unbroadcasted_funding_tx = self.unbroadcasted_funding(funding);
5354-
5355-
let monitor_update = if let Some(funding_txo) = funding.get_funding_txo() {
5356-
// If we haven't yet exchanged funding signatures (ie channel_state < AwaitingChannelReady),
5357-
// returning a channel monitor update here would imply a channel monitor update before
5358-
// we even registered the channel monitor to begin with, which is invalid.
5359-
// Thus, if we aren't actually at a point where we could conceivably broadcast the
5360-
// funding transaction, don't return a funding txo (which prevents providing the
5361-
// monitor update to the user, even if we return one).
5362-
// See test_duplicate_chan_id and test_pre_lockin_no_chan_closed_update for more.
5363-
if !self.channel_state.is_pre_funded_state() {
5364-
self.latest_monitor_update_id += 1;
5365-
let update = ChannelMonitorUpdate {
5366-
update_id: self.latest_monitor_update_id,
5367-
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed {
5368-
should_broadcast: false,
5369-
}],
5370-
channel_id: Some(self.channel_id()),
5371-
};
5372-
Some((self.get_counterparty_node_id(), funding_txo, self.channel_id(), update))
5373-
} else {
5374-
None
5375-
}
5376-
} else {
5377-
None
5378-
};
5379-
5380-
if let ClosureReason::HolderForceClosed { ref mut broadcasted_latest_txn, .. } =
5381-
&mut closure_reason
5382-
{
5383-
*broadcasted_latest_txn = Some(false);
5384-
}
5385-
5386-
self.channel_state = ChannelState::ShutdownComplete;
5387-
self.update_time_counter += 1;
5388-
ShutdownResult {
5389-
closure_reason,
5390-
monitor_update,
5391-
dropped_outbound_htlcs: Vec::new(),
5392-
unbroadcasted_batch_funding_txid,
5393-
channel_id: self.channel_id,
5394-
user_channel_id: self.user_id,
5395-
channel_capacity_satoshis: funding.get_value_satoshis(),
5396-
counterparty_node_id: self.counterparty_node_id,
5397-
unbroadcasted_funding_tx,
5398-
is_manual_broadcast: self.is_manual_broadcast,
5399-
channel_funding_txo: funding.get_funding_txo(),
5400-
last_local_balance_msat: funding.value_to_self_msat,
5401-
}
5402-
}
5403-
5404-
/// Shuts down this Channel (no more calls into this Channel may be made afterwards except
5405-
/// those explicitly stated to be alowed after shutdown, e.g. some simple getters).
5406-
pub fn force_shutdown(
5407-
&mut self, funding: &FundingScope, mut closure_reason: ClosureReason,
5341+
pub fn do_force_shutdown(
5342+
&mut self, funding: &FundingScope, mut closure_reason: ClosureReason, broadcast: bool,
54085343
) -> ShutdownResult {
54095344
// Note that we MUST only generate a monitor update that indicates force-closure - we're
54105345
// called during initialization prior to the chain_monitor in the encompassing ChannelManager
@@ -5413,7 +5348,7 @@ where
54135348
assert!(!matches!(self.channel_state, ChannelState::ShutdownComplete));
54145349

54155350
if !self.is_funding_broadcast() {
5416-
return self.abandon_unfunded_chan(funding, closure_reason);
5351+
debug_assert!(!broadcast);
54175352
}
54185353

54195354
// We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
@@ -5446,7 +5381,7 @@ where
54465381
let update = ChannelMonitorUpdate {
54475382
update_id: self.latest_monitor_update_id,
54485383
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed {
5449-
should_broadcast: true,
5384+
should_broadcast: broadcast,
54505385
}],
54515386
channel_id: Some(self.channel_id()),
54525387
};
@@ -5463,7 +5398,7 @@ where
54635398
if let ClosureReason::HolderForceClosed { ref mut broadcasted_latest_txn, .. } =
54645399
&mut closure_reason
54655400
{
5466-
*broadcasted_latest_txn = Some(true);
5401+
*broadcasted_latest_txn = Some(broadcast);
54675402
}
54685403

54695404
self.channel_state = ChannelState::ShutdownComplete;
@@ -5484,6 +5419,25 @@ where
54845419
}
54855420
}
54865421

5422+
/// Shuts down this channel (no more calls into this Channel may be made afterwards except
5423+
/// those explicitly stated to be allowed after shutdown, e.g. some simple getters).
5424+
///
5425+
/// Only allowed for channels which never been used (i.e. have never broadcasted their funding
5426+
/// transaction).
5427+
fn abandon_unfunded_chan(
5428+
&mut self, funding: &FundingScope, closure_reason: ClosureReason,
5429+
) -> ShutdownResult {
5430+
self.do_force_shutdown(funding, closure_reason, false)
5431+
}
5432+
5433+
/// Shuts down this Channel (no more calls into this Channel may be made afterwards except
5434+
/// those explicitly stated to be alowed after shutdown, e.g. some simple getters).
5435+
pub fn force_shutdown(
5436+
&mut self, funding: &FundingScope, closure_reason: ClosureReason,
5437+
) -> ShutdownResult {
5438+
self.do_force_shutdown(funding, closure_reason, self.is_funding_broadcast())
5439+
}
5440+
54875441
/// Only allowed after [`FundingScope::channel_transaction_parameters`] is set.
54885442
#[rustfmt::skip]
54895443
fn get_funding_signed_msg<L: Deref>(

0 commit comments

Comments
 (0)