]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Refactor handle_shutdown to wrapper error handling function
authorAntoine Riard <ariard@student.42.fr>
Fri, 7 Sep 2018 03:10:10 +0000 (03:10 +0000)
committerAntoine Riard <ariard@student.42.fr>
Fri, 7 Sep 2018 03:10:10 +0000 (03:10 +0000)
src/ln/channelmanager.rs

index d2dab46a3a3ab6fc23ef93355a6debf1f6cb4fda..600342f0b35126b02d0ee5d03d0d4c80d7c4ef82 100644 (file)
@@ -1552,6 +1552,43 @@ impl ChannelManager {
                };
        }
 
+       fn internal_shutdown(&self, their_node_id: &PublicKey, msg: &msgs::Shutdown) -> Result<(Option<msgs::Shutdown>, Option<msgs::ClosingSigned>), 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<msgs::Shutdown>, Option<msgs::ClosingSigned>), 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<Option<msgs::ClosingSigned>, HandleError> {