1
1
from cytoolz import (
2
+ complement ,
2
3
compose ,
3
4
curry ,
4
5
dissoc ,
5
6
)
6
7
from eth_utils .curried import (
7
8
apply_formatter_at_index ,
9
+ apply_formatter_if ,
8
10
apply_formatters_to_dict ,
11
+ is_null ,
12
+ )
13
+ from hexbytes import (
14
+ HexBytes ,
9
15
)
10
16
11
17
from web3 .exceptions import (
15
21
construct_web3_formatting_middleware ,
16
22
)
17
23
24
+ MAX_EXTRADATA_LENGTH = 32
25
+
26
+
27
+ is_not_null = complement (is_null )
28
+
18
29
19
30
@curry
20
31
def validate_chain_id (web3 , chain_id ):
@@ -30,6 +41,23 @@ def validate_chain_id(web3, chain_id):
30
41
)
31
42
32
43
44
+ def check_extradata_length (val ):
45
+ if not isinstance (val , (str , int , bytes )):
46
+ return val
47
+ result = HexBytes (val )
48
+ if len (result ) > MAX_EXTRADATA_LENGTH :
49
+ raise ValidationError (
50
+ "The field extraData is %d bytes, but should be %d. "
51
+ "It is quite likely that you are connected to a POA chain. "
52
+ "Refer "
53
+ "http://web3py.readthedocs.io/en/latest/middleware.html#geth-style-proof-of-authority "
54
+ "for more details. The full extraData is: %r" % (
55
+ len (result ), MAX_EXTRADATA_LENGTH , result
56
+ )
57
+ )
58
+ return val
59
+
60
+
33
61
def transaction_normalizer (transaction ):
34
62
return dissoc (transaction , 'chainId' )
35
63
@@ -48,6 +76,14 @@ def transaction_param_validator(web3):
48
76
)
49
77
50
78
79
+ BLOCK_VALIDATORS = {
80
+ 'extraData' : check_extradata_length ,
81
+ }
82
+
83
+
84
+ block_validator = apply_formatters_to_dict (BLOCK_VALIDATORS )
85
+
86
+
51
87
@curry
52
88
def chain_id_validator (web3 ):
53
89
return compose (
@@ -56,13 +92,20 @@ def chain_id_validator(web3):
56
92
)
57
93
58
94
95
+ extra_data_validator = apply_formatter_if (is_not_null , block_validator )
96
+
97
+
59
98
def build_validators_with_web3 (w3 ):
60
99
return dict (
61
100
request_formatters = {
62
101
'eth_sendTransaction' : chain_id_validator (w3 ),
63
102
'eth_estimateGas' : chain_id_validator (w3 ),
64
103
'eth_call' : chain_id_validator (w3 ),
65
- }
104
+ },
105
+ result_formatters = {
106
+ 'eth_getBlockByHash' : extra_data_validator ,
107
+ 'eth_getBlockByNumber' : extra_data_validator ,
108
+ },
66
109
)
67
110
68
111
0 commit comments