@@ -7411,6 +7411,18 @@ where
74117411 assert!(self.context.channel_state.is_monitor_update_in_progress());
74127412 self.context.channel_state.clear_monitor_update_in_progress();
74137413
7414+ // For channels established with V2 establishment we won't send a `tx_signatures` when we're in
7415+ // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
7416+ // transaction and waits for us to do it).
7417+ let tx_signatures = self.context.monitor_pending_tx_signatures.take();
7418+ if tx_signatures.is_some() {
7419+ if self.context.channel_state.is_their_tx_signatures_sent() {
7420+ self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
7421+ } else {
7422+ self.context.channel_state.set_our_tx_signatures_ready();
7423+ }
7424+ }
7425+
74147426 // If we're past (or at) the AwaitingChannelReady stage on an outbound (or V2-established) channel,
74157427 // try to (re-)broadcast the funding transaction as we may have declined to broadcast it when we
74167428 // first received the funding_signed.
@@ -7450,17 +7462,6 @@ where
74507462 mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
74517463 let mut pending_update_adds = Vec::new();
74527464 mem::swap(&mut pending_update_adds, &mut self.context.monitor_pending_update_adds);
7453- // For channels established with V2 establishment we won't send a `tx_signatures` when we're in
7454- // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
7455- // transaction and waits for us to do it).
7456- let tx_signatures = self.context.monitor_pending_tx_signatures.take();
7457- if tx_signatures.is_some() {
7458- if self.context.channel_state.is_their_tx_signatures_sent() {
7459- self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
7460- } else {
7461- self.context.channel_state.set_our_tx_signatures_ready();
7462- }
7463- }
74647465
74657466 if self.context.channel_state.is_peer_disconnected() {
74667467 self.context.monitor_pending_revoke_and_ack = false;
@@ -8749,7 +8750,7 @@ where
87498750 #[rustfmt::skip]
87508751 pub fn is_awaiting_initial_mon_persist(&self) -> bool {
87518752 if !self.is_awaiting_monitor_update() { return false; }
8752- if matches!(
8753+ if self.context.channel_state.is_interactive_signing() || matches!(
87538754 self.context.channel_state, ChannelState::AwaitingChannelReady(flags)
87548755 if flags.clone().clear(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY | FundedStateFlags::PEER_DISCONNECTED | FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS | AwaitingChannelReadyFlags::WAITING_FOR_BATCH).is_empty()
87558756 ) {
@@ -11110,6 +11111,31 @@ where
1111011111 script_pubkey: funding.get_funding_redeemscript().to_p2wsh(),
1111111112 };
1111211113
11114+ // Optionally add change output
11115+ let change_script = signer_provider.get_destination_script(context.channel_keys_id)
11116+ .map_err(|_| ChannelError::close("Error getting change destination script".to_string()))?;
11117+ let change_value_opt = calculate_change_output_value(
11118+ funding.is_outbound(), dual_funding_context.our_funding_satoshis,
11119+ &our_funding_inputs, None, &shared_funding_output.script_pubkey, &vec![],
11120+ dual_funding_context.funding_feerate_sat_per_1000_weight,
11121+ change_script.minimal_non_dust().to_sat(),
11122+ ).map_err(|_| ChannelError::close("Error calculating change output value".to_string()))?;
11123+ let mut our_funding_outputs = vec![];
11124+ if let Some(change_value) = change_value_opt {
11125+ let mut change_output = TxOut {
11126+ value: Amount::from_sat(change_value),
11127+ script_pubkey: change_script,
11128+ };
11129+ let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
11130+ let change_output_fee = fee_for_weight(dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
11131+ let change_value_decreased_with_fee = change_value.saturating_sub(change_output_fee);
11132+ // Check dust limit again
11133+ if change_value_decreased_with_fee > context.holder_dust_limit_satoshis {
11134+ change_output.value = Amount::from_sat(change_value_decreased_with_fee);
11135+ our_funding_outputs.push(change_output);
11136+ }
11137+ }
11138+
1111311139 let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
1111411140 InteractiveTxConstructorArgs {
1111511141 entropy_source,
@@ -11122,7 +11148,7 @@ where
1112211148 inputs_to_contribute: our_funding_inputs,
1112311149 shared_funding_input: None,
1112411150 shared_funding_output: (shared_funding_output, our_funding_satoshis),
11125- outputs_to_contribute: Vec::new() ,
11151+ outputs_to_contribute: our_funding_outputs ,
1112611152 }
1112711153 ).map_err(|_| ChannelError::Close((
1112811154 "V2 channel rejected due to sender error".into(),
0 commit comments