From 245ee7b63a1a24ed1e3f9600e8ce02d45d2e9e29 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sun, 22 Jan 2023 13:33:53 +0100 Subject: [PATCH] Normalize input `bls_withdrawal_credentials` and add `test_existing_mnemonic_bls_withdrawal_interactive` --- staking_deposit/utils/validation.py | 11 +++- tests/test_cli/helpers.py | 14 +++++ .../test_generate_bls_to_execution_change.py | 53 +++++++++++++++---- 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/staking_deposit/utils/validation.py b/staking_deposit/utils/validation.py index a0650e0..8d447f8 100644 --- a/staking_deposit/utils/validation.py +++ b/staking_deposit/utils/validation.py @@ -203,8 +203,17 @@ def validate_bls_to_execution_change(btec_dict: Dict[str, Any], return True -def validate_bls_withdrawal_credentials(bls_withdrawal_credentials: str) -> bytes: +def normalize_bls_withdrawal_credentials_to_bytes(bls_withdrawal_credentials: str) -> bytes: + if bls_withdrawal_credentials.startswith('0x'): + bls_withdrawal_credentials = bls_withdrawal_credentials[2:] + bls_withdrawal_credentials_bytes = bytes.fromhex(bls_withdrawal_credentials) + return bls_withdrawal_credentials_bytes + + +def validate_bls_withdrawal_credentials(bls_withdrawal_credentials: str) -> bytes: + bls_withdrawal_credentials_bytes = normalize_bls_withdrawal_credentials_to_bytes(bls_withdrawal_credentials) + try: assert len(bls_withdrawal_credentials_bytes) == 32 assert bls_withdrawal_credentials_bytes[:1] == BLS_WITHDRAWAL_PREFIX diff --git a/tests/test_cli/helpers.py b/tests/test_cli/helpers.py index 8d5aa8b..4342791 100644 --- a/tests/test_cli/helpers.py +++ b/tests/test_cli/helpers.py @@ -35,3 +35,17 @@ def get_uuid(key_file: str) -> str: def get_permissions(path: str, file_name: str) -> str: return oct(os.stat(os.path.join(path, file_name)).st_mode & 0o777) + + +def verify_file_permission(os_ref, folder_path, files): + if os_ref.name == 'posix': + for file_name in files: + assert get_permissions(folder_path, file_name) == '0o440' + + +def prepare_testing_folder(os_ref, testing_folder_name='TESTING_TEMP_FOLDER'): + my_folder_path = os_ref.path.join(os_ref.getcwd(), testing_folder_name) + clean_btec_folder(my_folder_path) + if not os_ref.path.exists(my_folder_path): + os_ref.mkdir(my_folder_path) + return my_folder_path diff --git a/tests/test_cli/test_generate_bls_to_execution_change.py b/tests/test_cli/test_generate_bls_to_execution_change.py index 42e9a1c..b83af59 100644 --- a/tests/test_cli/test_generate_bls_to_execution_change.py +++ b/tests/test_cli/test_generate_bls_to_execution_change.py @@ -6,19 +6,17 @@ from staking_deposit.deposit import cli from staking_deposit.utils.constants import DEFAULT_BLS_TO_EXECUTION_CHANGES_FOLDER_NAME from .helpers import ( clean_btec_folder, - get_permissions, + prepare_testing_folder, + verify_file_permission, ) def test_existing_mnemonic_bls_withdrawal() -> None: # Prepare folder - my_folder_path = os.path.join(os.getcwd(), 'TESTING_TEMP_FOLDER') - clean_btec_folder(my_folder_path) - if not os.path.exists(my_folder_path): - os.mkdir(my_folder_path) + my_folder_path = prepare_testing_folder(os) runner = CliRunner() - inputs = ['0'] # confirm `validator_start_index` + inputs = [] data = '\n'.join(inputs) arguments = [ '--language', 'english', @@ -26,7 +24,7 @@ def test_existing_mnemonic_bls_withdrawal() -> None: '--bls_to_execution_changes_folder', my_folder_path, '--chain', 'mainnet', '--mnemonic', 'sister protect peanut hill ready work profit fit wish want small inflict flip member tail between sick setup bright duck morning sell paper worry', # noqa: E501 - '--bls_withdrawal_credentials', '00bd0b5a34de5fb17df08410b5e615dda87caf4fb72d0aac91ce5e52fc6aa8de', + '--bls_withdrawal_credentials', '0x00bd0b5a34de5fb17df08410b5e615dda87caf4fb72d0aac91ce5e52fc6aa8de', '--validator_start_index', '0', '--validator_index', '1', '--execution_address', '3434343434343434343434343434343434343434', @@ -42,9 +40,44 @@ def test_existing_mnemonic_bls_withdrawal() -> None: assert len(set(btec_files)) == 1 # Verify file permissions - if os.name == 'posix': - for file_name in btec_files: - assert get_permissions(bls_to_execution_changes_folder_path, file_name) == '0o440' + verify_file_permission(os, folder_path=bls_to_execution_changes_folder_path, files=btec_files) + + # Clean up + clean_btec_folder(my_folder_path) + + +def test_existing_mnemonic_bls_withdrawal_interactive() -> None: + # Prepare folder + my_folder_path = prepare_testing_folder(os) + + runner = CliRunner() + inputs = [ + 'mainnet', # network/chain + 'sister protect peanut hill ready work profit fit wish want small inflict flip member tail between sick setup bright duck morning sell paper worry', # noqa: E501 + '0', # validator_start_index + '1', # validator_index + '0x00bd0b5a34de5fb17df08410b5e615dda87caf4fb72d0aac91ce5e52fc6aa8de', + '0x3434343434343434343434343434343434343434', + + ] + data = '\n'.join(inputs) + arguments = [ + '--language', 'english', + 'generate-bls-to-execution-change', + '--bls_to_execution_changes_folder', my_folder_path, + ] + result = runner.invoke(cli, arguments, input=data) + assert result.exit_code == 0 + + # Check files + bls_to_execution_changes_folder_path = os.path.join(my_folder_path, DEFAULT_BLS_TO_EXECUTION_CHANGES_FOLDER_NAME) + _, _, btec_files = next(os.walk(bls_to_execution_changes_folder_path)) + + # TODO verify file content + assert len(set(btec_files)) == 1 + + # Verify file permissions + verify_file_permission(os, folder_path=bls_to_execution_changes_folder_path, files=btec_files) # Clean up clean_btec_folder(my_folder_path)