diff --git a/crates/mempool_node/src/config/config_test.rs b/crates/mempool_node/src/config/config_test.rs index 1e2640ac..f7bdae7c 100644 --- a/crates/mempool_node/src/config/config_test.rs +++ b/crates/mempool_node/src/config/config_test.rs @@ -1,77 +1,43 @@ #[cfg(any(feature = "testing", test))] -use std::fs::File; -use std::path::Path; - use assert_matches::assert_matches; -use papyrus_config::loading::load_and_process_config; -use papyrus_config::validators::ParsedValidationErrors; -use starknet_gateway::config::{ - GatewayNetworkConfig, RpcStateReaderConfig, StatefulTransactionValidatorConfig, - StatelessTransactionValidatorConfig, -}; +use papyrus_config::validators::{ParsedValidationError, ParsedValidationErrors}; use validator::Validate; -use crate::config::{ - node_command, ComponentConfig, ComponentExecutionConfig, GatewayConfig, MempoolNodeConfig, -}; - -const TEST_FILES_FOLDER: &str = "./src/test_files"; -const CONFIG_FILE: &str = "mempool_node_config.json"; - -fn get_config_file(file_name: &str) -> Result { - let config_file = File::open(Path::new(TEST_FILES_FOLDER).join(file_name)).unwrap(); - load_and_process_config::(config_file, node_command(), vec![]) -} +use crate::config::{ComponentConfig, ComponentExecutionConfig}; +/// Test the validation of the struct ComponentConfig. +/// The validation validates at least one of the components is set with execute: true. #[test] -fn test_valid_config() { - // Read the valid config file and validate its content. - let expected_config = MempoolNodeConfig { - components: ComponentConfig { - gateway_component: ComponentExecutionConfig { execute: true }, - mempool_component: ComponentExecutionConfig { execute: false }, - }, - gateway_config: GatewayConfig { - network_config: GatewayNetworkConfig { ip: "0.0.0.0".parse().unwrap(), port: 8080 }, - stateless_transaction_validator_config: StatelessTransactionValidatorConfig { - validate_non_zero_l1_gas_fee: true, - validate_non_zero_l2_gas_fee: false, - max_calldata_length: 10, - max_signature_length: 2, - }, - stateful_transaction_validator_config: - StatefulTransactionValidatorConfig::create_for_testing(), - }, - rpc_state_reader_config: RpcStateReaderConfig::default(), +fn test_components_config_validation() { + // Initialize an invalid config and check that the validator finds an error. + let mut component_config = ComponentConfig { + gateway_component: ComponentExecutionConfig { execute: false }, + mempool_component: ComponentExecutionConfig { execute: false }, }; - let loaded_config = get_config_file(CONFIG_FILE).unwrap(); - - assert!(loaded_config.validate().is_ok()); - assert_eq!(loaded_config, expected_config); -} - -#[test] -fn test_components_config() { - // Read the valid config file and check that the validator finds no errors. - let mut config = get_config_file(CONFIG_FILE).unwrap(); - assert!(config.validate().is_ok()); - // Invalidate the gateway component and check that the validator finds an error. - config.components.gateway_component.execute = false; - - assert_matches!(config.validate(), Err(e) => { - let parse_err = ParsedValidationErrors::from(e); - let mut error_msg = String::new(); - for error in parse_err.0 { - if error.param_path == "components.__all__" { - error_msg.push_str(&error.code); - break; - } - } - assert_eq!(error_msg, "Invalid components configuration."); + assert_matches!(component_config.validate().unwrap_err(), validation_errors => { + let parsed_errors = ParsedValidationErrors::from(validation_errors); + assert_eq!(parsed_errors.0.len(), 1); + let parsed_validation_error = &parsed_errors.0[0]; + assert_matches!( + parsed_validation_error, + ParsedValidationError { param_path, code, message, params} + if ( + param_path == "__all__" && + code == "Invalid components configuration." && + params.is_empty() && + *message == Some("At least one component should be allowed to execute.".to_string()) + ) + ) }); - // Validate the mempool component and check that the validator finds no errors. - config.components.mempool_component.execute = true; - assert!(config.validate().is_ok()); + // Update the config to be valid and check that the validator finds no errors. + for (gateway_component_execute, mempool_component_execute) in + [(true, false), (false, true), (true, true)] + { + component_config.gateway_component.execute = gateway_component_execute; + component_config.mempool_component.execute = mempool_component_execute; + + assert!(component_config.validate().is_ok()); + } } diff --git a/crates/mempool_node/src/test_files/mempool_node_config.json b/crates/mempool_node/src/test_files/mempool_node_config.json deleted file mode 100644 index ee2699e4..00000000 --- a/crates/mempool_node/src/test_files/mempool_node_config.json +++ /dev/null @@ -1,82 +0,0 @@ -{ - "components.gateway_component.execute": { - "description": "The component execution flag.", - "privacy": "Public", - "value": true - }, - "components.mempool_component.execute": { - "description": "The component execution flag.", - "privacy": "Public", - "value": false - }, - "gateway_config.network_config.ip": { - "description": "The gateway server ip.", - "privacy": "Public", - "value": "0.0.0.0" - }, - "gateway_config.network_config.port": { - "description": "The gateway server port.", - "privacy": "Public", - "value": 8080 - }, - "gateway_config.stateful_transaction_validator_config.chain_info.chain_id": { - "description": "The chain ID of the StarkNet chain.", - "privacy": "Public", - "value": "SN_GOERLI" - }, - "gateway_config.stateful_transaction_validator_config.chain_info.eth_fee_token_address": { - "description": "Address of the ETH fee token.", - "privacy": "Public", - "value": "0x1001" - }, - "gateway_config.stateful_transaction_validator_config.chain_info.strk_fee_token_address": { - "description": "Address of the STRK fee token.", - "privacy": "Public", - "value": "0x1002" - }, - "gateway_config.stateful_transaction_validator_config.max_nonce_for_validation_skip": { - "description": "The maximum nonce for which the validation is skipped.", - "privacy": "Public", - "value": "0x0" - }, - "gateway_config.stateful_transaction_validator_config.max_recursion_depth": { - "description": "The maximum recursion depth allowed in a transaction.", - "privacy": "Public", - "value": 50 - }, - "gateway_config.stateful_transaction_validator_config.validate_max_n_steps": { - "description": "The maximum number of steps the validation function is allowed to take.", - "privacy": "Public", - "value": 1000000 - }, - "gateway_config.stateless_transaction_validator_config.max_calldata_length": { - "description": "Validates that a transaction has signature length less than or equal to this value.", - "privacy": "Public", - "value": 10 - }, - "gateway_config.stateless_transaction_validator_config.max_signature_length": { - "description": "Validates that a transaction has calldata length less than or equal to this value.", - "privacy": "Public", - "value": 2 - }, - "gateway_config.stateless_transaction_validator_config.validate_non_zero_l1_gas_fee": { - "description": "If true, validates that a transaction has non-zero L1 resource bounds.", - "privacy": "Public", - "value": true - }, - "gateway_config.stateless_transaction_validator_config.validate_non_zero_l2_gas_fee": { - "description": "If true, validates that a transaction has non-zero L2 resource bounds.", - "privacy": "Public", - "value": false - }, - "rpc_state_reader_config.json_rpc_version": { - "description": "The json rpc version.", - "privacy": "Public", - "value": "" - }, - "rpc_state_reader_config.url": { - "description": "The url of the rpc server.", - "privacy": "Public", - "value": "" - } -} \ No newline at end of file