diff --git a/newsfragments/1666.feature.rst b/newsfragments/1666.feature.rst new file mode 100644 index 0000000000..802102fc0e --- /dev/null +++ b/newsfragments/1666.feature.rst @@ -0,0 +1 @@ +Introduce a more specific validation error, ``ExtraDataLengthError``. This enables tools to detect when someone may be connected to a POA network, for example, and provide a smoother developer experience. diff --git a/tests/core/eth-module/test_poa.py b/tests/core/eth-module/test_poa.py index 2600cb10f2..370dd48537 100644 --- a/tests/core/eth-module/test_poa.py +++ b/tests/core/eth-module/test_poa.py @@ -1,7 +1,7 @@ import pytest from web3.exceptions import ( - ValidationError, + ExtraDataLengthError, ) from web3.middleware import ( construct_fixture_middleware, @@ -15,7 +15,7 @@ def test_long_extra_data(web3): 'eth_getBlockByNumber': {'extraData': '0x' + 'ff' * 33}, }) web3.middleware_onion.inject(return_block_with_long_extra_data, layer=0) - with pytest.raises(ValidationError): + with pytest.raises(ExtraDataLengthError): web3.eth.getBlock('latest') diff --git a/web3/exceptions.py b/web3/exceptions.py index be6c409321..194f714f0c 100644 --- a/web3/exceptions.py +++ b/web3/exceptions.py @@ -100,6 +100,13 @@ class ValidationError(Exception): pass +class ExtraDataLengthError(ValidationError): + """ + Raised when an RPC call returns >32 bytes of extraData. + """ + pass + + class NoABIFunctionsFound(AttributeError): """ Raised when an ABI is present, but doesn't contain any functions. diff --git a/web3/middleware/validation.py b/web3/middleware/validation.py index a4006fb38d..b8d1b0cc25 100644 --- a/web3/middleware/validation.py +++ b/web3/middleware/validation.py @@ -24,6 +24,7 @@ RPC, ) from web3.exceptions import ( + ExtraDataLengthError, ValidationError, ) from web3.middleware.formatting import ( @@ -61,10 +62,10 @@ def check_extradata_length(val: Any) -> Any: return val result = HexBytes(val) if len(result) > MAX_EXTRADATA_LENGTH: - raise ValidationError( + raise ExtraDataLengthError( "The field extraData is %d bytes, but should be %d. " "It is quite likely that you are connected to a POA chain. " - "Refer " + "Refer to " "http://web3py.readthedocs.io/en/stable/middleware.html#geth-style-proof-of-authority " "for more details. The full extraData is: %r" % ( len(result), MAX_EXTRADATA_LENGTH, result