]> git.bitcoin.ninja Git - rust-lightning/commitdiff
DRY ChannelError conversion macros
authorMatt Corallo <git@bluematt.me>
Thu, 18 Mar 2021 22:23:05 +0000 (18:23 -0400)
committerMatt Corallo <git@bluematt.me>
Fri, 21 May 2021 15:10:45 +0000 (15:10 +0000)
Both break_chan_entry and try_chan_entry do almost identical work,
only differing on if they `break` or `return` in response to an
error. Because we will now also need an option to do neither, we
break out the common code into a shared `convert_chan_err` macro.

lightning/src/ln/channelmanager.rs

index 55271bce44e84c7235d9e3bb6d490a64dfaf3b43..2effd410cc66a43d819a2280cfe8514f28681c5d 100644 (file)
@@ -766,22 +766,44 @@ macro_rules! handle_error {
        }
 }
 
+/// Returns (boolean indicating if we should remove the Channel object from memory, a mapped error)
+macro_rules! convert_chan_err {
+       ($self: ident, $err: expr, $short_to_id: expr, $channel: expr, $channel_id: expr) => {
+               match $err {
+                       ChannelError::Ignore(msg) => {
+                               (false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $channel_id.clone()))
+                       },
+                       ChannelError::Close(msg) => {
+                               log_trace!($self.logger, "Closing channel {} due to close-required error: {}", log_bytes!($channel_id[..]), msg);
+                               if let Some(short_id) = $channel.get_short_channel_id() {
+                                       $short_to_id.remove(&short_id);
+                               }
+                               let shutdown_res = $channel.force_shutdown(true);
+                               (true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update(&$channel).ok()))
+                       },
+                       ChannelError::CloseDelayBroadcast(msg) => {
+                               log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($channel_id[..]), msg);
+                               if let Some(short_id) = $channel.get_short_channel_id() {
+                                       $short_to_id.remove(&short_id);
+                               }
+                               let shutdown_res = $channel.force_shutdown(false);
+                               (true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update(&$channel).ok()))
+                       }
+               }
+       }
+}
+
 macro_rules! break_chan_entry {
        ($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
                match $res {
                        Ok(res) => res,
-                       Err(ChannelError::Ignore(msg)) => {
-                               break Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
-                       },
-                       Err(ChannelError::Close(msg)) => {
-                               log_trace!($self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
-                               let (channel_id, mut chan) = $entry.remove_entry();
-                               if let Some(short_id) = chan.get_short_channel_id() {
-                                       $channel_state.short_to_id.remove(&short_id);
+                       Err(e) => {
+                               let (drop, res) = convert_chan_err!($self, e, $channel_state.short_to_id, $entry.get_mut(), $entry.key());
+                               if drop {
+                                       $entry.remove_entry();
                                }
-                               break Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok()))
-                       },
-                       Err(ChannelError::CloseDelayBroadcast(_)) => { panic!("Wait is only generated on receipt of channel_reestablish, which is handled by try_chan_entry, we don't bother to support it here"); }
+                               break Err(res);
+                       }
                }
        }
 }
@@ -790,25 +812,12 @@ macro_rules! try_chan_entry {
        ($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
                match $res {
                        Ok(res) => res,
-                       Err(ChannelError::Ignore(msg)) => {
-                               return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
-                       },
-                       Err(ChannelError::Close(msg)) => {
-                               log_trace!($self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
-                               let (channel_id, mut chan) = $entry.remove_entry();
-                               if let Some(short_id) = chan.get_short_channel_id() {
-                                       $channel_state.short_to_id.remove(&short_id);
-                               }
-                               return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok()))
-                       },
-                       Err(ChannelError::CloseDelayBroadcast(msg)) => {
-                               log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($entry.key()[..]), msg);
-                               let (channel_id, mut chan) = $entry.remove_entry();
-                               if let Some(short_id) = chan.get_short_channel_id() {
-                                       $channel_state.short_to_id.remove(&short_id);
+                       Err(e) => {
+                               let (drop, res) = convert_chan_err!($self, e, $channel_state.short_to_id, $entry.get_mut(), $entry.key());
+                               if drop {
+                                       $entry.remove_entry();
                                }
-                               let shutdown_res = chan.force_shutdown(false);
-                               return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, $self.get_channel_update(&chan).ok()))
+                               return Err(res);
                        }
                }
        }