Skip to content

Commit cea6413

Browse files
jkczyzAnyitechs
authored andcommitted
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 80f0b1b commit cea6413

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>,
@@ -8735,6 +8738,17 @@ where
87358738
}
87368739
}
87378740

8741+
// An active interactive signing session or an awaiting channel_ready state implies that a
8742+
// commitment_signed retransmission is an initial one for funding negotiation. Thus, the
8743+
// signatures should be sent before channel_ready.
8744+
let channel_ready_order = if self.interactive_tx_signing_session.is_some() {
8745+
ChannelReadyOrder::SignaturesFirst
8746+
} else if matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(_)) {
8747+
ChannelReadyOrder::SignaturesFirst
8748+
} else {
8749+
ChannelReadyOrder::ChannelReadyFirst
8750+
};
8751+
87388752
// We will never broadcast the funding transaction when we're in MonitorUpdateInProgress
87398753
// (and we assume the user never directly broadcasts the funding transaction and waits for
87408754
// us to do it). Thus, we can only ever hit monitor_pending_channel_ready when we're
@@ -8763,9 +8777,10 @@ where
87638777
self.context.monitor_pending_revoke_and_ack = false;
87648778
self.context.monitor_pending_commitment_signed = false;
87658779
return MonitorRestoreUpdates {
8766-
raa: None, commitment_update: None, order: RAACommitmentOrder::RevokeAndACKFirst,
8780+
raa: None, commitment_update: None, commitment_order: RAACommitmentOrder::RevokeAndACKFirst,
87678781
accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, pending_update_adds,
8768-
funding_broadcastable, channel_ready, announcement_sigs, tx_signatures: None
8782+
funding_broadcastable, channel_ready, announcement_sigs, tx_signatures: None,
8783+
channel_ready_order,
87698784
};
87708785
}
87718786

@@ -8788,14 +8803,15 @@ where
87888803

87898804
self.context.monitor_pending_revoke_and_ack = false;
87908805
self.context.monitor_pending_commitment_signed = false;
8791-
let order = self.context.resend_order.clone();
8806+
let commitment_order = self.context.resend_order.clone();
87928807
log_debug!(logger, "Restored monitor updating in channel {} resulting in {}{} commitment update and {} RAA, with {} first",
87938808
&self.context.channel_id(), if funding_broadcastable.is_some() { "a funding broadcastable, " } else { "" },
87948809
if commitment_update.is_some() { "a" } else { "no" }, if raa.is_some() { "an" } else { "no" },
8795-
match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
8810+
match commitment_order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
87968811
MonitorRestoreUpdates {
8797-
raa, commitment_update, order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs,
8798-
pending_update_adds, funding_broadcastable, channel_ready, announcement_sigs, tx_signatures: None
8812+
raa, commitment_update, commitment_order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs,
8813+
pending_update_adds, funding_broadcastable, channel_ready, announcement_sigs, tx_signatures: None,
8814+
channel_ready_order,
87998815
}
88008816
}
88018817

@@ -9205,8 +9221,9 @@ where
92059221
// Short circuit the whole handler as there is nothing we can resend them
92069222
return Ok(ReestablishResponses {
92079223
channel_ready: None,
9224+
channel_ready_order: ChannelReadyOrder::ChannelReadyFirst,
92089225
raa: None, commitment_update: None,
9209-
order: RAACommitmentOrder::CommitmentFirst,
9226+
commitment_order: RAACommitmentOrder::CommitmentFirst,
92109227
shutdown_msg, announcement_sigs,
92119228
tx_signatures: None,
92129229
tx_abort: None,
@@ -9216,8 +9233,9 @@ where
92169233
// We have OurChannelReady set!
92179234
return Ok(ReestablishResponses {
92189235
channel_ready: self.get_channel_ready(logger),
9236+
channel_ready_order: ChannelReadyOrder::ChannelReadyFirst,
92199237
raa: None, commitment_update: None,
9220-
order: RAACommitmentOrder::CommitmentFirst,
9238+
commitment_order: RAACommitmentOrder::CommitmentFirst,
92219239
shutdown_msg, announcement_sigs,
92229240
tx_signatures: None,
92239241
tx_abort: None,
@@ -9343,10 +9361,13 @@ where
93439361
};
93449362

93459363
Ok(ReestablishResponses {
9346-
channel_ready, shutdown_msg, announcement_sigs,
9364+
channel_ready,
9365+
channel_ready_order: ChannelReadyOrder::SignaturesFirst,
9366+
shutdown_msg,
9367+
announcement_sigs,
93479368
raa: required_revoke,
93489369
commitment_update,
9349-
order: self.context.resend_order.clone(),
9370+
commitment_order: self.context.resend_order.clone(),
93509371
tx_signatures,
93519372
tx_abort,
93529373
})
@@ -9360,9 +9381,11 @@ where
93609381
if self.context.channel_state.is_monitor_update_in_progress() {
93619382
self.context.monitor_pending_commitment_signed = true;
93629383
Ok(ReestablishResponses {
9363-
channel_ready, shutdown_msg, announcement_sigs,
9384+
channel_ready,
9385+
channel_ready_order: ChannelReadyOrder::ChannelReadyFirst,
9386+
shutdown_msg, announcement_sigs,
93649387
commitment_update: None, raa: None,
9365-
order: self.context.resend_order.clone(),
9388+
commitment_order: self.context.resend_order.clone(),
93669389
tx_signatures: None,
93679390
tx_abort: None,
93689391
})
@@ -9384,9 +9407,11 @@ where
93849407
required_revoke
93859408
};
93869409
Ok(ReestablishResponses {
9387-
channel_ready, shutdown_msg, announcement_sigs,
9410+
channel_ready,
9411+
channel_ready_order: ChannelReadyOrder::ChannelReadyFirst,
9412+
shutdown_msg, announcement_sigs,
93889413
raa, commitment_update,
9389-
order: self.context.resend_order.clone(),
9414+
commitment_order: self.context.resend_order.clone(),
93909415
tx_signatures: None,
93919416
tx_abort: None,
93929417
})

lightning/src/ln/channelmanager.rs

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,16 @@ pub(super) enum RAACommitmentOrder {
938938
RevokeAndACKFirst,
939939
}
940940

941+
/// Similar to scenarios used by [`RAACommitmentOrder`], this determines whether a `channel_ready`
942+
/// message should be sent first (i.e., prior to a `commitment_update`) or after the initial
943+
/// `commitment_update` and `tx_signatures` for channel funding.
944+
pub(super) enum ChannelReadyOrder {
945+
/// Send `channel_ready` message first.
946+
ChannelReadyFirst,
947+
/// Send initial `commitment_update` and `tx_signatures` first.
948+
SignaturesFirst,
949+
}
950+
941951
/// Information about a payment which is currently being claimed.
942952
#[derive(Clone, Debug, PartialEq, Eq)]
943953
struct ClaimingPayment {
@@ -3415,9 +3425,10 @@ macro_rules! handle_monitor_update_completion {
34153425

34163426
let (htlc_forwards, decode_update_add_htlcs) = $self.handle_channel_resumption(
34173427
&mut $peer_state.pending_msg_events, $chan, updates.raa,
3418-
updates.commitment_update, updates.order, updates.accepted_htlcs, updates.pending_update_adds,
3419-
updates.funding_broadcastable, updates.channel_ready,
3420-
updates.announcement_sigs, updates.tx_signatures, None);
3428+
updates.commitment_update, updates.commitment_order, updates.accepted_htlcs,
3429+
updates.pending_update_adds, updates.funding_broadcastable, updates.channel_ready,
3430+
updates.announcement_sigs, updates.tx_signatures, None, updates.channel_ready_order,
3431+
);
34213432
if let Some(upd) = channel_update {
34223433
$peer_state.pending_msg_events.push(upd);
34233434
}
@@ -8895,11 +8906,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
88958906
#[rustfmt::skip]
88968907
fn handle_channel_resumption(&self, pending_msg_events: &mut Vec<MessageSendEvent>,
88978908
channel: &mut FundedChannel<SP>, raa: Option<msgs::RevokeAndACK>,
8898-
commitment_update: Option<msgs::CommitmentUpdate>, order: RAACommitmentOrder,
8909+
commitment_update: Option<msgs::CommitmentUpdate>, commitment_order: RAACommitmentOrder,
88998910
pending_forwards: Vec<(PendingHTLCInfo, u64)>, pending_update_adds: Vec<msgs::UpdateAddHTLC>,
89008911
funding_broadcastable: Option<Transaction>,
89018912
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>,
89028913
tx_signatures: Option<msgs::TxSignatures>, tx_abort: Option<msgs::TxAbort>,
8914+
channel_ready_order: ChannelReadyOrder,
89038915
) -> (Option<(u64, PublicKey, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
89048916
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
89058917
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",
@@ -8930,14 +8942,17 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
89308942
decode_update_add_htlcs = Some((short_channel_id, pending_update_adds));
89318943
}
89328944

8933-
if let Some(msg) = channel_ready {
8934-
send_channel_ready!(self, pending_msg_events, channel, msg);
8935-
}
8936-
if let Some(msg) = announcement_sigs {
8937-
pending_msg_events.push(MessageSendEvent::SendAnnouncementSignatures {
8938-
node_id: counterparty_node_id,
8939-
msg,
8940-
});
8945+
if let ChannelReadyOrder::ChannelReadyFirst = channel_ready_order {
8946+
if let Some(msg) = &channel_ready {
8947+
send_channel_ready!(self, pending_msg_events, channel, msg.clone());
8948+
}
8949+
8950+
if let Some(msg) = &announcement_sigs {
8951+
pending_msg_events.push(MessageSendEvent::SendAnnouncementSignatures {
8952+
node_id: counterparty_node_id,
8953+
msg: msg.clone(),
8954+
});
8955+
}
89418956
}
89428957

89438958
macro_rules! handle_cs { () => {
@@ -8957,7 +8972,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
89578972
});
89588973
}
89598974
} }
8960-
match order {
8975+
match commitment_order {
89618976
RAACommitmentOrder::CommitmentFirst => {
89628977
handle_cs!();
89638978
handle_raa!();
@@ -8982,6 +8997,19 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
89828997
});
89838998
}
89848999

9000+
if let ChannelReadyOrder::SignaturesFirst = channel_ready_order {
9001+
if let Some(msg) = channel_ready {
9002+
send_channel_ready!(self, pending_msg_events, channel, msg);
9003+
}
9004+
9005+
if let Some(msg) = announcement_sigs {
9006+
pending_msg_events.push(MessageSendEvent::SendAnnouncementSignatures {
9007+
node_id: counterparty_node_id,
9008+
msg,
9009+
});
9010+
}
9011+
}
9012+
89859013
if let Some(tx) = funding_broadcastable {
89869014
if channel.context.is_manual_broadcast() {
89879015
log_info!(logger, "Not broadcasting funding transaction with txid {} as it is manually managed", tx.compute_txid());
@@ -11047,9 +11075,10 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1104711075
}
1104811076
let need_lnd_workaround = chan.context.workaround_lnd_bug_4006.take();
1104911077
let (htlc_forwards, decode_update_add_htlcs) = self.handle_channel_resumption(
11050-
&mut peer_state.pending_msg_events, chan, responses.raa, responses.commitment_update, responses.order,
11078+
&mut peer_state.pending_msg_events, chan, responses.raa, responses.commitment_update, responses.commitment_order,
1105111079
Vec::new(), Vec::new(), None, responses.channel_ready, responses.announcement_sigs,
11052-
responses.tx_signatures, responses.tx_abort);
11080+
responses.tx_signatures, responses.tx_abort, responses.channel_ready_order,
11081+
);
1105311082
debug_assert!(htlc_forwards.is_none());
1105411083
debug_assert!(decode_update_add_htlcs.is_none());
1105511084
if let Some(upd) = channel_update {

0 commit comments

Comments
 (0)