From c42e74beacf30d97b1ddfb3f493fef1105d45508 Mon Sep 17 00:00:00 2001 From: smypmsa Date: Mon, 10 Feb 2025 13:00:23 +0000 Subject: [PATCH 1/2] fix: update provider selection for state updates --- api/support/update_state.py | 47 +++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/api/support/update_state.py b/api/support/update_state.py index 2348ac7..c63c8fc 100644 --- a/api/support/update_state.py +++ b/api/support/update_state.py @@ -1,19 +1,29 @@ -"""State update handler for blockchain data collection.""" +"""State update handler for blockchain data collection with provider filtering.""" import asyncio import json import logging import os from http.server import BaseHTTPRequestHandler -from typing import Dict, Tuple +from typing import Dict, Set, Tuple from common.state.blob_storage import BlobConfig, BlobStorageHandler from common.state.blockchain_fetcher import BlockchainData, BlockchainDataFetcher SUPPORTED_BLOCKCHAINS = ["ethereum", "solana", "ton", "base"] +ALLOWED_PROVIDERS = {"Chainstack"} ALLOWED_REGIONS = {"fra1"} +class MissingEndpointsError(Exception): + """Raised when required blockchain endpoints are not found.""" + + def __init__(self, missing_chains: Set[str]): + self.missing_chains = missing_chains + chains = ", ".join(missing_chains) + super().__init__(f"Missing Chainstack endpoints for: {chains}") + + class StateUpdateManager: def __init__(self): store_id = os.getenv("STORE_ID") @@ -23,13 +33,26 @@ def __init__(self): self.blob_config = BlobConfig(store_id=store_id, token=token) # type: ignore - async def _fetch_provider_endpoints(self) -> Dict[str, str]: + async def _get_chainstack_endpoints(self) -> Dict[str, str]: + """Get Chainstack endpoints for supported blockchains.""" endpoints = json.loads(os.getenv("ENDPOINTS", "{}")) - return { - p["blockchain"].lower(): p["http_endpoint"] - for p in endpoints.get("providers", []) - if p["blockchain"].lower() in SUPPORTED_BLOCKCHAINS - } + chainstack_endpoints: Dict[str, str] = {} + missing_chains: Set[str] = set(SUPPORTED_BLOCKCHAINS) + + for provider in endpoints.get("providers", []): + blockchain = provider["blockchain"].lower() + if ( + blockchain in SUPPORTED_BLOCKCHAINS + and provider["name"] in ALLOWED_PROVIDERS + and blockchain not in chainstack_endpoints + ): + chainstack_endpoints[blockchain] = provider["http_endpoint"] + missing_chains.remove(blockchain) + + if missing_chains: + raise MissingEndpointsError(missing_chains) + + return chainstack_endpoints async def _collect_blockchain_data( self, providers: Dict[str, str] @@ -62,10 +85,7 @@ async def update(self) -> str: return "Region not authorized for state updates" try: - providers = await self._fetch_provider_endpoints() - if not providers: - return "No valid providers configured" - + providers = await self._get_chainstack_endpoints() blockchain_data = await self._collect_blockchain_data(providers) if not blockchain_data: return "No blockchain data collected" @@ -75,6 +95,9 @@ async def update(self) -> str: return "State updated successfully" + except MissingEndpointsError as e: + logging.error(f"Configuration error: {e}") + raise except Exception as e: logging.error(f"State update failed: {e}") raise From 5a578e3af05e8c3abb2181c8bd4b80b9859facc5 Mon Sep 17 00:00:00 2001 From: smypmsa Date: Mon, 10 Feb 2025 13:01:00 +0000 Subject: [PATCH 2/2] chore: adjust state update schedule to 20min --- vercel.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vercel.json b/vercel.json index 5e5b03a..fb9caba 100644 --- a/vercel.json +++ b/vercel.json @@ -42,7 +42,7 @@ }, { "path": "/api/support/update_state", - "schedule": "0 0 * * *" + "schedule": "*/20 * * * *" } ] } \ No newline at end of file