Skip to content

Commit

Permalink
Api comparator enhancement (#1912)
Browse files Browse the repository at this point in the history
* Update the comparator script

* Api Comparartor : Add url for protomainnet
  • Loading branch information
chetan-zilliqa authored Dec 11, 2024
1 parent e4596d7 commit 738b1ae
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
23 changes: 19 additions & 4 deletions scripts/zilliqa_api_comparator/config_mainnet.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"urls": {
"ZQ2": "http://34.124.218.159:4201/",
"ZQ2": "https://api.zq2-protomainnet.zilliqa.com",
"ZQ1": "http://34.124.185.137:4201/"
},
"headers": {
Expand All @@ -15,7 +15,8 @@
"zil1ad7krn8lkvm2g40eyu0f0er96t54je0ka7yldp",
"zil1jj9gmttszflvnze4jg6nd7d4vau4akd0xjwpkj",
"zil1gy44tg8tcyqplyc2h2xuzpcz9gazhfyyn8qsxf",
"zil1ascnfxmlzl4z3l6ecavxcae5hc37gdzr23enz0"
"zil1ascnfxmlzl4z3l6ecavxcae5hc37gdzr23enz0",
"zil1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq9yf6pz"
]
},
"eth_getBalance": {
Expand All @@ -39,6 +40,10 @@
[
"0xec31349b7f17ea28ff59c7586c7734be23e43443",
"latest"
],
[
"0x0000000000000000000000000000000000000000",
"latest"
]
]
}
Expand All @@ -53,7 +58,9 @@
"0xc504cbb9a8fecb52d6da9f23c236e72c96d20e368c221943b6090ab92581b911",
"e7bb4c68d06e784ce9c358212f532825cabd5d9549d080d70f63fcef6035de8e",
"0x4964bf50c8f598f6deedaed6c34cc5d739f8a9a8aecf8eb548206af8594e6c89",
"b95dd8837550bfdbcbb983a2860a2d0cd3eaad7a8902db51352ac8c178486ccf"
"b95dd8837550bfdbcbb983a2860a2d0cd3eaad7a8902db51352ac8c178486ccf",
"0x7b9808f32fc6ce4fa4d591a33d4d4346f30dd1c7c76c280502f3df3431d18c5f",
"0x165779aad8d325db02a4f2ef0aaedc177c9b659f9de881490551d8a56a715046"
]
},
"eth_call": {
Expand Down Expand Up @@ -194,6 +201,12 @@
]
]
},
"GetTxnBodiesForTxBlock": {
"params": [
"3277414",
"3277415"
]
},
"GetTxBlock": {
"params": [
"3277414",
Expand All @@ -214,7 +227,9 @@
},
"DSBlockListing": {
"params": [
[1]
[
1
]
]
},
"GetCurrentDSEpoch": {
Expand Down
36 changes: 24 additions & 12 deletions scripts/zilliqa_api_comparator/validate_zq1_zq2.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ def prompt_to_view_difference(mismatched_apis):
while True:
print("\nThe following APIs have mismatches:")
for i, api in enumerate(mismatched_apis):
print(f"{i + 1}. {api['method']} ({api['params'][0]})")
params = format_params(api["params"])
print(f"{i + 1}. {api['method']} ({params})")

choice = input("\nEnter the number of the API you'd like to view the difference for (or 0 to go back to method selection): ").strip()

Expand All @@ -310,7 +311,6 @@ def prompt_to_view_difference(mismatched_apis):
diff = compare_json_files(selected_api["local_file"], selected_api["zilliqa_file"])
display_key_differences(diff, selected_api["method"], selected_api["params"])

# Validate "y" or "n" input for viewing outputs
while True:
view_outputs = input(f"\nDo you want to view the outputs for {selected_api['method']} ({selected_api['params'][0]})? (y/n): ").strip().lower()
if view_outputs in {'y', 'n'}:
Expand All @@ -323,7 +323,6 @@ def prompt_to_view_difference(mismatched_apis):
print(f"\n ZQ1 Output ({selected_api['zilliqa_file']}):")
pretty_print_with_jq(selected_api["zilliqa_file"])

# Validate "y" or "n" input for continuing mismatches
while True:
more = input("\nDo you want to view another mismatch? (y/n): ").strip().lower()
if more in {'y', 'n'}:
Expand All @@ -345,14 +344,17 @@ def select_method_in_subset(subset):
print(f"\nAvailable methods in {subset.capitalize()}:")
for i, method in enumerate(methods, 1):
print(f"{i}. {method}")
print("0. Run all methods in this subset")
print("-1. Go back to Available API subsets")

method_choice = input(f"Select a method to run in {subset.capitalize()} (1-{len(methods)}) or press 0 to run all: ").strip()

if method_choice == "-1":
return None
elif method_choice == "0" or (method_choice.isdigit() and 1 <= int(method_choice) <= len(methods)):
method = methods[int(method_choice) - 1] if method_choice != "0" else None
return method
return None # User wants to go back to subset selection
elif method_choice == "0":
return "ALL" # New option to run all methods in the subset
elif method_choice.isdigit() and 1 <= int(method_choice) <= len(methods):
return methods[int(method_choice) - 1] # Return the selected method
else:
print(f"Invalid choice. Please enter a valid number (-1, 0-{len(methods)}).")

Expand Down Expand Up @@ -392,7 +394,9 @@ def main():
if method is None:
break

config, api_calls = load_config(subset=subset, method=method)
# Load configuration and API calls
config, api_calls = load_config(subset=subset, method=None if method == "ALL" else method)

headers = config["headers"]
results = []
mismatched_apis = []
Expand All @@ -402,25 +406,31 @@ def main():
params = api_call["params"]
output_file_prefix = api_call["output_file_prefix"]

local_file = f"{output_file_prefix}_ZQ2.json"
print(f"Making {method} API call to ZQ2 API - {config['urls']['ZQ2']} with param {params[0]}")
# Truncate parameters for logging
truncated_params = format_params(params)

# Make API calls to ZQ2
local_file = f"{output_file_prefix}_ZQ2.json"
print(f"Making {method} API call to ZQ2 API - {config['urls']['ZQ2']} with param {truncated_params}")
local_response_time, _ = make_api_call(
config["urls"]["ZQ2"], headers, method, params, f"{output_file_prefix}_ZQ2.txt", local_file
)

# Make API calls to ZQ1
zilliqa_file = f"{output_file_prefix}_ZQ1.json"
print(f"Making {method} API call to ZQ1 API - {config['urls']['ZQ1']} with param {params[0]}")
print(f"Making {method} API call to ZQ1 API - {config['urls']['ZQ1']} with param {truncated_params}")
zilliqa_response_time, _ = make_api_call(
config["urls"]["ZQ1"], headers, method, params, f"{output_file_prefix}_ZQ1.txt", zilliqa_file
)

# Compare results
diff = compare_json_files(local_file, zilliqa_file)
success = not bool(diff)
local_time_str, zilliqa_time_str = format_time(local_response_time, zilliqa_response_time)

# Append results
results.append([
f"{method} ({format_params(params)})",
f"{method} ({truncated_params})",
config["urls"]["ZQ2"],
config["urls"]["ZQ1"],
local_time_str,
Expand All @@ -438,9 +448,11 @@ def main():
"zilliqa_file": zilliqa_file
})

# Print results in a table
print("\nResults:")
print(tabulate(results, headers=["API Method", "ZQ2 URL", "ZQ1 URL", "ZQ2 Time", "ZQ1 Time", "Status", "ZQ2 File", "ZQ1 File"]))
prompt_to_view_difference(mismatched_apis)


if __name__ == "__main__":
main()

0 comments on commit 738b1ae

Please sign in to comment.