@@ -6959,6 +6959,18 @@ impl<SP: Deref> FundedChannel<SP> where
6959
6959
assert!(self.context.channel_state.is_monitor_update_in_progress());
6960
6960
self.context.channel_state.clear_monitor_update_in_progress();
6961
6961
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
+
6962
6974
// If we're past (or at) the AwaitingChannelReady stage on an outbound (or V2-established) channel,
6963
6975
// try to (re-)broadcast the funding transaction as we may have declined to broadcast it when we
6964
6976
// first received the funding_signed.
@@ -6998,17 +7010,6 @@ impl<SP: Deref> FundedChannel<SP> where
6998
7010
mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
6999
7011
let mut pending_update_adds = Vec::new();
7000
7012
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
- }
7012
7013
7013
7014
if self.context.channel_state.is_peer_disconnected() {
7014
7015
self.context.monitor_pending_revoke_and_ack = false;
@@ -8319,7 +8320,7 @@ impl<SP: Deref> FundedChannel<SP> where
8319
8320
/// advanced state.
8320
8321
pub fn is_awaiting_initial_mon_persist(&self) -> bool {
8321
8322
if !self.is_awaiting_monitor_update() { return false; }
8322
- if matches!(
8323
+ if self.context.channel_state.is_interactive_signing() || matches!(
8323
8324
self.context.channel_state, ChannelState::AwaitingChannelReady(flags)
8324
8325
if flags.clone().clear(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY | FundedStateFlags::PEER_DISCONNECTED | FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS | AwaitingChannelReadyFlags::WAITING_FOR_BATCH).is_empty()
8325
8326
) {
@@ -10606,6 +10607,31 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
10606
10607
our_funding_inputs: our_funding_inputs.clone(),
10607
10608
};
10608
10609
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
+
10609
10635
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
10610
10636
InteractiveTxConstructorArgs {
10611
10637
entropy_source,
@@ -10616,7 +10642,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
10616
10642
funding_tx_locktime: dual_funding_context.funding_tx_locktime,
10617
10643
is_initiator: false,
10618
10644
inputs_to_contribute: our_funding_inputs,
10619
- outputs_to_contribute: Vec::new() ,
10645
+ outputs_to_contribute: our_funding_outputs ,
10620
10646
expected_remote_shared_funding_output: Some((funding.get_funding_redeemscript().to_p2wsh(), funding.get_value_satoshis())),
10621
10647
}
10622
10648
).map_err(|_| ChannelError::Close((
0 commit comments