Skip to content

Commit

Permalink
update config util to make preset loading optional, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
protolambda committed May 7, 2021
1 parent 02ccd0c commit 38d98fc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
17 changes: 12 additions & 5 deletions tests/core/pyspec/eth2spec/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ For configuration, see [Configs documentation](../../../../../configs/README.md)
## Usage:

```python
configs_path = 'configs/'

...

from eth2spec.config import config_util
from eth2spec.phase0 import spec
from importlib import reload
config_util.prepare_config(configs_path, 'mainnet')
from pathlib import Path

# To load the presets and configurations
config_util.load_defaults(Path("eth2.0-specs/configs")) # change path to point to equivalent of specs `configs` dir.
# After loading the defaults, a config can be chosen: 'mainnet', 'minimal', or custom network config
config_util.prepare_config('minimal')
# Alternatively, load a custom testnet config:
config_util.prepare_config('my_config.yaml')
# reload spec to make loaded config effective
reload(spec)
```

Note: previously the testnet config files included both preset and runtime-configuration data.
The new config loader is compatible with this: just run `prepare_config` without loading preset defaults,
and omit the `PRESET_BASE` from the config.

WARNING: this overwrites globals, make sure to prevent accidental collisions with other usage of the same imported specs package.
15 changes: 9 additions & 6 deletions tests/core/pyspec/eth2spec/config/config_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ def prepare_config(config_path: Union[Path, BinaryIO, TextIO, Literal['mainnet']
conf_data = deepcopy(minimal_config_data)
else:
conf_data = load_config_file(config_path)
# Check the configured preset
base = conf_data['PRESET_BASE']
if base not in ('minimal', 'mainnet'):
raise Exception(f"unknown PRESET_BASE: {base}")
# Apply configuration if everything checks out
global config
config = deepcopy(mainnet_preset_data if base == 'mainnet' else minimal_preset_data)
config.update(conf_data)
if 'PRESET_BASE' in conf_data:
# Check the configured preset
base = conf_data['PRESET_BASE']
if base not in ('minimal', 'mainnet'):
raise Exception(f"unknown PRESET_BASE: {base}")
config = deepcopy(mainnet_preset_data if base == 'mainnet' else minimal_preset_data)
config.update(conf_data)
else:
config = conf_data


def parse_config_vars(conf: Dict[str, Any]) -> Dict[str, Any]:
Expand Down

0 comments on commit 38d98fc

Please sign in to comment.