@@ -583,9 +583,15 @@ pub(crate) const MIN_AFFORDABLE_HTLC_COUNT: usize = 4;
583
583
/// * `EXPIRE_PREV_CONFIG_TICKS` = convergence_delay / tick_interval
584
584
pub(crate) const EXPIRE_PREV_CONFIG_TICKS: usize = 5;
585
585
586
- /// The number of ticks that may elapse while we're waiting for a response to a
587
- /// [`msgs::RevokeAndACK`] or [`msgs::ChannelReestablish`] message before we attempt to disconnect
588
- /// them.
586
+ /// The number of ticks that may elapse while we're waiting for a response to any of the following
587
+ /// messages
588
+ ///
589
+ /// [`msgs::RevokeAndACK`]
590
+ /// [`msgs::ChannelReestablish`]
591
+ /// [`msgs::OpenChannel`]
592
+ /// [`msgs::AcceptChannel`]
593
+ ///
594
+ /// before we attempt to disconnect them.
589
595
///
590
596
/// See [`ChannelContext::sent_message_awaiting_response`] for more information.
591
597
pub(crate) const DISCONNECT_PEER_AWAITING_RESPONSE_TICKS: usize = 2;
@@ -1965,6 +1971,28 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
1965
1971
self.update_time_counter += 1;
1966
1972
(monitor_update, dropped_outbound_htlcs)
1967
1973
}
1974
+
1975
+ // Marks a channel as waiting for a response from the counterparty. If it's not received
1976
+ // [`DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`] after sending our own to them, then we'll attempt
1977
+ // a reconnection.
1978
+ fn mark_awaiting_response(&mut self) {
1979
+ self.sent_message_awaiting_response = Some(0);
1980
+ }
1981
+
1982
+ /// Determines whether we should disconnect the counterparty due to not receiving a response
1983
+ /// within our expected timeframe.
1984
+ ///
1985
+ /// This should be called on every [`super::channelmanager::ChannelManager::timer_tick_occurred`].
1986
+ pub fn should_disconnect_peer_awaiting_response(&mut self) -> bool {
1987
+ let ticks_elapsed = if let Some(ticks_elapsed) = self.sent_message_awaiting_response.as_mut() {
1988
+ ticks_elapsed
1989
+ } else {
1990
+ // Don't disconnect when we're not waiting on a response.
1991
+ return false;
1992
+ };
1993
+ *ticks_elapsed += 1;
1994
+ *ticks_elapsed >= DISCONNECT_PEER_AWAITING_RESPONSE_TICKS
1995
+ }
1968
1996
}
1969
1997
1970
1998
// Internal utility functions for channels
@@ -3663,7 +3691,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3663
3691
Some(self.get_last_revoke_and_ack())
3664
3692
} else { None };
3665
3693
let commitment_update = if self.context.monitor_pending_commitment_signed {
3666
- self.mark_awaiting_response();
3694
+ self.context. mark_awaiting_response();
3667
3695
Some(self.get_last_commitment_update(logger))
3668
3696
} else { None };
3669
3697
@@ -3918,7 +3946,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3918
3946
// the corresponding revoke_and_ack back yet.
3919
3947
let is_awaiting_remote_revoke = self.context.channel_state & ChannelState::AwaitingRemoteRevoke as u32 != 0;
3920
3948
if is_awaiting_remote_revoke && !self.is_awaiting_monitor_update() {
3921
- self.mark_awaiting_response();
3949
+ self.context. mark_awaiting_response();
3922
3950
}
3923
3951
let next_counterparty_commitment_number = INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number + if is_awaiting_remote_revoke { 1 } else { 0 };
3924
3952
@@ -4084,28 +4112,6 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4084
4112
}), None))
4085
4113
}
4086
4114
4087
- // Marks a channel as waiting for a response from the counterparty. If it's not received
4088
- // [`DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`] after sending our own to them, then we'll attempt
4089
- // a reconnection.
4090
- fn mark_awaiting_response(&mut self) {
4091
- self.context.sent_message_awaiting_response = Some(0);
4092
- }
4093
-
4094
- /// Determines whether we should disconnect the counterparty due to not receiving a response
4095
- /// within our expected timeframe.
4096
- ///
4097
- /// This should be called on every [`super::channelmanager::ChannelManager::timer_tick_occurred`].
4098
- pub fn should_disconnect_peer_awaiting_response(&mut self) -> bool {
4099
- let ticks_elapsed = if let Some(ticks_elapsed) = self.context.sent_message_awaiting_response.as_mut() {
4100
- ticks_elapsed
4101
- } else {
4102
- // Don't disconnect when we're not waiting on a response.
4103
- return false;
4104
- };
4105
- *ticks_elapsed += 1;
4106
- *ticks_elapsed >= DISCONNECT_PEER_AWAITING_RESPONSE_TICKS
4107
- }
4108
-
4109
4115
pub fn shutdown<SP: Deref>(
4110
4116
&mut self, signer_provider: &SP, their_features: &InitFeatures, msg: &msgs::Shutdown
4111
4117
) -> Result<(Option<msgs::Shutdown>, Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), ChannelError>
@@ -5017,7 +5023,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5017
5023
log_info!(logger, "Sending a data_loss_protect with no previous remote per_commitment_secret for channel {}", log_bytes!(self.context.channel_id()));
5018
5024
[0;32]
5019
5025
};
5020
- self.mark_awaiting_response();
5026
+ self.context. mark_awaiting_response();
5021
5027
msgs::ChannelReestablish {
5022
5028
channel_id: self.context.channel_id(),
5023
5029
// The protocol has two different commitment number concepts - the "commitment
@@ -5780,7 +5786,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5780
5786
Ok(self.get_open_channel(chain_hash))
5781
5787
}
5782
5788
5783
- pub fn get_open_channel(&self, chain_hash: BlockHash) -> msgs::OpenChannel {
5789
+ pub fn get_open_channel(&mut self, chain_hash: BlockHash) -> msgs::OpenChannel {
5784
5790
if !self.context.is_outbound() {
5785
5791
panic!("Tried to open a channel for an inbound channel?");
5786
5792
}
@@ -5792,6 +5798,10 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5792
5798
panic!("Tried to send an open_channel for a channel that has already advanced");
5793
5799
}
5794
5800
5801
+ // If we're not making progress on a new pending channel we'll want to disconnect. So we mark the
5802
+ // channel as awaiting response.
5803
+ self.context.mark_awaiting_response();
5804
+
5795
5805
let first_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
5796
5806
let keys = self.context.get_holder_pubkeys();
5797
5807
@@ -5949,6 +5959,8 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5949
5959
5950
5960
self.context.channel_state = ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32;
5951
5961
self.context.inbound_handshake_limits_override = None; // We're done enforcing limits on our peer's handshake now.
5962
+ // We're no longer awaiting our peer to respond to our open_channel message.
5963
+ self.context.sent_message_awaiting_response = None;
5952
5964
5953
5965
Ok(())
5954
5966
}
@@ -6320,6 +6332,9 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6320
6332
6321
6333
self.context.user_id = user_id;
6322
6334
self.context.inbound_awaiting_accept = false;
6335
+ // If we're not making progress on a new pending channel we'll want to disconnect. So we mark the
6336
+ // channel as awaiting response.
6337
+ self.context.mark_awaiting_response();
6323
6338
6324
6339
self.generate_accept_channel_message()
6325
6340
}
@@ -6477,6 +6492,10 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6477
6492
self.context.channel_id = funding_txo.to_channel_id();
6478
6493
self.context.cur_counterparty_commitment_transaction_number -= 1;
6479
6494
self.context.cur_holder_commitment_transaction_number -= 1;
6495
+ // We're no longer awaiting our peer to respond to our accept_channel message. Since we're now
6496
+ // just waiting for the channel to be ready, we're done handling pending channel establishment
6497
+ // timeouts.
6498
+ self.context.sent_message_awaiting_response = None;
6480
6499
6481
6500
log_info!(logger, "Generated funding_signed for peer for channel {}", log_bytes!(self.context.channel_id()));
6482
6501
@@ -7524,7 +7543,7 @@ mod tests {
7524
7543
7525
7544
let node_a_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
7526
7545
let config = UserConfig::default();
7527
- 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();
7546
+ 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();
7528
7547
7529
7548
// Now change the fee so we can check that the fee in the open_channel message is the
7530
7549
// same as the old fee.
@@ -7744,7 +7763,7 @@ mod tests {
7744
7763
// Test that `OutboundV1Channel::new` creates a channel with the correct value for
7745
7764
// `holder_max_htlc_value_in_flight_msat`, when configured with a valid percentage value,
7746
7765
// which is set to the lower bound + 1 (2%) of the `channel_value`.
7747
- 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();
7766
+ 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();
7748
7767
let chan_1_value_msat = chan_1.context.channel_value_satoshis * 1000;
7749
7768
assert_eq!(chan_1.context.holder_max_htlc_value_in_flight_msat, (chan_1_value_msat as f64 * 0.02) as u64);
7750
7769
@@ -7829,7 +7848,7 @@ mod tests {
7829
7848
7830
7849
let mut outbound_node_config = UserConfig::default();
7831
7850
outbound_node_config.channel_handshake_config.their_channel_reserve_proportional_millionths = (outbound_selected_channel_reserve_perc * 1_000_000.0) as u32;
7832
- 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();
7851
+ 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();
7833
7852
7834
7853
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);
7835
7854
assert_eq!(chan.context.holder_selected_channel_reserve_satoshis, expected_outbound_selected_chan_reserve);
@@ -8699,7 +8718,7 @@ mod tests {
8699
8718
8700
8719
let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
8701
8720
let config = UserConfig::default();
8702
- let node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider,
8721
+ let mut node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider,
8703
8722
node_b_node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
8704
8723
8705
8724
let mut channel_type_features = ChannelTypeFeatures::only_static_remote_key();
@@ -8743,7 +8762,7 @@ mod tests {
8743
8762
expected_channel_type.set_static_remote_key_required();
8744
8763
expected_channel_type.set_anchors_zero_fee_htlc_tx_required();
8745
8764
8746
- let channel_a = OutboundV1Channel::<EnforcingSigner>::new(
8765
+ let mut channel_a = OutboundV1Channel::<EnforcingSigner>::new(
8747
8766
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
8748
8767
&channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42
8749
8768
).unwrap();
@@ -8780,7 +8799,7 @@ mod tests {
8780
8799
let raw_init_features = static_remote_key_required | simple_anchors_required;
8781
8800
let init_features_with_simple_anchors = InitFeatures::from_le_bytes(raw_init_features.to_le_bytes().to_vec());
8782
8801
8783
- let channel_a = OutboundV1Channel::<EnforcingSigner>::new(
8802
+ let mut channel_a = OutboundV1Channel::<EnforcingSigner>::new(
8784
8803
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
8785
8804
&channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42
8786
8805
).unwrap();
@@ -8826,7 +8845,7 @@ mod tests {
8826
8845
// First, we'll try to open a channel between A and B where A requests a channel type for
8827
8846
// the original `option_anchors` feature (non zero fee htlc tx). This should be rejected by
8828
8847
// B as it's not supported by LDK.
8829
- let channel_a = OutboundV1Channel::<EnforcingSigner>::new(
8848
+ let mut channel_a = OutboundV1Channel::<EnforcingSigner>::new(
8830
8849
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
8831
8850
&channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42
8832
8851
).unwrap();
0 commit comments