Use python for scripts

This commit is contained in:
Gusto 2023-08-15 02:35:38 +03:00
parent 99ec9cec32
commit f6ae008e40
6 changed files with 104 additions and 81 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
__pycache__/
*.py[cod]
*$py.class
*.so
simulation

34
scripts/build_cases.py Normal file
View File

@ -0,0 +1,34 @@
import csv
import shutil
from build_config import build_config
def build_case(overlay, committees, nodes, config_name, max_view=1, network='default'):
build_config(overlay, committees, nodes, config_name, max_view, network)
shutil.move(f"{config_name}.json", "../configs/")
def build_cases(csv_path):
with open(csv_path, 'r') as csv_file:
reader = csv.reader(csv_file)
for row in reader:
overlay_type, node_count, committees = row
if overlay_type == "overlay":
continue
config_name = f"{overlay_type}_{node_count}_{committees}"
build_case(overlay_type, committees, node_count, f"{config_name}_view_1_default")
build_case(overlay_type, committees, node_count, f"{config_name}_view_10_default", max_view="10")
build_case(overlay_type, committees, node_count, f"{config_name}_view_10_optimistic", max_view="10", network="optimistic")
build_case(overlay_type, committees, node_count, f"{config_name}_view_10_pessimistic", max_view="10", network="pessimistic")
if __name__ == "__main__":
import sys
if len(sys.argv) != 2:
print("Usage: python generate_configs.py <path_to_csv_file>")
sys.exit(1)
csv_path = sys.argv[1]
build_cases(csv_path)

View File

@ -1,22 +0,0 @@
#!/bin/bash
# Check if the input file is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <path_to_csv_file>"
exit 1
fi
# Read the CSV file line by line
while IFS=, read -r overlay_type node_count committees; do
if [[ "$overlay_type" == "overlay" ]]; then
continue
fi
CONFIG_NAME="${overlay_type}_${node_count}_${committees}"
./build_config.sh "$overlay_type" "$committees" "$node_count" $CONFIG_NAME"_view_1_default"
./build_config.sh "$overlay_type" "$committees" "$node_count" $CONFIG_NAME"_view_10_default" 10
./build_config.sh "$overlay_type" "$committees" "$node_count" $CONFIG_NAME"_view_10_optimistic" 10 optimistic
./build_config.sh "$overlay_type" "$committees" "$node_count" $CONFIG_NAME"_view_10_pessimistic" 10 pessimistic
mv *.json ../configs/
done < $1

54
scripts/build_config.py Normal file
View File

@ -0,0 +1,54 @@
import sys
import os
import json
TEMPLATE_PATH = "config_builder/template.json"
TEMPORARY_PATH = "config_builder/temp.json"
NETWORK_UPDATE_PATH = "config_builder/network"
RECORD_UPDATE_PATH = "config_builder/record.json"
def build_config(overlay_type, number_of_committees, node_count, config_name, max_view=1, network='default'):
with open(TEMPLATE_PATH, 'r') as f:
data = json.load(f)
network_file = f"{NETWORK_UPDATE_PATH}/network_{network}.json"
with open(network_file, 'r') as f:
network_update = json.load(f)
data["network_settings"] = network_update["network_settings"]
with open(RECORD_UPDATE_PATH, 'r') as f:
record_update = json.load(f)
data["record_settings"] = record_update["record_settings"]
data["node_count"] = int(node_count)
data["stream_settings"]["path"] = f"output/{config_name}.json"
data["wards"][0]["max_view"] = int(max_view)
if overlay_type == "tree":
data["overlay_settings"]["number_of_committees"] = int(number_of_committees)
elif overlay_type == "branch":
data["overlay_settings"]["branch_depth"] = int(number_of_committees)
else:
print("Unknown overlay type. Supported types are 'tree' and 'branch'.")
return
with open(f"{config_name}.json", 'w') as f:
json.dump(data, f, indent=4)
print(f"Configuration built and saved as {config_name}.json")
if __name__ == "__main__":
if len(sys.argv) < 5:
print("Usage: python config_builder.py <overlay_type> <number_of_committees> <node_count> <config_name> [max_view] [network_config]")
sys.exit(1)
overlay_type = sys.argv[1]
number_of_committees = sys.argv[2]
node_count = sys.argv[3]
config_name = sys.argv[4]
max_view = sys.argv[5] if len(sys.argv) > 5 else 1
network_config = sys.argv[6] if len(sys.argv) > 6 else 'default'
build_config(overlay_type, number_of_committees, node_count, config_name, max_view, network_config)

View File

@ -1,57 +0,0 @@
#!/bin/bash
if [ "$#" -lt 4 ]; then
echo "Usage: $0 <overlay_type> <number_of_committees> <node_count> <config_name> [max_view] [network_config]"
exit 1
fi
if ! command -v jq &> /dev/null; then
echo "jq is not installed."
exit 1
fi
if ! command -v sponge &> /dev/null; then
echo "moreutils is not installed."
exit 1
fi
OVERLAY_TYPE=$1
NUMBER_OF_COMMITTEES=$2
NODE_COUNT=$3
CONFIG_NAME=$4
MAX_VIEW=${5:-1}
NETWORK_CONFIG=${6:-default}
TEMPLATE_PATH="config_builder/template.json"
TEMPORARY_PATH="config_builder/temp.json"
NETWORK_UPDATE_PATH="config_builder/network/network_$NETWORK_CONFIG.json"
RECORD_UPDATE_PATH="config_builder/record.json"
# Update the template with the network settings
jq --slurpfile networkUpdate $NETWORK_UPDATE_PATH \
'.network_settings = $networkUpdate[0].network_settings' $TEMPLATE_PATH | sponge $TEMPORARY_PATH
# Update the template with the record settings
jq --slurpfile recordUpdate $RECORD_UPDATE_PATH \
'.record_settings = $recordUpdate[0].record_settings' $TEMPORARY_PATH | sponge $TEMPORARY_PATH
# Update new JSON with the command line arguments
jq --arg numCommit "$NUMBER_OF_COMMITTEES" \
--arg nodeCount "$NODE_COUNT" \
--arg confName "$CONFIG_NAME" \
--arg maxView "$MAX_VIEW" \
'.node_count = ($nodeCount | tonumber) | .stream_settings.path = "output/" + $confName + ".json" | .wards[0].max_view = ($maxView | tonumber)' $TEMPORARY_PATH | sponge $TEMPORARY_PATH
if [ "$OVERLAY_TYPE" == "tree" ]; then
jq --arg numCommit "$NUMBER_OF_COMMITTEES" \
'.overlay_settings.number_of_committees = ($numCommit | tonumber)' $TEMPORARY_PATH | sponge $TEMPORARY_PATH
elif [ "$OVERLAY_TYPE" == "branch" ]; then
cat $TEMPORARY_PATH | jq --arg numCommit "$NUMBER_OF_COMMITTEES" \
'.overlay_settings.branch_depth = ($numCommit | tonumber)' $TEMPORARY_PATH | sponge $TEMPORARY_PATH
else
echo "Unknown overlay type. Supported types are 'tree' and 'branch'."
exit 1
fi
mv $TEMPORARY_PATH "$CONFIG_NAME.json"
echo "Configuration built and saved as $CONFIG_NAME.json"

View File

@ -1,6 +1,15 @@
{ {
"record_settings": { "record_settings": {
"node_id": true, "current_view": true,
"current_view": true "highest_voted_view": true,
"local_high_qc": true,
"safe_blocks": false,
"last_view_timeout_qc": true,
"latest_committed_block": true,
"latest_committed_view": true,
"root_committee": false,
"parent_committee": false,
"child_committees": false,
"committed_blocks": false
} }
} }