From f5821cf19cc277ee32949bc5aa16399c3cca9c18 Mon Sep 17 00:00:00 2001 From: Arnon Hod Date: Wed, 15 May 2024 17:07:14 +0300 Subject: [PATCH] feat: serialize config for stateless transaction validator config (#126) --- crates/gateway/src/config.rs | 43 +++++++++++++++++++ crates/gateway/src/config_test.rs | 41 +++++++++++++++--- crates/gateway/src/gateway.rs | 6 +-- crates/gateway/src/gateway_test.rs | 5 +-- ...tateless_transaction_validator_config.json | 22 ++++++++++ .../src/stateless_transaction_validator.rs | 11 +---- crates/gateway/tests/routing_test.rs | 3 +- 7 files changed, 107 insertions(+), 24 deletions(-) create mode 100644 crates/gateway/src/json_files_for_testing/stateless_transaction_validator_config.json diff --git a/crates/gateway/src/config.rs b/crates/gateway/src/config.rs index 5af92d32..3f3d5373 100644 --- a/crates/gateway/src/config.rs +++ b/crates/gateway/src/config.rs @@ -32,3 +32,46 @@ impl Default for GatewayNetworkConfig { Self { ip: "0.0.0.0".parse().unwrap(), port: 8080 } } } + +#[derive(Clone, Debug, Default, Serialize, Deserialize, Validate, PartialEq)] +pub struct StatelessTransactionValidatorConfig { + // If true, validates that the resource bounds are not zero. + pub validate_non_zero_l1_gas_fee: bool, + pub validate_non_zero_l2_gas_fee: bool, + + pub max_calldata_length: usize, + pub max_signature_length: usize, +} + +impl SerializeConfig for StatelessTransactionValidatorConfig { + fn dump(&self) -> BTreeMap { + BTreeMap::from_iter([ + ser_param( + "validate_non_zero_l1_gas_fee", + &self.validate_non_zero_l1_gas_fee, + "If true, validates that a transaction has non-zero L1 resource bounds.", + ParamPrivacyInput::Public, + ), + ser_param( + "validate_non_zero_l2_gas_fee", + &self.validate_non_zero_l2_gas_fee, + "If true, validates that a transaction has non-zero L2 resource bounds.", + ParamPrivacyInput::Public, + ), + ser_param( + "max_signature_length", + &self.max_signature_length, + "Validates that a transaction has calldata length less than or equal to this \ + value.", + ParamPrivacyInput::Public, + ), + ser_param( + "max_calldata_length", + &self.max_calldata_length, + "Validates that a transaction has signature length less than or equal to this \ + value.", + ParamPrivacyInput::Public, + ), + ]) + } +} diff --git a/crates/gateway/src/config_test.rs b/crates/gateway/src/config_test.rs index 973a8950..5d4cf5f5 100644 --- a/crates/gateway/src/config_test.rs +++ b/crates/gateway/src/config_test.rs @@ -8,10 +8,11 @@ use papyrus_config::loading::load_and_process_config; use serde::Deserialize; use validator::Validate; -use crate::config::GatewayNetworkConfig; +use crate::config::{GatewayNetworkConfig, StatelessTransactionValidatorConfig}; const TEST_FILES_FOLDER: &str = "./src/json_files_for_testing"; const NETWORK_CONFIG_FILE: &str = "gateway_network_config.json"; +const STATELESS_TRANSACTION_VALIDATOR_CONFIG: &str = "stateless_transaction_validator_config.json"; fn get_config_file_path(file_name: &str) -> PathBuf { Path::new(TEST_FILES_FOLDER).join(file_name) @@ -42,8 +43,8 @@ fn test_valid_config_body< } #[test] -/// Read the valid config file and validate its content. -fn test_valid_config() { +/// Read the network config file and validate its content. +fn test_valid_network_config() { let expected_config = GatewayNetworkConfig { ip: "0.0.0.0".parse().unwrap(), port: 8080 }; let file_path = get_config_file_path(NETWORK_CONFIG_FILE); let fix = false; @@ -53,10 +54,40 @@ fn test_valid_config() { // TODO(Arni, 7/5/2024): Dedup code with test_valid_config. #[test] #[ignore] -/// Fix the config file for test_valid_config. Run with 'cargo test -- --ignored'. -fn fix_test_valid_config() { +/// Fix the config file for test_valid_network_config. Run with 'cargo test -- --ignored'. +fn fix_test_valid_network_config() { let expected_config = GatewayNetworkConfig { ip: "0.0.0.0".parse().unwrap(), port: 8080 }; let file_path = get_config_file_path(NETWORK_CONFIG_FILE); let fix = true; test_valid_config_body(expected_config, file_path, fix); } + +#[test] +/// Read the stateless transaction validator config file and validate its content. +fn test_valid_stateless_transaction_validator_config() { + let expected_config = StatelessTransactionValidatorConfig { + validate_non_zero_l1_gas_fee: true, + validate_non_zero_l2_gas_fee: false, + max_calldata_length: 10, + max_signature_length: 0, + }; + let file_path = get_config_file_path(STATELESS_TRANSACTION_VALIDATOR_CONFIG); + let fix = false; + test_valid_config_body(expected_config, file_path, fix); +} + +#[test] +#[ignore] +/// Fix the config file for test_valid_stateless_transaction_validator_config. +/// Run with 'cargo test -- --ignored'. +fn fix_test_valid_stateless_transaction_validator_config() { + let expected_config = StatelessTransactionValidatorConfig { + validate_non_zero_l1_gas_fee: true, + validate_non_zero_l2_gas_fee: false, + max_calldata_length: 10, + max_signature_length: 0, + }; + let file_path = get_config_file_path(STATELESS_TRANSACTION_VALIDATOR_CONFIG); + let fix = true; + test_valid_config_body(expected_config, file_path, fix); +} diff --git a/crates/gateway/src/gateway.rs b/crates/gateway/src/gateway.rs index 4e1e4fa0..661aadc7 100644 --- a/crates/gateway/src/gateway.rs +++ b/crates/gateway/src/gateway.rs @@ -7,11 +7,9 @@ use axum::{Json, Router}; use starknet_api::external_transaction::ExternalTransaction; use starknet_mempool_types::mempool_types::GatewayNetworkComponent; -use crate::config::GatewayNetworkConfig; +use crate::config::{GatewayNetworkConfig, StatelessTransactionValidatorConfig}; use crate::errors::GatewayError; -use crate::stateless_transaction_validator::{ - StatelessTransactionValidator, StatelessTransactionValidatorConfig, -}; +use crate::stateless_transaction_validator::StatelessTransactionValidator; #[cfg(test)] #[path = "gateway_test.rs"] diff --git a/crates/gateway/src/gateway_test.rs b/crates/gateway/src/gateway_test.rs index 987e1ed1..14bb2fc5 100644 --- a/crates/gateway/src/gateway_test.rs +++ b/crates/gateway/src/gateway_test.rs @@ -14,10 +14,9 @@ use starknet_mempool_types::mempool_types::{ }; use tokio::sync::mpsc::channel; +use crate::config::StatelessTransactionValidatorConfig; use crate::gateway::{async_add_transaction, AppState}; -use crate::stateless_transaction_validator::{ - StatelessTransactionValidator, StatelessTransactionValidatorConfig, -}; +use crate::stateless_transaction_validator::StatelessTransactionValidator; const TEST_FILES_FOLDER: &str = "./tests/fixtures"; diff --git a/crates/gateway/src/json_files_for_testing/stateless_transaction_validator_config.json b/crates/gateway/src/json_files_for_testing/stateless_transaction_validator_config.json new file mode 100644 index 00000000..b727d6a0 --- /dev/null +++ b/crates/gateway/src/json_files_for_testing/stateless_transaction_validator_config.json @@ -0,0 +1,22 @@ +{ + "max_calldata_length": { + "description": "Validates that a transaction has signature length less than or equal to this value.", + "privacy": "Public", + "value": 10 + }, + "max_signature_length": { + "description": "Validates that a transaction has calldata length less than or equal to this value.", + "privacy": "Public", + "value": 0 + }, + "validate_non_zero_l1_gas_fee": { + "description": "If true, validates that a transaction has non-zero L1 resource bounds.", + "privacy": "Public", + "value": true + }, + "validate_non_zero_l2_gas_fee": { + "description": "If true, validates that a transaction has non-zero L2 resource bounds.", + "privacy": "Public", + "value": false + } +} \ No newline at end of file diff --git a/crates/gateway/src/stateless_transaction_validator.rs b/crates/gateway/src/stateless_transaction_validator.rs index a4625a4a..a5b49690 100644 --- a/crates/gateway/src/stateless_transaction_validator.rs +++ b/crates/gateway/src/stateless_transaction_validator.rs @@ -3,22 +3,13 @@ use starknet_api::external_transaction::{ }; use starknet_api::transaction::{Resource, ResourceBoundsMapping}; +use crate::config::StatelessTransactionValidatorConfig; use crate::errors::{StatelessTransactionValidatorError, StatelessTransactionValidatorResult}; #[cfg(test)] #[path = "stateless_transaction_validator_test.rs"] mod stateless_transaction_validator_test; -#[derive(Clone, Default)] -pub struct StatelessTransactionValidatorConfig { - // If true, validates that the resource bounds are not zero. - pub validate_non_zero_l1_gas_fee: bool, - pub validate_non_zero_l2_gas_fee: bool, - - pub max_calldata_length: usize, - pub max_signature_length: usize, -} - #[derive(Clone)] pub struct StatelessTransactionValidator { pub config: StatelessTransactionValidatorConfig, diff --git a/crates/gateway/tests/routing_test.rs b/crates/gateway/tests/routing_test.rs index 72bfe589..4f3629ea 100644 --- a/crates/gateway/tests/routing_test.rs +++ b/crates/gateway/tests/routing_test.rs @@ -6,9 +6,8 @@ use axum::body::{Body, Bytes, HttpBody}; use axum::http::{Request, StatusCode}; use pretty_assertions::assert_str_eq; use rstest::{fixture, rstest}; -use starknet_gateway::config::GatewayNetworkConfig; +use starknet_gateway::config::{GatewayNetworkConfig, StatelessTransactionValidatorConfig}; use starknet_gateway::gateway::Gateway; -use starknet_gateway::stateless_transaction_validator::StatelessTransactionValidatorConfig; use starknet_mempool_types::mempool_types::{ GatewayNetworkComponent, GatewayToMempoolMessage, MempoolToGatewayMessage, };