@@ -1144,9 +1144,8 @@ pub(crate) const MIN_AFFORDABLE_HTLC_COUNT: usize = 4;
1144
1144
/// * `EXPIRE_PREV_CONFIG_TICKS` = convergence_delay / tick_interval
1145
1145
pub(crate) const EXPIRE_PREV_CONFIG_TICKS: usize = 5;
1146
1146
1147
- /// The number of ticks that may elapse while we're waiting for a response to a
1148
- /// [`msgs::RevokeAndACK`] or [`msgs::ChannelReestablish`] message before we attempt to disconnect
1149
- /// them.
1147
+ /// The number of ticks that may elapse while we're waiting for a response before we attempt to
1148
+ /// disconnect them.
1150
1149
///
1151
1150
/// See [`ChannelContext::sent_message_awaiting_response`] for more information.
1152
1151
pub(crate) const DISCONNECT_PEER_AWAITING_RESPONSE_TICKS: usize = 2;
@@ -1874,16 +1873,14 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
1874
1873
pub workaround_lnd_bug_4006: Option<msgs::ChannelReady>,
1875
1874
1876
1875
/// An option set when we wish to track how many ticks have elapsed while waiting for a response
1877
- /// from our counterparty after sending a message . If the peer has yet to respond after reaching
1878
- /// `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`, a reconnection should be attempted to try to
1879
- /// unblock the state machine.
1876
+ /// from our counterparty after entering specific states . If the peer has yet to respond after
1877
+ /// reaching `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`, a reconnection should be attempted to
1878
+ /// try to unblock the state machine.
1880
1879
///
1881
- /// This behavior is mostly motivated by a lnd bug in which we don't receive a message we expect
1882
- /// to in a timely manner, which may lead to channels becoming unusable and/or force-closed. An
1883
- /// example of such can be found at <https://github.com/lightningnetwork/lnd/issues/7682>.
1884
- ///
1885
- /// This is currently only used when waiting for a [`msgs::ChannelReestablish`] or
1886
- /// [`msgs::RevokeAndACK`] message from the counterparty.
1880
+ /// This behavior was initially motivated by a lnd bug in which we don't receive a message we
1881
+ /// expect to in a timely manner, which may lead to channels becoming unusable and/or
1882
+ /// force-closed. An example of such can be found at
1883
+ /// <https://github.com/lightningnetwork/lnd/issues/7682>.
1887
1884
sent_message_awaiting_response: Option<usize>,
1888
1885
1889
1886
/// This channel's type, as negotiated during channel open
@@ -5929,7 +5926,7 @@ impl<SP: Deref> FundedChannel<SP> where
5929
5926
// OK, we step the channel here and *then* if the new generation fails we can fail the
5930
5927
// channel based on that, but stepping stuff here should be safe either way.
5931
5928
self.context.channel_state.clear_awaiting_remote_revoke();
5932
- self.context.sent_message_awaiting_response = None ;
5929
+ self.mark_response_received() ;
5933
5930
self.context.counterparty_prev_commitment_point = self.context.counterparty_cur_commitment_point;
5934
5931
self.context.counterparty_cur_commitment_point = Some(msg.next_per_commitment_point);
5935
5932
self.context.cur_counterparty_commitment_transaction_number -= 1;
@@ -6295,6 +6292,10 @@ impl<SP: Deref> FundedChannel<SP> where
6295
6292
return Err(())
6296
6293
}
6297
6294
6295
+ // We only clear `peer_disconnected` if we were able to reestablish the channel. We always
6296
+ // reset our awaiting response in case we failed reestablishment and are disconnecting.
6297
+ self.context.sent_message_awaiting_response = None;
6298
+
6298
6299
if self.context.channel_state.is_peer_disconnected() {
6299
6300
// While the below code should be idempotent, it's simpler to just return early, as
6300
6301
// redundant disconnect events can fire, though they should be rare.
@@ -6355,8 +6356,6 @@ impl<SP: Deref> FundedChannel<SP> where
6355
6356
}
6356
6357
}
6357
6358
6358
- self.context.sent_message_awaiting_response = None;
6359
-
6360
6359
// Reset any quiescence-related state as it is implicitly terminated once disconnected.
6361
6360
if matches!(self.context.channel_state, ChannelState::ChannelReady(_)) {
6362
6361
self.context.channel_state.clear_awaiting_quiescence();
@@ -6481,10 +6480,6 @@ impl<SP: Deref> FundedChannel<SP> where
6481
6480
commitment_update = None;
6482
6481
}
6483
6482
6484
- if commitment_update.is_some() {
6485
- self.mark_awaiting_response();
6486
- }
6487
-
6488
6483
self.context.monitor_pending_revoke_and_ack = false;
6489
6484
self.context.monitor_pending_commitment_signed = false;
6490
6485
let order = self.context.resend_order.clone();
@@ -6841,7 +6836,7 @@ impl<SP: Deref> FundedChannel<SP> where
6841
6836
// Go ahead and unmark PeerDisconnected as various calls we may make check for it (and all
6842
6837
// remaining cases either succeed or ErrorMessage-fail).
6843
6838
self.context.channel_state.clear_peer_disconnected();
6844
- self.context.sent_message_awaiting_response = None ;
6839
+ self.mark_response_received() ;
6845
6840
6846
6841
let shutdown_msg = self.get_outbound_shutdown();
6847
6842
@@ -6897,9 +6892,6 @@ impl<SP: Deref> FundedChannel<SP> where
6897
6892
// AwaitingRemoteRevoke set, which indicates we sent a commitment_signed but haven't gotten
6898
6893
// the corresponding revoke_and_ack back yet.
6899
6894
let is_awaiting_remote_revoke = self.context.channel_state.is_awaiting_remote_revoke();
6900
- if is_awaiting_remote_revoke && !self.is_awaiting_monitor_update() {
6901
- self.mark_awaiting_response();
6902
- }
6903
6895
let next_counterparty_commitment_number = INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number + if is_awaiting_remote_revoke { 1 } else { 0 };
6904
6896
6905
6897
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.holder_commitment_point.transaction_number() == 1 {
@@ -7084,26 +7076,34 @@ impl<SP: Deref> FundedChannel<SP> where
7084
7076
Ok((closing_signed, None, None))
7085
7077
}
7086
7078
7087
- // Marks a channel as waiting for a response from the counterparty. If it's not received
7088
- // [`DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`] after sending our own to them, then we'll attempt
7089
- // a reconnection.
7090
- fn mark_awaiting_response(&mut self) {
7091
- self.context.sent_message_awaiting_response = Some(0);
7079
+ fn mark_response_received(&mut self) {
7080
+ self.context.sent_message_awaiting_response = None;
7092
7081
}
7093
7082
7094
7083
/// Determines whether we should disconnect the counterparty due to not receiving a response
7095
7084
/// within our expected timeframe.
7096
7085
///
7097
- /// This should be called on every [`super::channelmanager::ChannelManager::timer_tick_occurred`].
7086
+ /// This should be called for peers with an active socket on every
7087
+ /// [`super::channelmanager::ChannelManager::timer_tick_occurred`].
7088
+ #[allow(clippy::assertions_on_constants)]
7098
7089
pub fn should_disconnect_peer_awaiting_response(&mut self) -> bool {
7099
- let ticks_elapsed = if let Some(ticks_elapsed) = self.context.sent_message_awaiting_response.as_mut() {
7100
- ticks_elapsed
7090
+ if let Some(ticks_elapsed) = self.context.sent_message_awaiting_response.as_mut() {
7091
+ *ticks_elapsed += 1;
7092
+ *ticks_elapsed >= DISCONNECT_PEER_AWAITING_RESPONSE_TICKS
7093
+ } else if
7094
+ // Cleared upon receiving `channel_reestablish`.
7095
+ self.context.channel_state.is_peer_disconnected()
7096
+ // Cleared upon receiving `revoke_and_ack`.
7097
+ || self.context.has_pending_channel_update()
7098
+ {
7099
+ // This is the first tick we've seen after expecting to make forward progress.
7100
+ self.context.sent_message_awaiting_response = Some(1);
7101
+ debug_assert!(DISCONNECT_PEER_AWAITING_RESPONSE_TICKS > 1);
7102
+ false
7101
7103
} else {
7102
7104
// Don't disconnect when we're not waiting on a response.
7103
- return false;
7104
- };
7105
- *ticks_elapsed += 1;
7106
- *ticks_elapsed >= DISCONNECT_PEER_AWAITING_RESPONSE_TICKS
7105
+ false
7106
+ }
7107
7107
}
7108
7108
7109
7109
pub fn shutdown(
@@ -8266,7 +8266,6 @@ impl<SP: Deref> FundedChannel<SP> where
8266
8266
log_info!(logger, "Sending a data_loss_protect with no previous remote per_commitment_secret for channel {}", &self.context.channel_id());
8267
8267
[0;32]
8268
8268
};
8269
- self.mark_awaiting_response();
8270
8269
msgs::ChannelReestablish {
8271
8270
channel_id: self.context.channel_id(),
8272
8271
// The protocol has two different commitment number concepts - the "commitment
0 commit comments