Skip to content

Commit

Permalink
feat: serialize config for stateless transaction validator config (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware authored May 15, 2024
1 parent 603a539 commit f5821cf
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 24 deletions.
43 changes: 43 additions & 0 deletions crates/gateway/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParamPath, SerializedParam> {
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,
),
])
}
}
41 changes: 36 additions & 5 deletions crates/gateway/src/config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
6 changes: 2 additions & 4 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
5 changes: 2 additions & 3 deletions crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
11 changes: 1 addition & 10 deletions crates/gateway/src/stateless_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions crates/gateway/tests/routing_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down

0 comments on commit f5821cf

Please sign in to comment.