Skip to content

Commit d8306e9

Browse files
committed
f consolidate logic for sending tx_signatures
1 parent e5930ce commit d8306e9

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
@@ -7029,49 +7029,51 @@ impl<SP: Deref> FundedChannel<SP> where
70297029
// if next_funding_txid matches the latest interactive funding transaction:
70307030
if session.unsigned_tx().compute_txid() == next_funding_txid {
70317031
debug_assert_eq!(session.unsigned_tx().compute_txid(), self.maybe_get_next_funding_txid().unwrap());
7032-
// if it has not received tx_signatures for that funding transaction:
7033-
if !session.counterparty_sent_tx_signatures() {
7032+
7033+
let commitment_update = if !session.counterparty_sent_tx_signatures() && msg.next_local_commitment_number == 0 {
7034+
// if it has not received tx_signatures for that funding transaction AND
70347035
// if next_commitment_number is zero:
7035-
let commitment_update = if msg.next_local_commitment_number == 0 {
7036-
// MUST retransmit its commitment_signed for that funding transaction.
7037-
let commitment_signed = self.context.get_initial_commitment_signed(&self.funding, logger)?;
7038-
Some(msgs::CommitmentUpdate {
7039-
commitment_signed,
7040-
update_add_htlcs: vec![],
7041-
update_fulfill_htlcs: vec![],
7042-
update_fail_htlcs: vec![],
7043-
update_fail_malformed_htlcs: vec![],
7044-
update_fee: None,
7045-
})
7046-
} else { None };
7047-
// if it has already received commitment_signed and it should sign first, as specified in the tx_signatures requirements:
7048-
if session.has_received_commitment_signed() && session.holder_sends_tx_signatures_first() {
7049-
// MUST send its tx_signatures for that funding transaction.
7050-
if self.context.channel_state.is_monitor_update_in_progress() {
7051-
log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
7052-
self.context.monitor_pending_tx_signatures = session.holder_tx_signatures().clone();
7053-
// We can still send the initial commitment transaction if a monitor update is pending.
7054-
(commitment_update, None, None)
7055-
} else {
7056-
(commitment_update, session.holder_tx_signatures().clone(), None)
7057-
}
7058-
} else {
7059-
(commitment_update, None, None)
7060-
}
7061-
} else {
7062-
// if it has already received tx_signatures for that funding transaction:
7063-
// MUST send its tx_signatures for that funding transaction.
7036+
// MUST retransmit its commitment_signed for that funding transaction.
7037+
let commitment_signed = self.context.get_initial_commitment_signed(&self.funding, logger)?;
7038+
Some(msgs::CommitmentUpdate {
7039+
commitment_signed,
7040+
update_add_htlcs: vec![],
7041+
update_fulfill_htlcs: vec![],
7042+
update_fail_htlcs: vec![],
7043+
update_fail_malformed_htlcs: vec![],
7044+
update_fee: None,
7045+
})
7046+
} else { None };
7047+
// if it has not received tx_signatures for that funding transaction AND
7048+
// if it has already received commitment_signed AND it should sign first, as specified in the tx_signatures requirements:
7049+
// MUST send its tx_signatures for that funding transaction.
7050+
// else if it HAS received commitment_signed AND has received tx_signatures for that funding transaction:
7051+
// MUST send its tx_signatures for that funding transaction.
7052+
let tx_signatures = if session.has_received_commitment_signed() && ((
7053+
!session.counterparty_sent_tx_signatures() &&
7054+
session.holder_sends_tx_signatures_first()
7055+
) || session.counterparty_sent_tx_signatures()) {
7056+
// This should have already been set in `commitment_signed_initial_v2`, but check again
7057+
// just in case.
70647058
if self.context.channel_state.is_monitor_update_in_progress() {
70657059
log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
7066-
self.context.monitor_pending_tx_signatures = session.holder_tx_signatures().clone();
7067-
(None, None, None)
7060+
if self.context.monitor_pending_tx_signatures.is_none() {
7061+
self.context.monitor_pending_tx_signatures = session.holder_tx_signatures().clone();
7062+
}
7063+
None
70687064
} else {
70697065
// If `holder_tx_signatures` is `None` here, the `tx_signatures` message will be sent
70707066
// when the holder provides their witnesses as this will queue a `tx_signatures` if the
70717067
// holder must send one.
7072-
(None, session.holder_tx_signatures().clone(), None)
7068+
session.holder_tx_signatures().clone()
70737069
}
7074-
}
7070+
} else {
7071+
if !session.has_received_commitment_signed() {
7072+
self.context.expecting_peer_commitment_signed = true;
7073+
}
7074+
None
7075+
};
7076+
(commitment_update, tx_signatures, None)
70757077
} else {
70767078
// MUST send tx_abort to let the sending node know that they can forget this funding transaction.
70777079
(None, None, Some(msgs::TxAbort { channel_id: self.context.channel_id(), data: vec![] }))
@@ -7080,6 +7082,7 @@ impl<SP: Deref> FundedChannel<SP> where
70807082
return Err(ChannelError::close("Counterparty set `next_funding_txid` at incorrect state".into()));
70817083
}
70827084
} else {
7085+
// Don't send anything related to interactive signing if `next_funding_txid` is not set.
70837086
(None, None, None)
70847087
};
70857088

0 commit comments

Comments
 (0)