Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ExtraDataLengthError #1666

Merged
merged 4 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/1666.feature.rst
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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