From 699bed8773baf09b84403fe4290eb3dee300c60d Mon Sep 17 00:00:00 2001 From: "s. rannou" Date: Mon, 20 Nov 2023 18:49:36 +0100 Subject: [PATCH] feat: adapt unit tests to new config file --- eth_validator_watcher/beacon.py | 4 +- eth_validator_watcher/config.py | 6 ++- eth_validator_watcher/entrypoint.py | 4 +- tests/entrypoint/test__handler.py | 76 ++++++++--------------------- tests/utils/test_get_our_pubkeys.py | 9 +++- 5 files changed, 34 insertions(+), 65 deletions(-) diff --git a/eth_validator_watcher/beacon.py b/eth_validator_watcher/beacon.py index 17eecbc..13560bb 100644 --- a/eth_validator_watcher/beacon.py +++ b/eth_validator_watcher/beacon.py @@ -349,11 +349,9 @@ def get_validators_liveness( print( "❓ You can ignore this message if the watcher just started less " "than one epoch ago. Otherwise, please check that you used the correct " - f"`--beacon-type` option (currently set to `{beacon_type}`). " + f"`beacon_type` option (currently set to `{beacon_type}`). " ) - print("❓ Use `--help` for more details.") - return {index: True for index in validators_index} validators_liveness_dict = response.json() diff --git a/eth_validator_watcher/config.py b/eth_validator_watcher/config.py index 0aeeb45..b8379fa 100644 --- a/eth_validator_watcher/config.py +++ b/eth_validator_watcher/config.py @@ -1,3 +1,4 @@ +from .models import BeaconType from dataclasses import dataclass, field from typing import Any, List, Optional @@ -26,7 +27,7 @@ class Config: and the more complex ones that can't. """ beacon_url: Optional[str] = os.getenv('ETH_WATCHER_BEACON_URL') - beacon_type: Optional[str] = os.getenv('ETH_WATCHER_BEACON_TYPE') + beacon_type: Optional[str] = os.getenv('ETH_WATCHER_BEACON_TYPE', BeaconType.OTHER) execution_url: Optional[str] = os.getenv('ETH_WATCHER_EXEC_URL') config_file: Optional[str] = os.getenv('ETH_WATCHER_CONFIG_FILE', 'etc/config.local.yaml') web3signer_url: Optional[str] = os.getenv('ETH_WATCHER_WEB3SIGNER_URL') @@ -44,6 +45,9 @@ def load_config(config_file: str) -> Config: Parameters: config_file : path to the YAML configuration file + + Returns: + The effective configuration used by the watcher """ with open(config_file, 'r') as fh: yml = yaml.safe_load(fh) diff --git a/eth_validator_watcher/entrypoint.py b/eth_validator_watcher/entrypoint.py index b5f163e..04b6bef 100644 --- a/eth_validator_watcher/entrypoint.py +++ b/eth_validator_watcher/entrypoint.py @@ -163,7 +163,7 @@ def _handler( default_fee_recipient: str | None, slack_channel: str | None, slack_token: str | None, - beacon_type: BeaconType, + beacon_type: BeaconType | None, relays_url: List[str], liveness_file: Path | None, ) -> None: @@ -380,7 +380,7 @@ def _handler( ) process_fee_recipient( - block, our_active_idx2val, execution, fee_recipient, slack + block, our_active_idx2val, execution, default_fee_recipient, slack ) is_our_validator = process_missed_blocks_head( diff --git a/tests/entrypoint/test__handler.py b/tests/entrypoint/test__handler.py index d960d47..62b7649 100644 --- a/tests/entrypoint/test__handler.py +++ b/tests/entrypoint/test__handler.py @@ -1,8 +1,9 @@ from os import environ from pathlib import Path -from typing import Iterator, Optional, Tuple +from typing import Iterator, List, Optional, Tuple from eth_validator_watcher import entrypoint +from eth_validator_watcher.config import WatchedKeyConfig from eth_validator_watcher.entrypoint import _handler from eth_validator_watcher.models import BeaconType, Genesis, Validators from eth_validator_watcher.utils import LimitedDict, Slack @@ -20,10 +21,11 @@ def test_fee_recipient_set_while_execution_url_not_set() -> None: _handler( beacon_url="", execution_url=None, - pubkeys_file_path=None, + watched_keys=None, web3signer_url=None, - fee_recipient="something", + default_fee_recipient="something", slack_channel="MY SLACK CHANNEL", + slack_token=None, beacon_type=BeaconType.OLD_TEKU, relays_url=[], liveness_file=None, @@ -35,10 +37,11 @@ def test_fee_recipient_not_valid() -> None: _handler( beacon_url="", execution_url="http://localhost:8545", - pubkeys_file_path=None, + watched_keys=None, web3signer_url=None, - fee_recipient="something", + default_fee_recipient="something", slack_channel="MY SLACK CHANNEL", + slack_token=None, beacon_type=BeaconType.OLD_TEKU, relays_url=[], liveness_file=None, @@ -50,58 +53,17 @@ def test_slack_token_not_defined() -> None: _handler( beacon_url="", execution_url=None, - pubkeys_file_path=None, + watched_keys=None, web3signer_url=None, - fee_recipient=None, + default_fee_recipient=None, slack_channel="MY SLACK CHANNEL", + slack_token=None, beacon_type=BeaconType.OLD_TEKU, relays_url=[], liveness_file=None, ) -def test_invalid_pubkeys() -> None: - class Beacon: - def __init__(self, url: str) -> None: - assert url == "http://localhost:5052" - - def get_genesis(self) -> Genesis: - return Genesis( - data=Genesis.Data( - genesis_time=0, - ) - ) - - def get_our_pubkeys(pubkeys_file_path: Path, web3signer: None) -> set[str]: - assert pubkeys_file_path == Path("/path/to/pubkeys") - raise ValueError("Invalid pubkeys") - - def slots(genesis_time: int) -> Iterator[Tuple[(int, int)]]: - assert genesis_time == 0 - yield 63, 1664 - yield 64, 1676 - - def start_http_server(_: int) -> None: - pass - - entrypoint.get_our_pubkeys = get_our_pubkeys # type: ignore - entrypoint.Beacon = Beacon # type: ignore - entrypoint.slots = slots # type: ignore - entrypoint.start_http_server = start_http_server # type: ignore - - with raises(BadParameter): - _handler( - beacon_url="http://localhost:5052", - execution_url=None, - pubkeys_file_path=Path("/path/to/pubkeys"), - web3signer_url=None, - fee_recipient=None, - slack_channel=None, - beacon_type=BeaconType.OLD_TEKU, - relays_url=[], - liveness_file=None, - ) - def test_chain_not_ready() -> None: class Beacon: @@ -142,10 +104,11 @@ def start_http_server(_: int) -> None: _handler( beacon_url="http://localhost:5052", execution_url=None, - pubkeys_file_path=Path("/path/to/pubkeys"), + watched_keys=None, web3signer_url=None, - fee_recipient=None, + default_fee_recipient=None, slack_channel=None, + slack_token=None, beacon_type=BeaconType.OLD_TEKU, relays_url=[], liveness_file=Path("/path/to/liveness"), @@ -221,8 +184,8 @@ def slots(genesis_time: int) -> Iterator[Tuple[(int, int)]]: yield 63, 1664 yield 64, 1676 - def get_our_pubkeys(pubkeys_file_path: Path, web3signer: Web3Signer) -> set[str]: - assert pubkeys_file_path == Path("/path/to/pubkeys") + def get_our_pubkeys(watched_keys: List[WatchedKeyConfig], web3signer: Web3Signer) -> set[str]: + assert watched_keys == [WatchedKeyConfig(public_key="0xfff")] assert isinstance(web3signer, Web3Signer) return {"0xaaa", "0xbbb", "0xccc", "0xddd", "0xeee", "0xfff"} @@ -358,15 +321,14 @@ def write_liveness_file(liveness_file: Path) -> None: entrypoint.process_rewards = process_rewards # type: ignore entrypoint.write_liveness_file = write_liveness_file # type: ignore - environ["SLACK_TOKEN"] = "my_slack_token" - _handler( beacon_url="http://localhost:5052", execution_url=None, - pubkeys_file_path=Path("/path/to/pubkeys"), + watched_keys=[WatchedKeyConfig(public_key="0xfff")], web3signer_url="http://localhost:9000", - fee_recipient=None, + default_fee_recipient=None, slack_channel="my slack channel", + slack_token="my_slack_token", beacon_type=BeaconType.OLD_TEKU, relays_url=["http://my-awesome-relay.com"], liveness_file=Path("/path/to/liveness"), diff --git a/tests/utils/test_get_our_pubkeys.py b/tests/utils/test_get_our_pubkeys.py index 35b58d7..47b2c4e 100644 --- a/tests/utils/test_get_our_pubkeys.py +++ b/tests/utils/test_get_our_pubkeys.py @@ -1,5 +1,6 @@ from pathlib import Path +from eth_validator_watcher.config import WatchedKeyConfig from eth_validator_watcher.utils import get_our_pubkeys from tests.utils import assets @@ -15,7 +16,11 @@ def load_pubkeys() -> set[str]: def test_get_our_pubkeys() -> None: - pubkey_path = Path(assets.__file__).parent / "pubkeys.txt" + watched_keys = [ + WatchedKeyConfig(public_key="0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), + WatchedKeyConfig(public_key="0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"), + WatchedKeyConfig(public_key="0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"), + ] web3signer = Web3Signer() expected = { @@ -26,4 +31,4 @@ def test_get_our_pubkeys() -> None: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", } - assert get_our_pubkeys(pubkey_path, web3signer) == expected # type: ignore + assert get_our_pubkeys(watched_keys, web3signer) == expected # type: ignore