diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 69bcf34b994..89a862b8371 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -6236,25 +6236,27 @@ where let funded_channel_id = chan.context.channel_id(); + macro_rules! fail_chan { ($err: expr) => { { + // Note that at this point we've filled in the funding outpoint on our + // channel, but its actually in conflict with another channel. Thus, if + // we call `convert_chan_phase_err` immediately (thus calling + // `update_maps_on_chan_removal`), we'll remove the existing channel + // from `id_to_peer`. Thus, we must first unset the funding outpoint on + // the channel. + let err = ChannelError::Close($err.to_owned()); + chan.unset_funding_info(msg.temporary_channel_id); + return Err(convert_chan_phase_err!(self, err, &mut ChannelPhase::Funded(chan), &funded_channel_id).1); + } } } + match peer_state.channel_by_id.entry(funded_channel_id) { hash_map::Entry::Occupied(_) => { - let err = ChannelError::Close("Already had channel with the new channel_id".to_owned()); - // Note that at this point we've filled in the funding outpoint on our - // channel, but its actually in conflict with another channel. Thus, if - // we call `convert_chan_phase_err` immediately (thus calling - // `update_maps_on_chan_removal`), we'll remove the existing channel - // from `id_to_peer`. Thus, we must first unset the funding outpoint on - // the channel. - chan.unset_funding_info(msg.temporary_channel_id); - return Err(convert_chan_phase_err!(self, err, &mut ChannelPhase::Funded(chan), &funded_channel_id).1); + fail_chan!(err); }, hash_map::Entry::Vacant(e) => { let mut outpoint_to_peer_lock = self.outpoint_to_peer.lock().unwrap(); match outpoint_to_peer_lock.entry(monitor.get_funding_txo().0) { hash_map::Entry::Occupied(_) => { - return Err(MsgHandleErrInternal::send_err_msg_no_close( - "The funding_created message had the same funding_txid as an existing channel - funding is not possible".to_owned(), - chan.context.channel_id())) + fail_chan!("The funding_created message had the same funding_txid as an existing channel - funding is not possible"); }, hash_map::Entry::Vacant(i_e) => { let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor); @@ -6283,15 +6285,7 @@ where } else { let logger = WithChannelContext::from(&self.logger, &chan.context); log_error!(logger, "Persisting initial ChannelMonitor failed, implying the funding outpoint was duplicated"); - let err = ChannelError::Close("Duplicate funding outpoint".to_owned()); - // Note that at this point we've filled in the funding outpoint on our - // channel, but its actually in conflict with another channel. Thus, if - // we call `convert_chan_phase_err` immediately (thus calling - // `update_maps_on_chan_removal`), we'll remove the existing channel - // from `id_to_peer`. Thus, we must first unset the funding outpoint on - // the channel. - chan.unset_funding_info(msg.temporary_channel_id); - return Err(convert_chan_phase_err!(self, err, &mut ChannelPhase::Funded(chan), &funded_channel_id).1); + fail_chan!("Duplicate funding outpoint"); } } }