Skip to content

Commit

Permalink
added a check for POA extraData
Browse files Browse the repository at this point in the history
  • Loading branch information
voith committed Apr 29, 2018
1 parent c8320cc commit eeb973e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
5 changes: 4 additions & 1 deletion tests/core/eth-module/test_poa.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest

from web3.exceptions import (
ValidationError,
)
from web3.middleware import (
construct_fixture_middleware,
geth_poa_middleware,
Expand All @@ -12,7 +15,7 @@ def test_long_extra_data(web3):
'eth_getBlockByNumber': {'extraData': '0x' + 'ff' * 33},
})
web3.middleware_stack.inject(return_block_with_long_extra_data, layer=0)
with pytest.raises(ValueError):
with pytest.raises(ValidationError):
web3.eth.getBlock('latest')


Expand Down
44 changes: 43 additions & 1 deletion web3/middleware/validation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from cytoolz import (
complement,
compose,
curry,
dissoc,
)
from eth_utils.curried import (
apply_formatter_at_index,
apply_formatter_if,
apply_formatters_to_dict,
is_null,
)
from hexbytes import (
HexBytes,
)

from web3.exceptions import (
Expand All @@ -16,6 +22,12 @@
)


MAX_EXTRADATA_LENGTH = 32


is_not_null = complement(is_null)


@curry
def validate_chain_id(web3, chain_id):
if chain_id == web3.net.chainId:
Expand All @@ -30,6 +42,23 @@ def validate_chain_id(web3, chain_id):
)


def check_extradata_length(val):
if not isinstance(val, (str, int, bytes)):
return val
result = HexBytes(val)
if len(result) > MAX_EXTRADATA_LENGTH:
raise ValidationError(
"The field extraData is %d bytes, but should be %d. "
"It is quite likely that you are connected to a POA chain. "
"Refer "
"http://web3py.readthedocs.io/en/latest/middleware.html#geth-style-proof-of-authority "
"for more details. The full extraData is: %r" % (
len(result), MAX_EXTRADATA_LENGTH, result
)
)
return val


def transaction_normalizer(transaction):
return dissoc(transaction, 'chainId')

Expand All @@ -43,6 +72,13 @@ def transaction_param_validator(web3):
0
)

BLOCK_VALIDATORS = {
'extraData': check_extradata_length,
}


block_validator = apply_formatters_to_dict(BLOCK_VALIDATORS)


@curry
def chain_id_validator(web3):
Expand All @@ -51,14 +87,20 @@ def chain_id_validator(web3):
transaction_param_validator(web3)
)

extra_data_validator = apply_formatter_if(is_not_null, block_validator)


def build_validators_with_web3(w3):
return dict(
request_formatters={
'eth_sendTransaction': chain_id_validator(w3),
'eth_estimateGas': chain_id_validator(w3),
'eth_call': chain_id_validator(w3),
}
},
result_formatters={
'eth_getBlockByHash': extra_data_validator,
'eth_getBlockByNumber': extra_data_validator,
},
)


Expand Down

0 comments on commit eeb973e

Please sign in to comment.