Skip to content

Commit 3d284c9

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 dec719f commit 3d284c9

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
@@ -736,22 +736,43 @@ macro_rules! handle_error {
736736
}
737737
}
738738

739+
macro_rules! convert_chan_err {
740+
($self: ident, $err: expr, $short_to_id: expr, $channel: expr, $channel_id: expr) => {
741+
match $err {
742+
ChannelError::Ignore(msg) => {
743+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $channel_id.clone()))
744+
},
745+
ChannelError::Close(msg) => {
746+
log_trace!($self.logger, "Closing channel {} due to close-required error: {}", log_bytes!($channel_id[..]), msg);
747+
if let Some(short_id) = $channel.get_short_channel_id() {
748+
$short_to_id.remove(&short_id);
749+
}
750+
let shutdown_res = $channel.force_shutdown(true);
751+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update(&$channel).ok()))
752+
},
753+
ChannelError::CloseDelayBroadcast(msg) => {
754+
log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($channel_id[..]), msg);
755+
if let Some(short_id) = $channel.get_short_channel_id() {
756+
$short_to_id.remove(&short_id);
757+
}
758+
let shutdown_res = $channel.force_shutdown(false);
759+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update(&$channel).ok()))
760+
}
761+
}
762+
}
763+
}
764+
739765
macro_rules! break_chan_entry {
740766
($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
741767
match $res {
742768
Ok(res) => res,
743-
Err(ChannelError::Ignore(msg)) => {
744-
break Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
745-
},
746-
Err(ChannelError::Close(msg)) => {
747-
log_trace!($self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
748-
let (channel_id, mut chan) = $entry.remove_entry();
749-
if let Some(short_id) = chan.get_short_channel_id() {
750-
$channel_state.short_to_id.remove(&short_id);
769+
Err(e) => {
770+
let (drop, res) = convert_chan_err!($self, e, $channel_state.short_to_id, $entry.get_mut(), $entry.key());
771+
if drop {
772+
$entry.remove_entry();
751773
}
752-
break Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok()))
753-
},
754-
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"); }
774+
break Err(res);
775+
}
755776
}
756777
}
757778
}
@@ -760,25 +781,12 @@ macro_rules! try_chan_entry {
760781
($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
761782
match $res {
762783
Ok(res) => res,
763-
Err(ChannelError::Ignore(msg)) => {
764-
return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
765-
},
766-
Err(ChannelError::Close(msg)) => {
767-
log_trace!($self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
768-
let (channel_id, mut chan) = $entry.remove_entry();
769-
if let Some(short_id) = chan.get_short_channel_id() {
770-
$channel_state.short_to_id.remove(&short_id);
771-
}
772-
return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok()))
773-
},
774-
Err(ChannelError::CloseDelayBroadcast(msg)) => {
775-
log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($entry.key()[..]), msg);
776-
let (channel_id, mut chan) = $entry.remove_entry();
777-
if let Some(short_id) = chan.get_short_channel_id() {
778-
$channel_state.short_to_id.remove(&short_id);
784+
Err(e) => {
785+
let (drop, res) = convert_chan_err!($self, e, $channel_state.short_to_id, $entry.get_mut(), $entry.key());
786+
if drop {
787+
$entry.remove_entry();
779788
}
780-
let shutdown_res = chan.force_shutdown(false);
781-
return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, $self.get_channel_update(&chan).ok()))
789+
return Err(res);
782790
}
783791
}
784792
}

0 commit comments

Comments
 (0)