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
@@ -44,6 +72,14 @@ def transaction_param_validator(web3):
44
72
)
45
73
46
74
75
+ BLOCK_VALIDATORS = {
76
+ 'extraData' : check_extradata_length ,
77
+ }
78
+
79
+
80
+ block_validator = apply_formatters_to_dict (BLOCK_VALIDATORS )
81
+
82
+
47
83
@curry
48
84
def chain_id_validator (web3 ):
49
85
return compose (
@@ -52,13 +88,20 @@ def chain_id_validator(web3):
52
88
)
53
89
54
90
91
+ extra_data_validator = apply_formatter_if (is_not_null , block_validator )
92
+
93
+
55
94
def build_validators_with_web3 (w3 ):
56
95
return dict (
57
96
request_formatters = {
58
97
'eth_sendTransaction' : chain_id_validator (w3 ),
59
98
'eth_estimateGas' : chain_id_validator (w3 ),
60
99
'eth_call' : chain_id_validator (w3 ),
61
- }
100
+ },
101
+ result_formatters = {
102
+ 'eth_getBlockByHash' : extra_data_validator ,
103
+ 'eth_getBlockByNumber' : extra_data_validator ,
104
+ },
62
105
)
63
106
64
107
0 commit comments