From eda6e9d0ec4a72b3797730faa0b7f54507a2014b Mon Sep 17 00:00:00 2001 From: Duncan Dean Date: Mon, 14 Aug 2023 16:21:35 +0200 Subject: [PATCH] Send error message to peer if we drop an unfunded channel on timeout --- lightning/src/ln/channelmanager.rs | 20 +++++++++++++++++--- lightning/src/ln/functional_tests.rs | 20 ++++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 961f25cc6..b7c7e0332 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -4472,20 +4472,34 @@ where chan_id: &[u8; 32], chan_context: &mut ChannelContext<::Signer>, unfunded_chan_context: &mut UnfundedChannelContext, + pending_msg_events: &mut Vec, | { chan_context.maybe_expire_prev_config(); if unfunded_chan_context.should_expire_unfunded_channel() { - log_error!(self.logger, "Force-closing pending outbound channel {} for not establishing in a timely manner", log_bytes!(&chan_id[..])); + log_error!(self.logger, + "Force-closing pending channel with ID {} for not establishing in a timely manner", + log_bytes!(&chan_id[..])); update_maps_on_chan_removal!(self, &chan_context); self.issue_channel_close_events(&chan_context, ClosureReason::HolderForceClosed); self.finish_force_close_channel(chan_context.force_shutdown(false)); + pending_msg_events.push(MessageSendEvent::HandleError { + node_id: counterparty_node_id, + action: msgs::ErrorAction::SendErrorMessage { + msg: msgs::ErrorMessage { + channel_id: *chan_id, + data: "Force-closing pending channel due to timeout awaiting establishment handshake".to_owned(), + }, + }, + }); false } else { true } }; - peer_state.outbound_v1_channel_by_id.retain(|chan_id, chan| process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context)); - peer_state.inbound_v1_channel_by_id.retain(|chan_id, chan| process_unfunded_channel_tick(chan_id, &mut chan.context, &mut chan.unfunded_context)); + peer_state.outbound_v1_channel_by_id.retain(|chan_id, chan| process_unfunded_channel_tick( + chan_id, &mut chan.context, &mut chan.unfunded_context, pending_msg_events)); + peer_state.inbound_v1_channel_by_id.retain(|chan_id, chan| process_unfunded_channel_tick( + chan_id, &mut chan.context, &mut chan.unfunded_context, pending_msg_events)); if peer_state.ok_to_remove(true) { pending_peers_awaiting_removal.push(counterparty_node_id); diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 53aa75d82..6e96e269f 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -10036,7 +10036,15 @@ fn test_remove_expired_outbound_unfunded_channels() { nodes[0].node.timer_tick_occurred(); check_outbound_channel_existence(false); - check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000); + let msg_events = nodes[0].node.get_and_clear_pending_msg_events(); + assert_eq!(msg_events.len(), 1); + match msg_events[0] { + MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => { + assert_eq!(msg.data, "Force-closing pending channel due to timeout awaiting establishment handshake"); + }, + _ => panic!("Unexpected event"), + } + check_closed_event(&nodes[0], 1, ClosureReason::HolderForceClosed, false, &[nodes[1].node.get_our_node_id()], 100000); } #[test] @@ -10079,5 +10087,13 @@ fn test_remove_expired_inbound_unfunded_channels() { nodes[1].node.timer_tick_occurred(); check_inbound_channel_existence(false); - check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000); + let msg_events = nodes[1].node.get_and_clear_pending_msg_events(); + assert_eq!(msg_events.len(), 1); + match msg_events[0] { + MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => { + assert_eq!(msg.data, "Force-closing pending channel due to timeout awaiting establishment handshake"); + }, + _ => panic!("Unexpected event"), + } + check_closed_event(&nodes[1], 1, ClosureReason::HolderForceClosed, false, &[nodes[0].node.get_our_node_id()], 100000); } -- 2.39.5