Skip to content

Commit

Permalink
Compress health checks
Browse files Browse the repository at this point in the history
Signed-off-by: Dylan Schultz <[email protected]>
  • Loading branch information
dylanschultzie committed Jul 5, 2024
1 parent 8c8e837 commit 1c273d7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
File renamed without changes.
26 changes: 17 additions & 9 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 31 additions & 0 deletions app/solana/solana.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1c273d7

Please sign in to comment.