From 1c273d76acb5077895b209d7335ab60d76a51b51 Mon Sep 17 00:00:00 2001 From: Dylan Schultz <9121234+dylanschultzie@users.noreply.github.com> Date: Fri, 5 Jul 2024 15:51:30 -0700 Subject: [PATCH] Compress health checks Signed-off-by: Dylan Schultz <9121234+dylanschultzie@users.noreply.github.com> --- app/{ethereum => evm}/ethereum.py | 0 app/routes.py | 26 +++++++++++++++++--------- app/solana/solana.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 9 deletions(-) rename app/{ethereum => evm}/ethereum.py (100%) create mode 100644 app/solana/solana.py diff --git a/app/ethereum/ethereum.py b/app/evm/ethereum.py similarity index 100% rename from app/ethereum/ethereum.py rename to app/evm/ethereum.py diff --git a/app/routes.py b/app/routes.py index be89d8e..d8fb881 100644 --- a/app/routes.py +++ b/app/routes.py @@ -2,20 +2,28 @@ from flask import Blueprint, request from app.common import parse_request from app.cosmos.cosmos import cosmos_health -from app.ethereum.ethereum import ethereum_health +from app.evm.ethereum import ethereum_health +from app.solana.solana import solana_health api = Blueprint("api", __name__) +health_check_functions = { + "45": ethereum_health, + "57": cosmos_health, + "17": cosmos_health, + "90": cosmos_health, + "99": solana_health, +} -@api.route("/ethereum", methods=["GET"]) -def ethereum_health_check(): +@api.route("/health", methods=["GET"]) +def health_check(): ip, port = parse_request(request) - message, status = ethereum_health(ip, port) - return message, status + rpc_type = port[:2] + health_check_function = health_check_functions.get(rpc_type) + if health_check_function: + message, status = health_check_function(ip, port) + else: + message, status = "Unknown RPC type", 400 -@api.route("/cosmos", methods=["GET"]) -def cosmos_health_check(): - ip, port = parse_request(request) - message, status = cosmos_health(ip, port) return message, status diff --git a/app/solana/solana.py b/app/solana/solana.py new file mode 100644 index 0000000..8d10757 --- /dev/null +++ b/app/solana/solana.py @@ -0,0 +1,31 @@ +from datetime import datetime, timedelta, timezone +import requests + + +def solana_health(ip: str, port: str, acceptable_time_delta: int = 60): + acceptable_time_delta = timedelta(seconds=acceptable_time_delta) + + # Get latest block data + payload = {"jsonrpc": "2.0", "method": "eth_getBlockByNumber", "params": ["latest", False], "id": 1} + headers = {"Content-Type": "application/json"} + url = f"http://{ip}:{port}" + response = requests.post(url, headers=headers, json=payload) + + if response.status_code != 200: + return "Error occurred while fetching latest block data", 500 + + latest_block_data = response.json() + + # Check sync status + if "result" not in latest_block_data: + return "Sync status check failed", 500 + + current_dt = datetime.now(timezone.utc) + latest_block_time = int(latest_block_data["result"]["timestamp"], 16) + latest_dt = datetime.fromtimestamp(latest_block_time, timezone.utc) + + block_time_delta = current_dt - latest_dt + + if latest_dt is None or block_time_delta > acceptable_time_delta: + return "Block time delta exceeded acceptable limit", 500 + return "OK", 200