@@ -977,6 +977,8 @@ pub(super) struct ReestablishResponses {
977977 pub order: RAACommitmentOrder,
978978 pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
979979 pub shutdown_msg: Option<msgs::Shutdown>,
980+ pub tx_signatures: Option<msgs::TxSignatures>,
981+ pub tx_abort: Option<msgs::TxAbort>,
980982}
981983
982984/// The first message we send to our peer after connection
@@ -2443,7 +2445,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
24432445
24442446 let mut output_index = None;
24452447 let expected_spk = self.funding.get_funding_redeemscript().to_p2wsh();
2446- for (idx, outp) in signing_session.unsigned_tx.outputs().enumerate() {
2448+ for (idx, outp) in signing_session.unsigned_tx() .outputs().enumerate() {
24472449 if outp.script_pubkey() == &expected_spk && outp.value() == self.funding.get_value_satoshis() {
24482450 if output_index.is_some() {
24492451 return Err(ChannelError::Close(
@@ -2456,7 +2458,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
24562458 }
24572459 }
24582460 let outpoint = if let Some(output_index) = output_index {
2459- OutPoint { txid: signing_session.unsigned_tx.compute_txid(), index: output_index }
2461+ OutPoint { txid: signing_session.unsigned_tx() .compute_txid(), index: output_index }
24602462 } else {
24612463 return Err(ChannelError::Close(
24622464 (
@@ -2470,7 +2472,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
24702472 let commitment_signed = self.context.get_initial_commitment_signed(&self.funding, logger);
24712473 let commitment_signed = match commitment_signed {
24722474 Ok(commitment_signed) => {
2473- self.funding.funding_transaction = Some(signing_session.unsigned_tx.build_unsigned_tx());
2475+ self.funding.funding_transaction = Some(signing_session.unsigned_tx() .build_unsigned_tx());
24742476 commitment_signed
24752477 },
24762478 Err(err) => {
@@ -6437,7 +6439,7 @@ impl<SP: Deref> FundedChannel<SP> where
64376439 }
64386440
64396441 if let Some(ref mut signing_session) = self.interactive_tx_signing_session {
6440- if msg.tx_hash != signing_session.unsigned_tx.compute_txid() {
6442+ if msg.tx_hash != signing_session.unsigned_tx() .compute_txid() {
64416443 return Err(ChannelError::Close(
64426444 (
64436445 "The txid for the transaction does not match".to_string(),
@@ -7087,7 +7089,10 @@ impl<SP: Deref> FundedChannel<SP> where
70877089 }
70887090
70897091 if msg.next_local_commitment_number >= INITIAL_COMMITMENT_NUMBER || msg.next_remote_commitment_number >= INITIAL_COMMITMENT_NUMBER ||
7090- msg.next_local_commitment_number == 0 {
7092+ (msg.next_local_commitment_number == 0 && msg.next_funding_txid.is_none()) {
7093+ // Note: This also covers the following case in the V2 channel establishment specification:
7094+ // if `next_funding_txid` is not set, and `next_commitment_number` is zero:
7095+ // MUST immediately fail the channel and broadcast any relevant latest commitment transaction.
70917096 return Err(ChannelError::close("Peer sent an invalid channel_reestablish to force close in a non-standard way".to_owned()));
70927097 }
70937098
@@ -7151,6 +7156,8 @@ impl<SP: Deref> FundedChannel<SP> where
71517156 raa: None, commitment_update: None,
71527157 order: RAACommitmentOrder::CommitmentFirst,
71537158 shutdown_msg, announcement_sigs,
7159+ tx_signatures: None,
7160+ tx_abort: None,
71547161 });
71557162 }
71567163
@@ -7160,6 +7167,8 @@ impl<SP: Deref> FundedChannel<SP> where
71607167 raa: None, commitment_update: None,
71617168 order: RAACommitmentOrder::CommitmentFirst,
71627169 shutdown_msg, announcement_sigs,
7170+ tx_signatures: None,
7171+ tx_abort: None,
71637172 });
71647173 }
71657174
@@ -7202,11 +7211,84 @@ impl<SP: Deref> FundedChannel<SP> where
72027211 log_debug!(logger, "Reconnected channel {} with no loss", &self.context.channel_id());
72037212 }
72047213
7214+ // if next_funding_txid is set:
7215+ let (commitment_update, tx_signatures, tx_abort) = if let Some(next_funding_txid) = msg.next_funding_txid {
7216+ if let Some(session) = &self.interactive_tx_signing_session {
7217+ // if next_funding_txid matches the latest interactive funding transaction:
7218+ let our_next_funding_txid = session.unsigned_tx().compute_txid();
7219+ if our_next_funding_txid == next_funding_txid {
7220+ debug_assert_eq!(session.unsigned_tx().compute_txid(), self.maybe_get_next_funding_txid().unwrap());
7221+
7222+ let commitment_update = if !session.has_received_tx_signatures() && msg.next_local_commitment_number == 0 {
7223+ // if it has not received tx_signatures for that funding transaction AND
7224+ // if next_commitment_number is zero:
7225+ // MUST retransmit its commitment_signed for that funding transaction.
7226+ let commitment_signed = self.context.get_initial_commitment_signed(&self.funding, logger)?;
7227+ Some(msgs::CommitmentUpdate {
7228+ commitment_signed: vec![commitment_signed],
7229+ update_add_htlcs: vec![],
7230+ update_fulfill_htlcs: vec![],
7231+ update_fail_htlcs: vec![],
7232+ update_fail_malformed_htlcs: vec![],
7233+ update_fee: None,
7234+ })
7235+ } else { None };
7236+ // TODO(dual_funding): For async signing support we need to hold back `tx_signatures` until the `commitment_signed` is ready.
7237+ let tx_signatures = if (
7238+ // if it has not received tx_signatures for that funding transaction AND
7239+ // if it has already received commitment_signed AND it should sign first, as specified in the tx_signatures requirements:
7240+ // MUST send its tx_signatures for that funding transaction.
7241+ !session.has_received_tx_signatures() && session.has_received_commitment_signed() && session.holder_sends_tx_signatures_first()
7242+ // else if it has already received tx_signatures for that funding transaction:
7243+ // MUST send its tx_signatures for that funding transaction.
7244+ ) || session.has_received_tx_signatures() {
7245+ if self.context.channel_state.is_monitor_update_in_progress() {
7246+ // The `monitor_pending_tx_signatures` field should have already been set in `commitment_signed_initial_v2`
7247+ // if we were up first for signing and had a monitor update in progress, but check again just in case.
7248+ debug_assert!(self.context.monitor_pending_tx_signatures.is_some(), "monitor_pending_tx_signatures should already be set");
7249+ log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
7250+ if self.context.monitor_pending_tx_signatures.is_none() {
7251+ self.context.monitor_pending_tx_signatures = session.holder_tx_signatures().clone();
7252+ }
7253+ None
7254+ } else {
7255+ // If `holder_tx_signatures` is `None` here, the `tx_signatures` message will be sent
7256+ // when the holder provides their witnesses as this will queue a `tx_signatures` if the
7257+ // holder must send one.
7258+ session.holder_tx_signatures().clone()
7259+ }
7260+ } else {
7261+ None
7262+ };
7263+ if !session.has_received_commitment_signed() {
7264+ self.context.expecting_peer_commitment_signed = true;
7265+ }
7266+ (commitment_update, tx_signatures, None)
7267+ } else {
7268+ // The `next_funding_txid` does not match the latest interactive funding transaction so we
7269+ // MUST send tx_abort to let the remote know that they can forget this funding transaction.
7270+ (None, None, Some(msgs::TxAbort {
7271+ channel_id: self.context.channel_id(),
7272+ data: format!(
7273+ "next_funding_txid {} does match our latest interactive funding txid {}",
7274+ next_funding_txid, our_next_funding_txid,
7275+ ).into_bytes() }))
7276+ }
7277+ } else {
7278+ return Err(ChannelError::Warn("No active signing session. The associated funding transaction may have already been broadcast.".into()));
7279+ }
7280+ } else {
7281+ // Don't send anything related to interactive signing if `next_funding_txid` is not set.
7282+ (None, None, None)
7283+ };
7284+
72057285 Ok(ReestablishResponses {
72067286 channel_ready, shutdown_msg, announcement_sigs,
72077287 raa: required_revoke,
7208- commitment_update: None ,
7288+ commitment_update,
72097289 order: self.context.resend_order.clone(),
7290+ tx_signatures,
7291+ tx_abort,
72107292 })
72117293 } else if msg.next_local_commitment_number == next_counterparty_commitment_number - 1 {
72127294 if required_revoke.is_some() || self.context.signer_pending_revoke_and_ack {
@@ -7221,6 +7303,8 @@ impl<SP: Deref> FundedChannel<SP> where
72217303 channel_ready, shutdown_msg, announcement_sigs,
72227304 commitment_update: None, raa: None,
72237305 order: self.context.resend_order.clone(),
7306+ tx_signatures: None,
7307+ tx_abort: None,
72247308 })
72257309 } else {
72267310 let commitment_update = if self.context.resend_order == RAACommitmentOrder::RevokeAndACKFirst
@@ -7243,6 +7327,8 @@ impl<SP: Deref> FundedChannel<SP> where
72437327 channel_ready, shutdown_msg, announcement_sigs,
72447328 raa, commitment_update,
72457329 order: self.context.resend_order.clone(),
7330+ tx_signatures: None,
7331+ tx_abort: None,
72467332 })
72477333 }
72487334 } else if msg.next_local_commitment_number < next_counterparty_commitment_number {
@@ -8533,7 +8619,7 @@ impl<SP: Deref> FundedChannel<SP> where
85338619 // to the txid of that interactive transaction, else we MUST NOT set it.
85348620 if let Some(signing_session) = &self.interactive_tx_signing_session {
85358621 // Since we have a signing_session, this implies we've sent an initial `commitment_signed`...
8536- if !signing_session.counterparty_sent_tx_signatures {
8622+ if !signing_session.has_received_tx_signatures() {
85378623 // ...but we didn't receive a `tx_signatures` from the counterparty yet.
85388624 Some(self.funding_outpoint().txid)
85398625 } else {
0 commit comments