Skip to content

Commit

Permalink
chore(starknet_sequencer_node): move config_to_preset util (#3060)
Browse files Browse the repository at this point in the history
commit-id:f3b90442
  • Loading branch information
Itay-Tsabary-Starkware authored Jan 2, 2025
1 parent 4ce974e commit 2a57cdf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
41 changes: 2 additions & 39 deletions crates/starknet_integration_tests/src/config_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::io::Write;
use std::path::PathBuf;

use papyrus_config::dumping::{combine_config_map_and_pointers, SerializeConfig};
use serde_json::{Map, Value};
use serde_json::Value;
use starknet_sequencer_node::config::node_config::{
SequencerNodeConfig,
CONFIG_NON_POINTERS_WHITELIST,
CONFIG_POINTERS,
};
use starknet_sequencer_node::config::test_utils::RequiredParams;
use starknet_sequencer_node::config::test_utils::{config_to_preset, RequiredParams};
use tracing::info;

// TODO(Tsabary): Move here all config-related functions from "integration_test_utils.rs".
Expand Down Expand Up @@ -56,43 +56,6 @@ fn dump_json_data(json_data: Value, path: &str, dir: PathBuf) -> PathBuf {
temp_dir_path
}

/// Transforms a nested JSON dictionary object into a simplified JSON dictionary object by
/// extracting specific values from the inner dictionaries.
///
/// # Parameters
/// - `config_map`: A reference to a `serde_json::Value` that must be a JSON dictionary object. Each
/// key in the object maps to another JSON dictionary object.
///
/// # Returns
/// - A `serde_json::Value` dictionary object where:
/// - Each key is preserved from the top-level dictionary.
/// - Each value corresponds to the `"value"` field of the nested JSON dictionary under the
/// original key.
///
/// # Panics
/// This function panics if the provided `config_map` is not a JSON dictionary object.
fn config_to_preset(config_map: &Value) -> Value {
// Ensure the config_map is a JSON object.
if let Value::Object(map) = config_map {
let mut result = Map::new();

for (key, value) in map {
if let Value::Object(inner_map) = value {
// Extract the value.
if let Some(inner_value) = inner_map.get("value") {
// Add it to the result map
result.insert(key.clone(), inner_value.clone());
}
}
}

// Return the transformed result as a JSON object.
Value::Object(result)
} else {
panic!("Config map is not a JSON object: {:?}", config_map);
}
}

/// Merges required parameters into an existing preset JSON object.
///
/// # Parameters
Expand Down
40 changes: 38 additions & 2 deletions crates/starknet_sequencer_node/src/config/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::collections::HashSet;
use std::vec::Vec; // Used by #[gen_field_names_fn].

use papyrus_protobuf::consensus::DEFAULT_VALIDATOR_ID;
use serde::Serialize;
use serde_json::{to_value, Value};
use serde_json::{to_value, Map, Value};
use starknet_api::core::{ChainId, ContractAddress};
use url::Url;

Expand Down Expand Up @@ -64,3 +63,40 @@ pub fn create_test_config_load_args(required_params: RequiredParams) -> Vec<Stri
cli_args.extend(required_params.cli_args());
cli_args
}

/// Transforms a nested JSON dictionary object into a simplified JSON dictionary object by
/// extracting specific values from the inner dictionaries.
///
/// # Parameters
/// - `config_map`: A reference to a `serde_json::Value` that must be a JSON dictionary object. Each
/// key in the object maps to another JSON dictionary object.
///
/// # Returns
/// - A `serde_json::Value` dictionary object where:
/// - Each key is preserved from the top-level dictionary.
/// - Each value corresponds to the `"value"` field of the nested JSON dictionary under the
/// original key.
///
/// # Panics
/// This function panics if the provided `config_map` is not a JSON dictionary object.
pub fn config_to_preset(config_map: &Value) -> Value {
// Ensure the config_map is a JSON object.
if let Value::Object(map) = config_map {
let mut result = Map::new();

for (key, value) in map {
if let Value::Object(inner_map) = value {
// Extract the value.
if let Some(inner_value) = inner_map.get("value") {
// Add it to the result map
result.insert(key.clone(), inner_value.clone());
}
}
}

// Return the transformed result as a JSON object.
Value::Object(result)
} else {
panic!("Config map is not a JSON object: {:?}", config_map);
}
}

0 comments on commit 2a57cdf

Please sign in to comment.