Skip to content

Commit 0ef70dd

Browse files
authored
Merge pull request #9 from miohtama/feat/wait-prysm
Wait until Prysm is ready on startup
2 parents b5b8cb2 + badcdb4 commit 0ef70dd

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
MAX_TX_WAIT_SECONDS,
2121
LOG_LEVEL,
2222
PROCESS_INTERVAL,
23+
BEACON_CHAIN_RPC_ENDPOINT,
2324
)
2425
from src.utils import (
2526
get_web3_client,
2627
configure_default_account,
2728
InterruptHandler,
2829
check_default_account_balance,
30+
wait_prysm_ready,
2931
)
3032

3133
# Send notification to admins on error
@@ -61,6 +63,11 @@ def main() -> None:
6163
# wait for interrupt
6264
interrupt_handler = InterruptHandler()
6365

66+
# wait that node is synced before trying to do anything
67+
wait_prysm_ready(
68+
interrupt_handler, BEACON_CHAIN_RPC_ENDPOINT, PROCESS_INTERVAL, logger
69+
)
70+
6471
reward_token_total_rewards = RewardToken(
6572
w3=web3_client, interrupt_handler=interrupt_handler
6673
)

src/utils.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import decimal
22
import signal
3+
import time
34
from asyncio.exceptions import TimeoutError
45
from datetime import datetime, timezone
56
from enum import Enum
67
from typing import Union, Any, Callable, Set, Dict
78

9+
810
from eth_typing.bls import BLSPubkey
911
from eth_typing.evm import HexAddress
1012
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
1315
from notifiers.core import get_notifier # type: ignore
1416
from web3 import Web3
1517
from web3.contract import Contract
@@ -198,3 +200,34 @@ def get_pool_validator_public_keys(pool_contract: Contract) -> Set[BLSPubkey]:
198200
pool_contract.web3.eth.uninstallFilter(event_filter.filter_id)
199201

200202
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

Comments
 (0)