Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLAT-955: default to local RPC and fallback to gateway #5239

Merged
merged 5 commits into from
May 16, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 19 additions & 5 deletions discovery-provider/src/utils/web3_provider.py
Expand Up @@ -2,26 +2,39 @@
Interface for using a web3 provider
"""

import logging
import os
from typing import Optional

import requests
from src.utils.config import shared_config
from src.utils.multi_provider import MultiProvider
from web3 import HTTPProvider, Web3
from web3.middleware import geth_poa_middleware

logger = logging.getLogger(__name__)

web3: Optional[Web3] = None
LOCAL_RPC = "http://chain:8545" # TODO: Needs nethermind locally I think
LOCAL_RPC = "http://chain:8545"


def get_web3(web3endpoint=None):
# only use ACDC web3 provider when
# final_poa_block is indexed

# pylint: disable=W0603
global web3
if not web3endpoint:
web3endpoint = os.getenv("audius_web3_host")
# attempt local rpc, check if healthy
try:
r = requests.get(LOCAL_RPC + "/health")
if r.status_code == 200:
web3endpoint = LOCAL_RPC
logger.info("web3_provider.py | using local RPC")
else:
# if local rpc isn't healthy fall back to gateway
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be

try:
  if 200:
    # LOCAL_RPC
  else:
    raise Exception('local rpc unhealthy')
except Exception as e:
   logger.warn(e)
   web3endpoint = os...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nice! thanks

web3endpoint = os.getenv("audius_web3_host")
logger.warn("web3_provider.py | falling back to gateway RPC")
except:
web3endpoint = os.getenv("audius_web3_host")
logger.warn("web3_provider.py | exception, falling back to gateway RPC")
web3 = Web3(HTTPProvider(web3endpoint))

# required middleware for POA
Expand All @@ -33,6 +46,7 @@ def get_web3(web3endpoint=None):
eth_web3: Optional[Web3] = None


# mainnet eth, not ACDC
def get_eth_web3():
# pylint: disable=W0603
global eth_web3
Expand Down