Skip to content

Commit 47ea758

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 a240136 commit 47ea758

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
@@ -5898,7 +5895,7 @@ impl<SP: Deref> FundedChannel<SP> where
58985895
// OK, we step the channel here and *then* if the new generation fails we can fail the
58995896
// channel based on that, but stepping stuff here should be safe either way.
59005897
self.context.channel_state.clear_awaiting_remote_revoke();
5901-
self.context.sent_message_awaiting_response = None;
5898+
self.mark_response_received();
59025899
self.context.counterparty_prev_commitment_point = self.context.counterparty_cur_commitment_point;
59035900
self.context.counterparty_cur_commitment_point = Some(msg.next_per_commitment_point);
59045901
self.context.cur_counterparty_commitment_transaction_number -= 1;
@@ -6264,6 +6261,10 @@ impl<SP: Deref> FundedChannel<SP> where
62646261
return Err(())
62656262
}
62666263

6264+
// We only clear `peer_disconnected` if we were able to reestablish the channel. We always
6265+
// reset our awaiting response in case we failed reestablishment and are disconnecting.
6266+
self.context.sent_message_awaiting_response = None;
6267+
62676268
if self.context.channel_state.is_peer_disconnected() {
62686269
// While the below code should be idempotent, it's simpler to just return early, as
62696270
// redundant disconnect events can fire, though they should be rare.
@@ -6324,8 +6325,6 @@ impl<SP: Deref> FundedChannel<SP> where
63246325
}
63256326
}
63266327

6327-
self.context.sent_message_awaiting_response = None;
6328-
63296328
// Reset any quiescence-related state as it is implicitly terminated once disconnected.
63306329
if matches!(self.context.channel_state, ChannelState::ChannelReady(_)) {
63316330
self.context.channel_state.clear_awaiting_quiescence();
@@ -6450,10 +6449,6 @@ impl<SP: Deref> FundedChannel<SP> where
64506449
commitment_update = None;
64516450
}
64526451

6453-
if commitment_update.is_some() {
6454-
self.mark_awaiting_response();
6455-
}
6456-
64576452
self.context.monitor_pending_revoke_and_ack = false;
64586453
self.context.monitor_pending_commitment_signed = false;
64596454
let order = self.context.resend_order.clone();
@@ -6810,7 +6805,7 @@ impl<SP: Deref> FundedChannel<SP> where
68106805
// Go ahead and unmark PeerDisconnected as various calls we may make check for it (and all
68116806
// remaining cases either succeed or ErrorMessage-fail).
68126807
self.context.channel_state.clear_peer_disconnected();
6813-
self.context.sent_message_awaiting_response = None;
6808+
self.mark_response_received();
68146809

68156810
let shutdown_msg = self.get_outbound_shutdown();
68166811

@@ -6866,9 +6861,6 @@ impl<SP: Deref> FundedChannel<SP> where
68666861
// AwaitingRemoteRevoke set, which indicates we sent a commitment_signed but haven't gotten
68676862
// the corresponding revoke_and_ack back yet.
68686863
let is_awaiting_remote_revoke = self.context.channel_state.is_awaiting_remote_revoke();
6869-
if is_awaiting_remote_revoke && !self.is_awaiting_monitor_update() {
6870-
self.mark_awaiting_response();
6871-
}
68726864
let next_counterparty_commitment_number = INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number + if is_awaiting_remote_revoke { 1 } else { 0 };
68736865

68746866
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.holder_commitment_point.transaction_number() == 1 {
@@ -7053,26 +7045,34 @@ impl<SP: Deref> FundedChannel<SP> where
70537045
Ok((closing_signed, None, None))
70547046
}
70557047

7056-
// Marks a channel as waiting for a response from the counterparty. If it's not received
7057-
// [`DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`] after sending our own to them, then we'll attempt
7058-
// a reconnection.
7059-
fn mark_awaiting_response(&mut self) {
7060-
self.context.sent_message_awaiting_response = Some(0);
7048+
fn mark_response_received(&mut self) {
7049+
self.context.sent_message_awaiting_response = None;
70617050
}
70627051

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

70787078
pub fn shutdown(
@@ -8235,7 +8235,6 @@ impl<SP: Deref> FundedChannel<SP> where
82358235
log_info!(logger, "Sending a data_loss_protect with no previous remote per_commitment_secret for channel {}", &self.context.channel_id());
82368236
[0;32]
82378237
};
8238-
self.mark_awaiting_response();
82398238
msgs::ChannelReestablish {
82408239
channel_id: self.context.channel_id(),
82418240
// 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)