@@ -1966,14 +1966,6 @@ macro_rules! handle_error {
19661966 msg: update
19671967 });
19681968 }
1969- if let Some((channel_id, user_channel_id)) = chan_id {
1970- $self.pending_events.lock().unwrap().push_back((events::Event::ChannelClosed {
1971- channel_id, user_channel_id,
1972- reason: ClosureReason::ProcessingError { err: err.err.clone() },
1973- counterparty_node_id: Some($counterparty_node_id),
1974- channel_capacity_sats: channel_capacity,
1975- }, None));
1976- }
19771969 }
19781970
19791971 let logger = WithContext::from(
@@ -2039,7 +2031,8 @@ macro_rules! convert_chan_phase_err {
20392031 let logger = WithChannelContext::from(&$self.logger, &$channel.context);
20402032 log_error!(logger, "Closing channel {} due to close-required error: {}", $channel_id, msg);
20412033 update_maps_on_chan_removal!($self, $channel.context);
2042- let shutdown_res = $channel.context.force_shutdown(true);
2034+ let reason = ClosureReason::ProcessingError { err: msg.clone() };
2035+ let shutdown_res = $channel.context.force_shutdown(true, reason);
20432036 let user_id = $channel.context.get_user_id();
20442037 let channel_capacity_satoshis = $channel.context.get_value_satoshis();
20452038
@@ -2701,18 +2694,6 @@ where
27012694 .collect()
27022695 }
27032696
2704- /// Helper function that issues the channel close events
2705- fn issue_channel_close_events(&self, context: &ChannelContext<SP>, closure_reason: ClosureReason) {
2706- let mut pending_events_lock = self.pending_events.lock().unwrap();
2707- pending_events_lock.push_back((events::Event::ChannelClosed {
2708- channel_id: context.channel_id(),
2709- user_channel_id: context.get_user_id(),
2710- reason: closure_reason,
2711- counterparty_node_id: Some(context.get_counterparty_node_id()),
2712- channel_capacity_sats: Some(context.get_value_satoshis()),
2713- }, None));
2714- }
2715-
27162697 fn close_channel_internal(&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, target_feerate_sats_per_1000_weight: Option<u32>, override_shutdown_script: Option<ShutdownScript>) -> Result<(), APIError> {
27172698 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
27182699
@@ -2754,9 +2735,8 @@ where
27542735 peer_state_lock, peer_state, per_peer_state, chan);
27552736 }
27562737 } else {
2757- self.issue_channel_close_events(chan_phase_entry.get().context(), ClosureReason::HolderForceClosed);
27582738 let mut chan_phase = remove_channel_phase!(self, chan_phase_entry);
2759- shutdown_result = Some(chan_phase.context_mut().force_shutdown(false));
2739+ shutdown_result = Some(chan_phase.context_mut().force_shutdown(false, ClosureReason::HolderForceClosed ));
27602740 }
27612741 },
27622742 hash_map::Entry::Vacant(_) => {
@@ -2853,6 +2833,7 @@ where
28532833 let logger = WithContext::from(
28542834 &self.logger, Some(shutdown_res.counterparty_node_id), Some(shutdown_res.channel_id),
28552835 );
2836+
28562837 log_debug!(logger, "Finishing closure of channel with {} HTLCs to fail", shutdown_res.dropped_outbound_htlcs.len());
28572838 for htlc_source in shutdown_res.dropped_outbound_htlcs.drain(..) {
28582839 let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
@@ -2878,8 +2859,7 @@ where
28782859 let mut peer_state = peer_state_mutex.lock().unwrap();
28792860 if let Some(mut chan) = peer_state.channel_by_id.remove(&channel_id) {
28802861 update_maps_on_chan_removal!(self, &chan.context());
2881- self.issue_channel_close_events(&chan.context(), ClosureReason::FundingBatchClosure);
2882- shutdown_results.push(chan.context_mut().force_shutdown(false));
2862+ shutdown_results.push(chan.context_mut().force_shutdown(false, ClosureReason::FundingBatchClosure));
28832863 }
28842864 }
28852865 has_uncompleted_channel = Some(has_uncompleted_channel.map_or(!state, |v| v || !state));
@@ -2892,6 +2872,14 @@ where
28922872
28932873 {
28942874 let mut pending_events = self.pending_events.lock().unwrap();
2875+ pending_events.push_back((events::Event::ChannelClosed {
2876+ channel_id: shutdown_res.channel_id,
2877+ user_channel_id: shutdown_res.user_channel_id,
2878+ reason: shutdown_res.closure_reason,
2879+ counterparty_node_id: Some(shutdown_res.counterparty_node_id),
2880+ channel_capacity_sats: Some(shutdown_res.channel_capacity_satoshis),
2881+ }, None));
2882+
28952883 if let Some(transaction) = shutdown_res.unbroadcasted_funding_tx {
28962884 pending_events.push_back((events::Event::DiscardFunding {
28972885 channel_id: shutdown_res.channel_id, transaction
@@ -2920,17 +2908,16 @@ where
29202908 let logger = WithContext::from(&self.logger, Some(*peer_node_id), Some(*channel_id));
29212909 if let hash_map::Entry::Occupied(chan_phase_entry) = peer_state.channel_by_id.entry(channel_id.clone()) {
29222910 log_error!(logger, "Force-closing channel {}", channel_id);
2923- self.issue_channel_close_events(&chan_phase_entry.get().context(), closure_reason);
29242911 let mut chan_phase = remove_channel_phase!(self, chan_phase_entry);
29252912 mem::drop(peer_state);
29262913 mem::drop(per_peer_state);
29272914 match chan_phase {
29282915 ChannelPhase::Funded(mut chan) => {
2929- self.finish_close_channel(chan.context.force_shutdown(broadcast));
2916+ self.finish_close_channel(chan.context.force_shutdown(broadcast, closure_reason ));
29302917 (self.get_channel_update_for_broadcast(&chan).ok(), chan.context.get_counterparty_node_id())
29312918 },
29322919 ChannelPhase::UnfundedOutboundV1(_) | ChannelPhase::UnfundedInboundV1(_) => {
2933- self.finish_close_channel(chan_phase.context_mut().force_shutdown(false));
2920+ self.finish_close_channel(chan_phase.context_mut().force_shutdown(false, closure_reason ));
29342921 // Unfunded channel has no update
29352922 (None, chan_phase.context().get_counterparty_node_id())
29362923 },
@@ -3760,7 +3747,8 @@ where
37603747 .map_err(|(mut chan, e)| if let ChannelError::Close(msg) = e {
37613748 let channel_id = chan.context.channel_id();
37623749 let user_id = chan.context.get_user_id();
3763- let shutdown_res = chan.context.force_shutdown(false);
3750+ let reason = ClosureReason::ProcessingError { err: msg.clone() };
3751+ let shutdown_res = chan.context.force_shutdown(false, reason);
37643752 let channel_capacity = chan.context.get_value_satoshis();
37653753 (chan, MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, user_id, shutdown_res, None, channel_capacity))
37663754 } else { unreachable!(); });
@@ -3967,8 +3955,8 @@ where
39673955 .and_then(|mut peer_state| peer_state.channel_by_id.remove(&channel_id))
39683956 .map(|mut chan| {
39693957 update_maps_on_chan_removal!(self, &chan.context());
3970- self.issue_channel_close_events(&chan.context(), ClosureReason::ProcessingError { err: e.clone() }) ;
3971- shutdown_results.push(chan.context_mut().force_shutdown(false));
3958+ let closure_reason = ClosureReason::ProcessingError { err: e.clone() };
3959+ shutdown_results.push(chan.context_mut().force_shutdown(false, closure_reason ));
39723960 });
39733961 }
39743962 }
@@ -4890,8 +4878,7 @@ where
48904878 log_error!(logger,
48914879 "Force-closing pending channel with ID {} for not establishing in a timely manner", chan_id);
48924880 update_maps_on_chan_removal!(self, &context);
4893- self.issue_channel_close_events(&context, ClosureReason::HolderForceClosed);
4894- shutdown_channels.push(context.force_shutdown(false));
4881+ shutdown_channels.push(context.force_shutdown(false, ClosureReason::HolderForceClosed));
48954882 pending_msg_events.push(MessageSendEvent::HandleError {
48964883 node_id: counterparty_node_id,
48974884 action: msgs::ErrorAction::SendErrorMessage {
@@ -6511,9 +6498,8 @@ where
65116498 let context = phase.context_mut();
65126499 let logger = WithChannelContext::from(&self.logger, context);
65136500 log_error!(logger, "Immediately closing unfunded channel {} as peer asked to cooperatively shut it down (which is unnecessary)", &msg.channel_id);
6514- self.issue_channel_close_events(&context, ClosureReason::CounterpartyCoopClosedUnfundedChannel);
65156501 let mut chan = remove_channel_phase!(self, chan_phase_entry);
6516- finish_shutdown = Some(chan.context_mut().force_shutdown(false));
6502+ finish_shutdown = Some(chan.context_mut().force_shutdown(false, ClosureReason::CounterpartyCoopClosedUnfundedChannel ));
65176503 },
65186504 }
65196505 } else {
@@ -6582,7 +6568,6 @@ where
65826568 msg: update
65836569 });
65846570 }
6585- self.issue_channel_close_events(&chan.context, ClosureReason::CooperativeClosure);
65866571 }
65876572 mem::drop(per_peer_state);
65886573 if let Some(shutdown_result) = shutdown_result {
@@ -7234,13 +7219,12 @@ where
72347219 let pending_msg_events = &mut peer_state.pending_msg_events;
72357220 if let hash_map::Entry::Occupied(chan_phase_entry) = peer_state.channel_by_id.entry(funding_outpoint.to_channel_id()) {
72367221 if let ChannelPhase::Funded(mut chan) = remove_channel_phase!(self, chan_phase_entry) {
7237- failed_channels.push(chan.context.force_shutdown(false));
7222+ failed_channels.push(chan.context.force_shutdown(false, ClosureReason::HolderForceClosed ));
72387223 if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
72397224 pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
72407225 msg: update
72417226 });
72427227 }
7243- self.issue_channel_close_events(&chan.context, ClosureReason::HolderForceClosed);
72447228 pending_msg_events.push(events::MessageSendEvent::HandleError {
72457229 node_id: chan.context.get_counterparty_node_id(),
72467230 action: msgs::ErrorAction::DisconnectPeer {
@@ -7427,8 +7411,6 @@ where
74277411 });
74287412 }
74297413
7430- self.issue_channel_close_events(&chan.context, ClosureReason::CooperativeClosure);
7431-
74327414 log_info!(logger, "Broadcasting {}", log_tx!(tx));
74337415 self.tx_broadcaster.broadcast_transactions(&[&tx]);
74347416 update_maps_on_chan_removal!(self, &chan.context);
@@ -8441,14 +8423,13 @@ where
84418423 update_maps_on_chan_removal!(self, &channel.context);
84428424 // It looks like our counterparty went on-chain or funding transaction was
84438425 // reorged out of the main chain. Close the channel.
8444- failed_channels.push(channel.context.force_shutdown(true));
8426+ let reason_message = format!("{}", reason);
8427+ failed_channels.push(channel.context.force_shutdown(true, reason));
84458428 if let Ok(update) = self.get_channel_update_for_broadcast(&channel) {
84468429 pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
84478430 msg: update
84488431 });
84498432 }
8450- let reason_message = format!("{}", reason);
8451- self.issue_channel_close_events(&channel.context, reason);
84528433 pending_msg_events.push(events::MessageSendEvent::HandleError {
84538434 node_id: channel.context.get_counterparty_node_id(),
84548435 action: msgs::ErrorAction::DisconnectPeer {
@@ -8846,8 +8827,7 @@ where
88468827 };
88478828 // Clean up for removal.
88488829 update_maps_on_chan_removal!(self, &context);
8849- self.issue_channel_close_events(&context, ClosureReason::DisconnectedPeer);
8850- failed_channels.push(context.force_shutdown(false));
8830+ failed_channels.push(context.force_shutdown(false, ClosureReason::DisconnectedPeer));
88518831 false
88528832 });
88538833 // Note that we don't bother generating any events for pre-accept channels -
@@ -10293,7 +10273,7 @@ where
1029310273 log_error!(logger, " The ChannelMonitor for channel {} is at counterparty commitment transaction number {} but the ChannelManager is at counterparty commitment transaction number {}.",
1029410274 &channel.context.channel_id(), monitor.get_cur_counterparty_commitment_number(), channel.get_cur_counterparty_commitment_transaction_number());
1029510275 }
10296- let mut shutdown_result = channel.context.force_shutdown(true);
10276+ let mut shutdown_result = channel.context.force_shutdown(true, ClosureReason::OutdatedChannelManager );
1029710277 if shutdown_result.unbroadcasted_batch_funding_txid.is_some() {
1029810278 return Err(DecodeError::InvalidValue);
1029910279 }
@@ -10355,7 +10335,7 @@ where
1035510335 // If we were persisted and shut down while the initial ChannelMonitor persistence
1035610336 // was in-progress, we never broadcasted the funding transaction and can still
1035710337 // safely discard the channel.
10358- let _ = channel.context.force_shutdown(false);
10338+ let _ = channel.context.force_shutdown(false, ClosureReason::DisconnectedPeer );
1035910339 channel_closures.push_back((events::Event::ChannelClosed {
1036010340 channel_id: channel.context.channel_id(),
1036110341 user_channel_id: channel.context.get_user_id(),
0 commit comments