@@ -7092,6 +7092,18 @@ impl<SP: Deref> FundedChannel<SP> where
7092
7092
assert!(self.context.channel_state.is_monitor_update_in_progress());
7093
7093
self.context.channel_state.clear_monitor_update_in_progress();
7094
7094
7095
+ // For channels established with V2 establishment we won't send a `tx_signatures` when we're in
7096
+ // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
7097
+ // transaction and waits for us to do it).
7098
+ let tx_signatures = self.context.monitor_pending_tx_signatures.take();
7099
+ if tx_signatures.is_some() {
7100
+ if self.context.channel_state.is_their_tx_signatures_sent() {
7101
+ self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
7102
+ } else {
7103
+ self.context.channel_state.set_our_tx_signatures_ready();
7104
+ }
7105
+ }
7106
+
7095
7107
// If we're past (or at) the AwaitingChannelReady stage on an outbound (or V2-established) channel,
7096
7108
// try to (re-)broadcast the funding transaction as we may have declined to broadcast it when we
7097
7109
// first received the funding_signed.
@@ -7131,17 +7143,6 @@ impl<SP: Deref> FundedChannel<SP> where
7131
7143
mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
7132
7144
let mut pending_update_adds = Vec::new();
7133
7145
mem::swap(&mut pending_update_adds, &mut self.context.monitor_pending_update_adds);
7134
- // For channels established with V2 establishment we won't send a `tx_signatures` when we're in
7135
- // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
7136
- // transaction and waits for us to do it).
7137
- let tx_signatures = self.context.monitor_pending_tx_signatures.take();
7138
- if tx_signatures.is_some() {
7139
- if self.context.channel_state.is_their_tx_signatures_sent() {
7140
- self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
7141
- } else {
7142
- self.context.channel_state.set_our_tx_signatures_ready();
7143
- }
7144
- }
7145
7146
7146
7147
if self.context.channel_state.is_peer_disconnected() {
7147
7148
self.context.monitor_pending_revoke_and_ack = false;
@@ -8386,7 +8387,7 @@ impl<SP: Deref> FundedChannel<SP> where
8386
8387
/// advanced state.
8387
8388
pub fn is_awaiting_initial_mon_persist(&self) -> bool {
8388
8389
if !self.is_awaiting_monitor_update() { return false; }
8389
- if matches!(
8390
+ if self.context.channel_state.is_interactive_signing() || matches!(
8390
8391
self.context.channel_state, ChannelState::AwaitingChannelReady(flags)
8391
8392
if flags.clone().clear(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY | FundedStateFlags::PEER_DISCONNECTED | FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS | AwaitingChannelReadyFlags::WAITING_FOR_BATCH).is_empty()
8392
8393
) {
@@ -10668,6 +10669,31 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
10668
10669
our_funding_inputs: our_funding_inputs.clone(),
10669
10670
};
10670
10671
10672
+ // Optionally add change output
10673
+ let change_script = signer_provider.get_destination_script(context.channel_keys_id)
10674
+ .map_err(|_| ChannelError::close("Error getting change destination script".to_string()))?;
10675
+ let change_value_opt = calculate_change_output_value(
10676
+ funding.is_outbound(), dual_funding_context.our_funding_satoshis,
10677
+ &our_funding_inputs, &vec![],
10678
+ dual_funding_context.funding_feerate_sat_per_1000_weight,
10679
+ change_script.minimal_non_dust().to_sat(),
10680
+ ).map_err(|_| ChannelError::close("Error calculating change output value".to_string()))?;
10681
+ let mut our_funding_outputs = vec![];
10682
+ if let Some(change_value) = change_value_opt {
10683
+ let mut change_output = TxOut {
10684
+ value: Amount::from_sat(change_value),
10685
+ script_pubkey: change_script,
10686
+ };
10687
+ let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
10688
+ let change_output_fee = fee_for_weight(dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
10689
+ let change_value_decreased_with_fee = change_value.saturating_sub(change_output_fee);
10690
+ // Check dust limit again
10691
+ if change_value_decreased_with_fee > context.holder_dust_limit_satoshis {
10692
+ change_output.value = Amount::from_sat(change_value_decreased_with_fee);
10693
+ our_funding_outputs.push(OutputOwned::Single(change_output));
10694
+ }
10695
+ }
10696
+
10671
10697
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
10672
10698
InteractiveTxConstructorArgs {
10673
10699
entropy_source,
@@ -10678,7 +10704,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
10678
10704
funding_tx_locktime: dual_funding_context.funding_tx_locktime,
10679
10705
is_initiator: false,
10680
10706
inputs_to_contribute: our_funding_inputs,
10681
- outputs_to_contribute: Vec::new() ,
10707
+ outputs_to_contribute: our_funding_outputs ,
10682
10708
expected_remote_shared_funding_output: Some((funding.get_funding_redeemscript().to_p2wsh(), funding.get_value_satoshis())),
10683
10709
}
10684
10710
).map_err(|_| ChannelError::Close((
0 commit comments