Skip to content

Commit ae71a0e

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 9736f85 commit ae71a0e

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
@@ -666,22 +666,43 @@ macro_rules! handle_error {
666666
}
667667
}
668668

669+
macro_rules! convert_chan_err {
670+
($self: ident, $err: expr, $short_to_id: expr, $channel: expr, $channel_id: expr) => {
671+
match $err {
672+
ChannelError::Ignore(msg) => {
673+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $channel_id.clone()))
674+
},
675+
ChannelError::Close(msg) => {
676+
log_trace!($self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!($channel_id[..]), msg);
677+
if let Some(short_id) = $channel.get_short_channel_id() {
678+
$short_to_id.remove(&short_id);
679+
}
680+
let shutdown_res = $channel.force_shutdown(true);
681+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update(&$channel).ok()))
682+
},
683+
ChannelError::CloseDelayBroadcast(msg) => {
684+
log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($channel_id[..]), msg);
685+
if let Some(short_id) = $channel.get_short_channel_id() {
686+
$short_to_id.remove(&short_id);
687+
}
688+
let shutdown_res = $channel.force_shutdown(false);
689+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update(&$channel).ok()))
690+
}
691+
}
692+
}
693+
}
694+
669695
macro_rules! break_chan_entry {
670696
($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
671697
match $res {
672698
Ok(res) => res,
673-
Err(ChannelError::Ignore(msg)) => {
674-
break Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
675-
},
676-
Err(ChannelError::Close(msg)) => {
677-
log_trace!($self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
678-
let (channel_id, mut chan) = $entry.remove_entry();
679-
if let Some(short_id) = chan.get_short_channel_id() {
680-
$channel_state.short_to_id.remove(&short_id);
699+
Err(e) => {
700+
let (drop, res) = convert_chan_err!($self, e, $channel_state.short_to_id, $entry.get_mut(), $entry.key());
701+
if drop {
702+
$entry.remove_entry();
681703
}
682-
break Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok()))
683-
},
684-
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"); }
704+
break Err(res);
705+
}
685706
}
686707
}
687708
}
@@ -690,25 +711,12 @@ macro_rules! try_chan_entry {
690711
($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
691712
match $res {
692713
Ok(res) => res,
693-
Err(ChannelError::Ignore(msg)) => {
694-
return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
695-
},
696-
Err(ChannelError::Close(msg)) => {
697-
log_trace!($self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
698-
let (channel_id, mut chan) = $entry.remove_entry();
699-
if let Some(short_id) = chan.get_short_channel_id() {
700-
$channel_state.short_to_id.remove(&short_id);
701-
}
702-
return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok()))
703-
},
704-
Err(ChannelError::CloseDelayBroadcast(msg)) => {
705-
log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($entry.key()[..]), msg);
706-
let (channel_id, mut chan) = $entry.remove_entry();
707-
if let Some(short_id) = chan.get_short_channel_id() {
708-
$channel_state.short_to_id.remove(&short_id);
714+
Err(e) => {
715+
let (drop, res) = convert_chan_err!($self, e, $channel_state.short_to_id, $entry.get_mut(), $entry.key());
716+
if drop {
717+
$entry.remove_entry();
709718
}
710-
let shutdown_res = chan.force_shutdown(false);
711-
return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, $self.get_channel_update(&chan).ok()))
719+
return Err(res);
712720
}
713721
}
714722
}

0 commit comments

Comments
 (0)