@@ -2556,7 +2556,7 @@ where
2556
2556
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
2557
2557
2558
2558
let mut failed_htlcs: Vec<(HTLCSource, PaymentHash)>;
2559
- let mut shutdown_result = None ;
2559
+ let shutdown_result;
2560
2560
loop {
2561
2561
let per_peer_state = self.per_peer_state.read().unwrap();
2562
2562
@@ -2571,10 +2571,10 @@ where
2571
2571
if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
2572
2572
let funding_txo_opt = chan.context.get_funding_txo();
2573
2573
let their_features = &peer_state.latest_features;
2574
- let unbroadcasted_batch_funding_txid = chan.context.unbroadcasted_batch_funding_txid();
2575
- let (shutdown_msg, mut monitor_update_opt, htlcs) =
2574
+ let (shutdown_msg, mut monitor_update_opt, htlcs, local_shutdown_result) =
2576
2575
chan.get_shutdown(&self.signer_provider, their_features, target_feerate_sats_per_1000_weight, override_shutdown_script)?;
2577
2576
failed_htlcs = htlcs;
2577
+ shutdown_result = local_shutdown_result;
2578
2578
2579
2579
// We can send the `shutdown` message before updating the `ChannelMonitor`
2580
2580
// here as we don't need the monitor update to complete until we send a
@@ -2602,7 +2602,6 @@ where
2602
2602
});
2603
2603
}
2604
2604
self.issue_channel_close_events(&chan.context, ClosureReason::HolderForceClosed);
2605
- shutdown_result = Some((None, Vec::new(), unbroadcasted_batch_funding_txid));
2606
2605
}
2607
2606
}
2608
2607
break;
@@ -2692,30 +2691,29 @@ where
2692
2691
self.close_channel_internal(channel_id, counterparty_node_id, target_feerate_sats_per_1000_weight, shutdown_script)
2693
2692
}
2694
2693
2695
- fn finish_close_channel(&self, shutdown_res: ShutdownResult) {
2694
+ fn finish_close_channel(&self, mut shutdown_res: ShutdownResult) {
2696
2695
debug_assert_ne!(self.per_peer_state.held_by_thread(), LockHeldState::HeldByThread);
2697
2696
#[cfg(debug_assertions)]
2698
2697
for (_, peer) in self.per_peer_state.read().unwrap().iter() {
2699
2698
debug_assert_ne!(peer.held_by_thread(), LockHeldState::HeldByThread);
2700
2699
}
2701
2700
2702
- let (monitor_update_option, mut failed_htlcs, unbroadcasted_batch_funding_txid) = shutdown_res;
2703
- log_debug!(self.logger, "Finishing closure of channel with {} HTLCs to fail", failed_htlcs.len());
2704
- for htlc_source in failed_htlcs.drain(..) {
2701
+ log_debug!(self.logger, "Finishing closure of channel with {} HTLCs to fail", shutdown_res.dropped_outbound_htlcs.len());
2702
+ for htlc_source in shutdown_res.dropped_outbound_htlcs.drain(..) {
2705
2703
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
2706
2704
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
2707
2705
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
2708
2706
self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
2709
2707
}
2710
- if let Some((_, funding_txo, monitor_update)) = monitor_update_option {
2708
+ if let Some((_, funding_txo, monitor_update)) = shutdown_res.monitor_update {
2711
2709
// There isn't anything we can do if we get an update failure - we're already
2712
2710
// force-closing. The monitor update on the required in-memory copy should broadcast
2713
2711
// the latest local state, which is the best we can do anyway. Thus, it is safe to
2714
2712
// ignore the result here.
2715
2713
let _ = self.chain_monitor.update_channel(funding_txo, &monitor_update);
2716
2714
}
2717
2715
let mut shutdown_results = Vec::new();
2718
- if let Some(txid) = unbroadcasted_batch_funding_txid {
2716
+ if let Some(txid) = shutdown_res. unbroadcasted_batch_funding_txid {
2719
2717
let mut funding_batch_states = self.funding_batch_states.lock().unwrap();
2720
2718
let affected_channels = funding_batch_states.remove(&txid).into_iter().flatten();
2721
2719
let per_peer_state = self.per_peer_state.read().unwrap();
@@ -6240,22 +6238,19 @@ where
6240
6238
}
6241
6239
6242
6240
fn internal_closing_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::ClosingSigned) -> Result<(), MsgHandleErrInternal> {
6243
- let mut shutdown_result = None;
6244
- let unbroadcasted_batch_funding_txid;
6245
6241
let per_peer_state = self.per_peer_state.read().unwrap();
6246
6242
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
6247
6243
.ok_or_else(|| {
6248
6244
debug_assert!(false);
6249
6245
MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
6250
6246
})?;
6251
- let (tx, chan_option) = {
6247
+ let (tx, chan_option, shutdown_result ) = {
6252
6248
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6253
6249
let peer_state = &mut *peer_state_lock;
6254
6250
match peer_state.channel_by_id.entry(msg.channel_id.clone()) {
6255
6251
hash_map::Entry::Occupied(mut chan_phase_entry) => {
6256
6252
if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
6257
- unbroadcasted_batch_funding_txid = chan.context.unbroadcasted_batch_funding_txid();
6258
- let (closing_signed, tx) = try_chan_phase_entry!(self, chan.closing_signed(&self.fee_estimator, &msg), chan_phase_entry);
6253
+ let (closing_signed, tx, shutdown_result) = try_chan_phase_entry!(self, chan.closing_signed(&self.fee_estimator, &msg), chan_phase_entry);
6259
6254
if let Some(msg) = closing_signed {
6260
6255
peer_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
6261
6256
node_id: counterparty_node_id.clone(),
@@ -6268,8 +6263,8 @@ where
6268
6263
// also implies there are no pending HTLCs left on the channel, so we can
6269
6264
// fully delete it from tracking (the channel monitor is still around to
6270
6265
// watch for old state broadcasts)!
6271
- (tx, Some(remove_channel_phase!(self, chan_phase_entry)))
6272
- } else { (tx, None) }
6266
+ (tx, Some(remove_channel_phase!(self, chan_phase_entry)), shutdown_result )
6267
+ } else { (tx, None, shutdown_result ) }
6273
6268
} else {
6274
6269
return try_chan_phase_entry!(self, Err(ChannelError::Close(
6275
6270
"Got a closing_signed message for an unfunded channel!".into())), chan_phase_entry);
@@ -6291,7 +6286,6 @@ where
6291
6286
});
6292
6287
}
6293
6288
self.issue_channel_close_events(&chan.context, ClosureReason::CooperativeClosure);
6294
- shutdown_result = Some((None, Vec::new(), unbroadcasted_batch_funding_txid));
6295
6289
}
6296
6290
mem::drop(per_peer_state);
6297
6291
if let Some(shutdown_result) = shutdown_result {
@@ -6988,13 +6982,16 @@ where
6988
6982
ChannelPhase::Funded(chan) => {
6989
6983
let unbroadcasted_batch_funding_txid = chan.context.unbroadcasted_batch_funding_txid();
6990
6984
match chan.maybe_propose_closing_signed(&self.fee_estimator, &self.logger) {
6991
- Ok((msg_opt, tx_opt)) => {
6985
+ Ok((msg_opt, tx_opt, shutdown_result_opt )) => {
6992
6986
if let Some(msg) = msg_opt {
6993
6987
has_update = true;
6994
6988
pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
6995
6989
node_id: chan.context.get_counterparty_node_id(), msg,
6996
6990
});
6997
6991
}
6992
+ if let Some(shutdown_result) = shutdown_result_opt {
6993
+ shutdown_results.push(shutdown_result);
6994
+ }
6998
6995
if let Some(tx) = tx_opt {
6999
6996
// We're done with this channel. We got a closing_signed and sent back
7000
6997
// a closing_signed with a closing transaction to broadcast.
@@ -7009,7 +7006,6 @@ where
7009
7006
log_info!(self.logger, "Broadcasting {}", log_tx!(tx));
7010
7007
self.tx_broadcaster.broadcast_transactions(&[&tx]);
7011
7008
update_maps_on_chan_removal!(self, &chan.context);
7012
- shutdown_results.push((None, Vec::new(), unbroadcasted_batch_funding_txid));
7013
7009
false
7014
7010
} else { true }
7015
7011
},
@@ -7050,7 +7046,7 @@ where
7050
7046
// Channel::force_shutdown tries to make us do) as we may still be in initialization,
7051
7047
// so we track the update internally and handle it when the user next calls
7052
7048
// timer_tick_occurred, guaranteeing we're running normally.
7053
- if let Some((counterparty_node_id, funding_txo, update)) = failure.0 .take() {
7049
+ if let Some((counterparty_node_id, funding_txo, update)) = failure.monitor_update .take() {
7054
7050
assert_eq!(update.updates.len(), 1);
7055
7051
if let ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast } = update.updates[0] {
7056
7052
assert!(should_broadcast);
@@ -9267,16 +9263,16 @@ where
9267
9263
log_error!(args.logger, " The ChannelMonitor for channel {} is at counterparty commitment transaction number {} but the ChannelManager is at counterparty commitment transaction number {}.",
9268
9264
&channel.context.channel_id(), monitor.get_cur_counterparty_commitment_number(), channel.get_cur_counterparty_commitment_transaction_number());
9269
9265
}
9270
- let (monitor_update, mut new_failed_htlcs, batch_funding_txid) = channel.context.force_shutdown(true);
9271
- if batch_funding_txid .is_some() {
9266
+ let mut shutdown_result = channel.context.force_shutdown(true);
9267
+ if shutdown_result.unbroadcasted_batch_funding_txid .is_some() {
9272
9268
return Err(DecodeError::InvalidValue);
9273
9269
}
9274
- if let Some((counterparty_node_id, funding_txo, update)) = monitor_update {
9270
+ if let Some((counterparty_node_id, funding_txo, update)) = shutdown_result. monitor_update {
9275
9271
close_background_events.push(BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
9276
9272
counterparty_node_id, funding_txo, update
9277
9273
});
9278
9274
}
9279
- failed_htlcs.append(&mut new_failed_htlcs );
9275
+ failed_htlcs.append(&mut shutdown_result.dropped_outbound_htlcs );
9280
9276
channel_closures.push_back((events::Event::ChannelClosed {
9281
9277
channel_id: channel.context.channel_id(),
9282
9278
user_channel_id: channel.context.get_user_id(),
0 commit comments