@@ -7936,6 +7936,20 @@ where
79367936 assert!(self.context.channel_state.is_monitor_update_in_progress());
79377937 self.context.channel_state.clear_monitor_update_in_progress();
79387938
7939+ // For channels established with V2 establishment we won't send a `tx_signatures` when we're in
7940+ // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
7941+ // transaction and waits for us to do it).
7942+ let tx_signatures_ready = self.context.monitor_pending_tx_signatures;
7943+ self.context.monitor_pending_tx_signatures = false;
7944+ let tx_signatures = if tx_signatures_ready {
7945+ if self.context.channel_state.is_their_tx_signatures_sent() {
7946+ self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
7947+ } else {
7948+ self.context.channel_state.set_our_tx_signatures_ready();
7949+ }
7950+ self.interactive_tx_signing_session.as_ref().and_then(|session| session.holder_tx_signatures().clone())
7951+ } else { None };
7952+
79397953 // If we're past (or at) the AwaitingChannelReady stage on an outbound (or V2-established) channel,
79407954 // try to (re-)broadcast the funding transaction as we may have declined to broadcast it when we
79417955 // first received the funding_signed.
@@ -7975,19 +7989,6 @@ where
79757989 mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
79767990 let mut pending_update_adds = Vec::new();
79777991 mem::swap(&mut pending_update_adds, &mut self.context.monitor_pending_update_adds);
7978- // For channels established with V2 establishment we won't send a `tx_signatures` when we're in
7979- // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
7980- // transaction and waits for us to do it).
7981- let tx_signatures_ready = self.context.monitor_pending_tx_signatures;
7982- self.context.monitor_pending_tx_signatures = false;
7983- let tx_signatures = if tx_signatures_ready {
7984- if self.context.channel_state.is_their_tx_signatures_sent() {
7985- self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
7986- } else {
7987- self.context.channel_state.set_our_tx_signatures_ready();
7988- }
7989- self.interactive_tx_signing_session.as_ref().and_then(|session| session.holder_tx_signatures().clone())
7990- } else { None };
79917992
79927993 if self.context.channel_state.is_peer_disconnected() {
79937994 self.context.monitor_pending_revoke_and_ack = false;
@@ -9386,7 +9387,7 @@ where
93869387 #[rustfmt::skip]
93879388 pub fn is_awaiting_initial_mon_persist(&self) -> bool {
93889389 if !self.is_awaiting_monitor_update() { return false; }
9389- if matches!(
9390+ if self.context.channel_state.is_interactive_signing() || matches!(
93909391 self.context.channel_state, ChannelState::AwaitingChannelReady(flags)
93919392 if flags.clone().clear(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY | FundedStateFlags::PEER_DISCONNECTED | FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS | AwaitingChannelReadyFlags::WAITING_FOR_BATCH).is_empty()
93929393 ) {
@@ -12057,6 +12058,31 @@ where
1205712058 script_pubkey: funding.get_funding_redeemscript().to_p2wsh(),
1205812059 };
1205912060
12061+ // Optionally add change output
12062+ let change_script = signer_provider.get_destination_script(context.channel_keys_id)
12063+ .map_err(|_| ChannelError::close("Error getting change destination script".to_string()))?;
12064+ let change_value_opt = calculate_change_output_value(
12065+ funding.is_outbound(), dual_funding_context.our_funding_satoshis,
12066+ &our_funding_inputs, None, &shared_funding_output.script_pubkey, &vec![],
12067+ dual_funding_context.funding_feerate_sat_per_1000_weight,
12068+ change_script.minimal_non_dust().to_sat(),
12069+ ).map_err(|_| ChannelError::close("Error calculating change output value".to_string()))?;
12070+ let mut our_funding_outputs = vec![];
12071+ if let Some(change_value) = change_value_opt {
12072+ let mut change_output = TxOut {
12073+ value: Amount::from_sat(change_value),
12074+ script_pubkey: change_script,
12075+ };
12076+ let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
12077+ let change_output_fee = fee_for_weight(dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
12078+ let change_value_decreased_with_fee = change_value.saturating_sub(change_output_fee);
12079+ // Check dust limit again
12080+ if change_value_decreased_with_fee > context.holder_dust_limit_satoshis {
12081+ change_output.value = Amount::from_sat(change_value_decreased_with_fee);
12082+ our_funding_outputs.push(change_output);
12083+ }
12084+ }
12085+
1206012086 let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
1206112087 InteractiveTxConstructorArgs {
1206212088 entropy_source,
@@ -12069,7 +12095,7 @@ where
1206912095 inputs_to_contribute: our_funding_inputs,
1207012096 shared_funding_input: None,
1207112097 shared_funding_output: (shared_funding_output, our_funding_satoshis),
12072- outputs_to_contribute: Vec::new() ,
12098+ outputs_to_contribute: our_funding_outputs ,
1207312099 }
1207412100 ).map_err(|_| ChannelError::Close((
1207512101 "V2 channel rejected due to sender error".into(),
0 commit comments