Skip to content

Commit

Permalink
feat: add cli options for server configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Romsters committed Dec 11, 2024
1 parent 0903206 commit b8f57c0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions SUPPORTED_APIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The `status` options are:
| `ANVIL` | `anvil_setBalance` | `SUPPORTED` | Modifies the balance of an account |
| `ANVIL` | `anvil_setCode` | `SUPPORTED` | Sets the bytecode of a given account |
| `ANVIL` | `anvil_setStorageAt` | `SUPPORTED` | Sets the storage value at a given key for a given account |
| `ANVIL` | `anvil_setChainId` | `SUPPORTED` | Sets the chain id |
| [`CONFIG`](#config-namespace) | [`config_getShowCalls`](#config_getshowcalls) | `SUPPORTED` | Gets the current value of `show_calls` that's originally set with `--show-calls` option |
| [`CONFIG`](#config-namespace) | [`config_getShowOutputs`](#config_getshowoutputs) | `SUPPORTED` | Gets the current value of `show_outputs` that's originally set with `--show-outputs` option |
| [`CONFIG`](#config-namespace) | [`config_getCurrentTimestamp`](#config_getcurrenttimestamp) | `SUPPORTED` | Gets the value of `current_timestamp` for the node |
Expand Down
12 changes: 11 additions & 1 deletion crates/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ pub struct Cli {
/// Disable auto and interval mining, and mine on demand instead.
#[arg(long, visible_alias = "no-mine", conflicts_with = "block_time")]
pub no_mining: bool,

/// The cors `allow_origin` header
#[arg(long, default_value = "*", help_heading = "Server options")]
pub allow_origin: String,

/// Disable CORS.
#[arg(long, default_missing_value = "true", num_args(0..=1), conflicts_with = "allow_origin", help_heading = "Server options")]
pub no_cors: Option<bool>,
}

#[derive(Debug, Subcommand, Clone)]
Expand Down Expand Up @@ -381,7 +389,9 @@ impl Cli {
None
})
.with_block_time(self.block_time)
.with_no_mining(self.no_mining);
.with_no_mining(self.no_mining)
.with_allow_origin(self.allow_origin)
.with_no_cors(self.no_cors);

if self.emulate_evm && self.dev_system_contracts != Some(SystemContractsOptions::Local) {
return Err(eyre::eyre!(
Expand Down
9 changes: 8 additions & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use futures::{
FutureExt,
};
use jsonrpc_core::MetaIoHandler;
use jsonrpc_http_server::DomainsValidation;
use logging_middleware::LoggingMiddleware;
use std::fs::File;
use std::{env, net::SocketAddr, str::FromStr};
Expand All @@ -50,6 +51,8 @@ async fn build_json_http<
log_level_filter: LevelFilter,
node: InMemoryNode<S>,
enable_health_api: bool,
cors_allow_origin: String,
disable_cors: bool,
) -> tokio::task::JoinHandle<()> {
let (sender, recv) = oneshot::channel::<()>();

Expand All @@ -76,9 +79,11 @@ async fn build_json_http<
.build()

Check warning on line 79 in crates/cli/src/main.rs

View workflow job for this annotation

GitHub Actions / lint

Diff in /home/runner/work/anvil-zksync/anvil-zksync/crates/cli/src/main.rs
.unwrap();

let allow_origin = if disable_cors { "null" } else { &cors_allow_origin };
let mut builder = jsonrpc_http_server::ServerBuilder::new(io_handler)
.threads(1)
.event_loop_executor(runtime.handle().clone());
.event_loop_executor(runtime.handle().clone())
.cors(DomainsValidation::AllowOnly(vec![allow_origin.into()]));

if enable_health_api {
builder = builder.health_api(("/health", "web3_clientVersion"));
Expand Down Expand Up @@ -324,6 +329,8 @@ async fn main() -> anyhow::Result<()> {
log_level_filter,
node.clone(),
config.health_check_endpoint,
config.allow_origin.clone(),
config.no_cors,
)
}))
.await;
Expand Down
24 changes: 24 additions & 0 deletions crates/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ pub struct TestNodeConfig {
pub max_transactions: usize,
/// Disable automatic sealing mode and use `BlockSealer::Noop` instead
pub no_mining: bool,
/// The cors `allow_origin` header
pub allow_origin: String,
/// Disable CORS if true
pub no_cors: bool,
}

impl Default for TestNodeConfig {
Expand Down Expand Up @@ -171,6 +175,10 @@ impl Default for TestNodeConfig {
no_mining: false,

max_transactions: 1000,

// Server configuration
allow_origin: "*".to_string(),
no_cors: false,
}
}
}
Expand Down Expand Up @@ -869,4 +877,20 @@ impl TestNodeConfig {
self.no_mining = no_mining;
self
}

// Set allow_origin CORS header
#[must_use]
pub fn with_allow_origin(mut self, allow_origin: String) -> Self {
self.allow_origin = allow_origin;
self
}

// Enable or disable CORS
#[must_use]
pub fn with_no_cors(mut self, no_cors: Option<bool>) -> Self {
if let Some(no_cors) = no_cors {
self.no_cors = no_cors;
}
self
}
}

0 comments on commit b8f57c0

Please sign in to comment.