Skip to content

Commit

Permalink
feat: adapt unit tests to new config file
Browse files Browse the repository at this point in the history
  • Loading branch information
aimxhaisse committed Nov 20, 2023
1 parent 17c5b86 commit 699bed8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 65 deletions.
4 changes: 1 addition & 3 deletions eth_validator_watcher/beacon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion eth_validator_watcher/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .models import BeaconType
from dataclasses import dataclass, field
from typing import Any, List, Optional

Expand Down Expand Up @@ -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')
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions eth_validator_watcher/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
76 changes: 19 additions & 57 deletions tests/entrypoint/test__handler.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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"}
Expand Down Expand Up @@ -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"),
Expand Down
9 changes: 7 additions & 2 deletions tests/utils/test_get_our_pubkeys.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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 = {
Expand All @@ -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

0 comments on commit 699bed8

Please sign in to comment.