From 8d3080c60d85c694ea5add6f61173ccfcece8ffe Mon Sep 17 00:00:00 2001 From: Itay Tsabary Date: Wed, 15 Jan 2025 15:27:40 +0200 Subject: [PATCH] chore(papyrus_common): reallocate and remove redundant functions commit-id:53a51006 --- crates/papyrus_common/src/lib.rs | 1 - crates/papyrus_common/src/tcp.rs | 24 ------------------- .../src/bin/run_simulation.rs | 13 +++++++++- 3 files changed, 12 insertions(+), 26 deletions(-) delete mode 100644 crates/papyrus_common/src/tcp.rs diff --git a/crates/papyrus_common/src/lib.rs b/crates/papyrus_common/src/lib.rs index 3fc0c90bea..12e6eeeab7 100644 --- a/crates/papyrus_common/src/lib.rs +++ b/crates/papyrus_common/src/lib.rs @@ -8,7 +8,6 @@ pub mod pending_classes; pub mod python_json; pub mod state; pub mod storage_query; -pub mod tcp; pub(crate) fn usize_into_felt(u: usize) -> Felt { u128::try_from(u).expect("Expect at most 128 bits").into() diff --git a/crates/papyrus_common/src/tcp.rs b/crates/papyrus_common/src/tcp.rs deleted file mode 100644 index 072f07cf1c..0000000000 --- a/crates/papyrus_common/src/tcp.rs +++ /dev/null @@ -1,24 +0,0 @@ -use std::net::TcpListener; - -pub fn find_free_port() -> u16 { - // The socket is automatically closed when the function exits. - // The port may still be available when accessed, but this is not guaranteed. - // TODO(Asmaa): find a reliable way to ensure the port stays free. - let listener = TcpListener::bind("0.0.0.0:0").expect("Failed to bind"); - listener.local_addr().expect("Failed to get local address").port() -} - -pub fn find_n_free_ports(n: usize) -> Vec { - // The socket is automatically closed when the function exits. - // The port may still be available when accessed, but this is not guaranteed. - // TODO(Asmaa): find a reliable way to ensure the port stays free. - let listeners = - Vec::from_iter((0..n).map(|_| TcpListener::bind("0.0.0.0:0").expect("Failed to bind"))); - - let mut ports = Vec::with_capacity(n); - for listener in listeners { - let port = listener.local_addr().expect("Failed to get local address").port(); - ports.push(port); - } - ports -} diff --git a/crates/sequencing/papyrus_consensus/src/bin/run_simulation.rs b/crates/sequencing/papyrus_consensus/src/bin/run_simulation.rs index 65f32c7780..4d88f3b931 100644 --- a/crates/sequencing/papyrus_consensus/src/bin/run_simulation.rs +++ b/crates/sequencing/papyrus_consensus/src/bin/run_simulation.rs @@ -4,6 +4,7 @@ //! uses the `run_consensus` binary which is able to simulate network issues for consensus messages. use std::collections::HashSet; use std::fs::{self, File}; +use std::net::TcpListener; use std::os::unix::process::CommandExt; use std::process::Command; use std::str::FromStr; @@ -13,7 +14,6 @@ use clap::Parser; use fs2::FileExt; use lazy_static::lazy_static; use nix::unistd::Pid; -use papyrus_common::tcp::find_free_port; use papyrus_protobuf::consensus::DEFAULT_VALIDATOR_ID; use tokio::process::Command as TokioCommand; @@ -185,6 +185,17 @@ impl Drop for LockDir { } } +// WARNING(Tsabary): This is not a reliable way to obtain a free port; most notably it fails when +// multiple concurrent instances try to obtain ports using this function. Do NOT use this in +// production code, nor in tests, as they run concurrently. +fn find_free_port() -> u16 { + // The socket is automatically closed when the function exits. + // The port may still be available when accessed, but this is not guaranteed. + // TODO(Asmaa): find a reliable way to ensure the port stays free. + let listener = TcpListener::bind("0.0.0.0:0").expect("Failed to bind"); + listener.local_addr().expect("Failed to get local address").port() +} + fn parse_duration(s: &str) -> Result { let secs = u64::from_str(s)?; Ok(Duration::from_secs(secs))