fix: make modify_key_value more efficient

This commit is contained in:
Roman 2025-06-03 13:23:27 +08:00
parent 0c8a153c55
commit c90974118e
No known key found for this signature in database
GPG Key ID: 583BDF43C238B83E

View File

@ -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")