Skip to content

Commit

Permalink
fix: config issue with default values
Browse files Browse the repository at this point in the history
  • Loading branch information
aimxhaisse committed Jun 25, 2024
1 parent 1916986 commit 3be75b8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
22 changes: 19 additions & 3 deletions eth_validator_watcher/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ class Config(BaseSettings):

network: Optional[str] = None
beacon_url: Optional[str] = None
beacon_timeout_sec: Optional[int] = 90
metrics_port: Optional[int] = 8000
beacon_timeout_sec: Optional[int] = None
metrics_port: Optional[int] = None
start_at: Optional[int] = None
watched_keys: Optional[List[WatchedKeyConfig]] = None


def _default_config() -> Config:
"""Returns the default configuration.
"""
return Config(
network='mainnet',
beacon_url='http://localhost:5051/',
beacon_timeout_sec=90,
metrics_port=8000,
watched_keys=[],
)


def load_config(config_file: str) -> Config:
"""Loads the configuration file from environment and configfile.
Expand All @@ -51,12 +63,16 @@ def load_config(config_file: str) -> Config:
config = yaml.load(fh, Loader=yaml.CLoader) or dict()

logging.info(f'validating configuration file')
from_default = _default_config().model_dump()
from_env = Config().model_dump()
from_file = Config(**config).model_dump()

logging.info(f'merging with environment variables')
merged = from_file.copy()
merged = from_default.copy()

merged.update({k: v for k, v in from_file.items() if v})
merged.update({k: v for k, v in from_env.items() if v})

r = Config(**merged)

logging.info(f'configuration file is ready')
Expand Down
1 change: 1 addition & 0 deletions tests/assets/config.null.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
beacon_url: ~
beacon_timeout_sec: ~
metrics_port: ~
network: ~
watched_keys: ~
1 change: 1 addition & 0 deletions tests/assets/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
beacon_url: http://localhost:5051/
beacon_timeout_sec: 90
network: holesky
metrics_port: 4242

watched_keys:
- public_key: '0x832b8286f5d6535fd941c6c4ed8b9b20d214fc6aa726ce4fba1c9dbb4f278132646304f550e557231b6932aa02cf08d3'
Expand Down
17 changes: 11 additions & 6 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@ def test_null_config() -> None:
path = str(Path(assets.__file__).parent / "config.null.yaml")
config = load_config(path)

assert config.beacon_url is None
assert config.beacon_url == 'http://localhost:5051/'
assert config.metrics_port == 8000
assert config.beacon_timeout_sec == 90
assert config.network is None
assert config.watched_keys is None
assert config.network == 'mainnet'

assert config.watched_keys == []


def test_empty_config() -> None:
path = str(Path(assets.__file__).parent / "config.empty.yaml")
config = load_config(path)

assert config.beacon_url is None
assert config.beacon_url == 'http://localhost:5051/'
assert config.beacon_timeout_sec == 90
assert config.network is None
assert config.watched_keys is None
assert config.metrics_port == 8000
assert config.network == 'mainnet'

assert config.watched_keys == []


def test_filled_config() -> None:
Expand All @@ -31,6 +35,7 @@ def test_filled_config() -> None:

assert config.beacon_url == 'http://localhost:5051/'
assert config.beacon_timeout_sec == 90
assert config.metrics_port == 4242
assert config.network == 'holesky'

assert [k.public_key for k in config.watched_keys] == ['0x832b8286f5d6535fd941c6c4ed8b9b20d214fc6aa726ce4fba1c9dbb4f278132646304f550e557231b6932aa02cf08d3']
Expand Down

0 comments on commit 3be75b8

Please sign in to comment.