Skip to content

Commit 8ceeb00

Browse files
committed
Track message timeout ticks based on internal states
With the introduction of `has_pending_channel_update`, we can now determine whether any messages are owed to irrevocably commit HTLC updates based on the current channel state. We prefer using the channel state, over manually tracking as previously done, to have a single source of truth. We also gain the ability to expect to receive multiple messages at once, which will become relevant with the quiescence protocol, where we may be waiting on a counterparty `revoke_and_ack` and `stfu`.
1 parent 1678b2e commit 8ceeb00

File tree

2 files changed

+49
-48
lines changed

2 files changed

+49
-48
lines changed

lightning/src/ln/channel.rs

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,9 +1145,8 @@ pub(crate) const MIN_AFFORDABLE_HTLC_COUNT: usize = 4;
11451145
/// * `EXPIRE_PREV_CONFIG_TICKS` = convergence_delay / tick_interval
11461146
pub(crate) const EXPIRE_PREV_CONFIG_TICKS: usize = 5;
11471147

1148-
/// The number of ticks that may elapse while we're waiting for a response to a
1149-
/// [`msgs::RevokeAndACK`] or [`msgs::ChannelReestablish`] message before we attempt to disconnect
1150-
/// them.
1148+
/// The number of ticks that may elapse while we're waiting for a response before we attempt to
1149+
/// disconnect them.
11511150
///
11521151
/// See [`ChannelContext::sent_message_awaiting_response`] for more information.
11531152
pub(crate) const DISCONNECT_PEER_AWAITING_RESPONSE_TICKS: usize = 2;
@@ -1829,16 +1828,14 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
18291828
pub workaround_lnd_bug_4006: Option<msgs::ChannelReady>,
18301829

18311830
/// An option set when we wish to track how many ticks have elapsed while waiting for a response
1832-
/// from our counterparty after sending a message. If the peer has yet to respond after reaching
1833-
/// `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`, a reconnection should be attempted to try to
1834-
/// unblock the state machine.
1831+
/// from our counterparty after entering specific states. If the peer has yet to respond after
1832+
/// reaching `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`, a reconnection should be attempted to
1833+
/// try to unblock the state machine.
18351834
///
1836-
/// This behavior is mostly motivated by a lnd bug in which we don't receive a message we expect
1837-
/// to in a timely manner, which may lead to channels becoming unusable and/or force-closed. An
1838-
/// example of such can be found at <https://github.com/lightningnetwork/lnd/issues/7682>.
1839-
///
1840-
/// This is currently only used when waiting for a [`msgs::ChannelReestablish`] or
1841-
/// [`msgs::RevokeAndACK`] message from the counterparty.
1835+
/// This behavior was initially motivated by a lnd bug in which we don't receive a message we
1836+
/// expect to in a timely manner, which may lead to channels becoming unusable and/or
1837+
/// force-closed. An example of such can be found at
1838+
/// <https://github.com/lightningnetwork/lnd/issues/7682>.
18421839
sent_message_awaiting_response: Option<usize>,
18431840

18441841
/// This channel's type, as negotiated during channel open
@@ -5895,7 +5892,7 @@ impl<SP: Deref> FundedChannel<SP> where
58955892
// OK, we step the channel here and *then* if the new generation fails we can fail the
58965893
// channel based on that, but stepping stuff here should be safe either way.
58975894
self.context.channel_state.clear_awaiting_remote_revoke();
5898-
self.context.sent_message_awaiting_response = None;
5895+
self.mark_response_received();
58995896
self.context.counterparty_prev_commitment_point = self.context.counterparty_cur_commitment_point;
59005897
self.context.counterparty_cur_commitment_point = Some(msg.next_per_commitment_point);
59015898
self.context.cur_counterparty_commitment_transaction_number -= 1;
@@ -6261,6 +6258,10 @@ impl<SP: Deref> FundedChannel<SP> where
62616258
return Err(())
62626259
}
62636260

6261+
// We only clear `peer_disconnected` if we were able to reestablish the channel. We always
6262+
// reset our awaiting response in case we failed reestablishment and are disconnecting.
6263+
self.context.sent_message_awaiting_response = None;
6264+
62646265
if self.context.channel_state.is_peer_disconnected() {
62656266
// While the below code should be idempotent, it's simpler to just return early, as
62666267
// redundant disconnect events can fire, though they should be rare.
@@ -6321,8 +6322,6 @@ impl<SP: Deref> FundedChannel<SP> where
63216322
}
63226323
}
63236324

6324-
self.context.sent_message_awaiting_response = None;
6325-
63266325
// Reset any quiescence-related state as it is implicitly terminated once disconnected.
63276326
if matches!(self.context.channel_state, ChannelState::ChannelReady(_)) {
63286327
self.context.channel_state.clear_awaiting_quiescence();
@@ -6447,10 +6446,6 @@ impl<SP: Deref> FundedChannel<SP> where
64476446
commitment_update = None;
64486447
}
64496448

6450-
if commitment_update.is_some() {
6451-
self.mark_awaiting_response();
6452-
}
6453-
64546449
self.context.monitor_pending_revoke_and_ack = false;
64556450
self.context.monitor_pending_commitment_signed = false;
64566451
let order = self.context.resend_order.clone();
@@ -6807,7 +6802,7 @@ impl<SP: Deref> FundedChannel<SP> where
68076802
// Go ahead and unmark PeerDisconnected as various calls we may make check for it (and all
68086803
// remaining cases either succeed or ErrorMessage-fail).
68096804
self.context.channel_state.clear_peer_disconnected();
6810-
self.context.sent_message_awaiting_response = None;
6805+
self.mark_response_received();
68116806

68126807
let shutdown_msg = self.get_outbound_shutdown();
68136808

@@ -6863,9 +6858,6 @@ impl<SP: Deref> FundedChannel<SP> where
68636858
// AwaitingRemoteRevoke set, which indicates we sent a commitment_signed but haven't gotten
68646859
// the corresponding revoke_and_ack back yet.
68656860
let is_awaiting_remote_revoke = self.context.channel_state.is_awaiting_remote_revoke();
6866-
if is_awaiting_remote_revoke && !self.is_awaiting_monitor_update() {
6867-
self.mark_awaiting_response();
6868-
}
68696861
let next_counterparty_commitment_number = INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number + if is_awaiting_remote_revoke { 1 } else { 0 };
68706862

68716863
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.holder_commitment_point.transaction_number() == 1 {
@@ -7050,26 +7042,34 @@ impl<SP: Deref> FundedChannel<SP> where
70507042
Ok((closing_signed, None, None))
70517043
}
70527044

7053-
// Marks a channel as waiting for a response from the counterparty. If it's not received
7054-
// [`DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`] after sending our own to them, then we'll attempt
7055-
// a reconnection.
7056-
fn mark_awaiting_response(&mut self) {
7057-
self.context.sent_message_awaiting_response = Some(0);
7045+
fn mark_response_received(&mut self) {
7046+
self.context.sent_message_awaiting_response = None;
70587047
}
70597048

70607049
/// Determines whether we should disconnect the counterparty due to not receiving a response
70617050
/// within our expected timeframe.
70627051
///
7063-
/// This should be called on every [`super::channelmanager::ChannelManager::timer_tick_occurred`].
7052+
/// This should be called for peers with an active socket on every
7053+
/// [`super::channelmanager::ChannelManager::timer_tick_occurred`].
7054+
#[allow(clippy::assertions_on_constants)]
70647055
pub fn should_disconnect_peer_awaiting_response(&mut self) -> bool {
7065-
let ticks_elapsed = if let Some(ticks_elapsed) = self.context.sent_message_awaiting_response.as_mut() {
7066-
ticks_elapsed
7056+
if let Some(ticks_elapsed) = self.context.sent_message_awaiting_response.as_mut() {
7057+
*ticks_elapsed += 1;
7058+
*ticks_elapsed >= DISCONNECT_PEER_AWAITING_RESPONSE_TICKS
7059+
} else if
7060+
// Cleared upon receiving `channel_reestablish`.
7061+
self.context.channel_state.is_peer_disconnected()
7062+
// Cleared upon receiving `revoke_and_ack`.
7063+
|| self.context.has_pending_channel_update()
7064+
{
7065+
// This is the first tick we've seen after expecting to make forward progress.
7066+
self.context.sent_message_awaiting_response = Some(1);
7067+
debug_assert!(DISCONNECT_PEER_AWAITING_RESPONSE_TICKS > 1);
7068+
false
70677069
} else {
70687070
// Don't disconnect when we're not waiting on a response.
7069-
return false;
7070-
};
7071-
*ticks_elapsed += 1;
7072-
*ticks_elapsed >= DISCONNECT_PEER_AWAITING_RESPONSE_TICKS
7071+
false
7072+
}
70737073
}
70747074

70757075
pub fn shutdown(
@@ -8232,7 +8232,6 @@ impl<SP: Deref> FundedChannel<SP> where
82328232
log_info!(logger, "Sending a data_loss_protect with no previous remote per_commitment_secret for channel {}", &self.context.channel_id());
82338233
[0;32]
82348234
};
8235-
self.mark_awaiting_response();
82368235
msgs::ChannelReestablish {
82378236
channel_id: self.context.channel_id(),
82388237
// The protocol has two different commitment number concepts - the "commitment

lightning/src/ln/channelmanager.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6532,19 +6532,21 @@ where
65326532

65336533
funded_chan.context.maybe_expire_prev_config();
65346534

6535-
if funded_chan.should_disconnect_peer_awaiting_response() {
6536-
let logger = WithChannelContext::from(&self.logger, &funded_chan.context, None);
6537-
log_debug!(logger, "Disconnecting peer {} due to not making any progress on channel {}",
6538-
counterparty_node_id, chan_id);
6539-
pending_msg_events.push(MessageSendEvent::HandleError {
6540-
node_id: counterparty_node_id,
6541-
action: msgs::ErrorAction::DisconnectPeerWithWarning {
6542-
msg: msgs::WarningMessage {
6543-
channel_id: *chan_id,
6544-
data: "Disconnecting due to timeout awaiting response".to_owned(),
6535+
if peer_state.is_connected {
6536+
if funded_chan.should_disconnect_peer_awaiting_response() {
6537+
let logger = WithChannelContext::from(&self.logger, &funded_chan.context, None);
6538+
log_debug!(logger, "Disconnecting peer {} due to not making any progress on channel {}",
6539+
counterparty_node_id, chan_id);
6540+
pending_msg_events.push(MessageSendEvent::HandleError {
6541+
node_id: counterparty_node_id,
6542+
action: msgs::ErrorAction::DisconnectPeerWithWarning {
6543+
msg: msgs::WarningMessage {
6544+
channel_id: *chan_id,
6545+
data: "Disconnecting due to timeout awaiting response".to_owned(),
6546+
},
65456547
},
6546-
},
6547-
});
6548+
});
6549+
}
65486550
}
65496551

65506552
true

0 commit comments

Comments
 (0)