Skip to content

Commit

Permalink
Merge pull request #65 from remyroy/replace-assert
Browse files Browse the repository at this point in the history
Replace asserts with raising a ValidatorError
  • Loading branch information
valefar-on-discord authored Jun 13, 2024
2 parents 42b216b + c80706b commit 39acf2f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
4 changes: 3 additions & 1 deletion ethstaker_deposit/key_handling/key_derivation/mnemonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Sequence,
)

from ethstaker_deposit.exceptions import ValidationError
from ethstaker_deposit.utils.constants import (
MNEMONIC_LANG_OPTIONS,
)
Expand Down Expand Up @@ -127,7 +128,8 @@ def reconstruct_mnemonic(mnemonic: str, words_path: str) -> Optional[str]:
This check guarantees that only one language has a valid mnemonic.
It is needed to ensure abbrivated words aren't valid in multiple languages
"""
assert reconstructed_mnemonic is None
if reconstructed_mnemonic is not None:
raise ValidationError("This mnemonic abbreviated form is available in multiple languages.")
reconstructed_mnemonic = ' '.join([_index_to_word(full_word_list, index) for index in word_indices])
else:
pass
Expand Down
5 changes: 3 additions & 2 deletions ethstaker_deposit/utils/intl.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ def _get_from_dict(dataDict: Dict[str, Any], mapList: Iterable[str]) -> str:
'''
try:
ans = reduce(dict.get, mapList, dataDict)
assert isinstance(ans, str)
if not isinstance(ans, str):
raise ValidationError('Incomplete')
return ans
except TypeError:
raise KeyError('%s not in internationalisation json file.' % mapList)
except AssertionError:
except ValidationError:
raise KeyError('The provided params (%s) were incomplete.' % mapList)


Expand Down
18 changes: 11 additions & 7 deletions ethstaker_deposit/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,16 @@ def validate_password_strength(password: str) -> str:

def validate_int_range(num: Any, low: int, high: int) -> int:
'''
Verifies that `num` is an `int` andlow <= num < high
Verifies that `num` is an `int` and low <= num < high
'''
try:
num_int = int(num) # Try cast to int
assert num_int == float(num) # Check num is not float
assert low <= num_int < high # Check num in range
if num_int != float(num): # Check num is not float
raise ValidationError('Num is float')
if not (low <= num_int < high): # Check num in range
raise ValidationError('Num is not in range')
return num_int
except (ValueError, AssertionError):
except (ValueError, ValidationError):
raise ValidationError(load_text(['err_not_positive_integer']))


Expand Down Expand Up @@ -263,9 +265,11 @@ def validate_bls_withdrawal_credentials(bls_withdrawal_credentials: str) -> byte
raise ValidationError(load_text(['err_is_already_eth1_form']) + '\n')

try:
assert len(bls_withdrawal_credentials_bytes) == 32
assert bls_withdrawal_credentials_bytes[:1] == BLS_WITHDRAWAL_PREFIX
except (ValueError, AssertionError):
if len(bls_withdrawal_credentials_bytes) != 32:
raise ValidationError('Length is not 32')
if bls_withdrawal_credentials_bytes[:1] != BLS_WITHDRAWAL_PREFIX:
raise ValidationError('Prefix is wrong')
except (ValueError, ValidationError):
raise ValidationError(load_text(['err_not_bls_form']) + '\n')

return bls_withdrawal_credentials_bytes
Expand Down

0 comments on commit 39acf2f

Please sign in to comment.