Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use port 0 for anvil by default #135

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions crates/node-bindings/src/anvil.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Utilities for launching an Anvil instance.

use crate::unused_port;
use alloy_primitives::{hex, Address};
use k256::{ecdsa::SigningKey, SecretKey as K256SecretKey};
use std::{
io::{BufRead, BufReader},
net::SocketAddr,
path::PathBuf,
process::{Child, Command},
str::FromStr,
time::{Duration, Instant},
};

Expand Down Expand Up @@ -220,7 +221,12 @@ impl Anvil {
Command::new("anvil")
};
cmd.stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::inherit());
let port = if let Some(port) = self.port { port } else { unused_port() };
let mut port = if let Some(port) = self.port {
port
} else {
// let the OS choose a port for us
0
};
cmd.arg("-p").arg(port.to_string());

if let Some(mnemonic) = self.mnemonic {
Expand Down Expand Up @@ -265,7 +271,12 @@ impl Anvil {

let mut line = String::new();
reader.read_line(&mut line).expect("Failed to read line from anvil process");
if line.contains("Listening on") {
if let Some(addr) = line.strip_prefix("Listening on") {
// <Listening on 127.0.0.1:8545>
// parse the actual port
if let Ok(addr) = SocketAddr::from_str(addr.trim()) {
port = addr.port();
}
break;
}

Expand Down