Skip to content

Commit

Permalink
chore(papyrus_common): reallocate and remove redundant functions
Browse files Browse the repository at this point in the history
commit-id:53a51006
  • Loading branch information
Itay-Tsabary-Starkware committed Jan 15, 2025
1 parent 7df070e commit 683f1aa
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 26 deletions.
1 change: 0 additions & 1 deletion crates/papyrus_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
24 changes: 0 additions & 24 deletions crates/papyrus_common/src/tcp.rs

This file was deleted.

13 changes: 12 additions & 1 deletion crates/sequencing/papyrus_consensus/src/bin/run_simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<Duration, std::num::ParseIntError> {
let secs = u64::from_str(s)?;
Ok(Duration::from_secs(secs))
Expand Down

0 comments on commit 683f1aa

Please sign in to comment.