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

Cache chain-id #288

Merged
merged 5 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import logging
from typing import Any, Callable
from typing import Any, Callable, Set, cast
from urllib.parse import urlparse

from metrics.metrics import ETH_RPC_REQUESTS, ETH_RPC_REQUESTS_DURATION
from requests import HTTPError, Response
from web3 import Web3
from web3.middleware import construct_simple_cache_middleware
from web3.types import RPCEndpoint, RPCResponse

logger = logging.getLogger(__name__)


def add_requests_metric_middleware(web3: Web3):
def add_requests_metric_middleware(web3: Web3) -> Web3:
"""
Works correctly with MultiProvider and vanilla Providers.

Expand All @@ -20,6 +21,7 @@ def add_requests_metric_middleware(web3: Web3):

def metrics_collector(make_request: Callable[[RPCEndpoint, Any], RPCResponse], w3: Web3) -> Callable[[RPCEndpoint, Any], RPCResponse]:
"""Constructs a middleware which measure requests parameters"""
chain_id = w3.eth.chain_id

def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
try:
Expand All @@ -31,6 +33,7 @@ def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
method=method,
code=failed.status_code,
domain=urlparse(web3.provider.endpoint_uri).netloc, # pyright: ignore
chain_id=chain_id,
).inc()
raise

Expand All @@ -45,9 +48,26 @@ def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
method=method,
code=code,
domain=urlparse(web3.provider.endpoint_uri).netloc, # pyright: ignore
chain_id=chain_id,
).inc()
return response

return middleware

web3.middleware_onion.add(metrics_collector)
return web3


def add_cache_middleware(web3: Web3) -> Web3:
web3.middleware_onion.inject(
construct_simple_cache_middleware(
rpc_whitelist=cast(
Set[RPCEndpoint],
{
'eth_chainId',
},
)
),
layer=0,
)
return web3
26 changes: 10 additions & 16 deletions src/bots/depositor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from transport.msg_types.ping import PingMessageSchema, to_check_sum_address
from transport.types import TransportType
from web3.types import BlockData
from web3_multi_provider import FallbackProvider, MultiProvider

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -83,10 +82,12 @@ def __init__(
)
)

self._onchain_transport_w3 = None
if TransportType.ONCHAIN_TRANSPORT in variables.MESSAGE_TRANSPORTS:
self._onchain_transport_w3 = OnchainTransportProvider.create_ochain_transport_w3()
transports.append(
OnchainTransportProvider(
w3=Web3(FallbackProvider(variables.ONCHAIN_TRANSPORT_RPC_ENDPOINTS)),
w3=self._onchain_transport_w3,
onchain_address=variables.ONCHAIN_TRANSPORT_ADDRESS,
message_schema=Schema(Or(DepositMessageSchema, PingMessageSchema)),
parsers_providers=[DepositParser, PingParser],
Expand Down Expand Up @@ -118,28 +119,21 @@ def execute(self, block: BlockData) -> bool:
return True

def _check_balance(self):
eth_chain_id = self.w3.eth.chain_id

if variables.ACCOUNT:
balance = self.w3.eth.get_balance(variables.ACCOUNT.address)
ACCOUNT_BALANCE.labels(variables.ACCOUNT.address, eth_chain_id).set(balance)
ACCOUNT_BALANCE.labels(variables.ACCOUNT.address, self.w3.eth.chain_id).set(balance)
logger.info({'msg': 'Check account balance.', 'value': balance})

logger.info({'msg': 'Check guardians balances.'})

w3_databus, w3_databus_chain_id = None, None
if variables.ONCHAIN_TRANSPORT_RPC_ENDPOINTS:
w3_databus = Web3(MultiProvider(variables.ONCHAIN_TRANSPORT_RPC_ENDPOINTS))
w3_databus_chain_id = w3_databus.eth.chain_id

guardians = self.w3.lido.deposit_security_module.get_guardians()
providers = [self.w3]
if self._onchain_transport_w3 is not None:
providers.append(self._onchain_transport_w3)
for address in guardians:
eth_balance = self.w3.eth.get_balance(address)
GUARDIAN_BALANCE.labels(address=address, chain_id=eth_chain_id).set(eth_balance)

if w3_databus is not None:
balance = w3_databus.eth.get_balance(address)
GUARDIAN_BALANCE.labels(address=address, chain_id=w3_databus_chain_id).set(balance)
for provider in providers:
balance = self.w3.eth.get_balance(address)
GUARDIAN_BALANCE.labels(address=address, chain_id=provider.eth.chain_id).set(balance)

def _deposit_to_module(self, module_id: int) -> bool:
can_deposit = self.w3.lido.deposit_security_module.can_deposit(module_id)
Expand Down
3 changes: 1 addition & 2 deletions src/bots/pauser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from transport.msg_types.ping import PingMessageSchema, to_check_sum_address
from transport.types import TransportType
from web3.types import BlockData
from web3_multi_provider import FallbackProvider

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -51,7 +50,7 @@ def __init__(self, w3: Web3):
if TransportType.ONCHAIN_TRANSPORT in variables.MESSAGE_TRANSPORTS:
transports.append(
OnchainTransportProvider(
w3=Web3(FallbackProvider(variables.ONCHAIN_TRANSPORT_RPC_ENDPOINTS)),
w3=OnchainTransportProvider.create_ochain_transport_w3(),
onchain_address=variables.ONCHAIN_TRANSPORT_ADDRESS,
message_schema=Schema(Or(PauseMessageSchema, PingMessageSchema)),
parsers_providers=[PauseV2Parser, PauseV3Parser, PingParser],
Expand Down
3 changes: 1 addition & 2 deletions src/bots/unvetter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from transport.types import TransportType
from utils.bytes import from_hex_string_to_bytes
from web3.types import BlockData
from web3_multi_provider import FallbackProvider

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -56,7 +55,7 @@ def prepare_transport_bus(self):
if TransportType.ONCHAIN_TRANSPORT in variables.MESSAGE_TRANSPORTS:
transports.append(
OnchainTransportProvider(
w3=Web3(FallbackProvider(variables.ONCHAIN_TRANSPORT_RPC_ENDPOINTS)),
w3=OnchainTransportProvider.create_ochain_transport_w3(),
onchain_address=variables.ONCHAIN_TRANSPORT_ADDRESS,
message_schema=Schema(Or(UnvetMessageSchema, PingMessageSchema)),
parsers_providers=[UnvetParser, PingParser],
Expand Down
12 changes: 3 additions & 9 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import variables
from blockchain.typings import Web3
from blockchain.web3_extentions.lido_contracts import LidoContracts
from blockchain.web3_extentions.requests_metric_middleware import add_requests_metric_middleware
from blockchain.web3_extentions.middleware import add_cache_middleware, add_requests_metric_middleware
from blockchain.web3_extentions.transaction import TransactionUtils
from bots.depositor import run_depositor
from bots.pauser import run_pauser
Expand All @@ -24,13 +24,7 @@ class BotModule(StrEnum):


def main(bot_name: str):
logger.info(
{
'msg': 'Bot env variables',
'value': variables.PUBLIC_ENV_VARS,
'bot_name': bot_name
}
)
logger.info({'msg': 'Bot env variables', 'value': variables.PUBLIC_ENV_VARS, 'bot_name': bot_name})
if bot_name not in list(BotModule):
msg = f'Last arg should be one of {[str(item) for item in BotModule]}, received {BotModule}.'
logger.error({'msg': msg})
Expand All @@ -55,7 +49,7 @@ def main(bot_name: str):
)

logger.info({'msg': 'Add metrics to web3 requests.'})
add_requests_metric_middleware(w3)
add_requests_metric_middleware(add_cache_middleware(w3))

if bot_name == BotModule.DEPOSITOR:
run_depositor(w3)
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
ETH_RPC_REQUESTS = Counter(
'eth_rpc_requests',
'Total count of requests to ETH1 RPC',
['method', 'code', 'domain'],
['method', 'code', 'domain', 'chain_id'],
namespace=PROMETHEUS_PREFIX,
)

Expand Down
7 changes: 7 additions & 0 deletions src/transport/msg_providers/onchain_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
from typing import Callable, List, Optional

import variables
from blockchain.web3_extentions.middleware import add_cache_middleware, add_requests_metric_middleware
from eth_typing import ChecksumAddress
from eth_utils import to_bytes
from schema import Schema
Expand All @@ -16,6 +18,7 @@
from web3._utils.events import get_event_data
from web3.exceptions import BlockNotFound
from web3.types import EventData, FilterParams, LogReceipt
from web3_multi_provider import FallbackProvider

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -320,3 +323,7 @@ def _parse_log(self, log: LogReceipt) -> Optional[dict]:
}
)
return None

@staticmethod
def create_ochain_transport_w3() -> Web3:
return add_requests_metric_middleware(add_cache_middleware(Web3(FallbackProvider(variables.ONCHAIN_TRANSPORT_RPC_ENDPOINTS))))
12 changes: 7 additions & 5 deletions tests/bots/test_depositor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from datetime import datetime, timedelta
from unittest import mock
from unittest.mock import MagicMock, Mock, patch

import pytest
Expand Down Expand Up @@ -125,11 +126,12 @@ def depositor_bot(
block_data,
csm_strategy,
):
variables.MESSAGE_TRANSPORTS = ''
variables.DEPOSIT_MODULES_WHITELIST = [1, 2]
web3_lido_unit.lido.staking_router.get_staking_module_ids = Mock(return_value=[1, 2])
web3_lido_unit.eth.get_block = Mock(return_value=block_data)
yield DepositorBot(web3_lido_unit, deposit_transaction_sender, base_deposit_strategy, csm_strategy)
with mock.patch('web3.eth.Eth.chain_id', new_callable=mock.PropertyMock) as _:
variables.MESSAGE_TRANSPORTS = ''
variables.DEPOSIT_MODULES_WHITELIST = [1, 2]
web3_lido_unit.lido.staking_router.get_staking_module_ids = Mock(return_value=[1, 2])
web3_lido_unit.eth.get_block = Mock(return_value=block_data)
yield DepositorBot(web3_lido_unit, deposit_transaction_sender, base_deposit_strategy, csm_strategy)


@pytest.fixture
Expand Down
Loading