feat(utils): Add address and validation utility modules with comprehensive tests #1995
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds two new utility modules to
src/ethereum/utils/that provide commonly needed helper functions for Ethereum address operations and data validation. Both modules include comprehensive unit tests.Changes
New Files
src/ethereum/utils/address.pyAddress utility functions including:
is_valid_address_length()- Validate 20-byte address lengthis_zero_address()- Check if address is the zero addressis_precompile_address()- Check if address is a precompile (configurable max)address_to_uint()- Convert address to unsigned integeruint_to_address()- Convert unsigned integer to addressto_checksum_address()- Convert to EIP-55 checksummed stringis_valid_checksum_address()- Validate EIP-55 checksumsrc/ethereum/utils/validation.pyData validation utilities including:
is_valid_gas_limit()- Validate gas limit boundsis_valid_nonce()- Validate nonce per EIP-2681is_valid_block_number()- Validate block numbersis_valid_u256()/is_valid_u64()- Validate integer boundsis_valid_hash()/is_zero_hash()- Hash validationis_valid_chain_id()- Validate chain IDs per EIP-2294validate_transaction_value()- Validate transaction valuesis_valid_base_fee()- Validate base fee (post-London)Test Files
tests/common/test_address_utils.py- Comprehensive tests for address utilitiestests/common/test_validation_utils.py- Comprehensive tests for validation utilitiesModified Files
src/ethereum/utils/__init__.py- Updated docstring to document available modulesMotivation
These utilities consolidate common validation and address operations that are used across multiple forks. By centralizing them in the
utilspackage:hexadecimal.py,numeric.py,byte.py)Testing
All new functions include comprehensive unit tests covering:
References