@@ -7422,6 +7422,18 @@ where
74227422 assert!(self.context.channel_state.is_monitor_update_in_progress());
74237423 self.context.channel_state.clear_monitor_update_in_progress();
74247424
7425+ // For channels established with V2 establishment we won't send a `tx_signatures` when we're in
7426+ // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
7427+ // transaction and waits for us to do it).
7428+ let tx_signatures = self.context.monitor_pending_tx_signatures.take();
7429+ if tx_signatures.is_some() {
7430+ if self.context.channel_state.is_their_tx_signatures_sent() {
7431+ self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
7432+ } else {
7433+ self.context.channel_state.set_our_tx_signatures_ready();
7434+ }
7435+ }
7436+
74257437 // If we're past (or at) the AwaitingChannelReady stage on an outbound (or V2-established) channel,
74267438 // try to (re-)broadcast the funding transaction as we may have declined to broadcast it when we
74277439 // first received the funding_signed.
@@ -7461,17 +7473,6 @@ where
74617473 mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
74627474 let mut pending_update_adds = Vec::new();
74637475 mem::swap(&mut pending_update_adds, &mut self.context.monitor_pending_update_adds);
7464- // For channels established with V2 establishment we won't send a `tx_signatures` when we're in
7465- // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
7466- // transaction and waits for us to do it).
7467- let tx_signatures = self.context.monitor_pending_tx_signatures.take();
7468- if tx_signatures.is_some() {
7469- if self.context.channel_state.is_their_tx_signatures_sent() {
7470- self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
7471- } else {
7472- self.context.channel_state.set_our_tx_signatures_ready();
7473- }
7474- }
74757476
74767477 if self.context.channel_state.is_peer_disconnected() {
74777478 self.context.monitor_pending_revoke_and_ack = false;
@@ -8760,7 +8761,7 @@ where
87608761 #[rustfmt::skip]
87618762 pub fn is_awaiting_initial_mon_persist(&self) -> bool {
87628763 if !self.is_awaiting_monitor_update() { return false; }
8763- if matches!(
8764+ if self.context.channel_state.is_interactive_signing() || matches!(
87648765 self.context.channel_state, ChannelState::AwaitingChannelReady(flags)
87658766 if flags.clone().clear(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY | FundedStateFlags::PEER_DISCONNECTED | FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS | AwaitingChannelReadyFlags::WAITING_FOR_BATCH).is_empty()
87668767 ) {
@@ -11117,6 +11118,31 @@ where
1111711118 our_funding_inputs: our_funding_inputs.clone(),
1111811119 };
1111911120
11121+ // Optionally add change output
11122+ let change_script = signer_provider.get_destination_script(context.channel_keys_id)
11123+ .map_err(|_| ChannelError::close("Error getting change destination script".to_string()))?;
11124+ let change_value_opt = calculate_change_output_value(
11125+ funding.is_outbound(), dual_funding_context.our_funding_satoshis,
11126+ &our_funding_inputs, &vec![],
11127+ dual_funding_context.funding_feerate_sat_per_1000_weight,
11128+ change_script.minimal_non_dust().to_sat(),
11129+ ).map_err(|_| ChannelError::close("Error calculating change output value".to_string()))?;
11130+ let mut our_funding_outputs = vec![];
11131+ if let Some(change_value) = change_value_opt {
11132+ let mut change_output = TxOut {
11133+ value: Amount::from_sat(change_value),
11134+ script_pubkey: change_script,
11135+ };
11136+ let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
11137+ let change_output_fee = fee_for_weight(dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
11138+ let change_value_decreased_with_fee = change_value.saturating_sub(change_output_fee);
11139+ // Check dust limit again
11140+ if change_value_decreased_with_fee > context.holder_dust_limit_satoshis {
11141+ change_output.value = Amount::from_sat(change_value_decreased_with_fee);
11142+ our_funding_outputs.push(OutputOwned::Single(change_output));
11143+ }
11144+ }
11145+
1112011146 let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
1112111147 InteractiveTxConstructorArgs {
1112211148 entropy_source,
@@ -11127,7 +11153,7 @@ where
1112711153 funding_tx_locktime: dual_funding_context.funding_tx_locktime,
1112811154 is_initiator: false,
1112911155 inputs_to_contribute: our_funding_inputs,
11130- outputs_to_contribute: Vec::new() ,
11156+ outputs_to_contribute: our_funding_outputs ,
1113111157 expected_remote_shared_funding_output: Some((funding.get_funding_redeemscript().to_p2wsh(), funding.get_value_satoshis())),
1113211158 }
1113311159 ).map_err(|_| ChannelError::Close((
0 commit comments