Expands RPC health check logical branching to handle detailed failure cases; prepare for logging integration.

pull/3496/head
KPrasch 2024-05-14 13:17:56 +02:00
parent 6509b2b3d7
commit faba9a10b9
No known key found for this signature in database
1 changed files with 27 additions and 11 deletions

View File

@ -1,6 +1,6 @@
import time
from decimal import Decimal
from typing import List, Union, Dict
from typing import Dict, List, Union
import requests
from eth_typing import ChecksumAddress
@ -86,19 +86,35 @@ def rpc_endpoint_health_check(endpoint: str, max_drift_seconds: int = 60) -> boo
headers={"Content-Type": "application/json"},
timeout=5
)
if response.status_code == 200:
data = response.json()
if "result" in data and data["result"] is not None:
block_data = data["result"]
timestamp = int(block_data.get("timestamp"), 16)
system_time = time.time()
drift = abs(system_time - timestamp)
if drift < max_drift_seconds:
return True
return False
except requests.exceptions.RequestException:
return False
if response.status_code != 200:
return False
try:
data = response.json()
if "result" not in data:
return False
except requests.exceptions.RequestException:
return False
if data["result"] is None:
return False
block_data = data["result"]
try:
timestamp = int(block_data.get("timestamp"), 16)
except TypeError:
return False
system_time = time.time()
drift = abs(system_time - timestamp)
if drift > max_drift_seconds:
return False
return True # finally!
def get_default_rpc_endpoints() -> Dict[int, List[str]]:
"""