Skip to content

Commit

Permalink
add ExtraDataLengthError (#1666)
Browse files Browse the repository at this point in the history
* feat: add ExtraDataLengthError exception

* style: raise ExtraDataLengthError when extraData > 32 bytes

* test: update extraData test case to expect ExtraDataLengthError

* Add newsfragment

Co-authored-by: Marc Garreau <marcdgarreau@gmail.com>
  • Loading branch information
iamdefinitelyahuman and wolovim committed Jun 3, 2020
1 parent 7cc9967 commit 7e26088
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions 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.
4 changes: 2 additions & 2 deletions 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,
Expand All @@ -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')


Expand Down
7 changes: 7 additions & 0 deletions web3/exceptions.py
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions web3/middleware/validation.py
Expand Up @@ -24,6 +24,7 @@
RPC,
)
from web3.exceptions import (
ExtraDataLengthError,
ValidationError,
)
from web3.middleware.formatting import (
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7e26088

Please sign in to comment.