introduces default RPC endpoint health check utilities, and plant an initial entrypoint at node startup.

pull/3496/head
Kieran 2024-05-07 13:44:01 +02:00 committed by KPrasch
parent 2ac0aba53f
commit bd1962a58c
No known key found for this signature in database
3 changed files with 46 additions and 3 deletions

View File

@ -49,7 +49,7 @@ from nucypher.blockchain.eth.registry import ContractRegistry
from nucypher.blockchain.eth.signers import Signer
from nucypher.blockchain.eth.trackers import dkg
from nucypher.blockchain.eth.trackers.bonding import OperatorBondedTracker
from nucypher.blockchain.eth.utils import truncate_checksum_address
from nucypher.blockchain.eth.utils import truncate_checksum_address, get_healthy_default_rpc_endpoints
from nucypher.crypto.powers import (
CryptoPower,
RitualisticPower,
@ -265,6 +265,7 @@ class Operator(BaseActor):
provider = HTTPProvider(endpoint_uri=uri)
return provider
def connect_condition_providers(
self, endpoints: Dict[int, List[str]]
) -> DefaultDict[int, Set[HTTPProvider]]:
@ -293,6 +294,13 @@ class Operator(BaseActor):
)
providers[int(chain_id)].add(provider)
# Ingest default RPC providers for each chain
for chain_id in self.domain.condition_chain_ids:
default_endpoints = get_healthy_default_rpc_endpoints(chain_id)
for uri in default_endpoints:
provider = self._make_condition_provider(uri)
providers[chain_id].add(provider)
humanized_chain_ids = ", ".join(
_CONDITION_CHAINS[chain_id] for chain_id in providers
)

View File

@ -10,7 +10,7 @@ from web3.contract.contract import Contract
from web3.exceptions import TimeExhausted, TransactionNotFound
from web3.types import TxReceipt, Wei
from nucypher.blockchain.eth.constants import AVERAGE_BLOCK_TIME_IN_SECONDS
from nucypher.blockchain.eth.constants import AVERAGE_BLOCK_TIME_IN_SECONDS, PUBLIC_CHAINS
from nucypher.blockchain.middleware.retry import (
AlchemyRetryRequestMiddleware,
InfuraRetryRequestMiddleware,

View File

@ -1,6 +1,8 @@
from collections import defaultdict
from decimal import Decimal
from typing import Union
from typing import Union, Dict, List
import requests
from eth_typing import ChecksumAddress
from web3 import Web3
from web3.contract.contract import ContractConstructor, ContractFunction
@ -64,3 +66,36 @@ def get_tx_cost_data(transaction_dict: TxParams):
max_cost_wei = max_unit_price * transaction_dict["gas"]
max_cost = Web3.from_wei(max_cost_wei, "ether")
return max_cost, max_price_gwei, tx_type
def rpc_endpoint_health_check(endpoint: str) -> bool:
query = {
"jsonrpc": "2.0",
"method": "eth_getBlockByNumber",
"params": ["latest", False],
"id": 1
}
try:
response = requests.post(
endpoint,
json=query,
headers={"Content-Type": "application/json"},
timeout=5
)
if response.status_code == 200:
data = response.json()
if "result" in data and data["result"] is not None:
return True
except requests.exceptions.RequestException:
return False
def get_healthy_default_rpc_endpoints(chain_id: int) -> List[str]:
healthy = []
endpoints = DEFAULT_RPC_ENDPOINTS.get(chain_id)
if not endpoints:
return healthy
for endpoint in endpoints:
if rpc_endpoint_health_check(endpoint=endpoint):
healthy.append(endpoint)
return healthy