diff --git a/eth_validator_watcher/config.py b/eth_validator_watcher/config.py index 885331b..08ce95a 100644 --- a/eth_validator_watcher/config.py +++ b/eth_validator_watcher/config.py @@ -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. @@ -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') diff --git a/tests/assets/config.null.yaml b/tests/assets/config.null.yaml index e521bd3..402db79 100644 --- a/tests/assets/config.null.yaml +++ b/tests/assets/config.null.yaml @@ -1,4 +1,5 @@ beacon_url: ~ beacon_timeout_sec: ~ +metrics_port: ~ network: ~ watched_keys: ~ diff --git a/tests/assets/config.yaml b/tests/assets/config.yaml index d138743..98918c5 100644 --- a/tests/assets/config.yaml +++ b/tests/assets/config.yaml @@ -3,6 +3,7 @@ beacon_url: http://localhost:5051/ beacon_timeout_sec: 90 network: holesky +metrics_port: 4242 watched_keys: - public_key: '0x832b8286f5d6535fd941c6c4ed8b9b20d214fc6aa726ce4fba1c9dbb4f278132646304f550e557231b6932aa02cf08d3' diff --git a/tests/test_config.py b/tests/test_config.py index 0b98b0b..37aa65b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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: @@ -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']