Skip to content

Commit fd31d91

Browse files
committed
change ShutdownResult type to better capture the possibilites
The return value from Channel::force_shutdown previously always returned a `ChannelMonitorUpdate`, but expected it to only be applied in the case that it *also* returned a Some for the funding transaction output. This is confusing, instead we move the `ChannelMontiorUpdate` inside the Option, making it hold a tuple instead.
1 parent 53ea21b commit fd31d91

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

lightning/src/ln/channel.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4172,7 +4172,7 @@ impl<Signer: Sign> Channel<Signer> {
41724172
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
41734173
/// Also returns the list of payment_hashes for channels which we can safely fail backwards
41744174
/// immediately (others we will have to allow to time out).
4175-
pub fn force_shutdown(&mut self, should_broadcast: bool) -> (Option<OutPoint>, ChannelMonitorUpdate, Vec<(HTLCSource, PaymentHash)>) {
4175+
pub fn force_shutdown(&mut self, should_broadcast: bool) -> (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource, PaymentHash)>) {
41764176
assert!(self.channel_state != ChannelState::ShutdownComplete as u32);
41774177

41784178
// We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
@@ -4186,7 +4186,7 @@ impl<Signer: Sign> Channel<Signer> {
41864186
_ => {}
41874187
}
41884188
}
4189-
let funding_txo = if let Some(funding_txo) = self.get_funding_txo() {
4189+
let monitor_update = if let Some(funding_txo) = self.get_funding_txo() {
41904190
// If we haven't yet exchanged funding signatures (ie channel_state < FundingSent),
41914191
// returning a channel monitor update here would imply a channel monitor update before
41924192
// we even registered the channel monitor to begin with, which is invalid.
@@ -4195,17 +4195,17 @@ impl<Signer: Sign> Channel<Signer> {
41954195
// monitor update to the user, even if we return one).
41964196
// See test_duplicate_chan_id and test_pre_lockin_no_chan_closed_update for more.
41974197
if self.channel_state & (ChannelState::FundingSent as u32 | ChannelState::ChannelFunded as u32 | ChannelState::ShutdownComplete as u32) != 0 {
4198-
Some(funding_txo.clone())
4198+
Some((funding_txo, ChannelMonitorUpdate {
4199+
update_id: self.latest_monitor_update_id + 1,
4200+
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast }],
4201+
}))
41994202
} else { None }
42004203
} else { None };
42014204

42024205
self.channel_state = ChannelState::ShutdownComplete as u32;
42034206
self.update_time_counter += 1;
42044207
self.latest_monitor_update_id += 1;
4205-
(funding_txo, ChannelMonitorUpdate {
4206-
update_id: self.latest_monitor_update_id,
4207-
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast }],
4208-
}, dropped_outbound_htlcs)
4208+
(monitor_update, dropped_outbound_htlcs)
42094209
}
42104210
}
42114211

lightning/src/ln/channelmanager.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub struct PaymentPreimage(pub [u8;32]);
206206
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
207207
pub struct PaymentSecret(pub [u8;32]);
208208

209-
type ShutdownResult = (Option<OutPoint>, ChannelMonitorUpdate, Vec<(HTLCSource, PaymentHash)>);
209+
type ShutdownResult = (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource, PaymentHash)>);
210210

211211
/// Error type returned across the channel_state mutex boundary. When an Err is generated for a
212212
/// Channel, we generally end up with a ChannelError::Close for which we have to close the channel
@@ -941,12 +941,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
941941

942942
#[inline]
943943
fn finish_force_close_channel(&self, shutdown_res: ShutdownResult) {
944-
let (funding_txo_option, monitor_update, mut failed_htlcs) = shutdown_res;
944+
let (monitor_update_option, mut failed_htlcs) = shutdown_res;
945945
log_trace!(self.logger, "Finishing force-closure of channel {} HTLCs to fail", failed_htlcs.len());
946946
for htlc_source in failed_htlcs.drain(..) {
947947
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source.0, &htlc_source.1, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() });
948948
}
949-
if let Some(funding_txo) = funding_txo_option {
949+
if let Some((funding_txo, monitor_update)) = monitor_update_option {
950950
// There isn't anything we can do if we get an update failure - we're already
951951
// force-closing. The monitor update on the required in-memory copy should broadcast
952952
// the latest local state, which is the best we can do anyway. Thus, it is safe to
@@ -2417,7 +2417,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
24172417
// We do not do a force-close here as that would generate a monitor update for
24182418
// a monitor that we didn't manage to store (and that we don't care about - we
24192419
// don't respond with the funding_signed so the channel can never go on chain).
2420-
let (_funding_txo_option, _monitor_update, failed_htlcs) = chan.force_shutdown(true);
2420+
let (_monitor_update, failed_htlcs) = chan.force_shutdown(true);
24212421
assert!(failed_htlcs.is_empty());
24222422
return Err(MsgHandleErrInternal::send_err_msg_no_close("ChannelMonitor storage failure".to_owned(), funding_msg.channel_id));
24232423
},
@@ -4070,7 +4070,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
40704070
channel.get_cur_counterparty_commitment_transaction_number() > monitor.get_cur_counterparty_commitment_number() ||
40714071
channel.get_latest_monitor_update_id() < monitor.get_latest_update_id() {
40724072
// But if the channel is behind of the monitor, close the channel:
4073-
let (_, _, mut new_failed_htlcs) = channel.force_shutdown(true);
4073+
let (_, mut new_failed_htlcs) = channel.force_shutdown(true);
40744074
failed_htlcs.append(&mut new_failed_htlcs);
40754075
monitor.broadcast_latest_holder_commitment_txn(&args.tx_broadcaster, &args.logger);
40764076
} else {

0 commit comments

Comments
 (0)