From bc817376a343af01e629d740eb75c374832dfaea Mon Sep 17 00:00:00 2001 From: Itay-Tsabary-Starkware <106665835+Itay-Tsabary-Starkware@users.noreply.github.com> Date: Wed, 1 Jan 2025 12:09:18 +0200 Subject: [PATCH] chore(starknet_integration_tests): cleaner required params handling in test (#3046) commit-id:ba366206 --- .../src/config_utils.rs | 52 ++----------------- crates/starknet_sequencer_node/Cargo.toml | 3 +- .../src/config/test_utils.rs | 7 +++ 3 files changed, 14 insertions(+), 48 deletions(-) diff --git a/crates/starknet_integration_tests/src/config_utils.rs b/crates/starknet_integration_tests/src/config_utils.rs index c4386833c8..9ce1ccb13f 100644 --- a/crates/starknet_integration_tests/src/config_utils.rs +++ b/crates/starknet_integration_tests/src/config_utils.rs @@ -3,7 +3,7 @@ use std::io::Write; use std::path::PathBuf; use papyrus_config::dumping::{combine_config_map_and_pointers, SerializeConfig}; -use serde_json::{json, Map, Value}; +use serde_json::{Map, Value}; use starknet_sequencer_node::config::node_config::{ SequencerNodeConfig, CONFIG_NON_POINTERS_WHITELIST, @@ -16,46 +16,12 @@ use tracing::info; const NODE_CONFIG_CHANGES_FILE_PATH: &str = "node_integration_test_config_changes.json"; -/// A utility macro that takes a list of config fields and returns a json dictionary with "field -/// name : field value" entries, where prefixed "config." name is removed from the entry key. -/// -/// # Example (not running, to avoid function visibility modifications): -/// -/// use serde_json::json; -/// struct ConfigStruct { -/// field_1: u32, -/// field_2: String, -/// field_3: u32, -/// } -/// let config = ConfigStruct { field_1: 1, field_2: "2".to_string() , field_3: 3}; -/// let json_data = config_fields_to_json!(config.field_1, config.field_2); -/// assert_eq!(json_data, json!({"field_1": 1, "field_2": "2"})); -macro_rules! config_fields_to_json { - ( $( $expr:expr ),+ , ) => { - json!({ - $( - strip_config_prefix(stringify!($expr)): $expr - ),+ - }) - }; -} - /// Creates a config file for the sequencer node for an integration test. pub(crate) fn dump_config_file_changes( config: &SequencerNodeConfig, required_params: RequiredParams, dir: PathBuf, ) -> PathBuf { - // Dump config changes file for the sequencer node. - // TODO(Tsabary): auto dump the entirety of RequiredParams fields. - let required_params_json = config_fields_to_json!( - required_params.chain_id, - required_params.eth_fee_token_address, - required_params.strk_fee_token_address, - required_params.validator_id, - required_params.recorder_url, - ); - // Create the entire mapping of the config and the pointers, without the required params. let config_as_map = combine_config_map_and_pointers( config.dump(), @@ -68,7 +34,7 @@ pub(crate) fn dump_config_file_changes( let mut preset = config_to_preset(&config_as_map); // Add the required params to the preset. - add_required_params_to_preset(&mut preset, &required_params_json); + add_required_params_to_preset(&mut preset, required_params.as_json()); // Dump the preset to a file, return its path. let node_config_path = dump_json_data(preset, NODE_CONFIG_CHANGES_FILE_PATH, dir); @@ -90,14 +56,6 @@ fn dump_json_data(json_data: Value, path: &str, dir: PathBuf) -> PathBuf { temp_dir_path } -/// Strips the "config." and "required_params." prefixes from the input string. -fn strip_config_prefix(input: &str) -> &str { - input - .strip_prefix("config.") - .or_else(|| input.strip_prefix("required_params.")) - .unwrap_or(input) -} - /// Transforms a nested JSON dictionary object into a simplified JSON dictionary object by /// extracting specific values from the inner dictionaries. /// @@ -154,17 +112,17 @@ fn config_to_preset(config_map: &Value) -> Value { /// # Panics /// This function panics if either `preset` or `required_params` is not a JSON dictionary object, or /// if the `preset` already contains a key from the `required_params`. -fn add_required_params_to_preset(preset: &mut Value, required_params: &Value) { +fn add_required_params_to_preset(preset: &mut Value, required_params: Value) { if let (Value::Object(preset_map), Value::Object(required_params_map)) = (preset, required_params) { for (key, value) in required_params_map { assert!( - !preset_map.contains_key(key), + !preset_map.contains_key(&key), "Required parameter already exists in the preset: {:?}", key ); - preset_map.insert(key.clone(), value.clone()); + preset_map.insert(key, value); } } else { panic!("Expecting JSON object dictionary objects"); diff --git a/crates/starknet_sequencer_node/Cargo.toml b/crates/starknet_sequencer_node/Cargo.toml index fc9feeaef0..59d82c0cd8 100644 --- a/crates/starknet_sequencer_node/Cargo.toml +++ b/crates/starknet_sequencer_node/Cargo.toml @@ -6,7 +6,7 @@ repository.workspace = true license.workspace = true [features] -testing = ["url"] +testing = ["dep:serde_json", "dep:url"] [lints] workspace = true @@ -22,6 +22,7 @@ papyrus_proc_macros.workspace = true papyrus_protobuf.workspace = true rstest.workspace = true serde.workspace = true +serde_json = { workspace = true, optional = true } starknet_api.workspace = true starknet_batcher.workspace = true starknet_batcher_types.workspace = true diff --git a/crates/starknet_sequencer_node/src/config/test_utils.rs b/crates/starknet_sequencer_node/src/config/test_utils.rs index 6c351f4f83..c3d727d0b8 100644 --- a/crates/starknet_sequencer_node/src/config/test_utils.rs +++ b/crates/starknet_sequencer_node/src/config/test_utils.rs @@ -2,6 +2,8 @@ use std::vec::Vec; // Used by #[gen_field_names_fn]. use papyrus_proc_macros::gen_field_names_and_cli_args_fn; use papyrus_protobuf::consensus::DEFAULT_VALIDATOR_ID; +use serde::Serialize; +use serde_json::{to_value, Value}; use starknet_api::core::{ChainId, ContractAddress}; use url::Url; @@ -9,6 +11,7 @@ use crate::config::node_config::node_command; /// Required parameters utility struct. #[gen_field_names_and_cli_args_fn] +#[derive(Serialize)] pub struct RequiredParams { pub chain_id: ChainId, pub eth_fee_token_address: ContractAddress, @@ -27,6 +30,10 @@ impl RequiredParams { recorder_url: Url::parse("https://recorder_url").expect("The URL is valid"), } } + + pub fn as_json(&self) -> Value { + to_value(self).unwrap() + } } // Creates a vector of strings with the command name and required parameters that can be used as