@@ -8315,26 +8315,21 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
83158315 if let Some(msg_send_event) = msg_send_event_opt {
83168316 peer_state.pending_msg_events.push(msg_send_event);
83178317 };
8318- if let Some(mut signing_session) = signing_session_opt {
8318+ if let Some(signing_session) = signing_session_opt {
83198319 let (commitment_signed, funding_ready_for_sig_event_opt) = match chan_phase_entry.get_mut().as_unfunded_v2_mut() {
83208320 Some(chan) => {
8321- chan.funding_tx_constructed(&mut signing_session, &self.logger)
8321+ let (commitment_signed, funding_ready_for_sig_event_opt) =
8322+ chan.funding_tx_constructed(signing_session, &self.logger)
8323+ .map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
8324+ (commitment_signed, funding_ready_for_sig_event_opt)
83228325 },
8323- None => Err(ChannelError::Warn(
8324- "Got a tx_complete message with no interactive transaction construction expected or in-progress"
8325- .into())),
8326- }.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
8327- let (channel_id, channel_phase) = chan_phase_entry.remove_entry();
8328- let channel = match channel_phase.into_unfunded_v2() {
8329- Some(chan) => chan.into_channel(signing_session),
83308326 None => {
8331- debug_assert!(false); // It cannot be another variant as we are in the `Ok` branch of the above match.
8332- Err(ChannelError::Warn(
8327+ debug_assert!(false, "Should be in UnfundedV2 phase as checked earlier");
8328+ return Err(ChannelError::Warn(
83338329 "Got a tx_complete message with no interactive transaction construction expected or in-progress"
8334- .into()))
8335- },
8336- }.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
8337- peer_state.channel_by_id.insert(channel_id, Channel::from(channel));
8330+ .into())).map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id));
8331+ }
8332+ };
83388333 if let Some(funding_ready_for_sig_event) = funding_ready_for_sig_event_opt {
83398334 let mut pending_events = self.pending_events.lock().unwrap();
83408335 pending_events.push_back((funding_ready_for_sig_event, None));
@@ -8850,46 +8845,88 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
88508845 })?;
88518846 let mut peer_state_lock = peer_state_mutex.lock().unwrap();
88528847 let peer_state = &mut *peer_state_lock;
8853- match peer_state.channel_by_id.entry(msg.channel_id) {
8854- hash_map::Entry::Occupied(mut chan_phase_entry) => {
8855- if let Some(chan) = chan_phase_entry.get_mut().as_funded_mut() {
8856- let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8857- let funding_txo = chan.context.get_funding_txo();
8858-
8859- if chan.interactive_tx_signing_session.is_some() {
8860- let monitor = try_chan_phase_entry!(
8861- self, peer_state, chan.commitment_signed_initial_v2(msg, best_block, &self.signer_provider, &&logger),
8862- chan_phase_entry);
8863- let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
8864- if let Ok(persist_state) = monitor_res {
8865- handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
8866- per_peer_state, chan, INITIAL_MONITOR);
8867- } else {
8868- let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8869- log_error!(logger, "Persisting initial ChannelMonitor failed, implying the funding outpoint was duplicated");
8870- try_chan_phase_entry!(self, peer_state, Err(ChannelError::Close(
8848+ let (channel_id, mut funded_channel) = match peer_state.channel_by_id.entry(msg.channel_id) {
8849+ hash_map::Entry::Occupied(mut channel_entry) => {
8850+ match channel_entry.get_mut() {
8851+ Channel::UnfundedV2(chan) => {
8852+ if let Some(signing_session) = chan.interactive_tx_signing_session.take() {
8853+ let (channel_id, mut channel) = channel_entry.remove_entry();
8854+ if let Channel::UnfundedV2(chan) = channel {
88718855 (
8872- "Channel funding outpoint was a duplicate".to_owned(),
8873- ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
8856+ channel_id,
8857+ chan.into_channel(signing_session)
8858+ .map_err(|(mut chan, err)|
8859+ convert_chan_phase_err!(self, peer_state, err, chan.context, &channel_id, UNFUNDED_CHANNEL).1
8860+ )?
88748861 )
8875- )), chan_phase_entry)
8862+ } else {
8863+ debug_assert!(false, "The channel phase was not UnfundedV2");
8864+ let err = ChannelError::close(
8865+ "Closing due to unexpected sender error".into());
8866+ return Err(convert_chan_phase_err!(self, peer_state, err, &mut channel,
8867+ &channel_id).1)
8868+ }
8869+ } else {
8870+ return try_chan_phase_entry!(self, peer_state, Err(ChannelError::close(
8871+ "Got a commitment_signed message for a V2 channel before funding transaction constructed!".into())), channel_entry);
88768872 }
8877- } else {
8873+ },
8874+ Channel::Funded(chan) => {
8875+ let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8876+ let funding_txo = chan.context.get_funding_txo();
88788877 let monitor_update_opt = try_chan_phase_entry!(
8879- self, peer_state, chan.commitment_signed(msg, &&logger), chan_phase_entry );
8878+ self, peer_state, chan.commitment_signed(msg, &&logger), channel_entry );
88808879 if let Some(monitor_update) = monitor_update_opt {
88818880 handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update, peer_state_lock,
88828881 peer_state, per_peer_state, chan);
88838882 }
8883+ return Ok(())
8884+ },
8885+ _ => {
8886+ return try_chan_phase_entry!(self, peer_state, Err(ChannelError::close(
8887+ "Got a commitment_signed message for an unfunded channel!".into())), channel_entry);
88848888 }
8885- Ok(())
8886- } else {
8887- return try_chan_phase_entry!(self, peer_state, Err(ChannelError::close(
8888- "Got a commitment_signed message for an unfunded channel!".into())), chan_phase_entry);
88898889 }
88908890 },
8891- hash_map::Entry::Vacant(_) => Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
8891+ hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(
8892+ format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}",
8893+ counterparty_node_id), msg.channel_id))
8894+ };
8895+ let logger = WithChannelContext::from(&self.logger, &funded_channel.context, None);
8896+ let monitor = match funded_channel.commitment_signed_initial_v2(msg, best_block, &self.signer_provider, &&logger) {
8897+ Ok(monitor) => monitor,
8898+ Err(err) => {
8899+ let channel = &mut Channel::Funded(funded_channel);
8900+ return Err(convert_chan_phase_err!(self, peer_state, err, channel, &channel_id).1);
8901+ }
8902+ };
8903+ let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
8904+ if let Ok(persist_state) = monitor_res {
8905+ let mut occupied_entry = peer_state.channel_by_id.entry(channel_id).insert(Channel::Funded(funded_channel));
8906+ let channel_entry = occupied_entry.get_mut();
8907+ match channel_entry {
8908+ Channel::Funded(chan) => { handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
8909+ per_peer_state, chan, INITIAL_MONITOR); },
8910+ channel => {
8911+ debug_assert!(false, "Expected a Channel::Funded");
8912+ let err = ChannelError::close(
8913+ "Closing due to unexpected sender error".into());
8914+ return Err(convert_chan_phase_err!(self, peer_state, err, channel,
8915+ &channel_id).1)
8916+ },
8917+ }
8918+ } else {
8919+ log_error!(logger, "Persisting initial ChannelMonitor failed, implying the funding outpoint was duplicated");
8920+ let err = ChannelError::Close(
8921+ (
8922+ "Channel funding outpoint was a duplicate".to_owned(),
8923+ ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
8924+ )
8925+ );
8926+ let chan = &mut Channel::Funded(funded_channel);
8927+ return Err(convert_chan_phase_err!(self, peer_state, err, chan, &channel_id).1);
88928928 }
8929+ Ok(())
88938930 }
88948931
88958932 fn push_decode_update_add_htlcs(&self, mut update_add_htlcs: (u64, Vec<msgs::UpdateAddHTLC>)) {
0 commit comments