Skip to content

Commit f3bb32e

Browse files
committed
test(p2p): test reconnection to unreacheable peers
1 parent 34084a4 commit f3bb32e

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

node/testing/src/scenarios/p2p/basic_outgoing_connections.rs

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::{collections::BTreeSet, time::Duration};
1+
use std::{
2+
collections::{BTreeMap, BTreeSet},
3+
time::Duration,
4+
};
25

36
use node::p2p::{identity::SecretKey, P2pPeerState, P2pPeerStatus, PeerId};
47

@@ -354,6 +357,70 @@ impl ConnectToInitialPeers {
354357
}
355358
}
356359

360+
/// Node should repeat connecting to unavailable initial peer.
361+
#[derive(documented::Documented, Default, Clone, Copy)]
362+
pub struct ConnectToUnavailableInitialPeers;
363+
364+
impl ConnectToUnavailableInitialPeers {
365+
pub async fn run<'cluster>(self, runner: ClusterRunner<'cluster>) {
366+
const MAX: u16 = 2;
367+
const RETRIES: u8 = 3;
368+
369+
let mut driver = Driver::new(runner);
370+
371+
let (initial_peers, peer_ids): (Vec<_>, Vec<_>) = (0..MAX)
372+
.into_iter()
373+
.map(|i| {
374+
let port: u16 = 11200 + i;
375+
let peer_id = SecretKey::rand().public_key().peer_id();
376+
let addr = format!(
377+
"/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}",
378+
peer_id = peer_id.clone().to_libp2p_string()
379+
)
380+
.parse()
381+
.unwrap();
382+
(addr, peer_id)
383+
})
384+
.unzip();
385+
386+
let (node_ut, _) = driver
387+
.add_rust_node(RustNodeTestingConfig::berkeley_default().initial_peers(initial_peers));
388+
389+
let mut peer_retries =
390+
BTreeMap::from_iter(peer_ids.into_iter().map(|peer_id| (peer_id, 0_u8)));
391+
392+
// matches event "the node established connection with peer"
393+
let pred = |node_id, event: &_, _state: &_| {
394+
if node_id != node_ut {
395+
false
396+
} else if let Some((peer_id, res)) = as_connection_finalized_event(event) {
397+
assert!(res.is_err(), "connection to {peer_id} should succeed");
398+
let retries = peer_retries.get_mut(&peer_id).unwrap();
399+
*retries += 1;
400+
if *retries >= RETRIES {
401+
peer_retries.remove(&peer_id);
402+
}
403+
peer_retries.is_empty()
404+
} else {
405+
false
406+
}
407+
};
408+
409+
let satisfied = driver
410+
.run_until(Duration::from_secs(1 * 60), pred)
411+
.await
412+
.unwrap();
413+
414+
println!("{:#?}", driver.inner().node(node_ut).unwrap().state().p2p);
415+
416+
assert!(
417+
satisfied,
418+
"did not reach retry limit for peers: {:?}",
419+
peer_retries
420+
);
421+
}
422+
}
423+
357424
/// Node should be able to connect to all initial peers after they become ready.
358425
#[derive(documented::Documented, Default, Clone, Copy)]
359426
pub struct ConnectToInitialPeersBecomeReady;
@@ -384,7 +451,10 @@ impl ConnectToInitialPeersBecomeReady {
384451
let (node_ut, _) = driver
385452
.add_rust_node(RustNodeTestingConfig::berkeley_default().initial_peers(initial_peers));
386453

387-
driver.wait_for(Duration::from_secs(10), |_, _, _| false).await.unwrap();
454+
driver
455+
.wait_for(Duration::from_secs(10), |_, _, _| false)
456+
.await
457+
.unwrap();
388458

389459
let (_peers, mut peer_ids): (Vec<ClusterNodeId>, BTreeSet<PeerId>) = port_bytes
390460
.into_iter()

node/testing/tests/p2p_basic_outgoing.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use openmina_node_testing::scenarios::p2p::basic_outgoing_connections::{
22
ConnectToInitialPeers, ConnectToInitialPeersBecomeReady, DontConnectToInitialPeerWithSameId,
33
DontConnectToNodeWithSameId, DontConnectToSelfInitialPeer, MakeMultipleOutgoingConnections,
4-
MakeOutgoingConnection,
4+
MakeOutgoingConnection, ConnectToUnavailableInitialPeers,
55
};
66

77
mod common;
@@ -38,6 +38,11 @@ scenario_test!(
3838
ConnectToInitialPeers,
3939
ConnectToInitialPeers
4040
);
41+
scenario_test!(
42+
connect_to_offline_initial_peers,
43+
ConnectToUnavailableInitialPeers,
44+
ConnectToUnavailableInitialPeers
45+
);
4146
scenario_test!(
4247
connect_to_all_initial_peers_become_ready,
4348
ConnectToInitialPeersBecomeReady,

0 commit comments

Comments
 (0)