Skip to content

Commit

Permalink
Add test node herself in reserved nodes. (#2390)
Browse files Browse the repository at this point in the history
## Linked Issues/PRs
Closes #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 <XgreenX9999@gmail.com>
  • Loading branch information
AurelienFT and xgreenx authored Nov 14, 2024
1 parent 524b7f0 commit d013a99
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions crates/services/p2p/src/p2p_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit d013a99

Please sign in to comment.