diff --git a/crates/net/network-types/src/peers/config.rs b/crates/net/network-types/src/peers/config.rs index 890679f5d34b..221705c3846b 100644 --- a/crates/net/network-types/src/peers/config.rs +++ b/crates/net/network-types/src/peers/config.rs @@ -197,6 +197,12 @@ impl PeersConfig { self } + /// Configure how long to refill outbound slots + pub const fn with_refill_slots_interval(mut self, interval: Duration) -> Self { + self.refill_slots_interval = interval; + self + } + /// Maximum allowed outbound connections. pub const fn with_max_outbound(mut self, max_outbound: usize) -> Self { self.connection_info.max_outbound = max_outbound; diff --git a/crates/net/network/tests/it/connect.rs b/crates/net/network/tests/it/connect.rs index feac0cc516de..d5a0ed5915af 100644 --- a/crates/net/network/tests/it/connect.rs +++ b/crates/net/network/tests/it/connect.rs @@ -636,6 +636,48 @@ async fn test_trusted_peer_only() { assert_eq!(handle.num_connected_peers(), 2); } +#[tokio::test(flavor = "multi_thread")] +async fn test_network_state_change() { + let net = Testnet::create(1).await; + let secret_key = SecretKey::new(&mut rand::thread_rng()); + let peers_config = + PeersConfig::default().with_refill_slots_interval(Duration::from_millis(500)); + + let config = NetworkConfigBuilder::eth(secret_key) + .listener_port(0) + .disable_discovery() + .peer_config(peers_config) + .build(NoopProvider::default()); + + let network = NetworkManager::new(config).await.unwrap(); + + let handle = network.handle().clone(); + tokio::task::spawn(network); + + let mut handles = net.handles(); + let handle0 = handles.next().unwrap(); + + drop(handles); + let _handle = net.spawn(); + + // Set network state to Hibernate. + handle.set_network_hibernate(); + + handle.add_peer(*handle0.peer_id(), handle0.local_addr()); + + // wait 2 seconds, the number of connections is still 0, because network is Hibernate. + tokio::time::sleep(Duration::from_secs(2)).await; + assert_eq!(handle.num_connected_peers(), 0); + + // Set network state to Active. + handle.set_network_active(); + + // wait 2 seconds, the number of connections should be 1, because network is Active and outbound + // slot should be filled. + tokio::time::sleep(Duration::from_secs(2)).await; + assert_eq!(handle.num_connected_peers(), 1); +} + #[tokio::test(flavor = "multi_thread")] async fn test_exceed_outgoing_connections() { let net = Testnet::create(2).await;