Skip to content

Commit

Permalink
Make indexer properly resort to using gateway if chain isn't caught up (
Browse files Browse the repository at this point in the history
  • Loading branch information
phelpsdb committed May 8, 2024
1 parent 1e1d569 commit a0e2033
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions packages/discovery-provider/src/utils/web3_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import logging
import math
import os
from typing import Optional

Expand All @@ -17,6 +18,8 @@

web3: Optional[Web3] = None

GATEWAY_FALLBACK_BLOCKDIFF = 10000


def get_web3(web3endpoint=None):
# pylint: disable=W0603
Expand All @@ -25,21 +28,28 @@ def get_web3(web3endpoint=None):
if web3:
return web3

if not web3endpoint:
if web3endpoint:
web3 = Web3(HTTPProvider(web3endpoint))
else:
local_rpc = os.getenv("audius_web3_localhost")
local_web3 = Web3(HTTPProvider(local_rpc))
gateway_web3 = Web3(HTTPProvider(os.getenv("audius_web3_host")))
# attempt local rpc, check if healthy
try:
local_rpc = os.getenv("audius_web3_localhost")
if requests.get(local_rpc + "/health").status_code == 200:
web3endpoint = local_rpc
block_diff = math.abs(
local_web3.eth.get_block_number() - gateway_web3.eth.get_block_number()
)
resp = requests.get(local_rpc + "/health")
if resp.status_code == 200 and block_diff < GATEWAY_FALLBACK_BLOCKDIFF:
web3 = local_web3
logger.info("web3_provider.py | using local RPC")
else:
raise Exception("local RPC unhealthy or unreachable")
except Exception as e:
web3endpoint = os.getenv("audius_web3_host")
logger.warn(e)
web3 = Web3(HTTPProvider(web3endpoint))
web3.strict_bytes_type_checking = False
web3 = gateway_web3

web3.strict_bytes_type_checking = False
# required middleware for POA
# https://web3py.readthedocs.io/en/latest/middleware.html#proof-of-authority
web3.middleware_onion.inject(geth_poa_middleware, layer=0)
Expand Down

0 comments on commit a0e2033

Please sign in to comment.