From 4b5b48cb57703ce9a094eb32e0d85b2a45072f45 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Fri, 7 Sep 2018 03:10:10 +0000 Subject: [PATCH] Refactor handle_shutdown to wrapper error handling function --- src/ln/channelmanager.rs | 71 +++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index d2dab46a3..600342f0b 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -1552,6 +1552,43 @@ impl ChannelManager { }; } + fn internal_shutdown(&self, their_node_id: &PublicKey, msg: &msgs::Shutdown) -> Result<(Option, Option), MsgHandleErrInternal> { + let (res, chan_option) = { + let mut channel_state_lock = self.channel_state.lock().unwrap(); + let channel_state = channel_state_lock.borrow_parts(); + + match channel_state.by_id.entry(msg.channel_id.clone()) { + hash_map::Entry::Occupied(mut chan_entry) => { + if chan_entry.get().get_their_node_id() != *their_node_id { + //TODO: here and below MsgHandleErrInternal, #153 case + return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id)); + } + let res = chan_entry.get_mut().shutdown(&*self.fee_estimator, &msg).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))?; + if chan_entry.get().is_shutdown() { + if let Some(short_id) = chan_entry.get().get_short_channel_id() { + channel_state.short_to_id.remove(&short_id); + } + (res, Some(chan_entry.remove_entry().1)) + } else { (res, None) } + }, + hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id)) + } + }; + for payment_hash in res.2 { + // unknown_next_peer...I dunno who that is anymore.... + self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), &payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 10, data: Vec::new() }); + } + if let Some(chan) = chan_option { + if let Ok(update) = self.get_channel_update(&chan) { + let mut events = self.pending_events.lock().unwrap(); + events.push(events::Event::BroadcastChannelUpdate { + msg: update + }); + } + } + Ok((res.0, res.1)) + } + fn internal_announcement_signatures(&self, their_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) -> Result<(), MsgHandleErrInternal> { let (chan_announcement, chan_update) = { let mut channel_state = self.channel_state.lock().unwrap(); @@ -1774,39 +1811,7 @@ impl ChannelMessageHandler for ChannelManager { } fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &msgs::Shutdown) -> Result<(Option, Option), HandleError> { - let (res, chan_option) = { - let mut channel_state_lock = self.channel_state.lock().unwrap(); - let channel_state = channel_state_lock.borrow_parts(); - - match channel_state.by_id.entry(msg.channel_id.clone()) { - hash_map::Entry::Occupied(mut chan_entry) => { - if chan_entry.get().get_their_node_id() != *their_node_id { - return Err(HandleError{err: "Got a message for a channel from the wrong node!", action: None}) - } - let res = chan_entry.get_mut().shutdown(&*self.fee_estimator, &msg)?; - if chan_entry.get().is_shutdown() { - if let Some(short_id) = chan_entry.get().get_short_channel_id() { - channel_state.short_to_id.remove(&short_id); - } - (res, Some(chan_entry.remove_entry().1)) - } else { (res, None) } - }, - hash_map::Entry::Vacant(_) => return Err(HandleError{err: "Failed to find corresponding channel", action: None}) - } - }; - for payment_hash in res.2 { - // unknown_next_peer...I dunno who that is anymore.... - self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), &payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 10, data: Vec::new() }); - } - if let Some(chan) = chan_option { - if let Ok(update) = self.get_channel_update(&chan) { - let mut events = self.pending_events.lock().unwrap(); - events.push(events::Event::BroadcastChannelUpdate { - msg: update - }); - } - } - Ok((res.0, res.1)) + handle_error!(self, self.internal_shutdown(their_node_id, msg), their_node_id) } fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &msgs::ClosingSigned) -> Result, HandleError> { -- 2.39.5