@@ -8406,96 +8406,111 @@ where
8406
8406
.and_then(|_| self.get_channel_ready(logger))
8407
8407
} else { None };
8408
8408
8409
- if msg.next_local_commitment_number == next_counterparty_commitment_number {
8410
- if required_revoke.is_some() || self.context.signer_pending_revoke_and_ack {
8411
- log_debug!(logger, "Reconnected channel {} with only lost outbound RAA", &self.context.channel_id());
8412
- } else {
8413
- log_debug!(logger, "Reconnected channel {} with no loss", &self.context.channel_id());
8414
- }
8409
+ let mut commitment_update = None;
8410
+ let mut tx_signatures = None;
8411
+ let mut tx_abort = None;
8412
+
8413
+ // if next_funding_txid is set:
8414
+ if let Some(next_funding_txid) = msg.next_funding_txid {
8415
+ // - if `next_funding_txid` matches the latest interactive funding transaction
8416
+ // or the current channel funding transaction:
8417
+ if let Some(session) = &self.interactive_tx_signing_session {
8418
+ let our_next_funding_txid = self.maybe_get_next_funding_txid();
8419
+ if let Some(our_next_funding_txid) = our_next_funding_txid {
8420
+ if our_next_funding_txid != next_funding_txid {
8421
+ return Err(ChannelError::close(format!(
8422
+ "Unexpected next_funding_txid: {}; expected: {}",
8423
+ next_funding_txid, our_next_funding_txid,
8424
+ )));
8425
+ }
8415
8426
8416
- // if next_funding_txid is set:
8417
- let (commitment_update, tx_signatures, tx_abort) = if let Some(next_funding_txid) = msg.next_funding_txid {
8418
- if let Some(session) = &self.interactive_tx_signing_session {
8419
- // if next_funding_txid matches the latest interactive funding transaction:
8420
- let our_next_funding_txid = session.unsigned_tx().compute_txid();
8421
- if our_next_funding_txid == next_funding_txid {
8422
- debug_assert_eq!(session.unsigned_tx().compute_txid(), self.maybe_get_next_funding_txid().unwrap());
8423
-
8424
- let commitment_update = if !self.context.channel_state.is_their_tx_signatures_sent() && msg.next_local_commitment_number == 0 {
8425
- // if it has not received tx_signatures for that funding transaction AND
8426
- // if next_commitment_number is zero:
8427
- // MUST retransmit its commitment_signed for that funding transaction.
8428
- let commitment_signed = self.context.get_initial_commitment_signed(&self.funding, logger)
8429
- // TODO(splicing): Support async signing
8430
- .ok_or_else(|| ChannelError::Close(
8431
- (
8432
- "Failed to get signatures for new commitment_signed".to_owned(),
8433
- ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
8434
- )
8435
- ))?;
8436
- Some(msgs::CommitmentUpdate {
8437
- commitment_signed: vec![commitment_signed],
8438
- update_add_htlcs: vec![],
8439
- update_fulfill_htlcs: vec![],
8440
- update_fail_htlcs: vec![],
8441
- update_fail_malformed_htlcs: vec![],
8442
- update_fee: None,
8443
- })
8444
- } else { None };
8427
+ if !session.has_received_commitment_signed() {
8428
+ self.context.expecting_peer_commitment_signed = true;
8429
+ }
8430
+
8431
+ // - if `next_commitment_number` is equal to the commitment number of the
8432
+ // `commitment_signed` message it sent for this funding transaction:
8433
+ // - MUST retransmit its `commitment_signed` for that funding transaction.
8434
+ if msg.next_local_commitment_number == next_counterparty_commitment_number {
8435
+ // `next_counterparty_commitment_number` is guaranteed to always be the
8436
+ // commitment number of the `commitment_signed` message we sent for this
8437
+ // funding transaction. If they set `next_funding_txid`, then they should
8438
+ // not have processed our `tx_signatures` yet, which implies that our state
8439
+ // machine is still paused and no updates can happen that would increment
8440
+ // our `next_counterparty_commitment_number`.
8441
+ //
8442
+ // If they did set `next_funding_txid` even after processing our
8443
+ // `tx_signatures` erroneously, this may end up resulting in a force close.
8444
+ //
8445
8445
// TODO(dual_funding): For async signing support we need to hold back `tx_signatures` until the `commitment_signed` is ready.
8446
- let tx_signatures = if (
8447
- // if it has not received tx_signatures for that funding transaction AND
8448
- // if it has already received commitment_signed AND it should sign first, as specified in the tx_signatures requirements:
8449
- // MUST send its tx_signatures for that funding transaction.
8450
- !self.context.channel_state.is_their_tx_signatures_sent() && session.has_received_commitment_signed() && session.holder_sends_tx_signatures_first()
8451
- // else if it has already received tx_signatures for that funding transaction:
8452
- // MUST send its tx_signatures for that funding transaction.
8453
- ) || self.context.channel_state.is_their_tx_signatures_sent() {
8454
- if self.context.channel_state.is_monitor_update_in_progress() {
8455
- // The `monitor_pending_tx_signatures` field should have already been set in `commitment_signed_initial_v2`
8456
- // if we were up first for signing and had a monitor update in progress, but check again just in case.
8457
- debug_assert!(self.context.monitor_pending_tx_signatures.is_some(), "monitor_pending_tx_signatures should already be set");
8458
- log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
8459
- if self.context.monitor_pending_tx_signatures.is_none() {
8460
- self.context.monitor_pending_tx_signatures = session.holder_tx_signatures().clone();
8461
- }
8462
- None
8463
- } else {
8464
- // If `holder_tx_signatures` is `None` here, the `tx_signatures` message will be sent
8465
- // when the holder provides their witnesses as this will queue a `tx_signatures` if the
8466
- // holder must send one.
8467
- session.holder_tx_signatures().clone()
8446
+ let commitment_signed = self.context.get_initial_commitment_signed(&self.funding, logger)
8447
+ // TODO(splicing): Support async signing
8448
+ .ok_or_else(|| ChannelError::Close(
8449
+ (
8450
+ "Failed to get signatures for new commitment_signed".to_owned(),
8451
+ ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
8452
+ )
8453
+ ))?;
8454
+ commitment_update = Some(msgs::CommitmentUpdate {
8455
+ commitment_signed: vec![commitment_signed],
8456
+ update_add_htlcs: vec![],
8457
+ update_fulfill_htlcs: vec![],
8458
+ update_fail_htlcs: vec![],
8459
+ update_fail_malformed_htlcs: vec![],
8460
+ update_fee: None,
8461
+ });
8462
+ }
8463
+
8464
+ // - if it has already received `commitment_signed` and it should sign first,
8465
+ // as specified in the [`tx_signatures` requirements](#the-tx_signatures-message):
8466
+ // - MUST send its `tx_signatures` for that funding transaction.
8467
+ //
8468
+ // - if it has already received `tx_signatures` for that funding transaction:
8469
+ // - MUST send its `tx_signatures` for that funding transaction.
8470
+ if (session.has_received_commitment_signed() && session.holder_sends_tx_signatures_first())
8471
+ || self.context.channel_state.is_their_tx_signatures_sent()
8472
+ {
8473
+ if self.context.channel_state.is_monitor_update_in_progress() {
8474
+ // The `monitor_pending_tx_signatures` field should have already been
8475
+ // set in `commitment_signed_initial_v2` if we were up first for signing
8476
+ // and had a monitor update in progress.
8477
+ if session.holder_sends_tx_signatures_first() {
8478
+ debug_assert!(self.context.monitor_pending_tx_signatures.is_some());
8468
8479
}
8469
8480
} else {
8470
- None
8471
- };
8472
- if !session.has_received_commitment_signed() {
8473
- self.context.expecting_peer_commitment_signed = true;
8481
+ // If `holder_tx_signatures` is `None` here, the `tx_signatures` message
8482
+ // will be sent when the user provides their witnesses.
8483
+ tx_signatures = session.holder_tx_signatures().clone()
8474
8484
}
8475
- (commitment_update, tx_signatures, None)
8476
- } else {
8477
- // The `next_funding_txid` does not match the latest interactive funding transaction so we
8478
- // MUST send tx_abort to let the remote know that they can forget this funding transaction.
8479
- (None, None, Some(msgs::TxAbort {
8480
- channel_id: self.context.channel_id(),
8481
- data: format!(
8482
- "next_funding_txid {} does match our latest interactive funding txid {}",
8483
- next_funding_txid, our_next_funding_txid,
8484
- ).into_bytes() }))
8485
8485
}
8486
8486
} else {
8487
- // We'll just send a `tx_abort` here if we don't have a signing session for this channel
8488
- // on reestablish and tell our peer to just forget about it.
8489
- // Our peer is doing something strange, but it doesn't warrant closing the channel .
8490
- (None, None, Some(msgs::TxAbort {
8487
+ // The `next_funding_txid` does not match the latest interactive funding
8488
+ // transaction so we MUST send tx_abort to let the remote know that they can
8489
+ // forget this funding transaction .
8490
+ tx_abort = Some(msgs::TxAbort {
8491
8491
channel_id: self.context.channel_id(),
8492
- data:
8493
- "No active signing session. The associated funding transaction may have already been broadcast.".as_bytes().to_vec() }))
8492
+ data: format!(
8493
+ "Unexpected next_funding_txid {}",
8494
+ next_funding_txid,
8495
+ ).into_bytes() });
8494
8496
}
8495
8497
} else {
8496
- // Don't send anything related to interactive signing if `next_funding_txid` is not set.
8497
- (None, None, None)
8498
- };
8498
+ // We'll just send a `tx_abort` here if we don't have a signing session for this channel
8499
+ // on reestablish and tell our peer to just forget about it.
8500
+ // Our peer is doing something strange, but it doesn't warrant closing the channel.
8501
+ tx_abort = Some(msgs::TxAbort {
8502
+ channel_id: self.context.channel_id(),
8503
+ data:
8504
+ "No active signing session. The associated funding transaction may have already been broadcast.".as_bytes().to_vec() });
8505
+ }
8506
+ }
8507
+
8508
+ if msg.next_local_commitment_number == next_counterparty_commitment_number {
8509
+ if required_revoke.is_some() || self.context.signer_pending_revoke_and_ack {
8510
+ log_debug!(logger, "Reconnected channel {} with only lost outbound RAA", &self.context.channel_id());
8511
+ } else {
8512
+ log_debug!(logger, "Reconnected channel {} with no loss", &self.context.channel_id());
8513
+ }
8499
8514
8500
8515
Ok(ReestablishResponses {
8501
8516
channel_ready, shutdown_msg, announcement_sigs,
@@ -8506,6 +8521,11 @@ where
8506
8521
tx_abort,
8507
8522
})
8508
8523
} else if msg.next_local_commitment_number == next_counterparty_commitment_number - 1 {
8524
+ // We've made an update so we must have exchanged `tx_signatures`, implying that
8525
+ // `commitment_signed` was also exchanged. However, we may still need to retransmit our
8526
+ // `tx_signatures` if the counterparty sent theirs first but didn't get to process ours.
8527
+ debug_assert!(commitment_update.is_none());
8528
+
8509
8529
if required_revoke.is_some() || self.context.signer_pending_revoke_and_ack {
8510
8530
log_debug!(logger, "Reconnected channel {} with lost outbound RAA and lost remote commitment tx", &self.context.channel_id());
8511
8531
} else {
@@ -8518,8 +8538,8 @@ where
8518
8538
channel_ready, shutdown_msg, announcement_sigs,
8519
8539
commitment_update: None, raa: None,
8520
8540
order: self.context.resend_order.clone(),
8521
- tx_signatures: None ,
8522
- tx_abort: None ,
8541
+ tx_signatures,
8542
+ tx_abort,
8523
8543
})
8524
8544
} else {
8525
8545
let commitment_update = if self.context.resend_order == RAACommitmentOrder::RevokeAndACKFirst
@@ -8542,8 +8562,8 @@ where
8542
8562
channel_ready, shutdown_msg, announcement_sigs,
8543
8563
raa, commitment_update,
8544
8564
order: self.context.resend_order.clone(),
8545
- tx_signatures: None ,
8546
- tx_abort: None ,
8565
+ tx_signatures,
8566
+ tx_abort,
8547
8567
})
8548
8568
}
8549
8569
} else if msg.next_local_commitment_number < next_counterparty_commitment_number {
0 commit comments