@@ -6892,6 +6892,18 @@ impl<SP: Deref> FundedChannel<SP> where
68926892 assert!(self.context.channel_state.is_monitor_update_in_progress());
68936893 self.context.channel_state.clear_monitor_update_in_progress();
68946894
6895+ // For channels established with V2 establishment we won't send a `tx_signatures` when we're in
6896+ // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
6897+ // transaction and waits for us to do it).
6898+ let tx_signatures = self.context.monitor_pending_tx_signatures.take();
6899+ if tx_signatures.is_some() {
6900+ if self.context.channel_state.is_their_tx_signatures_sent() {
6901+ self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
6902+ } else {
6903+ self.context.channel_state.set_our_tx_signatures_ready();
6904+ }
6905+ }
6906+
68956907 // If we're past (or at) the AwaitingChannelReady stage on an outbound (or V2-established) channel,
68966908 // try to (re-)broadcast the funding transaction as we may have declined to broadcast it when we
68976909 // first received the funding_signed.
@@ -6931,17 +6943,6 @@ impl<SP: Deref> FundedChannel<SP> where
69316943 mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
69326944 let mut pending_update_adds = Vec::new();
69336945 mem::swap(&mut pending_update_adds, &mut self.context.monitor_pending_update_adds);
6934- // For channels established with V2 establishment we won't send a `tx_signatures` when we're in
6935- // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
6936- // transaction and waits for us to do it).
6937- let tx_signatures = self.context.monitor_pending_tx_signatures.take();
6938- if tx_signatures.is_some() {
6939- if self.context.channel_state.is_their_tx_signatures_sent() {
6940- self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
6941- } else {
6942- self.context.channel_state.set_our_tx_signatures_ready();
6943- }
6944- }
69456946
69466947 if self.context.channel_state.is_peer_disconnected() {
69476948 self.context.monitor_pending_revoke_and_ack = false;
@@ -8246,7 +8247,7 @@ impl<SP: Deref> FundedChannel<SP> where
82468247 /// advanced state.
82478248 pub fn is_awaiting_initial_mon_persist(&self) -> bool {
82488249 if !self.is_awaiting_monitor_update() { return false; }
8249- if matches!(
8250+ if self.context.channel_state.is_interactive_signing() || matches!(
82508251 self.context.channel_state, ChannelState::AwaitingChannelReady(flags)
82518252 if flags.clone().clear(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY | FundedStateFlags::PEER_DISCONNECTED | FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS | AwaitingChannelReadyFlags::WAITING_FOR_BATCH).is_empty()
82528253 ) {
@@ -10531,6 +10532,31 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1053110532 our_funding_inputs: our_funding_inputs.clone(),
1053210533 };
1053310534
10535+ // Optionally add change output
10536+ let change_script = signer_provider.get_destination_script(context.channel_keys_id)
10537+ .map_err(|_| ChannelError::close("Error getting change destination script".to_string()))?;
10538+ let change_value_opt = calculate_change_output_value(
10539+ funding.is_outbound(), dual_funding_context.our_funding_satoshis,
10540+ &our_funding_inputs, &vec![],
10541+ dual_funding_context.funding_feerate_sat_per_1000_weight,
10542+ change_script.minimal_non_dust().to_sat(),
10543+ ).map_err(|_| ChannelError::close("Error calculating change output value".to_string()))?;
10544+ let mut our_funding_outputs = vec![];
10545+ if let Some(change_value) = change_value_opt {
10546+ let mut change_output = TxOut {
10547+ value: Amount::from_sat(change_value),
10548+ script_pubkey: change_script,
10549+ };
10550+ let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
10551+ let change_output_fee = fee_for_weight(dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
10552+ let change_value_decreased_with_fee = change_value.saturating_sub(change_output_fee);
10553+ // Check dust limit again
10554+ if change_value_decreased_with_fee > context.holder_dust_limit_satoshis {
10555+ change_output.value = Amount::from_sat(change_value_decreased_with_fee);
10556+ our_funding_outputs.push(OutputOwned::Single(change_output));
10557+ }
10558+ }
10559+
1053410560 let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
1053510561 InteractiveTxConstructorArgs {
1053610562 entropy_source,
@@ -10541,7 +10567,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1054110567 funding_tx_locktime: dual_funding_context.funding_tx_locktime,
1054210568 is_initiator: false,
1054310569 inputs_to_contribute: our_funding_inputs,
10544- outputs_to_contribute: Vec::new() ,
10570+ outputs_to_contribute: our_funding_outputs ,
1054510571 expected_remote_shared_funding_output: Some((funding.get_funding_redeemscript().to_p2wsh(), funding.get_value_satoshis())),
1054610572 }
1054710573 ).map_err(|_| ChannelError::Close((
0 commit comments