From c90974118e6ed0865863978e4a72c9e0e2532da4 Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 3 Jun 2025 13:23:27 +0800 Subject: [PATCH] fix: make modify_key_value more efficient --- .../test_data_confidentiality.py | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/tests/data_confidentiality/test_data_confidentiality.py b/tests/data_confidentiality/test_data_confidentiality.py index f3453ca..05d5726 100644 --- a/tests/data_confidentiality/test_data_confidentiality.py +++ b/tests/data_confidentiality/test_data_confidentiality.py @@ -16,27 +16,28 @@ from src.test_data import DATA_TO_DISPERSE logger = get_custom_logger(__name__) -def modify_key_value(file_path, yaml_key_path): +def modify_key_value(file_path, yaml_key_paths): yaml = YAML() yaml.preserve_quotes = True with open(file_path, "r") as f: data = yaml.load(f) - keys = yaml_key_path.split(".") - ref = data - for key in keys[:-1]: - if key not in ref: - raise KeyError(f"Key '{key}' not found in path '{'.'.join(keys)}'") - ref = ref[key] + for yaml_key_path in yaml_key_paths: + keys = yaml_key_path.split(".") + ref = data + for key in keys[:-1]: + if key not in ref: + raise KeyError(f"Key '{key}' not found in path '{'.'.join(keys)}'") + ref = ref[key] - final_key = keys[-1] - if final_key not in ref: - raise KeyError(f"Key '{final_key}' not found in path '{'.'.join(keys)}'") + final_key = keys[-1] + if final_key not in ref: + raise KeyError(f"Key '{final_key}' not found in path '{'.'.join(keys)}'") - old_value = ref[final_key] - # Swap last two characters - ref[final_key] = old_value[:-2] + old_value[-1] + old_value[-2] + old_value = ref[final_key] + # Swap last two characters + ref[final_key] = old_value[:-2] + old_value[-1] + old_value[-2] with open(file_path, "w") as f: yaml.dump(data, f) @@ -73,8 +74,8 @@ class TestDataConfidentiality(StepsDataAvailability): self.node2.stop() # Change the private key -> PeerId of the nomos_node_0. This would create a stranger to existing membership list. - for yaml_key_path in ["network.backend.node_key", "blend.backend.node_key", "da_network.backend.node_key"]: - modify_key_value("./cluster_config/config.yaml", yaml_key_path) + yaml_key_paths = ["network.backend.node_key", "blend.backend.node_key", "da_network.backend.node_key"] + modify_key_value("./cluster_config/config.yaml", yaml_key_paths) # Start new node with the same hostname and configuration as first node self.nodeX = NomosNode(NOMOS_CUSTOM, "nomos_node_0")