Skip to content

Commit cd8ae56

Browse files
committed
Handle receiving channel_reestablish with next_funding_txid
This follows the the specification closely in branching without being too verbose, so that it should be easy to follow the logic. See: https://github.com/lightning/bolts/blob/aa5207a/02-peer-protocol.md?plain=1#L2520-L2531
1 parent f496e64 commit cd8ae56

File tree

3 files changed

+119
-17
lines changed

3 files changed

+119
-17
lines changed

lightning/src/ln/channel.rs

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,8 @@ pub(super) struct ReestablishResponses {
969969
pub order: RAACommitmentOrder,
970970
pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
971971
pub shutdown_msg: Option<msgs::Shutdown>,
972+
pub tx_signatures: Option<msgs::TxSignatures>,
973+
pub tx_abort: Option<msgs::TxAbort>,
972974
}
973975

974976
/// The first message we send to our peer after connection
@@ -2282,7 +2284,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22822284

22832285
let mut output_index = None;
22842286
let expected_spk = self.funding.get_funding_redeemscript().to_p2wsh();
2285-
for (idx, outp) in signing_session.unsigned_tx.outputs().enumerate() {
2287+
for (idx, outp) in signing_session.unsigned_tx().outputs().enumerate() {
22862288
if outp.script_pubkey() == &expected_spk && outp.value() == self.funding.get_value_satoshis() {
22872289
if output_index.is_some() {
22882290
return Err(ChannelError::Close(
@@ -2295,7 +2297,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22952297
}
22962298
}
22972299
let outpoint = if let Some(output_index) = output_index {
2298-
OutPoint { txid: signing_session.unsigned_tx.compute_txid(), index: output_index }
2300+
OutPoint { txid: signing_session.unsigned_tx().compute_txid(), index: output_index }
22992301
} else {
23002302
return Err(ChannelError::Close(
23012303
(
@@ -2309,7 +2311,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23092311
let commitment_signed = self.context.get_initial_commitment_signed(&self.funding, logger);
23102312
let commitment_signed = match commitment_signed {
23112313
Ok(commitment_signed) => {
2312-
self.funding.funding_transaction = Some(signing_session.unsigned_tx.build_unsigned_tx());
2314+
self.funding.funding_transaction = Some(signing_session.unsigned_tx().build_unsigned_tx());
23132315
commitment_signed
23142316
},
23152317
Err(err) => {
@@ -6259,7 +6261,7 @@ impl<SP: Deref> FundedChannel<SP> where
62596261
}
62606262

62616263
if let Some(ref mut signing_session) = self.interactive_tx_signing_session {
6262-
if msg.tx_hash != signing_session.unsigned_tx.compute_txid() {
6264+
if msg.tx_hash != signing_session.unsigned_tx().compute_txid() {
62636265
return Err(ChannelError::Close(
62646266
(
62656267
"The txid for the transaction does not match".to_string(),
@@ -6904,7 +6906,10 @@ impl<SP: Deref> FundedChannel<SP> where
69046906
}
69056907

69066908
if msg.next_local_commitment_number >= INITIAL_COMMITMENT_NUMBER || msg.next_remote_commitment_number >= INITIAL_COMMITMENT_NUMBER ||
6907-
msg.next_local_commitment_number == 0 {
6909+
msg.next_local_commitment_number == 0 && msg.next_funding_txid.is_none() {
6910+
// Note: This also covers the following case in the V2 channel establishment specification:
6911+
// if `next_funding_txid` is not set, and `next_commitment_number` is zero:
6912+
// MUST immediately fail the channel and broadcast any relevant latest commitment transaction.
69086913
return Err(ChannelError::close("Peer sent an invalid channel_reestablish to force close in a non-standard way".to_owned()));
69096914
}
69106915

@@ -6968,6 +6973,8 @@ impl<SP: Deref> FundedChannel<SP> where
69686973
raa: None, commitment_update: None,
69696974
order: RAACommitmentOrder::CommitmentFirst,
69706975
shutdown_msg, announcement_sigs,
6976+
tx_signatures: None,
6977+
tx_abort: None,
69716978
});
69726979
}
69736980

@@ -6977,6 +6984,8 @@ impl<SP: Deref> FundedChannel<SP> where
69776984
raa: None, commitment_update: None,
69786985
order: RAACommitmentOrder::CommitmentFirst,
69796986
shutdown_msg, announcement_sigs,
6987+
tx_signatures: None,
6988+
tx_abort: None,
69806989
});
69816990
}
69826991

@@ -7019,11 +7028,72 @@ impl<SP: Deref> FundedChannel<SP> where
70197028
log_debug!(logger, "Reconnected channel {} with no loss", &self.context.channel_id());
70207029
}
70217030

7031+
// if next_funding_txid is set:
7032+
let (commitment_update, tx_signatures, tx_abort) = if let Some(next_funding_txid) = msg.next_funding_txid {
7033+
if let Some(session) = &self.interactive_tx_signing_session {
7034+
// if next_funding_txid matches the latest interactive funding transaction:
7035+
if session.unsigned_tx().compute_txid() == next_funding_txid {
7036+
// if it has not received tx_signatures for that funding transaction:
7037+
if !session.counterparty_sent_tx_signatures() {
7038+
// if next_commitment_number is zero:
7039+
let commitment_update = if msg.next_local_commitment_number == 0 {
7040+
// MUST retransmit its commitment_signed for that funding transaction.
7041+
let commitment_signed = self.context.get_initial_commitment_signed(&self.funding, logger)?;
7042+
Some(msgs::CommitmentUpdate {
7043+
commitment_signed,
7044+
update_add_htlcs: vec![],
7045+
update_fulfill_htlcs: vec![],
7046+
update_fail_htlcs: vec![],
7047+
update_fail_malformed_htlcs: vec![],
7048+
update_fee: None,
7049+
})
7050+
} else { None };
7051+
// if it has already received commitment_signed and it should sign first, as specified in the tx_signatures requirements:
7052+
if session.has_received_commitment_signed() && session.holder_sends_tx_signatures_first() {
7053+
// MUST send its tx_signatures for that funding transaction.
7054+
if self.context.channel_state.is_monitor_update_in_progress() {
7055+
log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
7056+
self.context.monitor_pending_tx_signatures = session.holder_tx_signatures().clone();
7057+
// We can still send the initial commitment transaction if a monitor update is pending.
7058+
(commitment_update, None, None)
7059+
} else {
7060+
(commitment_update, session.holder_tx_signatures().clone(), None)
7061+
}
7062+
} else {
7063+
(commitment_update, None, None)
7064+
}
7065+
} else {
7066+
// if it has already received tx_signatures for that funding transaction:
7067+
// MUST send its tx_signatures for that funding transaction.
7068+
if self.context.channel_state.is_monitor_update_in_progress() {
7069+
log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
7070+
self.context.monitor_pending_tx_signatures = session.holder_tx_signatures().clone();
7071+
(None, None, None)
7072+
} else {
7073+
// If `holder_tx_signatures` is `None` here, the `tx_signatures` message will be sent
7074+
// when the holder provides their witnesses as this will queue a `tx_signatures` if the
7075+
// holder must send one.
7076+
(None, session.holder_tx_signatures().clone(), None)
7077+
}
7078+
}
7079+
} else {
7080+
// MUST send tx_abort to let the sending node know that they can forget this funding transaction.
7081+
(None, None, Some(msgs::TxAbort { channel_id: self.context.channel_id(), data: vec![] }))
7082+
}
7083+
} else {
7084+
return Err(ChannelError::close("Counterparty set `next_funding_txid` at incorrect state".into()));
7085+
}
7086+
} else {
7087+
(None, None, None)
7088+
};
7089+
70227090
Ok(ReestablishResponses {
70237091
channel_ready, shutdown_msg, announcement_sigs,
70247092
raa: required_revoke,
7025-
commitment_update: None,
7093+
commitment_update,
70267094
order: self.context.resend_order.clone(),
7095+
tx_signatures,
7096+
tx_abort,
70277097
})
70287098
} else if msg.next_local_commitment_number == next_counterparty_commitment_number - 1 {
70297099
if required_revoke.is_some() || self.context.signer_pending_revoke_and_ack {
@@ -7038,6 +7108,8 @@ impl<SP: Deref> FundedChannel<SP> where
70387108
channel_ready, shutdown_msg, announcement_sigs,
70397109
commitment_update: None, raa: None,
70407110
order: self.context.resend_order.clone(),
7111+
tx_signatures: None,
7112+
tx_abort: None,
70417113
})
70427114
} else {
70437115
let commitment_update = if self.context.resend_order == RAACommitmentOrder::RevokeAndACKFirst
@@ -7060,6 +7132,8 @@ impl<SP: Deref> FundedChannel<SP> where
70607132
channel_ready, shutdown_msg, announcement_sigs,
70617133
raa, commitment_update,
70627134
order: self.context.resend_order.clone(),
7135+
tx_signatures: None,
7136+
tx_abort: None,
70637137
})
70647138
}
70657139
} else if msg.next_local_commitment_number < next_counterparty_commitment_number {
@@ -8353,7 +8427,7 @@ impl<SP: Deref> FundedChannel<SP> where
83538427
// to the txid of that interactive transaction, else we MUST NOT set it.
83548428
if let Some(signing_session) = &self.interactive_tx_signing_session {
83558429
// Since we have a signing_session, this implies we've sent an initial `commitment_signed`...
8356-
if !signing_session.counterparty_sent_tx_signatures {
8430+
if !signing_session.counterparty_sent_tx_signatures() {
83578431
// ...but we didn't receive a `tx_signatures` from the counterparty yet.
83588432
Some(self.funding_outpoint().txid)
83598433
} else {

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3225,7 +3225,7 @@ macro_rules! handle_monitor_update_completion {
32253225
&mut $peer_state.pending_msg_events, $chan, updates.raa,
32263226
updates.commitment_update, updates.order, updates.accepted_htlcs, updates.pending_update_adds,
32273227
updates.funding_broadcastable, updates.channel_ready,
3228-
updates.announcement_sigs, updates.tx_signatures);
3228+
updates.announcement_sigs, updates.tx_signatures, None);
32293229
if let Some(upd) = channel_update {
32303230
$peer_state.pending_msg_events.push(upd);
32313231
}
@@ -7661,10 +7661,10 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
76617661
pending_forwards: Vec<(PendingHTLCInfo, u64)>, pending_update_adds: Vec<msgs::UpdateAddHTLC>,
76627662
funding_broadcastable: Option<Transaction>,
76637663
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>,
7664-
tx_signatures: Option<msgs::TxSignatures>
7664+
tx_signatures: Option<msgs::TxSignatures>, tx_abort: Option<msgs::TxAbort>,
76657665
) -> (Option<(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
76667666
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
7667-
log_trace!(logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {} pending update_add_htlcs, {}broadcasting funding, {} channel ready, {} announcement, {} tx_signatures",
7667+
log_trace!(logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {} pending update_add_htlcs, {}broadcasting funding, {} channel ready, {} announcement, {} tx_signatures, {} tx_abort",
76687668
&channel.context.channel_id(),
76697669
if raa.is_some() { "an" } else { "no" },
76707670
if commitment_update.is_some() { "a" } else { "no" },
@@ -7673,6 +7673,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
76737673
if channel_ready.is_some() { "sending" } else { "without" },
76747674
if announcement_sigs.is_some() { "sending" } else { "without" },
76757675
if tx_signatures.is_some() { "sending" } else { "without" },
7676+
if tx_abort.is_some() { "sending" } else { "without" },
76767677
);
76777678

76787679
let counterparty_node_id = channel.context.get_counterparty_node_id();
@@ -7706,6 +7707,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
77067707
msg,
77077708
});
77087709
}
7710+
if let Some(msg) = tx_abort {
7711+
pending_msg_events.push(MessageSendEvent::SendTxAbort {
7712+
node_id: counterparty_node_id,
7713+
msg,
7714+
});
7715+
}
77097716

77107717
macro_rules! handle_cs { () => {
77117718
if let Some(update) = commitment_update {
@@ -9454,7 +9461,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
94549461
let need_lnd_workaround = chan.context.workaround_lnd_bug_4006.take();
94559462
let (htlc_forwards, decode_update_add_htlcs) = self.handle_channel_resumption(
94569463
&mut peer_state.pending_msg_events, chan, responses.raa, responses.commitment_update, responses.order,
9457-
Vec::new(), Vec::new(), None, responses.channel_ready, responses.announcement_sigs, None);
9464+
Vec::new(), Vec::new(), None, responses.channel_ready, responses.announcement_sigs,
9465+
responses.tx_signatures, responses.tx_abort);
94589466
debug_assert!(htlc_forwards.is_none());
94599467
debug_assert!(decode_update_add_htlcs.is_none());
94609468
if let Some(upd) = channel_update {

lightning/src/ln/interactivetxs.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,36 @@ impl ConstructedTransaction {
288288
/// https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#sharing-funding-signatures-tx_signatures
289289
#[derive(Debug, Clone, PartialEq)]
290290
pub(crate) struct InteractiveTxSigningSession {
291-
pub unsigned_tx: ConstructedTransaction,
292-
pub counterparty_sent_tx_signatures: bool,
291+
unsigned_tx: ConstructedTransaction,
292+
counterparty_sent_tx_signatures: bool,
293293
holder_sends_tx_signatures_first: bool,
294-
received_commitment_signed: bool,
294+
has_received_commitment_signed: bool,
295295
holder_tx_signatures: Option<TxSignatures>,
296296
}
297297

298298
impl InteractiveTxSigningSession {
299+
pub fn unsigned_tx(&self) -> &ConstructedTransaction {
300+
&self.unsigned_tx
301+
}
302+
303+
pub fn counterparty_sent_tx_signatures(&self) -> bool {
304+
self.counterparty_sent_tx_signatures
305+
}
306+
307+
pub fn holder_sends_tx_signatures_first(&self) -> bool {
308+
self.holder_sends_tx_signatures_first
309+
}
310+
311+
pub fn has_received_commitment_signed(&self) -> bool {
312+
self.has_received_commitment_signed
313+
}
314+
315+
pub fn holder_tx_signatures(&self) -> &Option<TxSignatures> {
316+
&self.holder_tx_signatures
317+
}
318+
299319
pub fn received_commitment_signed(&mut self) -> Option<TxSignatures> {
300-
self.received_commitment_signed = true;
320+
self.has_received_commitment_signed = true;
301321
if self.holder_sends_tx_signatures_first {
302322
self.holder_tx_signatures.clone()
303323
} else {
@@ -306,7 +326,7 @@ impl InteractiveTxSigningSession {
306326
}
307327

308328
pub fn get_tx_signatures(&self) -> Option<TxSignatures> {
309-
if self.received_commitment_signed {
329+
if self.has_received_commitment_signed {
310330
self.holder_tx_signatures.clone()
311331
} else {
312332
None
@@ -987,7 +1007,7 @@ macro_rules! define_state_transitions {
9871007
let signing_session = InteractiveTxSigningSession {
9881008
holder_sends_tx_signatures_first: tx.holder_sends_tx_signatures_first,
9891009
unsigned_tx: tx,
990-
received_commitment_signed: false,
1010+
has_received_commitment_signed: false,
9911011
holder_tx_signatures: None,
9921012
counterparty_sent_tx_signatures: false,
9931013
};

0 commit comments

Comments
 (0)