Skip to content

Commit e33012f

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 2934c37 commit e33012f

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
@@ -695,22 +695,43 @@ macro_rules! handle_error {
695695
}
696696
}
697697

698+
macro_rules! convert_chan_err {
699+
($self: ident, $err: expr, $short_to_id: expr, $channel: expr, $channel_id: expr) => {
700+
match $err {
701+
ChannelError::Ignore(msg) => {
702+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $channel_id.clone()))
703+
},
704+
ChannelError::Close(msg) => {
705+
log_trace!($self.logger, "Closing channel {} due to close-required error: {}", log_bytes!($channel_id[..]), msg);
706+
if let Some(short_id) = $channel.get_short_channel_id() {
707+
$short_to_id.remove(&short_id);
708+
}
709+
let shutdown_res = $channel.force_shutdown(true);
710+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update(&$channel).ok()))
711+
},
712+
ChannelError::CloseDelayBroadcast(msg) => {
713+
log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($channel_id[..]), msg);
714+
if let Some(short_id) = $channel.get_short_channel_id() {
715+
$short_to_id.remove(&short_id);
716+
}
717+
let shutdown_res = $channel.force_shutdown(false);
718+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update(&$channel).ok()))
719+
}
720+
}
721+
}
722+
}
723+
698724
macro_rules! break_chan_entry {
699725
($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
700726
match $res {
701727
Ok(res) => res,
702-
Err(ChannelError::Ignore(msg)) => {
703-
break Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
704-
},
705-
Err(ChannelError::Close(msg)) => {
706-
log_trace!($self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
707-
let (channel_id, mut chan) = $entry.remove_entry();
708-
if let Some(short_id) = chan.get_short_channel_id() {
709-
$channel_state.short_to_id.remove(&short_id);
728+
Err(e) => {
729+
let (drop, res) = convert_chan_err!($self, e, $channel_state.short_to_id, $entry.get_mut(), $entry.key());
730+
if drop {
731+
$entry.remove_entry();
710732
}
711-
break Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok()))
712-
},
713-
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"); }
733+
break Err(res);
734+
}
714735
}
715736
}
716737
}
@@ -719,25 +740,12 @@ macro_rules! try_chan_entry {
719740
($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
720741
match $res {
721742
Ok(res) => res,
722-
Err(ChannelError::Ignore(msg)) => {
723-
return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
724-
},
725-
Err(ChannelError::Close(msg)) => {
726-
log_trace!($self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
727-
let (channel_id, mut chan) = $entry.remove_entry();
728-
if let Some(short_id) = chan.get_short_channel_id() {
729-
$channel_state.short_to_id.remove(&short_id);
730-
}
731-
return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok()))
732-
},
733-
Err(ChannelError::CloseDelayBroadcast(msg)) => {
734-
log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($entry.key()[..]), msg);
735-
let (channel_id, mut chan) = $entry.remove_entry();
736-
if let Some(short_id) = chan.get_short_channel_id() {
737-
$channel_state.short_to_id.remove(&short_id);
743+
Err(e) => {
744+
let (drop, res) = convert_chan_err!($self, e, $channel_state.short_to_id, $entry.get_mut(), $entry.key());
745+
if drop {
746+
$entry.remove_entry();
738747
}
739-
let shutdown_res = chan.force_shutdown(false);
740-
return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, $self.get_channel_update(&chan).ok()))
748+
return Err(res);
741749
}
742750
}
743751
}

0 commit comments

Comments
 (0)