Skip to content

Commit

Permalink
Guardian balance metric (#267)
Browse files Browse the repository at this point in the history
* Guardian balance metric

* Fix

* chain_id in labels

* Fix tests

* fix

* try

* Increase time waiting

* Revert test

* assert

* without block

* increase wait time

* increase time

* remove block number

* revert

* variables
  • Loading branch information
hweawer authored Oct 11, 2024
1 parent d2d1f90 commit 7698acb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 27 deletions.
7 changes: 7 additions & 0 deletions src/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@
namespace=PROMETHEUS_PREFIX,
)

GUARDIAN_BALANCE = Gauge(
'guardian_balance',
'Balance of the guardian',
['address', 'chain_id'],
namespace=PROMETHEUS_PREFIX,
)

MODULES = Gauge('modules', 'Modules gauge', ['module_id'], namespace=PROMETHEUS_PREFIX)

for module_id in DEPOSIT_MODULES_WHITELIST:
Expand Down
7 changes: 6 additions & 1 deletion src/transport/msg_providers/onchain_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from eth_typing import ChecksumAddress
from eth_utils import to_bytes
from metrics.metrics import GUARDIAN_BALANCE
from schema import Schema
from transport.msg_providers.common import BaseMessageProvider
from transport.msg_providers.rabbit import MessageType
Expand Down Expand Up @@ -269,13 +270,17 @@ def _fetch_messages(self) -> list:
if from_block == latest_block_number:
return []
event_ids = [self._w3.keccak(text=parser.message_abi) for parser in self._parsers]
addresses_with_padding = [_32padding_address(address) for address in self._allowed_guardians_provider()]
guardians = self._allowed_guardians_provider()
addresses_with_padding = [_32padding_address(address) for address in guardians]
filter_params = FilterParams(
fromBlock=from_block,
toBlock=latest_block_number,
address=self._onchain_address,
topics=[event_ids, addresses_with_padding],
)
for guard in guardians:
balance = self._w3.eth.get_balance(guard)
GUARDIAN_BALANCE.labels(address=guard, chain_id=self._chain_id).set(balance)
try:
logs = self._w3.eth.get_logs(filter_params)
if logs:
Expand Down
4 changes: 3 additions & 1 deletion tests/fixtures/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def web3_provider_integration(request) -> Web3:
variables.WEB3_RPC_ENDPOINTS[0],
block_num,
):
yield Web3(HTTPProvider('http://127.0.0.1:8545', request_kwargs={'timeout': 3600}))
web3 = Web3(HTTPProvider('http://127.0.0.1:8545', request_kwargs={'timeout': 3600}))
assert web3.is_connected()
yield web3


@pytest.fixture
Expand Down
28 changes: 3 additions & 25 deletions tests/transport/test_data_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,14 @@
# DATA_BUS_ADDRESS: '0x5FbDB2315678afecb367f032d93F642f64180aa3'
# }
@pytest.mark.integration_chiado
@pytest.mark.parametrize(
'web3_provider_integration',
[12217621],
indirect=['web3_provider_integration'],
)
def test_data_bus_provider(
web3_provider_integration,
web3_transaction_integration,
):
"""
Utilise this function for an adhoc testing of data bus transport
"""
variables.ONCHAIN_TRANSPORT_ADDRESS = ChecksumAddress(HexAddress(HexStr('0x37De961D6bb5865867aDd416be07189D2Dd960e6')))
web3_transaction_integration.eth.get_balance = Mock(return_value=1)
provider = OnchainTransportProvider(
w3=web3_transaction_integration,
onchain_address=variables.ONCHAIN_TRANSPORT_ADDRESS,
Expand All @@ -63,17 +58,11 @@ def test_data_bus_provider(
allowed_guardians_provider=lambda: [Web3.to_checksum_address(_DEFAULT_GUARDIAN)],
)
messages = provider.get_messages()
assert len(messages) == 75
assert messages


@pytest.mark.integration_chiado
@pytest.mark.parametrize(
'web3_provider_integration',
[12217621],
indirect=['web3_provider_integration'],
)
def test_data_bus_provider_unvet(
web3_provider_integration,
web3_transaction_integration,
):
"""
Expand Down Expand Up @@ -123,13 +112,7 @@ def test_data_bus_provider_unvet(


@pytest.mark.integration_chiado
@pytest.mark.parametrize(
'web3_provider_integration',
[12217621],
indirect=['web3_provider_integration'],
)
def test_data_bus_provider_pause_v2(
web3_provider_integration,
web3_transaction_integration,
):
"""
Expand Down Expand Up @@ -175,13 +158,7 @@ def test_data_bus_provider_pause_v2(


@pytest.mark.integration_chiado
@pytest.mark.parametrize(
'web3_provider_integration',
[12217621],
indirect=['web3_provider_integration'],
)
def test_data_bus_provider_pause_v3(
web3_provider_integration,
web3_transaction_integration,
):
"""
Expand Down Expand Up @@ -224,6 +201,7 @@ def test_data_bus_mock_responses(web3_lido_unit):
receipts = mock_receipts(web3_lido_unit)
web3_lido_unit.eth.get_logs = Mock(side_effect=[receipts, None])
web3_lido_unit.is_connected = Mock(return_value=True)
web3_lido_unit.eth.get_balance = Mock(return_value=1)
web3_lido_unit.eth.get_block_number = Mock(return_value=1)
provider = OnchainTransportProvider(
w3=web3_lido_unit,
Expand Down

0 comments on commit 7698acb

Please sign in to comment.