@@ -6892,10 +6892,24 @@ impl<SP: Deref> FundedChannel<SP> where
6892
6892
assert!(self.context.channel_state.is_monitor_update_in_progress());
6893
6893
self.context.channel_state.clear_monitor_update_in_progress();
6894
6894
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
+
6895
6907
// If we're past (or at) the AwaitingChannelReady stage on an outbound (or V2-established) channel,
6896
6908
// try to (re-)broadcast the funding transaction as we may have declined to broadcast it when we
6897
6909
// first received the funding_signed.
6898
6910
let mut funding_broadcastable = None;
6911
+ dbg!(&self.funding.funding_transaction);
6912
+ dbg!(matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(_)));
6899
6913
if let Some(funding_transaction) = &self.funding.funding_transaction {
6900
6914
if (self.funding.is_outbound() || self.is_v2_established()) &&
6901
6915
(matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(flags) if !flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH)) ||
@@ -6931,17 +6945,6 @@ impl<SP: Deref> FundedChannel<SP> where
6931
6945
mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
6932
6946
let mut pending_update_adds = Vec::new();
6933
6947
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
- }
6945
6948
6946
6949
if self.context.channel_state.is_peer_disconnected() {
6947
6950
self.context.monitor_pending_revoke_and_ack = false;
@@ -8246,7 +8249,7 @@ impl<SP: Deref> FundedChannel<SP> where
8246
8249
/// advanced state.
8247
8250
pub fn is_awaiting_initial_mon_persist(&self) -> bool {
8248
8251
if !self.is_awaiting_monitor_update() { return false; }
8249
- if matches!(
8252
+ if self.context.channel_state.is_interactive_signing() || matches!(
8250
8253
self.context.channel_state, ChannelState::AwaitingChannelReady(flags)
8251
8254
if flags.clone().clear(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY | FundedStateFlags::PEER_DISCONNECTED | FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS | AwaitingChannelReadyFlags::WAITING_FOR_BATCH).is_empty()
8252
8255
) {
@@ -10531,6 +10534,31 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
10531
10534
our_funding_inputs: our_funding_inputs.clone(),
10532
10535
};
10533
10536
10537
+ // Optionally add change output
10538
+ let change_script = signer_provider.get_destination_script(context.channel_keys_id)
10539
+ .map_err(|_| ChannelError::close("Error getting change destination script".to_string()))?;
10540
+ let change_value_opt = calculate_change_output_value(
10541
+ funding.is_outbound(), dual_funding_context.our_funding_satoshis,
10542
+ &our_funding_inputs, &vec![],
10543
+ dual_funding_context.funding_feerate_sat_per_1000_weight,
10544
+ change_script.minimal_non_dust().to_sat(),
10545
+ ).map_err(|_| ChannelError::close("Error calculating change output value".to_string()))?;
10546
+ let mut our_funding_outputs = vec![];
10547
+ if let Some(change_value) = change_value_opt {
10548
+ let mut change_output = TxOut {
10549
+ value: Amount::from_sat(change_value),
10550
+ script_pubkey: change_script,
10551
+ };
10552
+ let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
10553
+ let change_output_fee = fee_for_weight(dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
10554
+ let change_value_decreased_with_fee = change_value.saturating_sub(change_output_fee);
10555
+ // Check dust limit again
10556
+ if change_value_decreased_with_fee > context.holder_dust_limit_satoshis {
10557
+ change_output.value = Amount::from_sat(change_value_decreased_with_fee);
10558
+ our_funding_outputs.push(OutputOwned::Single(change_output));
10559
+ }
10560
+ }
10561
+
10534
10562
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
10535
10563
InteractiveTxConstructorArgs {
10536
10564
entropy_source,
@@ -10541,7 +10569,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
10541
10569
funding_tx_locktime: dual_funding_context.funding_tx_locktime,
10542
10570
is_initiator: false,
10543
10571
inputs_to_contribute: our_funding_inputs,
10544
- outputs_to_contribute: Vec::new() ,
10572
+ outputs_to_contribute: our_funding_outputs ,
10545
10573
expected_remote_shared_funding_output: Some((funding.get_funding_redeemscript().to_p2wsh(), funding.get_value_satoshis())),
10546
10574
}
10547
10575
).map_err(|_| ChannelError::Close((
0 commit comments