Skip to content

Commit a1f36be

Browse files
committed
feat(testing): specify default cluster config in an individual scenario
1 parent e2c8959 commit a1f36be

File tree

6 files changed

+129
-32
lines changed

6 files changed

+129
-32
lines changed

node/testing/src/cluster/config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl ClusterConfig {
4242
})
4343
}
4444

45-
pub fn use_debugger(mut self) -> Self {
45+
pub fn use_debugger(&mut self) -> &mut Self {
4646
self.use_debugger = true;
4747
self
4848
}
@@ -51,7 +51,7 @@ impl ClusterConfig {
5151
self.use_debugger
5252
}
5353

54-
pub fn set_replay(mut self) -> Self {
54+
pub fn set_replay(&mut self) -> &mut Self {
5555
self.is_replay = true;
5656
self
5757
}
@@ -65,7 +65,7 @@ impl ClusterConfig {
6565
(range.0)..=(range.1)
6666
}
6767

68-
pub fn set_all_rust_to_rust_use_webrtc(mut self) -> Self {
68+
pub fn set_all_rust_to_rust_use_webrtc(&mut self) -> &mut Self {
6969
assert!(cfg!(feature = "p2p-webrtc"));
7070
self.all_rust_to_rust_use_webrtc = true;
7171
self
@@ -84,7 +84,7 @@ impl ClusterConfig {
8484
self.proof_kind
8585
}
8686

87-
pub fn set_ocaml_node_executable(mut self, executable: OcamlNodeExecutable) -> Self {
87+
pub fn set_ocaml_node_executable(&mut self, executable: OcamlNodeExecutable) -> &mut Self {
8888
self.ocaml_node_executable = Some(executable);
8989
self
9090
}

node/testing/src/main.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,27 @@ impl Command {
7878
Self::ScenariosGenerate(cmd) => {
7979
#[cfg(feature = "scenario-generators")]
8080
{
81-
let config = ClusterConfig::new(None).map_err(|err| {
82-
anyhow::anyhow!("failed to create cluster configuration: {err}")
83-
})?;
84-
let config = if cmd.use_debugger {
85-
config.use_debugger()
86-
} else {
87-
config
81+
let run_scenario = |scenario: Scenarios| -> Result<_, anyhow::Error> {
82+
let mut config = scenario.default_cluster_config()?;
83+
if cmd.use_debugger {
84+
config.use_debugger();
85+
}
86+
Ok(scenario.run_only_from_scratch(config))
8887
};
89-
9088
let fut = async move {
9189
if let Some(name) = cmd.name {
9290
if let Some(scenario) = Scenarios::find_by_name(&name) {
93-
scenario.run_only_from_scratch(config).await;
94-
// scenario.run_and_save_from_scratch(config).await;
91+
run_scenario(scenario)?.await;
9592
} else {
9693
anyhow::bail!("no such scenario: \"{name}\"");
9794
}
9895
} else {
9996
for scenario in Scenarios::iter() {
100-
scenario.run_only_from_scratch(config.clone()).await;
101-
// scenario.run_and_save_from_scratch(config.clone()).await;
97+
run_scenario(scenario)?.await;
10298
}
10399
}
104100
Ok(())
105101
};
106-
107102
rt.block_on(async {
108103
tokio::select! {
109104
res = fut => res,
@@ -119,10 +114,10 @@ impl Command {
119114
.into())
120115
}
121116
Self::ScenariosRun(cmd) => {
122-
let config = ClusterConfig::new(None).map_err(|err| {
117+
let mut config = ClusterConfig::new(None).map_err(|err| {
123118
anyhow::anyhow!("failed to create cluster configuration: {err}")
124119
})?;
125-
let config = config.set_replay();
120+
config.set_replay();
126121

127122
let id = cmd.name.parse()?;
128123
let fut = async move {

node/testing/src/scenarios/mod.rs

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub mod p2p;
1818

1919
mod driver;
2020
pub use driver::*;
21-
use p2p::signaling::P2pSignaling;
2221

2322
pub use crate::cluster::runner::*;
2423

@@ -29,13 +28,31 @@ use crate::scenario::{Scenario, ScenarioId, ScenarioStep};
2928

3029
use self::multi_node::basic_connectivity_initial_joining::MultiNodeBasicConnectivityInitialJoining;
3130
use self::multi_node::basic_connectivity_peer_discovery::MultiNodeBasicConnectivityPeerDiscovery;
31+
use self::multi_node::connection_discovery::RustNodeAsSeed as P2pConnectionDiscoveryRustNodeAsSeed;
32+
use self::multi_node::connection_discovery::{
33+
OCamlToRust, OCamlToRustViaSeed, RustToOCaml, RustToOCamlViaSeed,
34+
};
3235
use self::multi_node::pubsub_advanced::MultiNodePubsubPropagateBlock;
3336
use self::multi_node::sync_4_block_producers::MultiNodeSync4BlockProducers;
3437
use self::multi_node::vrf_correct_ledgers::MultiNodeVrfGetCorrectLedgers;
3538
use self::multi_node::vrf_correct_slots::MultiNodeVrfGetCorrectSlots;
3639
use self::multi_node::vrf_epoch_bounds_correct_ledgers::MultiNodeVrfEpochBoundsCorrectLedger;
3740
use self::multi_node::vrf_epoch_bounds_evaluation::MultiNodeVrfEpochBoundsEvaluation;
41+
use self::p2p::basic_connection_handling::{
42+
AllNodesConnectionsAreSymmetric, MaxNumberOfPeersIncoming, MaxNumberOfPeersIs1,
43+
SeedConnectionsAreSymmetric, SimultaneousConnections,
44+
};
45+
use self::p2p::basic_incoming_connections::{
46+
AcceptIncomingConnection, AcceptMultipleIncomingConnections,
47+
};
48+
use self::p2p::basic_outgoing_connections::{
49+
ConnectToInitialPeers, ConnectToInitialPeersBecomeReady, ConnectToUnavailableInitialPeers,
50+
DontConnectToInitialPeerWithSameId, DontConnectToNodeWithSameId, DontConnectToSelfInitialPeer,
51+
MakeMultipleOutgoingConnections, MakeOutgoingConnection,
52+
};
53+
use self::p2p::kademlia::KademliaBootstrap;
3854
use self::p2p::pubsub::P2pReceiveBlock;
55+
use self::p2p::signaling::P2pSignaling;
3956
use self::record_replay::block_production::RecordReplayBlockProduction;
4057
use self::record_replay::bootstrap::RecordReplayBootstrap;
4158
use self::simulation::small::SimulationSmall;
@@ -68,9 +85,31 @@ pub enum Scenarios {
6885
SimulationSmallForeverRealTime(SimulationSmallForeverRealTime),
6986
P2pReceiveBlock(P2pReceiveBlock),
7087
P2pSignaling(P2pSignaling),
88+
P2pConnectionDiscoveryRustNodeAsSeed(P2pConnectionDiscoveryRustNodeAsSeed),
7189
MultiNodePubsubPropagateBlock(MultiNodePubsubPropagateBlock),
7290
RecordReplayBootstrap(RecordReplayBootstrap),
7391
RecordReplayBlockProduction(RecordReplayBlockProduction),
92+
93+
RustToOCaml(RustToOCaml),
94+
OCamlToRust(OCamlToRust),
95+
OCamlToRustViaSeed(OCamlToRustViaSeed),
96+
RustToOCamlViaSeed(RustToOCamlViaSeed),
97+
KademliaBootstrap(KademliaBootstrap),
98+
AcceptIncomingConnection(AcceptIncomingConnection),
99+
MakeOutgoingConnection(MakeOutgoingConnection),
100+
AcceptMultipleIncomingConnections(AcceptMultipleIncomingConnections),
101+
MakeMultipleOutgoingConnections(MakeMultipleOutgoingConnections),
102+
DontConnectToNodeWithSameId(DontConnectToNodeWithSameId),
103+
DontConnectToInitialPeerWithSameId(DontConnectToInitialPeerWithSameId),
104+
DontConnectToSelfInitialPeer(DontConnectToSelfInitialPeer),
105+
SimultaneousConnections(SimultaneousConnections),
106+
ConnectToInitialPeers(ConnectToInitialPeers),
107+
ConnectToUnavailableInitialPeers(ConnectToUnavailableInitialPeers),
108+
AllNodesConnectionsAreSymmetric(AllNodesConnectionsAreSymmetric),
109+
ConnectToInitialPeersBecomeReady(ConnectToInitialPeersBecomeReady),
110+
SeedConnectionsAreSymmetric(SeedConnectionsAreSymmetric),
111+
MaxNumberOfPeersIncoming(MaxNumberOfPeersIncoming),
112+
MaxNumberOfPeersIs1(MaxNumberOfPeersIs1),
74113
}
75114

76115
impl Scenarios {
@@ -152,9 +191,43 @@ impl Scenarios {
152191
Self::SimulationSmallForeverRealTime(_) => SimulationSmallForeverRealTime::DOCS,
153192
Self::P2pReceiveBlock(_) => P2pReceiveBlock::DOCS,
154193
Self::P2pSignaling(_) => P2pSignaling::DOCS,
194+
Self::P2pConnectionDiscoveryRustNodeAsSeed(_) => {
195+
P2pConnectionDiscoveryRustNodeAsSeed::DOCS
196+
}
155197
Self::MultiNodePubsubPropagateBlock(_) => MultiNodePubsubPropagateBlock::DOCS,
156198
Self::RecordReplayBootstrap(_) => RecordReplayBootstrap::DOCS,
157199
Self::RecordReplayBlockProduction(_) => RecordReplayBlockProduction::DOCS,
200+
201+
Self::RustToOCaml(_) => RustToOCaml::DOCS,
202+
Self::OCamlToRust(_) => OCamlToRust::DOCS,
203+
Self::OCamlToRustViaSeed(_) => OCamlToRustViaSeed::DOCS,
204+
Self::RustToOCamlViaSeed(_) => RustToOCamlViaSeed::DOCS,
205+
Self::KademliaBootstrap(_) => KademliaBootstrap::DOCS,
206+
Self::AcceptIncomingConnection(_) => AcceptIncomingConnection::DOCS,
207+
Self::MakeOutgoingConnection(_) => MakeOutgoingConnection::DOCS,
208+
Self::AcceptMultipleIncomingConnections(_) => AcceptMultipleIncomingConnections::DOCS,
209+
Self::MakeMultipleOutgoingConnections(_) => MakeMultipleOutgoingConnections::DOCS,
210+
Self::DontConnectToNodeWithSameId(_) => DontConnectToNodeWithSameId::DOCS,
211+
Self::DontConnectToInitialPeerWithSameId(_) => DontConnectToInitialPeerWithSameId::DOCS,
212+
Self::DontConnectToSelfInitialPeer(_) => DontConnectToSelfInitialPeer::DOCS,
213+
Self::SimultaneousConnections(_) => SimultaneousConnections::DOCS,
214+
Self::ConnectToInitialPeers(_) => ConnectToInitialPeers::DOCS,
215+
Self::ConnectToUnavailableInitialPeers(_) => ConnectToUnavailableInitialPeers::DOCS,
216+
Self::AllNodesConnectionsAreSymmetric(_) => AllNodesConnectionsAreSymmetric::DOCS,
217+
Self::ConnectToInitialPeersBecomeReady(_) => ConnectToInitialPeersBecomeReady::DOCS,
218+
Self::SeedConnectionsAreSymmetric(_) => SeedConnectionsAreSymmetric::DOCS,
219+
Self::MaxNumberOfPeersIncoming(_) => MaxNumberOfPeersIncoming::DOCS,
220+
Self::MaxNumberOfPeersIs1(_) => MaxNumberOfPeersIs1::DOCS,
221+
}
222+
}
223+
224+
pub fn default_cluster_config(self) -> Result<ClusterConfig, anyhow::Error> {
225+
let config = ClusterConfig::new(None)
226+
.map_err(|err| anyhow::anyhow!("failed to create cluster configuration: {err}"))?;
227+
228+
match self {
229+
Self::P2pSignaling(v) => v.default_cluster_config(config),
230+
_ => Ok(config),
158231
}
159232
}
160233

@@ -189,9 +262,31 @@ impl Scenarios {
189262
Self::SimulationSmallForeverRealTime(v) => v.run(runner).await,
190263
Self::P2pReceiveBlock(v) => v.run(runner).await,
191264
Self::P2pSignaling(v) => v.run(runner).await,
265+
Self::P2pConnectionDiscoveryRustNodeAsSeed(v) => v.run(runner).await,
192266
Self::MultiNodePubsubPropagateBlock(v) => v.run(runner).await,
193267
Self::RecordReplayBootstrap(v) => v.run(runner).await,
194268
Self::RecordReplayBlockProduction(v) => v.run(runner).await,
269+
270+
Self::RustToOCaml(v) => v.run(runner).await,
271+
Self::OCamlToRust(v) => v.run(runner).await,
272+
Self::OCamlToRustViaSeed(v) => v.run(runner).await,
273+
Self::RustToOCamlViaSeed(v) => v.run(runner).await,
274+
Self::KademliaBootstrap(v) => v.run(runner).await,
275+
Self::AcceptIncomingConnection(v) => v.run(runner).await,
276+
Self::MakeOutgoingConnection(v) => v.run(runner).await,
277+
Self::AcceptMultipleIncomingConnections(v) => v.run(runner).await,
278+
Self::MakeMultipleOutgoingConnections(v) => v.run(runner).await,
279+
Self::DontConnectToNodeWithSameId(v) => v.run(runner).await,
280+
Self::DontConnectToInitialPeerWithSameId(v) => v.run(runner).await,
281+
Self::DontConnectToSelfInitialPeer(v) => v.run(runner).await,
282+
Self::SimultaneousConnections(v) => v.run(runner).await,
283+
Self::ConnectToInitialPeers(v) => v.run(runner).await,
284+
Self::ConnectToUnavailableInitialPeers(v) => v.run(runner).await,
285+
Self::AllNodesConnectionsAreSymmetric(v) => v.run(runner).await,
286+
Self::ConnectToInitialPeersBecomeReady(v) => v.run(runner).await,
287+
Self::SeedConnectionsAreSymmetric(v) => v.run(runner).await,
288+
Self::MaxNumberOfPeersIncoming(v) => v.run(runner).await,
289+
Self::MaxNumberOfPeersIs1(v) => v.run(runner).await,
195290
}
196291
}
197292

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use node::{
66
};
77

88
use crate::{
9+
cluster::ClusterConfig,
910
node::RustNodeTestingConfig,
1011
scenarios::{ClusterRunner, DynEffectsData, RunCfg},
1112
};
@@ -16,6 +17,14 @@ use crate::{
1617
pub struct P2pSignaling;
1718

1819
impl P2pSignaling {
20+
pub fn default_cluster_config(
21+
self,
22+
mut config: ClusterConfig,
23+
) -> Result<ClusterConfig, anyhow::Error> {
24+
config.set_all_rust_to_rust_use_webrtc();
25+
Ok(config)
26+
}
27+
1928
pub async fn run(self, mut runner: ClusterRunner<'_>) {
2029
const NODES_N: usize = 4;
2130

node/testing/tests/common.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ macro_rules! scenario_test {
1818
$(#[$meta])?
1919
async fn $name() {
2020
use openmina_node_testing::{
21-
cluster::{Cluster, ClusterConfig},
22-
scenarios::ClusterRunner,
21+
cluster::Cluster,
22+
scenarios::{ClusterRunner, Scenarios},
2323
setup_without_rt, wait_for_other_tests,
2424
};
2525
use std::io::Write;
@@ -57,16 +57,15 @@ macro_rules! scenario_test {
5757
}));
5858
}
5959

60+
let scenario = $scenario_instance;
6061
#[allow(unused_mut)]
61-
let mut config = ClusterConfig::new(None).unwrap();
62+
let mut config = Scenarios::from(scenario).default_cluster_config().unwrap();
6263
#[cfg(feature = "p2p-webrtc")]
6364
if $can_test_webrtc {
64-
eprintln!("All rust to rust connections will be over webrtc transport");
65-
config = config.set_all_rust_to_rust_use_webrtc();
65+
config.set_all_rust_to_rust_use_webrtc();
6666
}
6767
let mut cluster = Cluster::new(config);
6868
let runner = ClusterRunner::new(&mut cluster, |_| {});
69-
let scenario = $scenario_instance;
7069
scenario.run(runner).await;
7170

7271
if let Some(summary) = std::env::var_os("GITHUB_STEP_SUMMARY") {
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
#[cfg(feature = "p2p-webrtc")]
2-
use openmina_node_testing::{cluster::ClusterConfig, scenarios::Scenarios, setup};
2+
use openmina_node_testing::{scenarios::Scenarios, setup};
33

44
#[cfg(feature = "p2p-webrtc")]
55
#[test]
66
fn node_libp2p_with_rust_to_rust_webrtc_all_scenarios() {
77
let rt = setup();
8-
let config = ClusterConfig::new(None)
9-
.unwrap()
10-
.set_all_rust_to_rust_use_webrtc();
118

129
for scenario in Scenarios::iter() {
1310
eprintln!("running scenario: {}", scenario.to_str());
14-
rt.block_on(async {
15-
scenario.run_only_from_scratch(config.clone()).await;
11+
let mut config = scenario.default_cluster_config().unwrap();
12+
config.set_all_rust_to_rust_use_webrtc();
13+
rt.block_on(async move {
14+
scenario.run_only_from_scratch(config).await;
1615
});
1716
}
1817
}

0 commit comments

Comments
 (0)