From d013a99eb085a4f96ad9abaedd3b4b3d52466ad3 Mon Sep 17 00:00:00 2001 From: AurelienFT <32803821+AurelienFT@users.noreply.github.com> Date: Thu, 14 Nov 2024 18:47:56 +0100 Subject: [PATCH] Add test node herself in reserved nodes. (#2390) ## Linked Issues/PRs Closes https://github.com/FuelLabs/fuel-core/issues/1188 ## Description Add a test to verfiy that we don't connect to ourself if we are in the reserved nodes. ## Checklist - [x] Breaking changes are clearly marked as such in the PR description and changelog - [x] New behavior is reflected in tests - [x] [The specification](https://github.com/FuelLabs/fuel-specs/) matches the implemented behavior (link update PR if changes are needed) ### Before requesting review - [x] I have reviewed the code myself - [x] I have created follow-up issues caused by this PR and linked them here --------- Co-authored-by: Green Baneling --- crates/services/p2p/src/p2p_service.rs | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/crates/services/p2p/src/p2p_service.rs b/crates/services/p2p/src/p2p_service.rs index 94646d50815..5de17783af6 100644 --- a/crates/services/p2p/src/p2p_service.rs +++ b/crates/services/p2p/src/p2p_service.rs @@ -1051,6 +1051,47 @@ mod tests { stop_sender.send(()).unwrap(); } + #[tokio::test] + #[instrument] + async fn dont_connect_to_node_with_same_peer_id() { + let mut p2p_config = + Config::default_initialized("dont_connect_to_node_with_same_peer_id"); + let mut node_a = build_service_from_config(p2p_config.clone()).await; + // We don't use build_service_from_config here, because we want to use the same keypair + // to have the same PeerId + let node_b = { + // Given + p2p_config.reserved_nodes = node_a.multiaddrs(); + let max_block_size = p2p_config.max_block_size; + let (sender, _) = + broadcast::channel(p2p_config.reserved_nodes.len().saturating_add(1)); + + let mut service = FuelP2PService::new( + sender, + p2p_config, + PostcardCodec::new(max_block_size), + ) + .await + .unwrap(); + service.start().await.unwrap(); + service + }; + // When + tokio::time::timeout(Duration::from_secs(5), async move { + loop { + let event = node_a.next_event().await; + if let Some(FuelP2PEvent::PeerConnected(_)) = event { + panic!("Node B should not connect to Node A because they have the same PeerId"); + } + assert_eq!(node_a.peer_manager().total_peers_connected(), 0); + } + }) + .await + // Then + .expect_err("The node should not connect to itself"); + assert_eq!(node_b.peer_manager().total_peers_connected(), 0); + } + // We start with two nodes, node_a and node_b, bootstrapped with `bootstrap_nodes_count` other nodes. // Yet node_a and node_b are only allowed to connect to specified amount of nodes. #[tokio::test]