Skip to content

Commit a0b6dd6

Browse files
committed
Correctly order channel_ready on channel_reestablish
When handling channel_reestablish, the order in which channel_ready is sent depends on whether or not the initial commitment_signed / tx_signatures are being retransmitted. When they are, then channel_ready should come after them. Otherwise, channel_ready should come before any commitment_signed.
1 parent df80eb0 commit a0b6dd6

File tree

2 files changed

+88
-34
lines changed

2 files changed

+88
-34
lines changed

lightning/src/ln/channel.rs

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ use crate::ln::channel_state::{
5454
OutboundHTLCDetails, OutboundHTLCStateDetails,
5555
};
5656
use crate::ln::channelmanager::{
57-
self, FundingConfirmedMessage, HTLCFailureMsg, HTLCSource, OpenChannelMessage,
58-
PaymentClaimDetails, PendingHTLCInfo, PendingHTLCStatus, RAACommitmentOrder, SentHTLCId,
59-
BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA,
57+
self, ChannelReadyOrder, FundingConfirmedMessage, HTLCFailureMsg, HTLCSource,
58+
OpenChannelMessage, PaymentClaimDetails, PendingHTLCInfo, PendingHTLCStatus,
59+
RAACommitmentOrder, SentHTLCId, BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT,
60+
MIN_CLTV_EXPIRY_DELTA,
6061
};
6162
use crate::ln::funding::FundingTxInput;
6263
#[cfg(splicing)]
@@ -1181,13 +1182,14 @@ pub enum UpdateFulfillCommitFetch {
11811182
pub(super) struct MonitorRestoreUpdates {
11821183
pub raa: Option<msgs::RevokeAndACK>,
11831184
pub commitment_update: Option<msgs::CommitmentUpdate>,
1184-
pub order: RAACommitmentOrder,
1185+
pub commitment_order: RAACommitmentOrder,
11851186
pub accepted_htlcs: Vec<(PendingHTLCInfo, u64)>,
11861187
pub failed_htlcs: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
11871188
pub finalized_claimed_htlcs: Vec<(HTLCSource, Option<AttributionData>)>,
11881189
pub pending_update_adds: Vec<msgs::UpdateAddHTLC>,
11891190
pub funding_broadcastable: Option<Transaction>,
11901191
pub channel_ready: Option<msgs::ChannelReady>,
1192+
pub channel_ready_order: ChannelReadyOrder,
11911193
pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
11921194
pub tx_signatures: Option<msgs::TxSignatures>,
11931195
}
@@ -1210,9 +1212,10 @@ pub(super) struct SignerResumeUpdates {
12101212
/// The return value of `channel_reestablish`
12111213
pub(super) struct ReestablishResponses {
12121214
pub channel_ready: Option<msgs::ChannelReady>,
1215+
pub channel_ready_order: ChannelReadyOrder,
12131216
pub raa: Option<msgs::RevokeAndACK>,
12141217
pub commitment_update: Option<msgs::CommitmentUpdate>,
1215-
pub order: RAACommitmentOrder,
1218+
pub commitment_order: RAACommitmentOrder,
12161219
pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
12171220
pub shutdown_msg: Option<msgs::Shutdown>,
12181221
pub tx_signatures: Option<msgs::TxSignatures>,
@@ -8705,6 +8708,17 @@ where
87058708
}
87068709
}
87078710

8711+
// An active interactive signing session or an awaiting channel_ready state implies that a
8712+
// commitment_signed retransmission is an initial one for funding negotiation. Thus, the
8713+
// signatures should be sent before channel_ready.
8714+
let channel_ready_order = if self.interactive_tx_signing_session.is_some() {
8715+
ChannelReadyOrder::SignaturesFirst
8716+
} else if matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(_)) {
8717+
ChannelReadyOrder::SignaturesFirst
8718+
} else {
8719+
ChannelReadyOrder::ChannelReadyFirst
8720+
};
8721+
87088722
// We will never broadcast the funding transaction when we're in MonitorUpdateInProgress
87098723
// (and we assume the user never directly broadcasts the funding transaction and waits for
87108724
// us to do it). Thus, we can only ever hit monitor_pending_channel_ready when we're
@@ -8733,9 +8747,10 @@ where
87338747
self.context.monitor_pending_revoke_and_ack = false;
87348748
self.context.monitor_pending_commitment_signed = false;
87358749
return MonitorRestoreUpdates {
8736-
raa: None, commitment_update: None, order: RAACommitmentOrder::RevokeAndACKFirst,
8750+
raa: None, commitment_update: None, commitment_order: RAACommitmentOrder::RevokeAndACKFirst,
87378751
accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, pending_update_adds,
8738-
funding_broadcastable, channel_ready, announcement_sigs, tx_signatures: None
8752+
funding_broadcastable, channel_ready, announcement_sigs, tx_signatures: None,
8753+
channel_ready_order,
87398754
};
87408755
}
87418756

@@ -8758,14 +8773,15 @@ where
87588773

87598774
self.context.monitor_pending_revoke_and_ack = false;
87608775
self.context.monitor_pending_commitment_signed = false;
8761-
let order = self.context.resend_order.clone();
8776+
let commitment_order = self.context.resend_order.clone();
87628777
log_debug!(logger, "Restored monitor updating in channel {} resulting in {}{} commitment update and {} RAA, with {} first",
87638778
&self.context.channel_id(), if funding_broadcastable.is_some() { "a funding broadcastable, " } else { "" },
87648779
if commitment_update.is_some() { "a" } else { "no" }, if raa.is_some() { "an" } else { "no" },
8765-
match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
8780+
match commitment_order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
87668781
MonitorRestoreUpdates {
8767-
raa, commitment_update, order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs,
8768-
pending_update_adds, funding_broadcastable, channel_ready, announcement_sigs, tx_signatures: None
8782+
raa, commitment_update, commitment_order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs,
8783+
pending_update_adds, funding_broadcastable, channel_ready, announcement_sigs, tx_signatures: None,
8784+
channel_ready_order,
87698785
}
87708786
}
87718787

@@ -9168,8 +9184,9 @@ where
91689184
// Short circuit the whole handler as there is nothing we can resend them
91699185
return Ok(ReestablishResponses {
91709186
channel_ready: None,
9187+
channel_ready_order: ChannelReadyOrder::ChannelReadyFirst,
91719188
raa: None, commitment_update: None,
9172-
order: RAACommitmentOrder::CommitmentFirst,
9189+
commitment_order: RAACommitmentOrder::CommitmentFirst,
91739190
shutdown_msg, announcement_sigs,
91749191
tx_signatures: None,
91759192
tx_abort: None,
@@ -9179,8 +9196,9 @@ where
91799196
// We have OurChannelReady set!
91809197
return Ok(ReestablishResponses {
91819198
channel_ready: self.get_channel_ready(logger),
9199+
channel_ready_order: ChannelReadyOrder::ChannelReadyFirst,
91829200
raa: None, commitment_update: None,
9183-
order: RAACommitmentOrder::CommitmentFirst,
9201+
commitment_order: RAACommitmentOrder::CommitmentFirst,
91849202
shutdown_msg, announcement_sigs,
91859203
tx_signatures: None,
91869204
tx_abort: None,
@@ -9306,10 +9324,13 @@ where
93069324
};
93079325

93089326
Ok(ReestablishResponses {
9309-
channel_ready, shutdown_msg, announcement_sigs,
9327+
channel_ready,
9328+
channel_ready_order: ChannelReadyOrder::SignaturesFirst,
9329+
shutdown_msg,
9330+
announcement_sigs,
93109331
raa: required_revoke,
93119332
commitment_update,
9312-
order: self.context.resend_order.clone(),
9333+
commitment_order: self.context.resend_order.clone(),
93139334
tx_signatures,
93149335
tx_abort,
93159336
})
@@ -9323,9 +9344,11 @@ where
93239344
if self.context.channel_state.is_monitor_update_in_progress() {
93249345
self.context.monitor_pending_commitment_signed = true;
93259346
Ok(ReestablishResponses {
9326-
channel_ready, shutdown_msg, announcement_sigs,
9347+
channel_ready,
9348+
channel_ready_order: ChannelReadyOrder::ChannelReadyFirst,
9349+
shutdown_msg, announcement_sigs,
93279350
commitment_update: None, raa: None,
9328-
order: self.context.resend_order.clone(),
9351+
commitment_order: self.context.resend_order.clone(),
93299352
tx_signatures: None,
93309353
tx_abort: None,
93319354
})
@@ -9347,9 +9370,11 @@ where
93479370
required_revoke
93489371
};
93499372
Ok(ReestablishResponses {
9350-
channel_ready, shutdown_msg, announcement_sigs,
9373+
channel_ready,
9374+
channel_ready_order: ChannelReadyOrder::ChannelReadyFirst,
9375+
shutdown_msg, announcement_sigs,
93519376
raa, commitment_update,
9352-
order: self.context.resend_order.clone(),
9377+
commitment_order: self.context.resend_order.clone(),
93539378
tx_signatures: None,
93549379
tx_abort: None,
93559380
})

lightning/src/ln/channelmanager.rs

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,16 @@ pub(super) enum RAACommitmentOrder {
917917
RevokeAndACKFirst,
918918
}
919919

920+
/// Similar to scenarios used by [`RAACommitmentOrder`], this determines whether a `channel_ready`
921+
/// message should be sent first (i.e., prior to a `commitment_update`) or after the initial
922+
/// `commitment_update` and `tx_signatures` for channel funding.
923+
pub(super) enum ChannelReadyOrder {
924+
/// Send `channel_ready` message first.
925+
ChannelReadyFirst,
926+
/// Send initial `commitment_update` and `tx_signatures` first.
927+
SignaturesFirst,
928+
}
929+
920930
/// Information about a payment which is currently being claimed.
921931
#[derive(Clone, Debug, PartialEq, Eq)]
922932
struct ClaimingPayment {
@@ -3394,9 +3404,10 @@ macro_rules! handle_monitor_update_completion {
33943404

33953405
let (htlc_forwards, decode_update_add_htlcs) = $self.handle_channel_resumption(
33963406
&mut $peer_state.pending_msg_events, $chan, updates.raa,
3397-
updates.commitment_update, updates.order, updates.accepted_htlcs, updates.pending_update_adds,
3398-
updates.funding_broadcastable, updates.channel_ready,
3399-
updates.announcement_sigs, updates.tx_signatures, None);
3407+
updates.commitment_update, updates.commitment_order, updates.accepted_htlcs,
3408+
updates.pending_update_adds, updates.funding_broadcastable, updates.channel_ready,
3409+
updates.announcement_sigs, updates.tx_signatures, None, updates.channel_ready_order,
3410+
);
34003411
if let Some(upd) = channel_update {
34013412
$peer_state.pending_msg_events.push(upd);
34023413
}
@@ -8900,11 +8911,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
89008911
#[rustfmt::skip]
89018912
fn handle_channel_resumption(&self, pending_msg_events: &mut Vec<MessageSendEvent>,
89028913
channel: &mut FundedChannel<SP>, raa: Option<msgs::RevokeAndACK>,
8903-
commitment_update: Option<msgs::CommitmentUpdate>, order: RAACommitmentOrder,
8914+
commitment_update: Option<msgs::CommitmentUpdate>, commitment_order: RAACommitmentOrder,
89048915
pending_forwards: Vec<(PendingHTLCInfo, u64)>, pending_update_adds: Vec<msgs::UpdateAddHTLC>,
89058916
funding_broadcastable: Option<Transaction>,
89068917
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>,
89078918
tx_signatures: Option<msgs::TxSignatures>, tx_abort: Option<msgs::TxAbort>,
8919+
channel_ready_order: ChannelReadyOrder,
89088920
) -> (Option<(u64, PublicKey, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
89098921
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
89108922
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",
@@ -8935,14 +8947,17 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
89358947
decode_update_add_htlcs = Some((short_channel_id, pending_update_adds));
89368948
}
89378949

8938-
if let Some(msg) = channel_ready {
8939-
send_channel_ready!(self, pending_msg_events, channel, msg);
8940-
}
8941-
if let Some(msg) = announcement_sigs {
8942-
pending_msg_events.push(MessageSendEvent::SendAnnouncementSignatures {
8943-
node_id: counterparty_node_id,
8944-
msg,
8945-
});
8950+
if let ChannelReadyOrder::ChannelReadyFirst = channel_ready_order {
8951+
if let Some(msg) = &channel_ready {
8952+
send_channel_ready!(self, pending_msg_events, channel, msg.clone());
8953+
}
8954+
8955+
if let Some(msg) = &announcement_sigs {
8956+
pending_msg_events.push(MessageSendEvent::SendAnnouncementSignatures {
8957+
node_id: counterparty_node_id,
8958+
msg: msg.clone(),
8959+
});
8960+
}
89468961
}
89478962

89488963
macro_rules! handle_cs { () => {
@@ -8962,7 +8977,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
89628977
});
89638978
}
89648979
} }
8965-
match order {
8980+
match commitment_order {
89668981
RAACommitmentOrder::CommitmentFirst => {
89678982
handle_cs!();
89688983
handle_raa!();
@@ -8987,6 +9002,19 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
89879002
});
89889003
}
89899004

9005+
if let ChannelReadyOrder::SignaturesFirst = channel_ready_order {
9006+
if let Some(msg) = channel_ready {
9007+
send_channel_ready!(self, pending_msg_events, channel, msg);
9008+
}
9009+
9010+
if let Some(msg) = announcement_sigs {
9011+
pending_msg_events.push(MessageSendEvent::SendAnnouncementSignatures {
9012+
node_id: counterparty_node_id,
9013+
msg,
9014+
});
9015+
}
9016+
}
9017+
89909018
if let Some(tx) = funding_broadcastable {
89919019
if channel.context.is_manual_broadcast() {
89929020
log_info!(logger, "Not broadcasting funding transaction with txid {} as it is manually managed", tx.compute_txid());
@@ -11049,9 +11077,10 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1104911077
}
1105011078
let need_lnd_workaround = chan.context.workaround_lnd_bug_4006.take();
1105111079
let (htlc_forwards, decode_update_add_htlcs) = self.handle_channel_resumption(
11052-
&mut peer_state.pending_msg_events, chan, responses.raa, responses.commitment_update, responses.order,
11080+
&mut peer_state.pending_msg_events, chan, responses.raa, responses.commitment_update, responses.commitment_order,
1105311081
Vec::new(), Vec::new(), None, responses.channel_ready, responses.announcement_sigs,
11054-
responses.tx_signatures, responses.tx_abort);
11082+
responses.tx_signatures, responses.tx_abort, responses.channel_ready_order,
11083+
);
1105511084
debug_assert!(htlc_forwards.is_none());
1105611085
debug_assert!(decode_update_add_htlcs.is_none());
1105711086
if let Some(upd) = channel_update {

0 commit comments

Comments
 (0)