Skip to content

Commit

Permalink
sync-validator-keys: Update config for Teku/ Prysm (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyc60 authored Aug 18, 2022
1 parent ea17ec9 commit 6f811cd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
33 changes: 18 additions & 15 deletions stakewise_cli/commands/sync_validator_keys.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import csv
import os
from os import mkdir
from os.path import exists, join
Expand Down Expand Up @@ -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",
Expand All @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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}]"""
13 changes: 4 additions & 9 deletions stakewise_cli/tests/test_sync_validator_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6f811cd

Please sign in to comment.