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(katana): configurable max gas for rpc call #3043

Merged
merged 3 commits into from
Feb 17, 2025
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
1 change: 1 addition & 0 deletions crates/katana/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ impl NodeArgs {
cors_origins: self.server.http_cors_origins.clone(),
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),
})
}

Expand Down
14 changes: 13 additions & 1 deletion crates/katana/cli/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use katana_node::config::rpc::{RpcModulesList, DEFAULT_RPC_MAX_PROOF_KEYS};
#[cfg(feature = "server")]
use katana_node::config::rpc::{
DEFAULT_RPC_ADDR, DEFAULT_RPC_MAX_EVENT_PAGE_SIZE, DEFAULT_RPC_PORT,
DEFAULT_RPC_ADDR, DEFAULT_RPC_MAX_CALL_GAS, DEFAULT_RPC_MAX_EVENT_PAGE_SIZE, DEFAULT_RPC_PORT,
};
use katana_primitives::block::BlockHashOrNumber;
use katana_primitives::chain::ChainId;
Expand Down Expand Up @@ -128,6 +128,12 @@
#[arg(default_value_t = DEFAULT_RPC_MAX_PROOF_KEYS)]
#[serde(default = "default_proof_keys")]
pub max_proof_keys: u64,

/// Maximum gas for the `starknet_call` RPC method.
#[arg(long = "rpc.max-call-gas", value_name = "GAS")]
#[arg(default_value_t = DEFAULT_RPC_MAX_CALL_GAS)]
#[serde(default = "default_max_call_gas")]
pub max_call_gas: u64,

Check warning on line 136 in crates/katana/cli/src/options.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/cli/src/options.rs#L136

Added line #L136 was not covered by tests
}

#[cfg(feature = "server")]
Expand All @@ -143,6 +149,7 @@
max_connections: None,
max_request_body_size: None,
max_response_body_size: None,
max_call_gas: DEFAULT_RPC_MAX_CALL_GAS,
}
}
}
Expand Down Expand Up @@ -410,3 +417,8 @@
fn default_metrics_port() -> u16 {
DEFAULT_METRICS_PORT
}

#[cfg(feature = "server")]
fn default_max_call_gas() -> u64 {
DEFAULT_RPC_MAX_CALL_GAS
}

Check warning on line 424 in crates/katana/cli/src/options.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/cli/src/options.rs#L422-L424

Added lines #L422 - L424 were not covered by tests
21 changes: 18 additions & 3 deletions crates/katana/executor/src/implementation/blockifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ pub struct BlockifierFactory {
cfg: CfgEnv,
flags: ExecutionFlags,
limits: BlockLimits,
max_call_gas: u64,
}

impl BlockifierFactory {
/// Create a new factory with the given configuration and simulation flags.
pub fn new(cfg: CfgEnv, flags: ExecutionFlags, limits: BlockLimits) -> Self {
Self { cfg, flags, limits }
Self { cfg, flags, limits, max_call_gas: 1_000_000_000 }
}

pub fn set_max_call_gas(&mut self, max_call_gas: u64) {
self.max_call_gas = max_call_gas;
}
}

Expand All @@ -72,7 +77,14 @@ impl ExecutorFactory for BlockifierFactory {
let cfg_env = self.cfg.clone();
let flags = self.flags.clone();
let limits = self.limits.clone();
Box::new(StarknetVMProcessor::new(Box::new(state), block_env, cfg_env, flags, limits))
Box::new(StarknetVMProcessor::new(
Box::new(state),
block_env,
cfg_env,
flags,
limits,
self.max_call_gas,
))
}

fn cfg(&self) -> &CfgEnv {
Expand All @@ -93,6 +105,7 @@ pub struct StarknetVMProcessor<'a> {
simulation_flags: ExecutionFlags,
stats: ExecutionStats,
bouncer: Bouncer,
max_call_gas: u64,
}

impl<'a> StarknetVMProcessor<'a> {
Expand All @@ -102,6 +115,7 @@ impl<'a> StarknetVMProcessor<'a> {
cfg_env: CfgEnv,
simulation_flags: ExecutionFlags,
limits: BlockLimits,
max_call_gas: u64,
) -> Self {
let transactions = Vec::new();
let block_context = utils::block_context_from_envs(&block_env, &cfg_env);
Expand All @@ -118,6 +132,7 @@ impl<'a> StarknetVMProcessor<'a> {
simulation_flags,
stats: Default::default(),
bouncer,
max_call_gas,
}
}

Expand Down Expand Up @@ -329,7 +344,7 @@ impl ExecutorExt for StarknetVMProcessor<'_> {
let block_context = &self.block_context;
let mut state = self.state.inner.lock();
let state = MutRefState::new(&mut state.cached_state);
let retdata = call::execute_call(call, state, block_context, 1_000_000_000)?;
let retdata = call::execute_call(call, state, block_context, self.max_call_gas)?;
Ok(retdata)
}
}
11 changes: 11 additions & 0 deletions crates/katana/node-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
http_addr: Option<SocketAddr>,
http_port: Option<u16>,
rpc_max_connections: Option<u64>,
rpc_max_call_gas: Option<u64>,
http_cors_domain: Option<String>,

// Dev options
Expand Down Expand Up @@ -326,6 +327,12 @@
self
}

/// Sets the maximum gas for the `starknet_call` RPC method.
pub const fn rpc_max_call_gas(mut self, max_call_gas: u64) -> Self {
self.rpc_max_call_gas = Some(max_call_gas);
self
}

Check warning on line 334 in crates/katana/node-bindings/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/node-bindings/src/lib.rs#L331-L334

Added lines #L331 - L334 were not covered by tests

/// Enables the CORS layer and sets the allowed origins, separated by commas.
pub fn http_cors_domain<T: Into<String>>(mut self, allowed_origins: T) -> Self {
self.http_cors_domain = Some(allowed_origins.into());
Expand Down Expand Up @@ -525,6 +532,10 @@
cmd.arg("--rpc.max-connections").arg(max_connections.to_string());
}

if let Some(max_call_gas) = self.rpc_max_call_gas {
cmd.arg("--rpc.max-call-gas").arg(max_call_gas.to_string());

Check warning on line 536 in crates/katana/node-bindings/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/node-bindings/src/lib.rs#L536

Added line #L536 was not covered by tests
}

if let Some(allowed_origins) = self.http_cors_domain {
cmd.arg("--http.corsdomain").arg(allowed_origins);
}
Expand Down
4 changes: 4 additions & 0 deletions crates/katana/node/src/config/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub const DEFAULT_RPC_PORT: u16 = 5050;
pub const DEFAULT_RPC_MAX_EVENT_PAGE_SIZE: u64 = 1024;
/// Default maximmum number of keys for the `starknet_getStorageProof` RPC method.
pub const DEFAULT_RPC_MAX_PROOF_KEYS: u64 = 100;
/// Default maximum gas for the `starknet_call` RPC method.
pub const DEFAULT_RPC_MAX_CALL_GAS: u64 = 1_000_000_000;

/// List of RPC modules supported by Katana.
#[derive(
Expand Down Expand Up @@ -45,6 +47,7 @@ pub struct RpcConfig {
pub max_response_body_size: Option<u32>,
pub max_proof_keys: Option<u64>,
pub max_event_page_size: Option<u64>,
pub max_call_gas: Option<u64>,
}

impl RpcConfig {
Expand All @@ -66,6 +69,7 @@ impl Default for RpcConfig {
apis: RpcModulesList::default(),
max_event_page_size: Some(DEFAULT_RPC_MAX_EVENT_PAGE_SIZE),
max_proof_keys: Some(DEFAULT_RPC_MAX_PROOF_KEYS),
max_call_gas: Some(DEFAULT_RPC_MAX_CALL_GAS),
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions crates/katana/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,16 @@ pub async fn build(mut config: Config) -> Result<Node> {
.with_account_validation(config.dev.account_validation)
.with_fee(config.dev.fee);

let executor_factory = Arc::new(BlockifierFactory::new(
cfg_env,
execution_flags,
config.sequencing.block_limits(),
));
let executor_factory = {
let mut factory =
BlockifierFactory::new(cfg_env, execution_flags, config.sequencing.block_limits());

if let Some(max_call_gas) = config.rpc.max_call_gas {
factory.set_max_call_gas(max_call_gas);
}

Arc::new(factory)
};

// --- build backend

Expand Down
Loading