staking-deposit-cli/tests/test_cli/test_existing_menmonic.py

155 lines
5.1 KiB
Python
Raw Normal View History

2020-05-12 07:35:56 +00:00
import asyncio
import json
import os
2020-05-12 07:35:56 +00:00
import pytest
from click.testing import CliRunner
from eth_utils import decode_hex
2020-09-29 14:24:18 +00:00
from eth2deposit.deposit import cli
from eth2deposit.utils.constants import DEFAULT_VALIDATOR_KEYS_FOLDER_NAME, ETH1_ADDRESS_WITHDRAWAL_PREFIX
from.helpers import clean_key_folder, get_permissions, get_uuid
2021-03-30 11:14:41 +00:00
def test_existing_mnemonic_bls_withdrawal() -> None:
# Prepare folder
2020-05-12 08:08:49 +00:00
my_folder_path = os.path.join(os.getcwd(), 'TESTING_TEMP_FOLDER')
clean_key_folder(my_folder_path)
if not os.path.exists(my_folder_path):
os.mkdir(my_folder_path)
runner = CliRunner()
2020-09-29 14:30:57 +00:00
inputs = [
2021-04-23 14:23:23 +00:00
'TREZOR',
2020-09-29 14:30:57 +00:00
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about',
'2', '2', '5', 'mainnet', 'MyPassword', 'MyPassword']
data = '\n'.join(inputs)
2021-04-07 09:15:20 +00:00
arguments = [
'--language', 'english',
'existing-mnemonic',
'--folder', my_folder_path,
'--mnemonic-password', 'TREZOR',
]
result = runner.invoke(cli, arguments, input=data)
assert result.exit_code == 0
# Check files
validator_keys_folder_path = os.path.join(my_folder_path, DEFAULT_VALIDATOR_KEYS_FOLDER_NAME)
_, _, key_files = next(os.walk(validator_keys_folder_path))
2020-05-26 13:00:53 +00:00
all_uuid = [
get_uuid(validator_keys_folder_path + '/' + key_file)
2021-03-30 11:14:41 +00:00
for key_file in key_files
if key_file.startswith('keystore')
]
assert len(set(all_uuid)) == 5
# Verify file permissions
if os.name == 'posix':
for file_name in key_files:
assert get_permissions(validator_keys_folder_path, file_name) == '0o440'
# Clean up
clean_key_folder(my_folder_path)
def test_existing_mnemonic_eth1_address_withdrawal() -> None:
# Prepare folder
2020-05-12 08:08:49 +00:00
my_folder_path = os.path.join(os.getcwd(), 'TESTING_TEMP_FOLDER')
clean_key_folder(my_folder_path)
if not os.path.exists(my_folder_path):
os.mkdir(my_folder_path)
runner = CliRunner()
2020-09-29 14:30:57 +00:00
inputs = [
2021-04-23 14:23:23 +00:00
'TREZOR',
2020-09-29 14:30:57 +00:00
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about',
'2', '2', '5', 'mainnet', 'MyPassword', 'MyPassword']
data = '\n'.join(inputs)
eth1_withdrawal_address = '0x00000000219ab540356cbb839cbe05303d7705fa'
2021-03-30 11:14:41 +00:00
arguments = [
2021-04-07 09:15:20 +00:00
'--language', 'english',
2021-03-30 11:14:41 +00:00
'existing-mnemonic',
'--folder', my_folder_path,
'--mnemonic-password', 'TREZOR',
'--eth1_withdrawal_address', eth1_withdrawal_address,
2021-03-30 11:14:41 +00:00
]
result = runner.invoke(cli, arguments, input=data)
assert result.exit_code == 0
# Check files
validator_keys_folder_path = os.path.join(my_folder_path, DEFAULT_VALIDATOR_KEYS_FOLDER_NAME)
_, _, key_files = next(os.walk(validator_keys_folder_path))
2020-05-26 13:00:53 +00:00
deposit_file = [key_file for key_file in key_files if key_file.startswith('deposit_data')][0]
with open(validator_keys_folder_path + '/' + deposit_file, 'r') as f:
deposits_dict = json.load(f)
for deposit in deposits_dict:
withdrawal_credentials = bytes.fromhex(deposit['withdrawal_credentials'])
assert withdrawal_credentials == (
ETH1_ADDRESS_WITHDRAWAL_PREFIX + b'\x00' * 11 + decode_hex(eth1_withdrawal_address)
)
2020-05-26 13:00:53 +00:00
all_uuid = [
get_uuid(validator_keys_folder_path + '/' + key_file)
for key_file in key_files
if key_file.startswith('keystore')
]
assert len(set(all_uuid)) == 5
# Verify file permissions
if os.name == 'posix':
for file_name in key_files:
assert get_permissions(validator_keys_folder_path, file_name) == '0o440'
# Clean up
clean_key_folder(my_folder_path)
2020-05-12 07:35:56 +00:00
@pytest.mark.asyncio
async def test_script() -> None:
2020-05-12 08:08:49 +00:00
my_folder_path = os.path.join(os.getcwd(), 'TESTING_TEMP_FOLDER')
if not os.path.exists(my_folder_path):
os.mkdir(my_folder_path)
2020-05-21 19:37:43 +00:00
if os.name == 'nt': # Windows
run_script_cmd = 'sh deposit.sh'
else: # Mac or Linux
run_script_cmd = './deposit.sh'
install_cmd = run_script_cmd + ' install'
proc = await asyncio.create_subprocess_shell(
2020-05-21 19:37:43 +00:00
install_cmd,
)
2020-05-21 19:37:43 +00:00
await proc.wait()
2020-05-12 08:08:49 +00:00
cmd_args = [
run_script_cmd,
2021-04-06 12:48:52 +00:00
'--language', 'english',
2021-04-27 13:18:28 +00:00
'--non_interactive',
'existing-mnemonic',
2020-05-12 08:08:49 +00:00
'--num_validators', '1',
'--mnemonic="abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"',
'--mnemonic-password', 'TREZOR',
'--validator_start_index', '1',
'--chain', 'mainnet',
2020-09-29 14:24:18 +00:00
'--keystore_password', 'MyPassword',
2020-05-12 08:08:49 +00:00
'--folder', my_folder_path,
]
2020-05-12 07:35:56 +00:00
proc = await asyncio.create_subprocess_shell(
2020-05-12 08:08:49 +00:00
' '.join(cmd_args),
2020-05-12 07:35:56 +00:00
)
2021-04-27 13:18:28 +00:00
await proc.wait()
2020-05-12 08:08:49 +00:00
# Check files
validator_keys_folder_path = os.path.join(my_folder_path, DEFAULT_VALIDATOR_KEYS_FOLDER_NAME)
_, _, key_files = next(os.walk(validator_keys_folder_path))
# Verify file permissions
if os.name == 'posix':
for file_name in key_files:
assert get_permissions(validator_keys_folder_path, file_name) == '0o440'
2020-05-12 08:08:49 +00:00
# Clean up
2020-05-23 14:32:31 +00:00
clean_key_folder(my_folder_path)