Skip to content

Commit

Permalink
Set ports for 4 test nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ameba23 committed Dec 3, 2024
1 parent 1f62ab8 commit 1e01e8a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
37 changes: 18 additions & 19 deletions crates/testing-utils/src/node_proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ where
pub struct TestNodeProcessBuilder {
node_path: OsString,
authority: Option<AccountKeyring>,
scan_port_range: bool,
port: u16,
chain_type: String,
force_authoring: bool,
bootnode: Option<String>,
Expand All @@ -100,7 +100,7 @@ impl TestNodeProcessBuilder {
Self {
node_path: node_path.as_ref().into(),
authority: None,
scan_port_range: false,
port: 9944,
chain_type,
force_authoring,
bootnode,
Expand All @@ -114,11 +114,9 @@ impl TestNodeProcessBuilder {
self
}

/// Enable port scanning to scan for open ports.
///
/// Allows spawning multiple node instances for tests to run in parallel.
pub fn scan_for_open_ports(&mut self) -> &mut Self {
self.scan_port_range = true;
/// Set the port for websocket rpc
pub fn set_port(&mut self, port: u16) -> &mut Self {
self.port = port;
self
}

Expand Down Expand Up @@ -148,25 +146,26 @@ impl TestNodeProcessBuilder {
cmd.arg(arg);
}

let ws_port = if self.scan_port_range {
let (p2p_port, _http_port, ws_port) = next_open_port()
.ok_or_else(|| "No available ports in the given port range".to_owned())?;
cmd.arg(format!("--port={p2p_port}"));
cmd.arg(format!("--rpc-port={ws_port}"));
tracing::info!("ws port: {ws_port}");
ws_port
} else {
// the default Websockets port
9944
};
cmd.arg(format!("--port={}", self.port));
// let ws_port = if self.scan_port_range {
// // let (p2p_port, _http_port, ws_port) = next_open_port()
// // .ok_or_else(|| "No available ports in the given port range".to_owned())?;
// cmd.arg(format!("--port={p2p_port}"));
// cmd.arg(format!("--rpc-port={ws_port}"));
// tracing::info!("ws port: {ws_port}");
// ws_port
// } else {
// // the default Websockets port
// 9944
// };

let arg = "--rpc-external";
cmd.arg(arg);

let arg = "--rpc-cors=all";
cmd.arg(arg);

let ws_url = format!("ws://127.0.0.1:{ws_port}");
let ws_url = format!("ws://127.0.0.1:{}", self.port);

let mut proc = cmd.spawn().map_err(|e| {
format!("Error spawning substrate node '{}': {e}", self.node_path.to_string_lossy())
Expand Down
10 changes: 7 additions & 3 deletions crates/testing-utils/src/substrate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub async fn test_node_process_with(
force_authoring: bool,
bootnode: Option<String>,
tss_server_endpoint: Option<String>,
port: Option<u16>,
) -> TestNodeProcess<EntropyConfig> {
let path = get_path();
let path = path.to_str().expect("Path should've been checked to be valid earlier.");
Expand All @@ -75,7 +76,7 @@ pub async fn test_node_process_with(
tss_server_endpoint,
)
.with_authority(key)
.scan_for_open_ports()
.set_port(port.unwrap_or(9944))
.spawn::<EntropyConfig>()
.await;
proc.unwrap()
Expand All @@ -99,7 +100,8 @@ pub async fn test_node(
}

pub async fn test_node_process() -> TestNodeProcess<EntropyConfig> {
test_node_process_with(AccountKeyring::Alice, "--dev".to_string(), false, None, None).await
test_node_process_with(AccountKeyring::Alice, "--dev".to_string(), false, None, None, None)
.await
}

pub async fn test_node_process_stationary() -> TestNodeProcess<EntropyConfig> {
Expand All @@ -116,7 +118,6 @@ pub async fn test_node_process_testing_state(
"/ip4/127.0.0.1/tcp/30333/p2p/12D3KooWM7EoKJfwgzAR1nAVmYRuuFq2f3GpJPLrdfhQaRsKjn38"
.to_string(),
);
// reduses message from chain to same TSS cleaning up a lot of logging
let result =
test_node(AccountKeyring::Alice, "--chain=dev".to_string(), force_authoring, None).await;
let result_bob = test_node_process_with(
Expand All @@ -125,6 +126,7 @@ pub async fn test_node_process_testing_state(
force_authoring,
alice_bootnode.clone(),
Some("http://localhost:3002".to_string()),
Some(9945),
)
.await;
let result_charlie = test_node_process_with(
Expand All @@ -133,6 +135,7 @@ pub async fn test_node_process_testing_state(
force_authoring,
alice_bootnode.clone(),
Some("http://localhost:3003".to_string()),
Some(9946),
)
.await;
let result_dave = test_node_process_with(
Expand All @@ -141,6 +144,7 @@ pub async fn test_node_process_testing_state(
force_authoring,
alice_bootnode.clone(),
Some("http://localhost:3004".to_string()),
Some(9947),
)
.await;

Expand Down

0 comments on commit 1e01e8a

Please sign in to comment.