Skip to content

Commit e15b191

Browse files
committed
feat(testing): remove option to hardcode node p2p port and specify initial_peers as node ids instead of multiaddr, unless peer is external, in order to handle dynamic ports
re: #188 Hardcoded port might not be available during runtime and it might limit the ability to run multiple scenarios at the same time due to port collisions.
1 parent 2c9a336 commit e15b191

File tree

12 files changed

+82
-198
lines changed

12 files changed

+82
-198
lines changed

node/testing/src/cluster/mod.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,32 @@ impl Cluster {
153153
)
154154
})
155155
.unwrap();
156-
let libp2p_port = testing_config.libp2p_port.unwrap_or_else(|| {
157-
self.available_ports
158-
.next()
159-
.ok_or_else(|| {
160-
anyhow::anyhow!(
161-
"couldn't find available port in port range: {:?}",
162-
self.config.port_range()
163-
)
164-
})
165-
.unwrap()
166-
});
156+
let libp2p_port = self
157+
.available_ports
158+
.next()
159+
.ok_or_else(|| {
160+
anyhow::anyhow!(
161+
"couldn't find available port in port range: {:?}",
162+
self.config.port_range()
163+
)
164+
})
165+
.unwrap();
167166

168167
let (block_producer_sec_key, block_producer_config) = testing_config
169168
.block_producer
170169
.map(|v| (v.sec_key, v.config))
171170
.unzip();
172171

172+
let initial_peers = testing_config
173+
.initial_peers
174+
.into_iter()
175+
.map(|node| match node {
176+
ListenerNode::Rust(id) => self.node(id).unwrap().dial_addr(),
177+
ListenerNode::Ocaml(id) => self.ocaml_node(id).unwrap().dial_addr(),
178+
ListenerNode::Custom(addr) => addr,
179+
})
180+
.collect();
181+
173182
let config = Config {
174183
ledger: LedgerConfig {},
175184
snark: SnarkConfig {
@@ -187,7 +196,7 @@ impl Cluster {
187196
libp2p_port: Some(libp2p_port),
188197
listen_port: http_port,
189198
identity_pub_key: pub_key,
190-
initial_peers: testing_config.initial_peers,
199+
initial_peers,
191200
max_peers: testing_config.max_peers,
192201
ask_initial_peers_interval: testing_config.ask_initial_peers_interval,
193202
enabled_channels: ChannelId::iter_all().collect(),

node/testing/src/node/rust/config.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use std::time::Duration;
22

3-
use node::{
4-
account::AccountSecretKey, p2p::connection::outgoing::P2pConnectionOutgoingInitOpts,
5-
BlockProducerConfig,
6-
};
3+
use node::{account::AccountSecretKey, BlockProducerConfig};
74
use serde::{Deserialize, Serialize};
85

6+
use crate::scenario::ListenerNode;
7+
98
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
109
pub enum TestPeerId {
1110
/// NOTE This option results a deterministic private key derived from the
@@ -24,8 +23,7 @@ pub struct RustNodeTestingConfig {
2423
pub initial_time: redux::Timestamp,
2524
pub max_peers: usize,
2625
pub ask_initial_peers_interval: Duration,
27-
pub initial_peers: Vec<P2pConnectionOutgoingInitOpts>,
28-
pub libp2p_port: Option<u16>,
26+
pub initial_peers: Vec<ListenerNode>,
2927
pub peer_id: TestPeerId,
3028
pub block_producer: Option<RustNodeBlockProducerTestingConfig>,
3129
}
@@ -44,7 +42,6 @@ impl RustNodeTestingConfig {
4442
max_peers: 100,
4543
ask_initial_peers_interval: Duration::from_secs(10),
4644
initial_peers: Vec::new(),
47-
libp2p_port: None,
4845
peer_id: TestPeerId::default(),
4946
block_producer: None,
5047
}
@@ -65,16 +62,11 @@ impl RustNodeTestingConfig {
6562
self
6663
}
6764

68-
pub fn initial_peers(mut self, v: Vec<P2pConnectionOutgoingInitOpts>) -> Self {
65+
pub fn initial_peers(mut self, v: Vec<ListenerNode>) -> Self {
6966
self.initial_peers = v;
7067
self
7168
}
7269

73-
pub fn libp2p_port(mut self, v: u16) -> Self {
74-
self.libp2p_port = Some(v);
75-
self
76-
}
77-
7870
pub fn with_random_peer_id(mut self) -> Self {
7971
self.peer_id = TestPeerId::Random;
8072
self

node/testing/src/scenario/step.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub enum ScenarioStep {
4545
},
4646
}
4747

48-
#[derive(Serialize, Deserialize, Debug, Clone)]
48+
#[derive(Serialize, Deserialize, derive_more::From, Debug, Clone)]
4949
pub enum ListenerNode {
5050
Rust(ClusterNodeId),
5151
Ocaml(ClusterOcamlNodeId),

node/testing/src/scenarios/multi_node/basic_connectivity_initial_joining.rs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,9 @@ use std::{
33
time::Duration,
44
};
55

6-
use libp2p::Multiaddr;
76
use node::{
87
event_source::Event,
9-
p2p::{
10-
connection::outgoing::{
11-
P2pConnectionOutgoingInitLibp2pOpts, P2pConnectionOutgoingInitOpts,
12-
},
13-
webrtc::SignalingMethod,
14-
P2pConnectionEvent, P2pEvent,
15-
},
8+
p2p::{P2pConnectionEvent, P2pEvent},
169
};
1710

1811
use crate::{
@@ -39,26 +32,8 @@ impl MultiNodeBasicConnectivityInitialJoining {
3932

4033
let seed_node =
4134
runner.add_rust_node(RustNodeTestingConfig::berkeley_default().max_peers(TOTAL_PEERS));
42-
let full_config = &runner
43-
.node(seed_node)
44-
.expect("must exist")
45-
.state()
46-
.p2p
47-
.config;
4835

49-
let peer_id = full_config.identity_pub_key.peer_id();
50-
let this = format!(
51-
"/ip4/127.0.0.1/tcp/{}/p2p/{}",
52-
full_config.libp2p_port.unwrap(),
53-
libp2p::PeerId::from(peer_id)
54-
);
55-
let this_maddr = this.parse::<Multiaddr>().unwrap();
56-
eprintln!("launch Openmina seed node, id: {seed_node}, addr: {this}");
57-
let init_opts = P2pConnectionOutgoingInitOpts::LibP2P(
58-
P2pConnectionOutgoingInitLibp2pOpts::try_from(&this_maddr).unwrap(),
59-
);
60-
let signaling = SignalingMethod::Http(([127, 0, 0, 1], full_config.listen_port).into());
61-
let init_opts_webrtc = P2pConnectionOutgoingInitOpts::WebRTC { peer_id, signaling };
36+
eprintln!("launch Openmina seed node, id: {seed_node}");
6237

6338
let mut nodes = vec![seed_node];
6439

@@ -69,7 +44,7 @@ impl MultiNodeBasicConnectivityInitialJoining {
6944
let node = runner.add_rust_node(
7045
RustNodeTestingConfig::berkeley_default()
7146
.max_peers(MAX_PEERS_PER_NODE)
72-
.initial_peers(vec![init_opts.clone(), init_opts_webrtc.clone()])
47+
.initial_peers(vec![seed_node.into()])
7348
.ask_initial_peers_interval(Duration::from_secs(10)),
7449
);
7550
eprintln!("launch Openmina node, id: {node}, connects to {seed_node}");

node/testing/src/scenarios/multi_node/basic_connectivity_peer_discovery.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,11 @@ impl MultiNodeBasicConnectivityPeerDiscovery {
6767
let config = RustNodeTestingConfig::berkeley_default()
6868
.ask_initial_peers_interval(Duration::from_secs(3600))
6969
.max_peers(100)
70-
.libp2p_port(10000)
7170
.initial_peers(
7271
nodes
7372
.iter()
7473
.chain(std::iter::once(&seed_a))
75-
.filter_map(|node_id| runner.ocaml_node(*node_id))
76-
.map(|node| node.dial_addr())
74+
.map(|node_id| (*node_id).into())
7775
.collect(),
7876
);
7977
let node_id = runner.add_rust_node(config);

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

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ use std::time::Duration;
22

33
use node::{
44
event_source::Event,
5-
p2p::{
6-
connection::outgoing::P2pConnectionOutgoingInitOpts, P2pConnectionEvent, P2pEvent,
7-
P2pPeerState, P2pPeerStatus, P2pState, PeerId,
8-
},
5+
p2p::{P2pConnectionEvent, P2pEvent, P2pPeerState, P2pPeerStatus, P2pState, PeerId},
96
};
107

118
use crate::{
@@ -109,26 +106,13 @@ impl AllNodesConnectionsAreSymmetric {
109106

110107
let mut driver = Driver::new(runner);
111108

112-
let (_, (_, seed_addr)) =
113-
driver.add_rust_node_with(RustNodeTestingConfig::berkeley_default(), |state| {
114-
let config = &state.p2p.config;
115-
let port = config.libp2p_port.unwrap();
116-
let peer_id = config.identity_pub_key.peer_id();
117-
let addr = format!(
118-
"/ip4/127.0.0.1/tcp/{port}/p2p/{}",
119-
peer_id.clone().to_libp2p_string()
120-
)
121-
.parse::<P2pConnectionOutgoingInitOpts>()
122-
.unwrap();
123-
(peer_id, addr)
124-
});
109+
let (seed_id, _) = driver.add_rust_node(RustNodeTestingConfig::berkeley_default());
125110

126111
let peers: Vec<_> = (0..MAX)
127112
.into_iter()
128113
.map(|_| {
129114
driver.add_rust_node(
130-
RustNodeTestingConfig::berkeley_default()
131-
.initial_peers(vec![seed_addr.clone()]),
115+
RustNodeTestingConfig::berkeley_default().initial_peers(vec![seed_id.into()]),
132116
)
133117
})
134118
.collect();
@@ -182,26 +166,14 @@ impl SeedConnectionsAreSymmetric {
182166

183167
let mut driver = Driver::new(runner);
184168

185-
let (node_ut, (node_ut_peer_id, seed_addr)) =
186-
driver.add_rust_node_with(RustNodeTestingConfig::berkeley_default(), |state| {
187-
let config = &state.p2p.config;
188-
let port = config.libp2p_port.unwrap();
189-
let peer_id = config.identity_pub_key.peer_id();
190-
let addr = format!(
191-
"/ip4/127.0.0.1/tcp/{port}/p2p/{}",
192-
peer_id.clone().to_libp2p_string()
193-
)
194-
.parse::<P2pConnectionOutgoingInitOpts>()
195-
.unwrap();
196-
(peer_id, addr)
197-
});
169+
let (node_ut, node_ut_peer_id) =
170+
driver.add_rust_node(RustNodeTestingConfig::berkeley_default());
198171

199172
let peers: Vec<_> = (0..MAX)
200173
.into_iter()
201174
.map(|_| {
202175
driver.add_rust_node(
203-
RustNodeTestingConfig::berkeley_default()
204-
.initial_peers(vec![seed_addr.clone()]),
176+
RustNodeTestingConfig::berkeley_default().initial_peers(vec![node_ut.into()]),
205177
)
206178
})
207179
.collect();

0 commit comments

Comments
 (0)