|
1 | 1 | import decimal |
2 | 2 | import signal |
| 3 | +import time |
3 | 4 | from asyncio.exceptions import TimeoutError |
4 | 5 | from datetime import datetime, timezone |
5 | 6 | from enum import Enum |
6 | 7 | from typing import Union, Any, Callable, Set, Dict |
7 | 8 |
|
| 9 | + |
8 | 10 | from eth_typing.bls import BLSPubkey |
9 | 11 | from eth_typing.evm import HexAddress |
10 | 12 | from google.protobuf import empty_pb2 |
11 | | -from grpc import insecure_channel |
12 | | -from loguru import logger |
| 13 | +from grpc import insecure_channel, RpcError, StatusCode |
| 14 | +from loguru import logger, Logger |
13 | 15 | from notifiers.core import get_notifier # type: ignore |
14 | 16 | from web3 import Web3 |
15 | 17 | from web3.contract import Contract |
@@ -198,3 +200,34 @@ def get_pool_validator_public_keys(pool_contract: Contract) -> Set[BLSPubkey]: |
198 | 200 | pool_contract.web3.eth.uninstallFilter(event_filter.filter_id) |
199 | 201 |
|
200 | 202 | return set(event["args"]["publicKey"] for event in events) |
| 203 | + |
| 204 | + |
| 205 | +def wait_prysm_ready( |
| 206 | + interrupt_handler: InterruptHandler, |
| 207 | + endpoint: str, |
| 208 | + process_interval: int, |
| 209 | + logger: Logger, |
| 210 | +) -> None: |
| 211 | + """Wait that Prysm accepts requests and is synced. |
| 212 | +
|
| 213 | + Prysm RPC APIs return unavailable until Prysm is synced. |
| 214 | + """ |
| 215 | + while not interrupt_handler.exit: |
| 216 | + try: |
| 217 | + beacon_chain_stub = get_beacon_chain_stub(endpoint) |
| 218 | + |
| 219 | + # This will bomb with RPC error if Prysm is not ready |
| 220 | + get_chain_config(beacon_chain_stub) |
| 221 | + break |
| 222 | + except RpcError as e: |
| 223 | + code = e.code() |
| 224 | + if code == StatusCode.UNAVAILABLE: |
| 225 | + logger.warning( |
| 226 | + f"Could not connect to {endpoint} gRPC endpoint. " |
| 227 | + f"Maybe Prysm node is not synced? " |
| 228 | + f"Will keep trying every {process_interval} seconds." |
| 229 | + ) |
| 230 | + else: |
| 231 | + logger.warning("Unknown gRPC error connecting to Prysm: {e}") |
| 232 | + |
| 233 | + time.sleep(process_interval) |
0 commit comments