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

feat: adds-host-cli-option #395

Merged
merged 7 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jsonrpc-core = { git = "https://github.com/matter-labs/jsonrpc.git", branch = "m
jsonrpc-core-client = { git = "https://github.com/matter-labs/jsonrpc.git", branch = "master" }
jsonrpc-derive = { git = "https://github.com/matter-labs/jsonrpc.git", branch = "master" }

clap = { version = "4.2.4", features = ["derive"] }
clap = { version = "4.2.4", features = ["derive", "env"] }
reqwest = { version = "0.11", features = ["blocking"] }
serde = { version = "1.0", features = ["derive"] }
tracing = { version = "0.1.26", features = ["log"] }
Expand Down
67 changes: 67 additions & 0 deletions src/config/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::system_contracts::Options as SystemContractsOptions;

use super::DEFAULT_DISK_CACHE_DIR;
use alloy_signer_local::coins_bip39::{English, Mnemonic};
use std::net::IpAddr;

#[derive(Debug, Parser, Clone)]
#[command(
Expand Down Expand Up @@ -45,6 +46,17 @@ pub struct Cli {
/// Port to listen on (default: 8011).
pub port: Option<u16>,

/// The hosts the server will listen on.
#[arg(
long,
value_name = "IP_ADDR",
env = "ANVIL_ZKSYNC_IP_ADDR",
default_value = "127.0.0.1",
value_delimiter = ',',
help_heading = "Network Options"
)]
pub host: Vec<IpAddr>,
itegulov marked this conversation as resolved.
Show resolved Hide resolved

#[arg(long, help_heading = "Network Options")]
/// Specify chain ID (default: 260).
pub chain_id: Option<u32>,
Expand Down Expand Up @@ -299,6 +311,7 @@ impl Cli {
}))
.with_chain_id(self.chain_id)
.set_config_out(self.config_out)
.with_host(self.host)
.with_evm_emulator(if self.emulate_evm { Some(true) } else { None })
.with_health_check_endpoint(if self.health_check_endpoint {
Some(true)
Expand Down Expand Up @@ -346,3 +359,57 @@ impl Cli {
gen
}
}

#[cfg(test)]
mod tests {
use crate::config::cli::Cli;
use clap::Parser;
use std::{
env,
net::{IpAddr, Ipv4Addr},
};

#[test]
fn can_parse_host() {
// Test adapted from https://github.com/foundry-rs/foundry/blob/398ef4a3d55d8dd769ce86cada5ec845e805188b/crates/anvil/src/cmd.rs#L895
let args = Cli::parse_from(["era_test_node"]);
assert_eq!(args.host, vec![IpAddr::V4(Ipv4Addr::LOCALHOST)]);

let args = Cli::parse_from([
"era_test_node",
"--host",
"::1",
"--host",
"1.1.1.1",
"--host",
"2.2.2.2",
]);
assert_eq!(
args.host,
["::1", "1.1.1.1", "2.2.2.2"]
.map(|ip| ip.parse::<IpAddr>().unwrap())
.to_vec()
);

let args = Cli::parse_from(["era_test_node", "--host", "::1,1.1.1.1,2.2.2.2"]);
assert_eq!(
args.host,
["::1", "1.1.1.1", "2.2.2.2"]
.map(|ip| ip.parse::<IpAddr>().unwrap())
.to_vec()
);

env::set_var("ANVIL_ZKSYNC_IP_ADDR", "1.1.1.1");
let args = Cli::parse_from(["era_test_node"]);
assert_eq!(args.host, vec!["1.1.1.1".parse::<IpAddr>().unwrap()]);

env::set_var("ANVIL_ZKSYNC_IP_ADDR", "::1,1.1.1.1,2.2.2.2");
let args = Cli::parse_from(["era_test_node"]);
assert_eq!(
args.host,
["::1", "1.1.1.1", "2.2.2.2"]
.map(|ip| ip.parse::<IpAddr>().unwrap())
.to_vec()
);
}
}
27 changes: 22 additions & 5 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::net::{IpAddr, Ipv4Addr};

use crate::{observability, system_contracts};

use crate::config::{
Expand Down Expand Up @@ -99,6 +101,8 @@ pub struct TestNodeConfig {
pub signer_accounts: Vec<PrivateKeySigner>,
/// Whether the node operates in offline mode
pub offline: bool,
/// The host the server will listen on
pub host: Vec<IpAddr>,
/// Whether we need to enable the health check endpoint.
pub health_check_endpoint: bool,
}
Expand Down Expand Up @@ -145,6 +149,7 @@ impl Default for TestNodeConfig {

// Offline mode disabled by default
offline: false,
host: vec![IpAddr::V4(Ipv4Addr::LOCALHOST)],
health_check_endpoint: false,
}
}
Expand Down Expand Up @@ -300,11 +305,13 @@ impl TestNodeConfig {
);
println!("\n");
tracing::info!("========================================");
tracing::info!(
" Listening on {}:{}",
"127.0.0.1".green(),
self.port.to_string().green()
);
for host in &self.host {
tracing::info!(
" Listening on {}:{}",
host.to_string().green(),
self.port.to_string().green()
);
}
tracing::info!("========================================");
println!("\n");
}
Expand Down Expand Up @@ -635,6 +642,16 @@ impl TestNodeConfig {
self.offline
}

/// Sets the host the server will listen on
#[must_use]
pub fn with_host(mut self, host: Vec<IpAddr>) -> Self {
self.host = if host.is_empty() {
vec![IpAddr::V4(Ipv4Addr::LOCALHOST)]
} else {
host
};
self
}
/// Set the health check endpoint mode
#[must_use]
pub fn with_health_check_endpoint(mut self, health_check_endpoint: Option<bool>) -> Self {
Expand Down
23 changes: 11 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ mod utils;

use node::InMemoryNode;
use std::fs::File;
use std::{
env,
net::{IpAddr, Ipv4Addr, SocketAddr},
str::FromStr,
};
use std::{env, net::SocketAddr, str::FromStr};
use zksync_types::fee_model::{FeeModelConfigV2, FeeParams};
use zksync_web3_decl::namespaces::ZksNamespaceClient;

Expand Down Expand Up @@ -294,17 +290,20 @@ async fn main() -> anyhow::Result<()> {
node.set_rich_account(H160::from_str(address).unwrap(), config.genesis_balance);
}

let threads = build_json_http(
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), config.port),
log_level_filter,
node,
config.health_check_endpoint,
)
let threads = future::join_all(config.host.iter().map(|host| {
let addr = SocketAddr::new(*host, config.port);
build_json_http(
addr,
log_level_filter,
node.clone(),
config.health_check_endpoint,
)
}))
.await;

config.print(fork_print_info.as_ref());

future::select_all(vec![threads]).await.0.unwrap();
future::select_all(threads).await.0.unwrap();

Ok(())
}
Loading