From 73d7da0b413d641eb4425ae558af7f0ef05999c3 Mon Sep 17 00:00:00 2001 From: ernestosperanza Date: Thu, 26 Aug 2021 15:45:51 -0300 Subject: [PATCH] Issue #2095 - Provide constants for e.g. the zero address and max uint256 (#2109) --- docs/constants.rst | 30 +++++++++++++++++++ docs/index.rst | 1 + newsfragments/2109.feature.rst | 1 + .../contracts/test_contract_deployment.py | 6 +++- .../test_contract_method_abi_encoding.py | 5 +++- .../test_time_based_gas_price_strategy.py | 9 ++++-- .../test_name_to_address_middleware.py | 7 +++-- tests/integration/generate_fixtures/common.py | 7 +++-- tests/integration/generate_fixtures/parity.py | 9 ++++-- web3/_utils/module_testing/personal_module.py | 11 ++++--- web3/constants.py | 9 ++++++ 11 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 docs/constants.rst create mode 100644 newsfragments/2109.feature.rst create mode 100644 web3/constants.py diff --git a/docs/constants.rst b/docs/constants.rst new file mode 100644 index 0000000000..ba134caf08 --- /dev/null +++ b/docs/constants.rst @@ -0,0 +1,30 @@ +.. _constants: + +Constants +========= + +The web3.contants module contains commonly used values. + +Strings +******* + +.. code-block:: python + + #The Address Zero, which is 20 bytes (40 nibbles) of zero. + web3.constants.ADDRESS_ZERO + + #The hexadecimal version of Max uint256. + web3.constants.MAX_INT + + #The Hash Zero, which is 32 bytes (64 nibbles) of zero. + web3.constants.HASH_ZERO + +Int +*** + +.. code-block:: python + + #The amount of Wei in one Ether + web3.constants.WEI_PER_ETHER + + diff --git a/docs/index.rst b/docs/index.rst index b28853d814..21faacf45d 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -68,6 +68,7 @@ Table of Contents web3.parity gas_price ens + constants .. toctree:: :maxdepth: 1 diff --git a/newsfragments/2109.feature.rst b/newsfragments/2109.feature.rst new file mode 100644 index 0000000000..d8f2ef32b8 --- /dev/null +++ b/newsfragments/2109.feature.rst @@ -0,0 +1 @@ +Add constants for the zero address, zero hash, max int, and wei per ether. diff --git a/tests/core/contracts/test_contract_deployment.py b/tests/core/contracts/test_contract_deployment.py index deacf2eaa4..20027ea701 100644 --- a/tests/core/contracts/test_contract_deployment.py +++ b/tests/core/contracts/test_contract_deployment.py @@ -5,6 +5,10 @@ decode_hex, ) +from web3 import ( + constants, +) + def test_contract_deployment_no_constructor(web3, MathContract, MATH_RUNTIME): @@ -56,7 +60,7 @@ def test_contract_deployment_with_constructor_with_arguments(web3, @pytest.mark.parametrize('constructor_arg', ( b'1234\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', # noqa: E501 - '0x0000000000000000000000000000000000000000000000000000000000000000') + constants.HASH_ZERO) ) def test_contract_deployment_with_constructor_with_arguments_strict(w3_strict_abi, WithConstructorArgumentsContractStrict, # noqa: E501 diff --git a/tests/core/contracts/test_contract_method_abi_encoding.py b/tests/core/contracts/test_contract_method_abi_encoding.py index c5e4540e93..19ba0f517f 100644 --- a/tests/core/contracts/test_contract_method_abi_encoding.py +++ b/tests/core/contracts/test_contract_method_abi_encoding.py @@ -1,6 +1,9 @@ import json import pytest +from web3 import ( + constants, +) from web3.exceptions import ( ValidationError, ) @@ -125,7 +128,7 @@ def test_contract_abi_encoding_strict_with_error(w3_strict_abi, arguments): ), pytest.param( ABI_C, - ['0x0000000000000000000000000000000000000000000000000000000000000000'], + [constants.HASH_ZERO], None, '0x9f3fab580000000000000000000000000000000000000000000000000000000000000000', id='ABI_C, valid hexstring args, no data' diff --git a/tests/core/gas-strategies/test_time_based_gas_price_strategy.py b/tests/core/gas-strategies/test_time_based_gas_price_strategy.py index d4e4109f06..c1d9906300 100644 --- a/tests/core/gas-strategies/test_time_based_gas_price_strategy.py +++ b/tests/core/gas-strategies/test_time_based_gas_price_strategy.py @@ -1,6 +1,9 @@ import pytest -from web3 import Web3 +from web3 import ( + Web3, + constants, +) from web3.exceptions import ( ValidationError, ) @@ -87,7 +90,7 @@ def _get_block_by_something(method, params): return { 'hash': '0x0000000000000000000000000000000000000000000000000000000000000001', 'number': 1, - 'parentHash': '0x0000000000000000000000000000000000000000000000000000000000000000', + 'parentHash': constants.HASH_ZERO, 'transactions': [ {'gasPrice': 30}, {'gasPrice': 35}, @@ -100,7 +103,7 @@ def _get_block_by_something(method, params): block_identifier == '0x0' ): return { - 'hash': '0x0000000000000000000000000000000000000000000000000000000000000000', + 'hash': constants.HASH_ZERO, 'number': 0, 'parentHash': None, 'transactions': [ diff --git a/tests/core/middleware/test_name_to_address_middleware.py b/tests/core/middleware/test_name_to_address_middleware.py index e586a3d352..3788c29367 100644 --- a/tests/core/middleware/test_name_to_address_middleware.py +++ b/tests/core/middleware/test_name_to_address_middleware.py @@ -1,6 +1,9 @@ import pytest -from web3 import Web3 +from web3 import ( + Web3, + constants, +) from web3.exceptions import ( InvalidAddress, ) @@ -13,7 +16,7 @@ ) NAME = "dump.eth" -ADDRESS = "0x0000000000000000000000000000000000000000" +ADDRESS = constants.ADDRESS_ZERO BALANCE = 0 diff --git a/tests/integration/generate_fixtures/common.py b/tests/integration/generate_fixtures/common.py index 34f947fda1..49ac85b8a2 100644 --- a/tests/integration/generate_fixtures/common.py +++ b/tests/integration/generate_fixtures/common.py @@ -12,6 +12,9 @@ to_text, ) +from web3 import ( + constants, +) from web3.exceptions import ( TransactionNotFound, ) @@ -57,11 +60,11 @@ "0000000000000000000000000000000000000006": {"balance": "1"}, }, "timestamp": "0x00", - "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "parentHash": constants.HASH_ZERO, "extraData": "0x3535353535353535353535353535353535353535353535353535353535353535", "gasLimit": "0x3b9aca00", # 1,000,000,000 "difficulty": "0x10000", - "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "mixhash": constants.HASH_ZERO, "coinbase": COINBASE } diff --git a/tests/integration/generate_fixtures/parity.py b/tests/integration/generate_fixtures/parity.py index d6eb24688a..48d4efa78c 100644 --- a/tests/integration/generate_fixtures/parity.py +++ b/tests/integration/generate_fixtures/parity.py @@ -18,7 +18,10 @@ from tests.utils import ( get_open_port, ) -from web3 import Web3 +from web3 import ( + Web3, + constants, +) CHAIN_CONFIG = { "name": "CrossClient", @@ -75,13 +78,13 @@ "seal": { "ethereum": { "nonce": "0x0000000000000042", - "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000" + "mixHash": constants.HASH_ZERO } }, "difficulty": "0x10000", "author": common.COINBASE, "timestamp": "0x00", - "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "parentHash": constants.HASH_ZERO, "extraData": "0x3535353535353535353535353535353535353535353535353535353535353535", "gasLimit": "0x1000000" }, diff --git a/web3/_utils/module_testing/personal_module.py b/web3/_utils/module_testing/personal_module.py index 20f3793dfc..077aae8d1f 100644 --- a/web3/_utils/module_testing/personal_module.py +++ b/web3/_utils/module_testing/personal_module.py @@ -18,6 +18,9 @@ HexBytes, ) +from web3 import ( + constants, +) from web3.types import ( # noqa: F401 TxParams, Wei, @@ -143,7 +146,7 @@ def test_personal_send_transaction( unlockable_account_dual_type: ChecksumAddress, unlockable_account_pw: str, ) -> None: - assert web3.eth.get_balance(unlockable_account_dual_type) > web3.toWei(1, 'ether') + assert web3.eth.get_balance(unlockable_account_dual_type) > constants.WEI_PER_ETHER txn_params: TxParams = { 'from': unlockable_account_dual_type, 'to': unlockable_account_dual_type, @@ -167,7 +170,7 @@ def test_personal_sendTransaction_deprecated( unlockable_account_dual_type: ChecksumAddress, unlockable_account_pw: str, ) -> None: - assert web3.eth.get_balance(unlockable_account_dual_type) > web3.toWei(1, 'ether') + assert web3.eth.get_balance(unlockable_account_dual_type) > constants.WEI_PER_ETHER txn_params: TxParams = { 'from': unlockable_account_dual_type, 'to': unlockable_account_dual_type, @@ -460,7 +463,7 @@ def test_personal_send_transaction( unlockable_account_dual_type: ChecksumAddress, unlockable_account_pw: str, ) -> None: - assert web3.eth.get_balance(unlockable_account_dual_type) > web3.toWei(1, 'ether') + assert web3.eth.get_balance(unlockable_account_dual_type) > constants.WEI_PER_ETHER txn_params: TxParams = { 'from': unlockable_account_dual_type, 'to': unlockable_account_dual_type, @@ -484,7 +487,7 @@ def test_personal_sendTransaction_deprecated( unlockable_account_dual_type: ChecksumAddress, unlockable_account_pw: str, ) -> None: - assert web3.eth.get_balance(unlockable_account_dual_type) > web3.toWei(1, 'ether') + assert web3.eth.get_balance(unlockable_account_dual_type) > constants.WEI_PER_ETHER txn_params: TxParams = { 'from': unlockable_account_dual_type, 'to': unlockable_account_dual_type, diff --git a/web3/constants.py b/web3/constants.py new file mode 100644 index 0000000000..4f8c58e5bd --- /dev/null +++ b/web3/constants.py @@ -0,0 +1,9 @@ +# Constants as Strings +ADDRESS_ZERO = "0x0000000000000000000000000000000000000000" + +MAX_INT = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + +HASH_ZERO = "0x0000000000000000000000000000000000000000000000000000000000000000" + +# Constants as Int +WEI_PER_ETHER = 1000000000000000000