diff --git a/stakewise_cli/commands/sync_validator_keys.py b/stakewise_cli/commands/sync_validator_keys.py index cf7b168..2a083b5 100644 --- a/stakewise_cli/commands/sync_validator_keys.py +++ b/stakewise_cli/commands/sync_validator_keys.py @@ -1,4 +1,3 @@ -import csv import os from os import mkdir from os.path import exists, join @@ -56,11 +55,10 @@ def sync_validator_keys( mkdir(output_dir) # check current public keys CSV - csv_filename = join(output_dir, PUBLIC_KEYS_CSV_FILENAME) - if exists(csv_filename): - with open(csv_filename) as f: - reader = csv.reader(f) - current_keys = list(reader)[0] + lighthouse_filename = join(output_dir, LIGHTHOUSE_CONFIG_FILENAME) + if exists(lighthouse_filename): + with open(lighthouse_filename) as f: + current_keys = _load_lighthouse_config(f) if is_lists_equal(keys, current_keys): click.secho( "Keys already synced to the last version.\n", @@ -69,11 +67,6 @@ def sync_validator_keys( ) return - # save CSV file - with open(csv_filename, "w") as f: - writer = csv.writer(f) - writer.writerow(keys) - # save lighthouse config web3signer_url = os.environ[web3signer_url_env] lighthouse_config = _generate_lighthouse_config( @@ -82,7 +75,7 @@ def sync_validator_keys( with open(join(output_dir, LIGHTHOUSE_CONFIG_FILENAME), "w") as f: f.write(lighthouse_config) - # save external signer public keys + # save teku/prysm config signer_keys_config = _generate_signer_keys_config(public_keys=keys) with open(join(output_dir, SIGNER_CONFIG_FILENAME), "w") as f: f.write(signer_keys_config) @@ -111,10 +104,20 @@ def _generate_lighthouse_config(public_keys: List[str], web3signer_url: str) -> return yaml.dump(items, explicit_start=True) +def _load_lighthouse_config(file) -> List[str]: + """ + Load config for Lighthouse clients + """ + try: + items = yaml.safe_load(file) + return [item.get("voting_public_key") for item in items] + except yaml.YAMLError: + return [] + + def _generate_signer_keys_config(public_keys: List[str]) -> str: """ Generate config for Teku and Prysm clients """ - items = [{"validators-external-signer-public-keys": public_keys}] - - return yaml.dump(items).replace("'", "")[2:] + keys = ",".join([f'"{public_key}"' for public_key in public_keys]) + return f"""validators-external-signer-public-keys: [{keys}]""" diff --git a/stakewise_cli/tests/test_sync_validator_keys.py b/stakewise_cli/tests/test_sync_validator_keys.py index 00994b8..cf533a1 100644 --- a/stakewise_cli/tests/test_sync_validator_keys.py +++ b/stakewise_cli/tests/test_sync_validator_keys.py @@ -13,7 +13,7 @@ w3 = Web3() mnemonic = get_mnemonic(language="english", words_path=WORD_LISTS_PATH) -keys_count = 5 +keys_count = 3 web3_signer_url = "http://web3signer:6174" @@ -67,15 +67,10 @@ def test_sync_validator_keys(self, *mocks): s += "\n" assert f.read() == s - with open("./valdata/validator_keys.csv") as f: - s = ",".join(public_keys) + "\n" - assert f.read() == s - with open("./valdata/signer_keys.yml") as f: - s = """validators-external-signer-public-keys:\n""" - for public_key in public_keys: - s += f" - {public_key}\n" - assert f.read() == s + s = f"""validators-external-signer-public-keys: ["{public_keys[0]}","{public_keys[1]}","{public_keys[2]}"]""" + ff = f.read() + assert ff == s, (ff, s) result = runner.invoke(sync_validator_keys, args) assert result.exit_code == 0