-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add signer yaml config generation to sync_validator_keys (#54)
* Add signer yaml config generation to sync_validator_keys * Add client specific comments to sync_validator_keys
- Loading branch information
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import os | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
from click.testing import CliRunner | ||
from py_ecc.bls import G2ProofOfPossession | ||
from staking_deposit.key_handling.key_derivation.mnemonic import get_mnemonic | ||
from web3 import Web3 | ||
|
||
from stakewise_cli.commands.sync_validator_keys import sync_validator_keys | ||
from stakewise_cli.eth2 import WORD_LISTS_PATH, get_mnemonic_signing_key | ||
|
||
w3 = Web3() | ||
|
||
mnemonic = get_mnemonic(language="english", words_path=WORD_LISTS_PATH) | ||
keys_count = 5 | ||
web3_signer_url = "http://web3signer:6174" | ||
|
||
|
||
def get_public_keys(mnemonic, keys_count): | ||
result = [] | ||
for i in range(keys_count): | ||
signing_key = get_mnemonic_signing_key(mnemonic, i, is_legacy=False) | ||
public_key = w3.toHex(G2ProofOfPossession.SkToPk(signing_key.key)) | ||
result.append(public_key) | ||
return result | ||
|
||
|
||
public_keys = get_public_keys(mnemonic=mnemonic, keys_count=keys_count) | ||
|
||
|
||
@patch("stakewise_cli.commands.sync_validator_keys.check_db_connection") | ||
@patch( | ||
"stakewise_cli.commands.sync_validator_keys.Database.fetch_public_keys_by_validator_index", | ||
return_value=public_keys, | ||
) | ||
@patch.dict(os.environ, {"WEB3SIGNER_URL": web3_signer_url}) | ||
class TestCommand(unittest.TestCase): | ||
def test_sync_validator_keys(self, *mocks): | ||
db_url = "postgresql://username:pass@hostname/dbname" | ||
index = 1 | ||
runner = CliRunner() | ||
args = [ | ||
"--index", | ||
index, | ||
"--db-url", | ||
db_url, | ||
"--output-dir", | ||
"./valdata", | ||
] | ||
with runner.isolated_filesystem(): | ||
result = runner.invoke(sync_validator_keys, args) | ||
assert result.exit_code == 0 | ||
|
||
assert ( | ||
f"The validator now uses {keys_count} public keys." | ||
== result.output.strip() | ||
) | ||
with open("./valdata/validator_definitions.yml") as f: | ||
s = """---""" | ||
for public_key in public_keys: | ||
s += f""" | ||
- enabled: true | ||
type: web3signer | ||
url: {web3_signer_url} | ||
voting_public_key: \'{public_key}\'""" | ||
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 | ||
|
||
result = runner.invoke(sync_validator_keys, args) | ||
assert result.exit_code == 0 | ||
assert "Keys already synced to the last version." == result.output.strip() |