Skip to content

Commit e43c838

Browse files
committed
f consolidate logic for sending tx_signatures
1 parent 2da38a3 commit e43c838

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

lightning/src/ln/channel.rs

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7034,49 +7034,51 @@ impl<SP: Deref> FundedChannel<SP> where
70347034
// if next_funding_txid matches the latest interactive funding transaction:
70357035
if session.unsigned_tx().compute_txid() == next_funding_txid {
70367036
debug_assert_eq!(session.unsigned_tx().compute_txid(), self.maybe_get_next_funding_txid().unwrap());
7037-
// if it has not received tx_signatures for that funding transaction:
7038-
if !session.counterparty_sent_tx_signatures() {
7037+
7038+
let commitment_update = if !session.counterparty_sent_tx_signatures() && msg.next_local_commitment_number == 0 {
7039+
// if it has not received tx_signatures for that funding transaction AND
70397040
// if next_commitment_number is zero:
7040-
let commitment_update = if msg.next_local_commitment_number == 0 {
7041-
// MUST retransmit its commitment_signed for that funding transaction.
7042-
let commitment_signed = self.context.get_initial_commitment_signed(&self.funding, logger)?;
7043-
Some(msgs::CommitmentUpdate {
7044-
commitment_signed,
7045-
update_add_htlcs: vec![],
7046-
update_fulfill_htlcs: vec![],
7047-
update_fail_htlcs: vec![],
7048-
update_fail_malformed_htlcs: vec![],
7049-
update_fee: None,
7050-
})
7051-
} else { None };
7052-
// if it has already received commitment_signed and it should sign first, as specified in the tx_signatures requirements:
7053-
if session.has_received_commitment_signed() && session.holder_sends_tx_signatures_first() {
7054-
// MUST send its tx_signatures for that funding transaction.
7055-
if self.context.channel_state.is_monitor_update_in_progress() {
7056-
log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
7057-
self.context.monitor_pending_tx_signatures = session.holder_tx_signatures().clone();
7058-
// We can still send the initial commitment transaction if a monitor update is pending.
7059-
(commitment_update, None, None)
7060-
} else {
7061-
(commitment_update, session.holder_tx_signatures().clone(), None)
7062-
}
7063-
} else {
7064-
(commitment_update, None, None)
7065-
}
7066-
} else {
7067-
// if it has already received tx_signatures for that funding transaction:
7068-
// MUST send its tx_signatures for that funding transaction.
7041+
// MUST retransmit its commitment_signed for that funding transaction.
7042+
let commitment_signed = self.context.get_initial_commitment_signed(&self.funding, logger)?;
7043+
Some(msgs::CommitmentUpdate {
7044+
commitment_signed,
7045+
update_add_htlcs: vec![],
7046+
update_fulfill_htlcs: vec![],
7047+
update_fail_htlcs: vec![],
7048+
update_fail_malformed_htlcs: vec![],
7049+
update_fee: None,
7050+
})
7051+
} else { None };
7052+
// if it has not received tx_signatures for that funding transaction AND
7053+
// if it has already received commitment_signed AND it should sign first, as specified in the tx_signatures requirements:
7054+
// MUST send its tx_signatures for that funding transaction.
7055+
// else if it HAS received commitment_signed AND has received tx_signatures for that funding transaction:
7056+
// MUST send its tx_signatures for that funding transaction.
7057+
let tx_signatures = if session.has_received_commitment_signed() && ((
7058+
!session.counterparty_sent_tx_signatures() &&
7059+
session.holder_sends_tx_signatures_first()
7060+
) || session.counterparty_sent_tx_signatures()) {
7061+
// This should have already been set in `commitment_signed_initial_v2`, but check again
7062+
// just in case.
70697063
if self.context.channel_state.is_monitor_update_in_progress() {
70707064
log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
7071-
self.context.monitor_pending_tx_signatures = session.holder_tx_signatures().clone();
7072-
(None, None, None)
7065+
if self.context.monitor_pending_tx_signatures.is_none() {
7066+
self.context.monitor_pending_tx_signatures = session.holder_tx_signatures().clone();
7067+
}
7068+
None
70737069
} else {
70747070
// If `holder_tx_signatures` is `None` here, the `tx_signatures` message will be sent
70757071
// when the holder provides their witnesses as this will queue a `tx_signatures` if the
70767072
// holder must send one.
7077-
(None, session.holder_tx_signatures().clone(), None)
7073+
session.holder_tx_signatures().clone()
70787074
}
7079-
}
7075+
} else {
7076+
if !session.has_received_commitment_signed() {
7077+
self.context.expecting_peer_commitment_signed = true;
7078+
}
7079+
None
7080+
};
7081+
(commitment_update, tx_signatures, None)
70807082
} else {
70817083
// MUST send tx_abort to let the sending node know that they can forget this funding transaction.
70827084
(None, None, Some(msgs::TxAbort { channel_id: self.context.channel_id(), data: vec![] }))
@@ -7085,6 +7087,7 @@ impl<SP: Deref> FundedChannel<SP> where
70857087
return Err(ChannelError::close("Counterparty set `next_funding_txid` at incorrect state".into()));
70867088
}
70877089
} else {
7090+
// Don't send anything related to interactive signing if `next_funding_txid` is not set.
70887091
(None, None, None)
70897092
};
70907093

0 commit comments

Comments
 (0)