2020-05-12 07:35:56 +00:00
|
|
|
import asyncio
|
2020-05-08 20:11:02 +00:00
|
|
|
import os
|
|
|
|
|
2020-05-12 07:35:56 +00:00
|
|
|
import pytest
|
|
|
|
|
2020-06-30 09:55:31 +00:00
|
|
|
from typing import (
|
|
|
|
Optional,
|
|
|
|
)
|
|
|
|
|
2020-05-08 20:11:02 +00:00
|
|
|
from click.testing import CliRunner
|
|
|
|
|
2020-05-11 15:38:00 +00:00
|
|
|
from eth2deposit import deposit
|
|
|
|
from eth2deposit.deposit import main
|
2020-05-08 20:11:02 +00:00
|
|
|
from eth2deposit.utils.constants import DEFAULT_VALIDATOR_KEYS_FOLDER_NAME
|
2020-05-26 13:00:53 +00:00
|
|
|
from eth2deposit.key_handling.keystore import Keystore
|
2020-05-08 20:11:02 +00:00
|
|
|
|
|
|
|
|
2020-06-30 09:55:31 +00:00
|
|
|
def clean_key_folder(my_folder_path: str) -> None:
|
2020-05-19 13:32:05 +00:00
|
|
|
validator_keys_folder_path = os.path.join(my_folder_path, DEFAULT_VALIDATOR_KEYS_FOLDER_NAME)
|
|
|
|
if not os.path.exists(validator_keys_folder_path):
|
|
|
|
return
|
|
|
|
|
|
|
|
_, _, key_files = next(os.walk(validator_keys_folder_path))
|
|
|
|
for key_file_name in key_files:
|
|
|
|
os.remove(os.path.join(validator_keys_folder_path, key_file_name))
|
|
|
|
os.rmdir(validator_keys_folder_path)
|
|
|
|
os.rmdir(my_folder_path)
|
|
|
|
|
|
|
|
|
2020-06-30 09:55:31 +00:00
|
|
|
def test_deposit(monkeypatch) -> None:
|
2020-05-08 20:11:02 +00:00
|
|
|
# monkeypatch get_mnemonic
|
2020-06-30 09:55:31 +00:00
|
|
|
def get_mnemonic(language: str, words_path: str, entropy: Optional[bytes]=None) -> str:
|
2020-05-08 20:11:02 +00:00
|
|
|
return "fakephrase"
|
|
|
|
|
|
|
|
monkeypatch.setattr(deposit, "get_mnemonic", get_mnemonic)
|
|
|
|
|
|
|
|
# Prepare folder
|
2020-05-12 08:08:49 +00:00
|
|
|
my_folder_path = os.path.join(os.getcwd(), 'TESTING_TEMP_FOLDER')
|
2020-05-19 13:32:05 +00:00
|
|
|
clean_key_folder(my_folder_path)
|
2020-05-08 20:11:02 +00:00
|
|
|
if not os.path.exists(my_folder_path):
|
|
|
|
os.mkdir(my_folder_path)
|
|
|
|
|
|
|
|
runner = CliRunner()
|
2020-05-26 13:00:53 +00:00
|
|
|
inputs = ['5', 'english', 'MyPassword', 'MyPassword', 'fakephrase']
|
2020-05-08 20:11:02 +00:00
|
|
|
data = '\n'.join(inputs)
|
|
|
|
result = runner.invoke(main, ['--folder', my_folder_path], 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
|
|
|
|
2020-06-30 09:55:31 +00:00
|
|
|
def get_uuid(key_file: str) -> str:
|
2020-05-26 13:00:53 +00:00
|
|
|
keystore = Keystore.from_json(key_file)
|
|
|
|
return keystore.uuid
|
|
|
|
|
|
|
|
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
|
2020-05-08 20:11:02 +00:00
|
|
|
|
|
|
|
# Clean up
|
2020-05-19 13:32:05 +00:00
|
|
|
clean_key_folder(my_folder_path)
|
2020-05-12 07:35:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2020-06-30 09:55:31 +00:00
|
|
|
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'
|
2020-05-21 16:18:09 +00:00
|
|
|
proc = await asyncio.create_subprocess_shell(
|
2020-05-21 19:37:43 +00:00
|
|
|
install_cmd,
|
2020-05-21 16:18:09 +00:00
|
|
|
)
|
2020-05-21 19:37:43 +00:00
|
|
|
await proc.wait()
|
2020-05-21 16:18:09 +00:00
|
|
|
|
2020-05-12 08:08:49 +00:00
|
|
|
cmd_args = [
|
2020-05-21 19:37:43 +00:00
|
|
|
run_script_cmd,
|
2020-05-12 08:08:49 +00:00
|
|
|
'--num_validators', '1',
|
|
|
|
'--mnemonic_language', 'english',
|
|
|
|
'--password', 'MyPassword',
|
|
|
|
'--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
|
|
|
stdin=asyncio.subprocess.PIPE,
|
|
|
|
stdout=asyncio.subprocess.PIPE,
|
|
|
|
)
|
|
|
|
|
|
|
|
seed_phrase = ''
|
|
|
|
parsing = False
|
|
|
|
async for out in proc.stdout:
|
|
|
|
output = out.decode('utf-8').rstrip()
|
|
|
|
if output.startswith("This is your seed phrase."):
|
|
|
|
parsing = True
|
|
|
|
elif output.startswith("Please type your mnemonic"):
|
|
|
|
parsing = False
|
|
|
|
elif parsing:
|
|
|
|
seed_phrase += output
|
|
|
|
if len(seed_phrase) > 0:
|
|
|
|
encoded_phrase = seed_phrase.encode()
|
|
|
|
proc.stdin.write(encoded_phrase)
|
|
|
|
proc.stdin.write(b'\n')
|
|
|
|
|
|
|
|
assert len(seed_phrase) > 0
|
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))
|
|
|
|
|
|
|
|
# Clean up
|
2020-05-23 14:32:31 +00:00
|
|
|
clean_key_folder(my_folder_path)
|