Skip to content

Commit

Permalink
Update msim pointer (MystenLabs#5005)
Browse files Browse the repository at this point in the history
* update mysten-sim pointer

* No need for specialized get_ephemeral_port() anymore

* Use abbreviated pubkey for node name
  • Loading branch information
mystenmark authored Oct 5, 2022
1 parent 79b27eb commit 66ab7b2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 71 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 25 additions & 65 deletions crates/sui-config/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,82 +1,42 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

#[cfg(not(msim))]
mod inner {
use std::net::{TcpListener, TcpStream};
use std::net::{TcpListener, TcpStream};

/// Return an ephemeral, available port. On unix systems, the port returned will be in the
/// TIME_WAIT state ensuring that the OS won't hand out this port for some grace period.
/// Callers should be able to bind to this port given they use SO_REUSEADDR.
pub fn get_available_port() -> u16 {
const MAX_PORT_RETRIES: u32 = 1000;
/// Return an ephemeral, available port. On unix systems, the port returned will be in the
/// TIME_WAIT state ensuring that the OS won't hand out this port for some grace period.
/// Callers should be able to bind to this port given they use SO_REUSEADDR.
pub fn get_available_port() -> u16 {
const MAX_PORT_RETRIES: u32 = 1000;

for _ in 0..MAX_PORT_RETRIES {
if let Ok(port) = get_ephemeral_port() {
return port;
}
for _ in 0..MAX_PORT_RETRIES {
if let Ok(port) = get_ephemeral_port() {
return port;
}

panic!("Error: could not find an available port");
}

fn get_ephemeral_port() -> ::std::io::Result<u16> {
// Request a random available port from the OS
let listener = TcpListener::bind(("localhost", 0))?;
let addr = listener.local_addr()?;

// Create and accept a connection (which we'll promptly drop) in order to force the port
// into the TIME_WAIT state, ensuring that the port will be reserved from some limited
// amount of time (roughly 60s on some Linux systems)
let _sender = TcpStream::connect(addr)?;
let _incoming = listener.accept()?;

Ok(addr.port())
}

pub fn new_network_address() -> multiaddr::Multiaddr {
format!("/ip4/127.0.0.1/tcp/{}/http", get_available_port())
.parse()
.unwrap()
}
panic!("Error: could not find an available port");
}

#[cfg(msim)]
mod inner {
use std::cell::Cell;

pub fn get_available_port() -> u16 {
thread_local! {
static PORT: Cell<u32> = Cell::new(32768);
}

// TODO: This is a bit of a hack - there is nothing to say that other services haven't already
// used this port for something else, which could cause problems. We should add a way to ask
// the simulator for an unused port.
PORT.with(|port| {
let ret = port.get();
port.set(ret + 1);
ret
})
.try_into()
.expect("ran out of ports")
}
fn get_ephemeral_port() -> ::std::io::Result<u16> {
// Request a random available port from the OS
let listener = TcpListener::bind(("localhost", 0))?;
let addr = listener.local_addr()?;

pub fn new_network_address() -> multiaddr::Multiaddr {
let ip_addr = sui_simulator::runtime::NodeHandle::try_current()
.map(|node| {
node.ip()
.expect("expected to be called within a simulator node")
})
.unwrap_or_else(|| "127.0.0.1".parse().unwrap());
// Create and accept a connection (which we'll promptly drop) in order to force the port
// into the TIME_WAIT state, ensuring that the port will be reserved from some limited
// amount of time (roughly 60s on some Linux systems)
let _sender = TcpStream::connect(addr)?;
let _incoming = listener.accept()?;

format!("/ip4/{}/tcp/{}/http", ip_addr, get_available_port())
.parse()
.unwrap()
}
Ok(addr.port())
}

pub use inner::*;
pub fn new_network_address() -> multiaddr::Multiaddr {
format!("/ip4/127.0.0.1/tcp/{}/http", get_available_port())
.parse()
.unwrap()
}

pub fn available_local_socket_address() -> std::net::SocketAddr {
format!("127.0.0.1:{}", get_available_port())
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ syn = "1"
workspace-hack.workspace = true

[target.'cfg(msim)'.dependencies]
msim-macros = { git = "https://github.com/MystenLabs/mysten-sim.git", rev = "e7773753dcb5becda0ea27b10694eb9c11e7af4a", package = "msim-macros" }
msim-macros = { git = "https://github.com/MystenLabs/mysten-sim.git", rev = "305f0ca15fe77df5b92b7f4bb57b5472e568edc3", package = "msim-macros" }
2 changes: 1 addition & 1 deletion crates/sui-simulator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ telemetry-subscribers.workspace = true
tracing = "0.1"

[target.'cfg(msim)'.dependencies]
msim = { git = "https://github.com/MystenLabs/mysten-sim.git", rev = "e7773753dcb5becda0ea27b10694eb9c11e7af4a", package = "msim" }
msim = { git = "https://github.com/MystenLabs/mysten-sim.git", rev = "305f0ca15fe77df5b92b7f4bb57b5472e568edc3", package = "msim" }
2 changes: 1 addition & 1 deletion crates/sui-swarm/src/memory/container-sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Container {

let node = builder
.ip(ip)
.name(format!("{}", config.protocol_public_key()))
.name(&format!("{}", config.protocol_public_key()).as_str()[0..8])
.init(|| async {
tracing::info!("node restarted");
})
Expand Down
2 changes: 1 addition & 1 deletion scripts/simtest/cargo-simtest
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ if [ -n "$LOCAL_MSIM_PATH" ]; then
else
cargo_patch_args+=(
--config 'patch.crates-io.tokio.git = "https://github.com/MystenLabs/mysten-sim.git"'
--config 'patch.crates-io.tokio.rev = "e7773753dcb5becda0ea27b10694eb9c11e7af4a"'
--config 'patch.crates-io.tokio.rev = "305f0ca15fe77df5b92b7f4bb57b5472e568edc3"'
)
fi

Expand Down

0 comments on commit 66ab7b2

Please sign in to comment.