Skip to content

Commit

Permalink
extract RpcOptions from ServerOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l committed Mar 7, 2025
1 parent 5b83295 commit 85aa3e3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
15 changes: 10 additions & 5 deletions crates/katana/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ pub struct NodeArgs {
#[command(flatten)]
pub server: ServerOptions,

#[cfg(feature = "server")]
#[command(flatten)]
pub rpc: RpcOptions,

#[command(flatten)]
pub starknet: StarknetOptions,

Expand Down Expand Up @@ -223,7 +227,7 @@ impl NodeArgs {
fn rpc_config(&self) -> Result<RpcConfig> {
#[cfg(feature = "server")]
{
let modules = if let Some(modules) = &self.server.http_modules {
let modules = if let Some(modules) = &self.rpc.http_modules {
// TODO: This check should be handled in the `katana-node` level. Right now if you
// instantiate katana programmatically, you can still add the dev module without
// enabling dev mode.
Expand Down Expand Up @@ -271,13 +275,13 @@ impl NodeArgs {
apis: modules,
port: self.server.http_port,
addr: self.server.http_addr,
max_connections: self.server.max_connections,
max_connections: self.rpc.max_connections,
cors_origins,
max_request_body_size: None,
max_response_body_size: None,
max_event_page_size: Some(self.server.max_event_page_size),
max_proof_keys: Some(self.server.max_proof_keys),
max_call_gas: Some(self.server.max_call_gas),
max_event_page_size: Some(self.rpc.max_event_page_size),
max_proof_keys: Some(self.rpc.max_proof_keys),
max_call_gas: Some(self.rpc.max_call_gas),
})
}

Expand Down Expand Up @@ -426,6 +430,7 @@ impl NodeArgs {
#[cfg(feature = "server")]
{
self.server.merge(config.server.as_ref());
self.rpc.merge(config.rpc.as_ref());

if self.metrics == MetricsOptions::default() {
if let Some(metrics) = config.metrics {
Expand Down
3 changes: 3 additions & 0 deletions crates/katana/cli/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct NodeArgsConfig {
#[cfg(feature = "server")]
pub server: Option<ServerOptions>,
#[cfg(feature = "server")]
pub rpc: Option<RpcOptions>,
#[cfg(feature = "server")]
pub metrics: Option<MetricsOptions>,
}

Expand Down Expand Up @@ -67,6 +69,7 @@ impl TryFrom<NodeArgs> for NodeArgsConfig {
{
node_config.server =
if args.server == ServerOptions::default() { None } else { Some(args.server) };
node_config.rpc = if args.rpc == RpcOptions::default() { None } else { Some(args.rpc) };
node_config.metrics =
if args.metrics == MetricsOptions::default() { None } else { Some(args.metrics) };
}
Expand Down
53 changes: 37 additions & 16 deletions crates/katana/cli/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,42 @@ pub struct ServerOptions {
deserialize_with = "deserialize_cors_origins"
)]
pub http_cors_origins: Vec<HeaderValue>,
}

#[cfg(feature = "server")]
impl Default for ServerOptions {
fn default() -> Self {
ServerOptions {
http_addr: DEFAULT_RPC_ADDR,
http_port: DEFAULT_RPC_PORT,
http_cors_origins: Vec::new(),
}
}
}

#[cfg(feature = "server")]
impl ServerOptions {
pub fn merge(&mut self, other: Option<&Self>) {
if let Some(other) = other {
if self.http_addr == DEFAULT_RPC_ADDR {
self.http_addr = other.http_addr;
}
if self.http_port == DEFAULT_RPC_PORT {
self.http_port = other.http_port;
}
if self.http_cors_origins.is_empty() {
self.http_cors_origins = other.http_cors_origins.clone();
}
}
}
}

#[cfg(feature = "server")]
#[derive(Debug, Args, Clone, Serialize, Deserialize, PartialEq)]
#[command(next_help_heading = "Rpc options")]
pub struct RpcOptions {
/// API's offered over the HTTP-RPC interface.
#[arg(long = "http.api", value_name = "MODULES")]
#[arg(long = "rpc.api", value_name = "MODULES")]
#[arg(value_parser = RpcModulesList::parse)]
#[serde(default)]
pub http_modules: Option<RpcModulesList>,
Expand Down Expand Up @@ -138,12 +171,9 @@ pub struct ServerOptions {
}

#[cfg(feature = "server")]
impl Default for ServerOptions {
impl Default for RpcOptions {
fn default() -> Self {
ServerOptions {
http_addr: DEFAULT_RPC_ADDR,
http_port: DEFAULT_RPC_PORT,
http_cors_origins: Vec::new(),
RpcOptions {
http_modules: None,
max_event_page_size: DEFAULT_RPC_MAX_EVENT_PAGE_SIZE,
max_proof_keys: DEFAULT_RPC_MAX_PROOF_KEYS,
Expand All @@ -156,18 +186,9 @@ impl Default for ServerOptions {
}

#[cfg(feature = "server")]
impl ServerOptions {
impl RpcOptions {
pub fn merge(&mut self, other: Option<&Self>) {
if let Some(other) = other {
if self.http_addr == DEFAULT_RPC_ADDR {
self.http_addr = other.http_addr;
}
if self.http_port == DEFAULT_RPC_PORT {
self.http_port = other.http_port;
}
if self.http_cors_origins.is_empty() {
self.http_cors_origins = other.http_cors_origins.clone();
}
if self.http_modules.is_none() {
self.http_modules = other.http_modules.clone();
}
Expand Down

0 comments on commit 85aa3e3

Please sign in to comment.