Skip to content

Commit 2ea27e0

Browse files
committed
Move Channel::force_shutdown to ChannelContext impl
1 parent baadeb7 commit 2ea27e0

File tree

2 files changed

+54
-54
lines changed

2 files changed

+54
-54
lines changed

lightning/src/ln/channel.rs

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,52 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
18741874
None
18751875
}
18761876
}
1877+
1878+
/// Gets the latest commitment transaction and any dependent transactions for relay (forcing
1879+
/// shutdown of this channel - no more calls into this Channel may be made afterwards except
1880+
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
1881+
/// Also returns the list of payment_hashes for channels which we can safely fail backwards
1882+
/// immediately (others we will have to allow to time out).
1883+
pub fn force_shutdown(&mut self, should_broadcast: bool) -> ShutdownResult {
1884+
// Note that we MUST only generate a monitor update that indicates force-closure - we're
1885+
// called during initialization prior to the chain_monitor in the encompassing ChannelManager
1886+
// being fully configured in some cases. Thus, its likely any monitor events we generate will
1887+
// be delayed in being processed! See the docs for `ChannelManagerReadArgs` for more.
1888+
assert!(self.channel_state != ChannelState::ShutdownComplete as u32);
1889+
1890+
// We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
1891+
// return them to fail the payment.
1892+
let mut dropped_outbound_htlcs = Vec::with_capacity(self.holding_cell_htlc_updates.len());
1893+
let counterparty_node_id = self.get_counterparty_node_id();
1894+
for htlc_update in self.holding_cell_htlc_updates.drain(..) {
1895+
match htlc_update {
1896+
HTLCUpdateAwaitingACK::AddHTLC { source, payment_hash, .. } => {
1897+
dropped_outbound_htlcs.push((source, payment_hash, counterparty_node_id, self.channel_id));
1898+
},
1899+
_ => {}
1900+
}
1901+
}
1902+
let monitor_update = if let Some(funding_txo) = self.get_funding_txo() {
1903+
// If we haven't yet exchanged funding signatures (ie channel_state < FundingSent),
1904+
// returning a channel monitor update here would imply a channel monitor update before
1905+
// we even registered the channel monitor to begin with, which is invalid.
1906+
// Thus, if we aren't actually at a point where we could conceivably broadcast the
1907+
// funding transaction, don't return a funding txo (which prevents providing the
1908+
// monitor update to the user, even if we return one).
1909+
// See test_duplicate_chan_id and test_pre_lockin_no_chan_closed_update for more.
1910+
if self.channel_state & (ChannelState::FundingSent as u32 | ChannelState::ChannelReady as u32 | ChannelState::ShutdownComplete as u32) != 0 {
1911+
self.latest_monitor_update_id = CLOSED_CHANNEL_UPDATE_ID;
1912+
Some((self.get_counterparty_node_id(), funding_txo, ChannelMonitorUpdate {
1913+
update_id: self.latest_monitor_update_id,
1914+
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast }],
1915+
}))
1916+
} else { None }
1917+
} else { None };
1918+
1919+
self.channel_state = ChannelState::ShutdownComplete as u32;
1920+
self.update_time_counter += 1;
1921+
(monitor_update, dropped_outbound_htlcs)
1922+
}
18771923
}
18781924

18791925
// Internal utility functions for channels
@@ -5823,52 +5869,6 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
58235869
Ok((shutdown, monitor_update, dropped_outbound_htlcs))
58245870
}
58255871

5826-
/// Gets the latest commitment transaction and any dependent transactions for relay (forcing
5827-
/// shutdown of this channel - no more calls into this Channel may be made afterwards except
5828-
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
5829-
/// Also returns the list of payment_hashes for channels which we can safely fail backwards
5830-
/// immediately (others we will have to allow to time out).
5831-
pub fn force_shutdown(&mut self, should_broadcast: bool) -> ShutdownResult {
5832-
// Note that we MUST only generate a monitor update that indicates force-closure - we're
5833-
// called during initialization prior to the chain_monitor in the encompassing ChannelManager
5834-
// being fully configured in some cases. Thus, its likely any monitor events we generate will
5835-
// be delayed in being processed! See the docs for `ChannelManagerReadArgs` for more.
5836-
assert!(self.context.channel_state != ChannelState::ShutdownComplete as u32);
5837-
5838-
// We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
5839-
// return them to fail the payment.
5840-
let mut dropped_outbound_htlcs = Vec::with_capacity(self.context.holding_cell_htlc_updates.len());
5841-
let counterparty_node_id = self.context.get_counterparty_node_id();
5842-
for htlc_update in self.context.holding_cell_htlc_updates.drain(..) {
5843-
match htlc_update {
5844-
HTLCUpdateAwaitingACK::AddHTLC { source, payment_hash, .. } => {
5845-
dropped_outbound_htlcs.push((source, payment_hash, counterparty_node_id, self.context.channel_id));
5846-
},
5847-
_ => {}
5848-
}
5849-
}
5850-
let monitor_update = if let Some(funding_txo) = self.context.get_funding_txo() {
5851-
// If we haven't yet exchanged funding signatures (ie channel_state < FundingSent),
5852-
// returning a channel monitor update here would imply a channel monitor update before
5853-
// we even registered the channel monitor to begin with, which is invalid.
5854-
// Thus, if we aren't actually at a point where we could conceivably broadcast the
5855-
// funding transaction, don't return a funding txo (which prevents providing the
5856-
// monitor update to the user, even if we return one).
5857-
// See test_duplicate_chan_id and test_pre_lockin_no_chan_closed_update for more.
5858-
if self.context.channel_state & (ChannelState::FundingSent as u32 | ChannelState::ChannelReady as u32 | ChannelState::ShutdownComplete as u32) != 0 {
5859-
self.context.latest_monitor_update_id = CLOSED_CHANNEL_UPDATE_ID;
5860-
Some((self.context.get_counterparty_node_id(), funding_txo, ChannelMonitorUpdate {
5861-
update_id: self.context.latest_monitor_update_id,
5862-
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast }],
5863-
}))
5864-
} else { None }
5865-
} else { None };
5866-
5867-
self.context.channel_state = ChannelState::ShutdownComplete as u32;
5868-
self.context.update_time_counter += 1;
5869-
(monitor_update, dropped_outbound_htlcs)
5870-
}
5871-
58725872
pub fn inflight_htlc_sources(&self) -> impl Iterator<Item=(&HTLCSource, &PaymentHash)> {
58735873
self.context.holding_cell_htlc_updates.iter()
58745874
.flat_map(|htlc_update| {

lightning/src/ln/channelmanager.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,7 @@ macro_rules! convert_chan_err {
16461646
ChannelError::Close(msg) => {
16471647
log_error!($self.logger, "Closing channel {} due to close-required error: {}", log_bytes!($channel_id[..]), msg);
16481648
update_maps_on_chan_removal!($self, $channel);
1649-
let shutdown_res = $channel.force_shutdown(true);
1649+
let shutdown_res = $channel.context.force_shutdown(true);
16501650
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.context.get_user_id(),
16511651
shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
16521652
},
@@ -1813,7 +1813,7 @@ macro_rules! handle_new_monitor_update {
18131813
update_maps_on_chan_removal!($self, $chan);
18141814
let res: Result<(), _> = Err(MsgHandleErrInternal::from_finish_shutdown(
18151815
"ChannelMonitor storage failure".to_owned(), $chan.context.channel_id(),
1816-
$chan.context.get_user_id(), $chan.force_shutdown(false),
1816+
$chan.context.get_user_id(), $chan.context.force_shutdown(false),
18171817
$self.get_channel_update_for_broadcast(&$chan).ok()));
18181818
$remove;
18191819
res
@@ -2345,7 +2345,7 @@ where
23452345
}
23462346
};
23472347
log_error!(self.logger, "Force-closing channel {}", log_bytes!(channel_id[..]));
2348-
self.finish_force_close_channel(chan.force_shutdown(broadcast));
2348+
self.finish_force_close_channel(chan.context.force_shutdown(broadcast));
23492349
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
23502350
let mut peer_state = peer_state_mutex.lock().unwrap();
23512351
peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
@@ -3106,7 +3106,7 @@ where
31063106

31073107
let funding_res = chan.get_outbound_funding_created(funding_transaction, funding_txo, &self.logger)
31083108
.map_err(|e| if let ChannelError::Close(msg) = e {
3109-
MsgHandleErrInternal::from_finish_shutdown(msg, chan.context.channel_id(), chan.context.get_user_id(), chan.force_shutdown(true), None)
3109+
MsgHandleErrInternal::from_finish_shutdown(msg, chan.context.channel_id(), chan.context.get_user_id(), chan.context.force_shutdown(true), None)
31103110
} else { unreachable!(); });
31113111
match funding_res {
31123112
Ok(funding_msg) => (funding_msg, chan),
@@ -5686,7 +5686,7 @@ where
56865686
let pending_msg_events = &mut peer_state.pending_msg_events;
56875687
if let hash_map::Entry::Occupied(chan_entry) = peer_state.channel_by_id.entry(funding_outpoint.to_channel_id()) {
56885688
let mut chan = remove_channel!(self, chan_entry);
5689-
failed_channels.push(chan.force_shutdown(false));
5689+
failed_channels.push(chan.context.force_shutdown(false));
56905690
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
56915691
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
56925692
msg: update
@@ -6533,7 +6533,7 @@ where
65336533
update_maps_on_chan_removal!(self, channel);
65346534
// It looks like our counterparty went on-chain or funding transaction was
65356535
// reorged out of the main chain. Close the channel.
6536-
failed_channels.push(channel.force_shutdown(true));
6536+
failed_channels.push(channel.context.force_shutdown(true));
65376537
if let Ok(update) = self.get_channel_update_for_broadcast(&channel) {
65386538
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
65396539
msg: update
@@ -7963,7 +7963,7 @@ where
79637963
log_error!(args.logger, " The channel will be force-closed and the latest commitment transaction from the ChannelMonitor broadcast.");
79647964
log_error!(args.logger, " The ChannelMonitor for channel {} is at update_id {} but the ChannelManager is at update_id {}.",
79657965
log_bytes!(channel.context.channel_id()), monitor.get_latest_update_id(), channel.context.get_latest_monitor_update_id());
7966-
let (monitor_update, mut new_failed_htlcs) = channel.force_shutdown(true);
7966+
let (monitor_update, mut new_failed_htlcs) = channel.context.force_shutdown(true);
79677967
if let Some((counterparty_node_id, funding_txo, update)) = monitor_update {
79687968
pending_background_events.push(BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
79697969
counterparty_node_id, funding_txo, update
@@ -8021,7 +8021,7 @@ where
80218021
// If we were persisted and shut down while the initial ChannelMonitor persistence
80228022
// was in-progress, we never broadcasted the funding transaction and can still
80238023
// safely discard the channel.
8024-
let _ = channel.force_shutdown(false);
8024+
let _ = channel.context.force_shutdown(false);
80258025
channel_closures.push_back((events::Event::ChannelClosed {
80268026
channel_id: channel.context.channel_id(),
80278027
user_channel_id: channel.context.get_user_id(),

0 commit comments

Comments
 (0)