Skip to content

Commit cecd971

Browse files
committed
DRY ChannelError conversion macros
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.
1 parent f3650df commit cecd971

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -766,22 +766,43 @@ macro_rules! handle_error {
766766
}
767767
}
768768

769+
macro_rules! convert_chan_err {
770+
($self: ident, $err: expr, $short_to_id: expr, $channel: expr, $channel_id: expr) => {
771+
match $err {
772+
ChannelError::Ignore(msg) => {
773+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $channel_id.clone()))
774+
},
775+
ChannelError::Close(msg) => {
776+
log_trace!($self.logger, "Closing channel {} due to close-required error: {}", log_bytes!($channel_id[..]), msg);
777+
if let Some(short_id) = $channel.get_short_channel_id() {
778+
$short_to_id.remove(&short_id);
779+
}
780+
let shutdown_res = $channel.force_shutdown(true);
781+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update(&$channel).ok()))
782+
},
783+
ChannelError::CloseDelayBroadcast(msg) => {
784+
log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($channel_id[..]), msg);
785+
if let Some(short_id) = $channel.get_short_channel_id() {
786+
$short_to_id.remove(&short_id);
787+
}
788+
let shutdown_res = $channel.force_shutdown(false);
789+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update(&$channel).ok()))
790+
}
791+
}
792+
}
793+
}
794+
769795
macro_rules! break_chan_entry {
770796
($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
771797
match $res {
772798
Ok(res) => res,
773-
Err(ChannelError::Ignore(msg)) => {
774-
break Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
775-
},
776-
Err(ChannelError::Close(msg)) => {
777-
log_trace!($self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
778-
let (channel_id, mut chan) = $entry.remove_entry();
779-
if let Some(short_id) = chan.get_short_channel_id() {
780-
$channel_state.short_to_id.remove(&short_id);
799+
Err(e) => {
800+
let (drop, res) = convert_chan_err!($self, e, $channel_state.short_to_id, $entry.get_mut(), $entry.key());
801+
if drop {
802+
$entry.remove_entry();
781803
}
782-
break Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok()))
783-
},
784-
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"); }
804+
break Err(res);
805+
}
785806
}
786807
}
787808
}
@@ -790,25 +811,12 @@ macro_rules! try_chan_entry {
790811
($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
791812
match $res {
792813
Ok(res) => res,
793-
Err(ChannelError::Ignore(msg)) => {
794-
return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
795-
},
796-
Err(ChannelError::Close(msg)) => {
797-
log_trace!($self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
798-
let (channel_id, mut chan) = $entry.remove_entry();
799-
if let Some(short_id) = chan.get_short_channel_id() {
800-
$channel_state.short_to_id.remove(&short_id);
801-
}
802-
return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok()))
803-
},
804-
Err(ChannelError::CloseDelayBroadcast(msg)) => {
805-
log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($entry.key()[..]), msg);
806-
let (channel_id, mut chan) = $entry.remove_entry();
807-
if let Some(short_id) = chan.get_short_channel_id() {
808-
$channel_state.short_to_id.remove(&short_id);
814+
Err(e) => {
815+
let (drop, res) = convert_chan_err!($self, e, $channel_state.short_to_id, $entry.get_mut(), $entry.key());
816+
if drop {
817+
$entry.remove_entry();
809818
}
810-
let shutdown_res = chan.force_shutdown(false);
811-
return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, $self.get_channel_update(&chan).ok()))
819+
return Err(res);
812820
}
813821
}
814822
}

0 commit comments

Comments
 (0)