@@ -6959,6 +6959,18 @@ impl<SP: Deref> FundedChannel<SP> where
69596959 assert!(self.context.channel_state.is_monitor_update_in_progress());
69606960 self.context.channel_state.clear_monitor_update_in_progress();
69616961
6962+ // For channels established with V2 establishment we won't send a `tx_signatures` when we're in
6963+ // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
6964+ // transaction and waits for us to do it).
6965+ let tx_signatures = self.context.monitor_pending_tx_signatures.take();
6966+ if tx_signatures.is_some() {
6967+ if self.context.channel_state.is_their_tx_signatures_sent() {
6968+ self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
6969+ } else {
6970+ self.context.channel_state.set_our_tx_signatures_ready();
6971+ }
6972+ }
6973+
69626974 // If we're past (or at) the AwaitingChannelReady stage on an outbound (or V2-established) channel,
69636975 // try to (re-)broadcast the funding transaction as we may have declined to broadcast it when we
69646976 // first received the funding_signed.
@@ -6998,17 +7010,6 @@ impl<SP: Deref> FundedChannel<SP> where
69987010 mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
69997011 let mut pending_update_adds = Vec::new();
70007012 mem::swap(&mut pending_update_adds, &mut self.context.monitor_pending_update_adds);
7001- // For channels established with V2 establishment we won't send a `tx_signatures` when we're in
7002- // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
7003- // transaction and waits for us to do it).
7004- let tx_signatures = self.context.monitor_pending_tx_signatures.take();
7005- if tx_signatures.is_some() {
7006- if self.context.channel_state.is_their_tx_signatures_sent() {
7007- self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
7008- } else {
7009- self.context.channel_state.set_our_tx_signatures_ready();
7010- }
7011- }
70127013
70137014 if self.context.channel_state.is_peer_disconnected() {
70147015 self.context.monitor_pending_revoke_and_ack = false;
@@ -8319,7 +8320,7 @@ impl<SP: Deref> FundedChannel<SP> where
83198320 /// advanced state.
83208321 pub fn is_awaiting_initial_mon_persist(&self) -> bool {
83218322 if !self.is_awaiting_monitor_update() { return false; }
8322- if matches!(
8323+ if self.context.channel_state.is_interactive_signing() || matches!(
83238324 self.context.channel_state, ChannelState::AwaitingChannelReady(flags)
83248325 if flags.clone().clear(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY | FundedStateFlags::PEER_DISCONNECTED | FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS | AwaitingChannelReadyFlags::WAITING_FOR_BATCH).is_empty()
83258326 ) {
@@ -10606,6 +10607,31 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1060610607 our_funding_inputs: our_funding_inputs.clone(),
1060710608 };
1060810609
10610+ // Optionally add change output
10611+ let change_script = signer_provider.get_destination_script(context.channel_keys_id)
10612+ .map_err(|_| ChannelError::close("Error getting change destination script".to_string()))?;
10613+ let change_value_opt = calculate_change_output_value(
10614+ funding.is_outbound(), dual_funding_context.our_funding_satoshis,
10615+ &our_funding_inputs, &vec![],
10616+ dual_funding_context.funding_feerate_sat_per_1000_weight,
10617+ change_script.minimal_non_dust().to_sat(),
10618+ ).map_err(|_| ChannelError::close("Error calculating change output value".to_string()))?;
10619+ let mut our_funding_outputs = vec![];
10620+ if let Some(change_value) = change_value_opt {
10621+ let mut change_output = TxOut {
10622+ value: Amount::from_sat(change_value),
10623+ script_pubkey: change_script,
10624+ };
10625+ let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
10626+ let change_output_fee = fee_for_weight(dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
10627+ let change_value_decreased_with_fee = change_value.saturating_sub(change_output_fee);
10628+ // Check dust limit again
10629+ if change_value_decreased_with_fee > context.holder_dust_limit_satoshis {
10630+ change_output.value = Amount::from_sat(change_value_decreased_with_fee);
10631+ our_funding_outputs.push(OutputOwned::Single(change_output));
10632+ }
10633+ }
10634+
1060910635 let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
1061010636 InteractiveTxConstructorArgs {
1061110637 entropy_source,
@@ -10616,7 +10642,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1061610642 funding_tx_locktime: dual_funding_context.funding_tx_locktime,
1061710643 is_initiator: false,
1061810644 inputs_to_contribute: our_funding_inputs,
10619- outputs_to_contribute: Vec::new() ,
10645+ outputs_to_contribute: our_funding_outputs ,
1062010646 expected_remote_shared_funding_output: Some((funding.get_funding_redeemscript().to_p2wsh(), funding.get_value_satoshis())),
1062110647 }
1062210648 ).map_err(|_| ChannelError::Close((
0 commit comments