From b4003fd260d72c94106554fd8883c1a3cf561d6f Mon Sep 17 00:00:00 2001 From: danb Date: Tue, 29 Aug 2023 13:13:21 -0400 Subject: [PATCH] pre-commit --- app.py | 56 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/app.py b/app.py index 6e5fc20..32323d2 100644 --- a/app.py +++ b/app.py @@ -31,13 +31,13 @@ def get_healthy_rpc_endpoints(rpc_endpoints): with ThreadPoolExecutor(max_workers=10) as executor: healthy_rpc_endpoints = [rpc for rpc, is_healthy in executor.map(lambda rpc: (rpc, is_endpoint_healthy(rpc['address'])), rpc_endpoints) if is_healthy] - + return healthy_rpc_endpoints[:5] # Select the first 5 healthy RPC endpoints def get_healthy_rest_endpoints(rest_endpoints): with ThreadPoolExecutor(max_workers=10) as executor: healthy_rest_endpoints = [rest for rest, is_healthy in executor.map(lambda rest: (rest, is_endpoint_healthy(rest['address'])), rest_endpoints) if is_healthy] - + return healthy_rest_endpoints[:5] # Select the first 5 healthy REST endpoints def is_endpoint_healthy(endpoint): @@ -49,14 +49,14 @@ def is_endpoint_healthy(endpoint): def get_healthy_endpoints(endpoints): healthy_endpoints = [] - + def check_endpoint(endpoint): if is_endpoint_healthy(endpoint['address']): healthy_endpoints.append(endpoint) - + with ThreadPoolExecutor(max_workers=10) as executor: executor.map(check_endpoint, endpoints) - + return healthy_endpoints def check_rest_endpoint(rest_url): @@ -66,7 +66,7 @@ def check_rest_endpoint(rest_url): response = requests.get(f"{rest_url}/node_info", timeout=3, verify=False) response.raise_for_status() elapsed_time = (datetime.now() - start_time).total_seconds() - + data = response.json() app_version = data.get('application_version', {}).get('version') return app_version, elapsed_time @@ -111,27 +111,27 @@ def fetch_endpoints(network, base_url): def fetch_active_upgrade_proposals(rest_url): try: response = requests.get(f"{rest_url}/cosmos/gov/v1beta1/proposals?proposal_status=2", verify=False) - + # Handle 501 Server Error if response.status_code == 501: return None, None - + response.raise_for_status() data = response.json() - + for proposal in data.get("proposals", []): content = proposal.get("content", {}) if content.get("@type") == "/cosmos.upgrade.v1beta1.SoftwareUpgradeProposal": # Extract version from the plan name plan_name = content.get("plan", {}).get("name", "") plan_version_match = SEMANTIC_VERSION_PATTERN.search(plan_name) - + # Extract version from the description description = content.get("description", "") description_version_match = SEMANTIC_VERSION_PATTERN.search(description) - + # Prioritize the longer semantic version - + if plan_version_match and description_version_match: version = plan_version_match.group(1) if len(plan_version_match.group(1)) > len(description_version_match.group(1)) else description_version_match.group(1) elif plan_version_match: @@ -140,12 +140,12 @@ def fetch_active_upgrade_proposals(rest_url): version = description_version_match.group(1) else: version = None - + try: height = int(content.get("plan", {}).get("height", 0)) except ValueError: height = 0 - + if version: return version, height return None, None @@ -161,7 +161,7 @@ def fetch_current_upgrade_plan(rest_url): response = requests.get(f"{rest_url}/cosmos/upgrade/v1beta1/current_plan", verify=False) response.raise_for_status() data = response.json() - + plan = data.get("plan", {}) if plan: version_match = SEMANTIC_VERSION_PATTERN.search(plan.get("name", "")) @@ -181,18 +181,18 @@ def fetch_current_upgrade_plan(rest_url): raise e def fetch_data_for_network(network, endpoints, network_type): - + print(f"Fetching data for network {network}") rest_endpoints = endpoints.get("rest", []) rpc_endpoints = endpoints.get("rpc", []) print(f"Found {len(rest_endpoints)} rest endpoints and {len(rpc_endpoints)} rpc endpoints") - + # Prioritize RPC endpoints for fetching the latest block height # Shuffle RPC endpoints to avoid calling the same one over and over latest_block_height = -1 healthy_rpc_endpoints = get_healthy_rpc_endpoints(rpc_endpoints) healthy_rest_endpoints = get_healthy_rest_endpoints(rest_endpoints) - + # Shuffle the healthy endpoints shuffle(healthy_rpc_endpoints) shuffle(healthy_rest_endpoints) @@ -203,17 +203,17 @@ def fetch_data_for_network(network, endpoints, network_type): if latest_block_height > 0: rpc_server_used = rpc_endpoint['address'] break - + # Check for active upgrade proposals # Shuffle RPC endpoints to avoid calling the same one over and over shuffle(healthy_rest_endpoints) upgrade_block_height = None upgrade_version = "" source = "" - + for index, rest_endpoint in enumerate(healthy_rest_endpoints): current_endpoint = rest_endpoint["address"] - + if current_endpoint in SERVER_BLACKLIST: continue try: @@ -238,7 +238,7 @@ def fetch_data_for_network(network, endpoints, network_type): upgrade_version = current_upgrade_version source = "current_upgrade_plan" break - + output_data = { "type": network_type, "network": network, @@ -257,9 +257,9 @@ def fetch_network_data(): request_data = request.get_json() if not request_data: return jsonify({"error": "Invalid payload"}), 400 - + results = [] - for network_type, base_url, networks in [("mainnet", BASE_URL, request_data.get("MAINNETS", [])), + for network_type, base_url, networks in [("mainnet", BASE_URL, request_data.get("MAINNETS", [])), ("testnet", BASE_URL + "/testnets", request_data.get("TESTNETS", []))]: endpoints_map = fetch_all_endpoints(network_type, base_url, request_data) for network in networks: @@ -268,15 +268,15 @@ def fetch_network_data(): results.append(network_data) except Exception as e: logging.error(f"Error fetching data for network {network}: {e}") - + # Sort the results by 'upgrade_found' in descending order (chain upgrades first) sorted_results = sorted(results, key=lambda x: x['upgrade_found'], reverse=True) - + return jsonify(sorted_results) - + except Exception as e: return jsonify({"error": str(e)}), 500 if __name__ == '__main__': - app.run(debug=True) \ No newline at end of file + app.run(debug=True)