@@ -579,9 +579,15 @@ pub(crate) const MIN_AFFORDABLE_HTLC_COUNT: usize = 4;
579
579
/// * `EXPIRE_PREV_CONFIG_TICKS` = convergence_delay / tick_interval
580
580
pub(crate) const EXPIRE_PREV_CONFIG_TICKS: usize = 5;
581
581
582
- /// The number of ticks that may elapse while we're waiting for a response to a
583
- /// [`msgs::RevokeAndACK`] or [`msgs::ChannelReestablish`] message before we attempt to disconnect
584
- /// them.
582
+ /// The number of ticks that may elapse while we're waiting for a response to any of the following
583
+ /// messages
584
+ ///
585
+ /// [`msgs::RevokeAndACK`]
586
+ /// [`msgs::ChannelReestablish`]
587
+ /// [`msgs::OpenChannel`]
588
+ /// [`msgs::AcceptChannel`]
589
+ ///
590
+ /// before we attempt to disconnect them.
585
591
///
586
592
/// See [`ChannelContext::sent_message_awaiting_response`] for more information.
587
593
pub(crate) const DISCONNECT_PEER_AWAITING_RESPONSE_TICKS: usize = 2;
@@ -1961,6 +1967,28 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
1961
1967
self.update_time_counter += 1;
1962
1968
(monitor_update, dropped_outbound_htlcs)
1963
1969
}
1970
+
1971
+ // Marks a channel as waiting for a response from the counterparty. If it's not received
1972
+ // [`DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`] after sending our own to them, then we'll attempt
1973
+ // a reconnection.
1974
+ fn mark_awaiting_response(&mut self) {
1975
+ self.sent_message_awaiting_response = Some(0);
1976
+ }
1977
+
1978
+ /// Determines whether we should disconnect the counterparty due to not receiving a response
1979
+ /// within our expected timeframe.
1980
+ ///
1981
+ /// This should be called on every [`super::channelmanager::ChannelManager::timer_tick_occurred`].
1982
+ pub fn should_disconnect_peer_awaiting_response(&mut self) -> bool {
1983
+ let ticks_elapsed = if let Some(ticks_elapsed) = self.sent_message_awaiting_response.as_mut() {
1984
+ ticks_elapsed
1985
+ } else {
1986
+ // Don't disconnect when we're not waiting on a response.
1987
+ return false;
1988
+ };
1989
+ *ticks_elapsed += 1;
1990
+ *ticks_elapsed >= DISCONNECT_PEER_AWAITING_RESPONSE_TICKS
1991
+ }
1964
1992
}
1965
1993
1966
1994
// Internal utility functions for channels
@@ -3659,7 +3687,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3659
3687
Some(self.get_last_revoke_and_ack())
3660
3688
} else { None };
3661
3689
let commitment_update = if self.context.monitor_pending_commitment_signed {
3662
- self.mark_awaiting_response();
3690
+ self.context. mark_awaiting_response();
3663
3691
Some(self.get_last_commitment_update(logger))
3664
3692
} else { None };
3665
3693
@@ -3914,7 +3942,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3914
3942
// the corresponding revoke_and_ack back yet.
3915
3943
let is_awaiting_remote_revoke = self.context.channel_state & ChannelState::AwaitingRemoteRevoke as u32 != 0;
3916
3944
if is_awaiting_remote_revoke && !self.is_awaiting_monitor_update() {
3917
- self.mark_awaiting_response();
3945
+ self.context. mark_awaiting_response();
3918
3946
}
3919
3947
let next_counterparty_commitment_number = INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number + if is_awaiting_remote_revoke { 1 } else { 0 };
3920
3948
@@ -4080,28 +4108,6 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4080
4108
}), None))
4081
4109
}
4082
4110
4083
- // Marks a channel as waiting for a response from the counterparty. If it's not received
4084
- // [`DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`] after sending our own to them, then we'll attempt
4085
- // a reconnection.
4086
- fn mark_awaiting_response(&mut self) {
4087
- self.context.sent_message_awaiting_response = Some(0);
4088
- }
4089
-
4090
- /// Determines whether we should disconnect the counterparty due to not receiving a response
4091
- /// within our expected timeframe.
4092
- ///
4093
- /// This should be called on every [`super::channelmanager::ChannelManager::timer_tick_occurred`].
4094
- pub fn should_disconnect_peer_awaiting_response(&mut self) -> bool {
4095
- let ticks_elapsed = if let Some(ticks_elapsed) = self.context.sent_message_awaiting_response.as_mut() {
4096
- ticks_elapsed
4097
- } else {
4098
- // Don't disconnect when we're not waiting on a response.
4099
- return false;
4100
- };
4101
- *ticks_elapsed += 1;
4102
- *ticks_elapsed >= DISCONNECT_PEER_AWAITING_RESPONSE_TICKS
4103
- }
4104
-
4105
4111
pub fn shutdown<SP: Deref>(
4106
4112
&mut self, signer_provider: &SP, their_features: &InitFeatures, msg: &msgs::Shutdown
4107
4113
) -> Result<(Option<msgs::Shutdown>, Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), ChannelError>
@@ -5013,7 +5019,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5013
5019
log_info!(logger, "Sending a data_loss_protect with no previous remote per_commitment_secret for channel {}", log_bytes!(self.context.channel_id()));
5014
5020
[0;32]
5015
5021
};
5016
- self.mark_awaiting_response();
5022
+ self.context. mark_awaiting_response();
5017
5023
msgs::ChannelReestablish {
5018
5024
channel_id: self.context.channel_id(),
5019
5025
// The protocol has two different commitment number concepts - the "commitment
@@ -5776,7 +5782,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5776
5782
Ok(self.get_open_channel(chain_hash))
5777
5783
}
5778
5784
5779
- pub fn get_open_channel(&self, chain_hash: BlockHash) -> msgs::OpenChannel {
5785
+ pub fn get_open_channel(&mut self, chain_hash: BlockHash) -> msgs::OpenChannel {
5780
5786
if !self.context.is_outbound() {
5781
5787
panic!("Tried to open a channel for an inbound channel?");
5782
5788
}
@@ -5788,6 +5794,10 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5788
5794
panic!("Tried to send an open_channel for a channel that has already advanced");
5789
5795
}
5790
5796
5797
+ // If we're not making progress on a new pending channel we'll want to disconnect. So we mark the
5798
+ // channel as awaiting response.
5799
+ self.context.mark_awaiting_response();
5800
+
5791
5801
let first_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
5792
5802
let keys = self.context.get_holder_pubkeys();
5793
5803
@@ -5945,6 +5955,8 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5945
5955
5946
5956
self.context.channel_state = ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32;
5947
5957
self.context.inbound_handshake_limits_override = None; // We're done enforcing limits on our peer's handshake now.
5958
+ // We're no longer awaiting our peer to respond to our open_channel message.
5959
+ self.context.sent_message_awaiting_response = None;
5948
5960
5949
5961
Ok(())
5950
5962
}
@@ -6316,6 +6328,9 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6316
6328
6317
6329
self.context.user_id = user_id;
6318
6330
self.context.inbound_awaiting_accept = false;
6331
+ // If we're not making progress on a new pending channel we'll want to disconnect. So we mark the
6332
+ // channel as awaiting response.
6333
+ self.context.mark_awaiting_response();
6319
6334
6320
6335
self.generate_accept_channel_message()
6321
6336
}
@@ -6473,6 +6488,10 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6473
6488
self.context.channel_id = funding_txo.to_channel_id();
6474
6489
self.context.cur_counterparty_commitment_transaction_number -= 1;
6475
6490
self.context.cur_holder_commitment_transaction_number -= 1;
6491
+ // We're no longer awaiting our peer to respond to our accept_channel message. Since we're now
6492
+ // just waiting for the channel to be ready, we're done handling pending channel establishment
6493
+ // timeouts.
6494
+ self.context.sent_message_awaiting_response = None;
6476
6495
6477
6496
log_info!(logger, "Generated funding_signed for peer for channel {}", log_bytes!(self.context.channel_id()));
6478
6497
@@ -7520,7 +7539,7 @@ mod tests {
7520
7539
7521
7540
let node_a_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
7522
7541
let config = UserConfig::default();
7523
- let node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(&bounded_fee_estimator, &&keys_provider, &&keys_provider, node_a_node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
7542
+ let mut node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(&bounded_fee_estimator, &&keys_provider, &&keys_provider, node_a_node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
7524
7543
7525
7544
// Now change the fee so we can check that the fee in the open_channel message is the
7526
7545
// same as the old fee.
@@ -7740,7 +7759,7 @@ mod tests {
7740
7759
// Test that `OutboundV1Channel::new` creates a channel with the correct value for
7741
7760
// `holder_max_htlc_value_in_flight_msat`, when configured with a valid percentage value,
7742
7761
// which is set to the lower bound + 1 (2%) of the `channel_value`.
7743
- let chan_1 = OutboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, outbound_node_id, &channelmanager::provided_init_features(&config_2_percent), 10000000, 100000, 42, &config_2_percent, 0, 42).unwrap();
7762
+ let mut chan_1 = OutboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, outbound_node_id, &channelmanager::provided_init_features(&config_2_percent), 10000000, 100000, 42, &config_2_percent, 0, 42).unwrap();
7744
7763
let chan_1_value_msat = chan_1.context.channel_value_satoshis * 1000;
7745
7764
assert_eq!(chan_1.context.holder_max_htlc_value_in_flight_msat, (chan_1_value_msat as f64 * 0.02) as u64);
7746
7765
@@ -7825,7 +7844,7 @@ mod tests {
7825
7844
7826
7845
let mut outbound_node_config = UserConfig::default();
7827
7846
outbound_node_config.channel_handshake_config.their_channel_reserve_proportional_millionths = (outbound_selected_channel_reserve_perc * 1_000_000.0) as u32;
7828
- let chan = OutboundV1Channel::<EnforcingSigner>::new(&&fee_est, &&keys_provider, &&keys_provider, outbound_node_id, &channelmanager::provided_init_features(&outbound_node_config), channel_value_satoshis, 100_000, 42, &outbound_node_config, 0, 42).unwrap();
7847
+ let mut chan = OutboundV1Channel::<EnforcingSigner>::new(&&fee_est, &&keys_provider, &&keys_provider, outbound_node_id, &channelmanager::provided_init_features(&outbound_node_config), channel_value_satoshis, 100_000, 42, &outbound_node_config, 0, 42).unwrap();
7829
7848
7830
7849
let expected_outbound_selected_chan_reserve = cmp::max(MIN_THEIR_CHAN_RESERVE_SATOSHIS, (chan.context.channel_value_satoshis as f64 * outbound_selected_channel_reserve_perc) as u64);
7831
7850
assert_eq!(chan.context.holder_selected_channel_reserve_satoshis, expected_outbound_selected_chan_reserve);
@@ -8695,7 +8714,7 @@ mod tests {
8695
8714
8696
8715
let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
8697
8716
let config = UserConfig::default();
8698
- let node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider,
8717
+ let mut node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider,
8699
8718
node_b_node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
8700
8719
8701
8720
let mut channel_type_features = ChannelTypeFeatures::only_static_remote_key();
@@ -8739,7 +8758,7 @@ mod tests {
8739
8758
expected_channel_type.set_static_remote_key_required();
8740
8759
expected_channel_type.set_anchors_zero_fee_htlc_tx_required();
8741
8760
8742
- let channel_a = OutboundV1Channel::<EnforcingSigner>::new(
8761
+ let mut channel_a = OutboundV1Channel::<EnforcingSigner>::new(
8743
8762
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
8744
8763
&channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42
8745
8764
).unwrap();
@@ -8776,7 +8795,7 @@ mod tests {
8776
8795
let raw_init_features = static_remote_key_required | simple_anchors_required;
8777
8796
let init_features_with_simple_anchors = InitFeatures::from_le_bytes(raw_init_features.to_le_bytes().to_vec());
8778
8797
8779
- let channel_a = OutboundV1Channel::<EnforcingSigner>::new(
8798
+ let mut channel_a = OutboundV1Channel::<EnforcingSigner>::new(
8780
8799
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
8781
8800
&channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42
8782
8801
).unwrap();
@@ -8822,7 +8841,7 @@ mod tests {
8822
8841
// First, we'll try to open a channel between A and B where A requests a channel type for
8823
8842
// the original `option_anchors` feature (non zero fee htlc tx). This should be rejected by
8824
8843
// B as it's not supported by LDK.
8825
- let channel_a = OutboundV1Channel::<EnforcingSigner>::new(
8844
+ let mut channel_a = OutboundV1Channel::<EnforcingSigner>::new(
8826
8845
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
8827
8846
&channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42
8828
8847
).unwrap();
0 commit comments