diff --git a/.env.example b/.env.example index e4e1722..691791b 100644 --- a/.env.example +++ b/.env.example @@ -9,3 +9,5 @@ ETHERSCAN_KEY= API_KEY= # Port to run the simulator on, defaults to 8080 PORT= +# Maximum size for incoming requests (in KB), defaults to 16 +MAX_REQUEST_SIZE= diff --git a/src/config.rs b/src/config.rs index 07597cc..884359d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,6 +6,7 @@ pub struct Config { pub fork_url: Option, pub etherscan_key: Option, pub api_key: Option, + pub max_request_size: u64, } pub fn config() -> Config { @@ -24,12 +25,18 @@ fn load_config() -> Config { .ok() .filter(|k| !k.is_empty()); let api_key = std::env::var("API_KEY").ok().filter(|k| !k.is_empty()); + let max_request_size = std::env::var("MAX_REQUEST_SIZE") + .unwrap_or("16".to_string()) + .parse::() + .expect("MAX_REQUEST_SIZE must be a valid u64") + * 1024; Config { fork_url, port, etherscan_key, api_key, + max_request_size, } } diff --git a/src/lib.rs b/src/lib.rs index ffc37b7..80a3725 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,16 +25,16 @@ pub fn simulate_routes( ) -> impl Filter + Clone { simulate(config.clone()) .or(simulate_bundle(config.clone())) - .or(simulate_stateful_new(config, state.clone())) + .or(simulate_stateful_new(config.clone(), state.clone())) .or(simulate_stateful_end(state.clone())) - .or(simulate_stateful(state)) + .or(simulate_stateful(config, state)) } /// POST /simulate pub fn simulate(config: Config) -> impl Filter + Clone { warp::path!("simulate") .and(warp::post()) - .and(json_body::()) + .and(json_body::(&config)) .and(with_config(config)) .and_then(simulation::simulate) } @@ -45,7 +45,7 @@ pub fn simulate_bundle( ) -> impl Filter + Clone { warp::path!("simulate-bundle") .and(warp::post()) - .and(json_body()) + .and(json_body(&config)) .and(with_config(config)) .and_then(simulation::simulate_bundle) } @@ -57,7 +57,7 @@ pub fn simulate_stateful_new( ) -> impl Filter + Clone { warp::path!("simulate-stateful") .and(warp::post()) - .and(json_body::()) + .and(json_body::(&config)) .and(with_config(config)) .and(with_state(state)) .and_then(simulation::simulate_stateful_new) @@ -75,11 +75,12 @@ pub fn simulate_stateful_end( /// POST /simulate-stateful/{statefulSimulationId} pub fn simulate_stateful( + config: Config, state: Arc, ) -> impl Filter + Clone { warp::path!("simulate-stateful" / Uuid) .and(warp::post()) - .and(json_body()) + .and(json_body(&config)) .and(with_state(state)) .and_then(simulation::simulate_stateful) } @@ -97,7 +98,8 @@ fn with_state( warp::any().map(move || state.clone()) } -fn json_body() -> impl Filter + Clone -{ - warp::body::content_length_limit(1024 * 16).and(warp::body::json()) +fn json_body( + config: &Config, +) -> impl Filter + Clone { + warp::body::content_length_limit(config.max_request_size).and(warp::body::json()) }