1
1
import functools
2
2
3
+ from typing import (
4
+ Any ,
5
+ Dict ,
6
+ Iterable ,
7
+ Sequence ,
8
+ Tuple ,
9
+ Type ,
10
+ TYPE_CHECKING ,
11
+ Union ,
12
+ )
13
+
14
+ from eth_typing import (
15
+ Address ,
16
+ Hash32 ,
17
+ )
18
+
3
19
from eth_utils import (
4
20
ValidationError ,
5
21
)
24
40
UINT_256_MAX ,
25
41
)
26
42
43
+ if TYPE_CHECKING :
44
+ from eth .vm .base import BaseVM # noqa: F401
45
+
27
46
28
- def validate_is_bytes (value , title = "Value" ):
47
+ def validate_is_bytes (value : bytes , title : str = "Value" ) -> None :
29
48
if not isinstance (value , bytes ):
30
49
raise ValidationError (
31
50
"{title} must be a byte string. Got: {0}" .format (type (value ), title = title )
32
51
)
33
52
34
53
35
- def validate_is_integer (value , title = "Value" ):
54
+ def validate_is_integer (value : Union [ int , bool ], title : str = "Value" ) -> None :
36
55
if not isinstance (value , int ) or isinstance (value , bool ):
37
56
raise ValidationError (
38
57
"{title} must be a an integer. Got: {0}" .format (type (value ), title = title )
39
58
)
40
59
41
60
42
- def validate_length (value , length , title = "Value" ):
61
+ def validate_length (value : Sequence [ Any ] , length : int , title : str = "Value" ) -> None :
43
62
if not len (value ) == length :
44
63
raise ValidationError (
45
64
"{title} must be of length {0}. Got {1} of length {2}" .format (
@@ -51,7 +70,7 @@ def validate_length(value, length, title="Value"):
51
70
)
52
71
53
72
54
- def validate_length_lte (value , maximum_length , title = "Value" ):
73
+ def validate_length_lte (value : Sequence [ Any ] , maximum_length : int , title : str = "Value" ) -> None :
55
74
if len (value ) > maximum_length :
56
75
raise ValidationError (
57
76
"{title} must be of length less than or equal to {0}. "
@@ -64,7 +83,7 @@ def validate_length_lte(value, maximum_length, title="Value"):
64
83
)
65
84
66
85
67
- def validate_gte (value , minimum , title = "Value" ):
86
+ def validate_gte (value : int , minimum : int , title : str = "Value" ) -> None :
68
87
if value < minimum :
69
88
raise ValidationError (
70
89
"{title} {0} is not greater than or equal to {1}" .format (
@@ -76,15 +95,15 @@ def validate_gte(value, minimum, title="Value"):
76
95
validate_is_integer (value )
77
96
78
97
79
- def validate_gt (value , minimum , title = "Value" ):
98
+ def validate_gt (value : int , minimum : int , title : str = "Value" ) -> None :
80
99
if value <= minimum :
81
100
raise ValidationError (
82
101
"{title} {0} is not greater than {1}" .format (value , minimum , title = title )
83
102
)
84
103
validate_is_integer (value , title = title )
85
104
86
105
87
- def validate_lte (value , maximum , title = "Value" ):
106
+ def validate_lte (value : int , maximum : int , title : str = "Value" ) -> None :
88
107
if value > maximum :
89
108
raise ValidationError (
90
109
"{title} {0} is not less than or equal to {1}" .format (
@@ -96,36 +115,36 @@ def validate_lte(value, maximum, title="Value"):
96
115
validate_is_integer (value , title = title )
97
116
98
117
99
- def validate_lt (value , maximum , title = "Value" ):
118
+ def validate_lt (value : int , maximum : int , title : str = "Value" ) -> None :
100
119
if value >= maximum :
101
120
raise ValidationError (
102
121
"{title} {0} is not less than {1}" .format (value , maximum , title = title )
103
122
)
104
123
validate_is_integer (value , title = title )
105
124
106
125
107
- def validate_canonical_address (value , title = "Value" ):
126
+ def validate_canonical_address (value : Address , title : str = "Value" ) -> None :
108
127
if not isinstance (value , bytes ) or not len (value ) == 20 :
109
128
raise ValidationError (
110
129
"{title} {0} is not a valid canonical address" .format (value , title = title )
111
130
)
112
131
113
132
114
- def validate_multiple_of (value , multiple_of , title = "Value" ):
133
+ def validate_multiple_of (value : int , multiple_of : int , title : str = "Value" ) -> None :
115
134
if not value % multiple_of == 0 :
116
135
raise ValidationError (
117
136
"{title} {0} is not a multiple of {1}" .format (value , multiple_of , title = title )
118
137
)
119
138
120
139
121
- def validate_is_boolean (value , title = "Value" ):
140
+ def validate_is_boolean (value : bool , title : str = "Value" ) -> None :
122
141
if not isinstance (value , bool ):
123
142
raise ValidationError (
124
143
"{title} must be an boolean. Got type: {0}" .format (type (value ), title = title )
125
144
)
126
145
127
146
128
- def validate_word (value , title = "Value" ):
147
+ def validate_word (value : Hash32 , title : str = "Value" ) -> None :
129
148
if not isinstance (value , bytes ):
130
149
raise ValidationError (
131
150
"{title} is not a valid word. Must be of bytes type: Got: {0}" .format (
@@ -142,7 +161,7 @@ def validate_word(value, title="Value"):
142
161
)
143
162
144
163
145
- def validate_uint256 (value , title = "Value" ):
164
+ def validate_uint256 (value : int , title : str = "Value" ) -> None :
146
165
if not isinstance (value , int ) or isinstance (value , bool ):
147
166
raise ValidationError (
148
167
"{title} must be an integer: Got: {0}" .format (
@@ -166,7 +185,7 @@ def validate_uint256(value, title="Value"):
166
185
)
167
186
168
187
169
- def validate_stack_item (value ) :
188
+ def validate_stack_item (value : Union [ int , bytes ]) -> None :
170
189
if isinstance (value , bytes ) and len (value ) <= 32 :
171
190
return
172
191
elif isinstance (value , int ) and 0 <= value <= UINT_256_MAX :
@@ -181,7 +200,7 @@ def validate_stack_item(value):
181
200
validate_lt_secpk1n2 = functools .partial (validate_lte , maximum = SECPK1_N // 2 - 1 )
182
201
183
202
184
- def validate_unique (values , title = "Value" ):
203
+ def validate_unique (values : Iterable [ Any ] , title : str = "Value" ) -> None :
185
204
if not isdistinct (values ):
186
205
duplicates = pipe (
187
206
values ,
@@ -198,27 +217,27 @@ def validate_unique(values, title="Value"):
198
217
)
199
218
200
219
201
- def validate_block_number (block_number , title = "Block Number" ):
220
+ def validate_block_number (block_number : int , title : str = "Block Number" ) -> None :
202
221
validate_is_integer (block_number , title )
203
222
validate_gte (block_number , 0 , title )
204
223
205
224
206
- def validate_vm_block_numbers (vm_block_numbers ) :
225
+ def validate_vm_block_numbers (vm_block_numbers : Iterable [ int ]) -> None :
207
226
validate_unique (vm_block_numbers , title = "Block Number set" )
208
227
209
228
for block_number in vm_block_numbers :
210
229
validate_block_number (block_number )
211
230
212
231
213
- def validate_vm_configuration (vm_configuration ) :
232
+ def validate_vm_configuration (vm_configuration : Tuple [ Tuple [ int , Type [ 'BaseVM' ]], ...]) -> None :
214
233
validate_vm_block_numbers (tuple (
215
234
block_number
216
235
for block_number , _
217
236
in vm_configuration
218
237
))
219
238
220
239
221
- def validate_gas_limit (gas_limit , parent_gas_limit ) :
240
+ def validate_gas_limit (gas_limit : int , parent_gas_limit : int ) -> None :
222
241
if gas_limit < GAS_LIMIT_MINIMUM :
223
242
raise ValidationError ("Gas limit {0} is below minimum {1}" .format (
224
243
gas_limit , GAS_LIMIT_MINIMUM ))
@@ -245,7 +264,7 @@ def validate_gas_limit(gas_limit, parent_gas_limit):
245
264
}
246
265
247
266
248
- def validate_header_params_for_configuration (header_params ) :
267
+ def validate_header_params_for_configuration (header_params : Dict [ str , Any ]) -> None :
249
268
extra_fields = set (header_params .keys ()).difference (ALLOWED_HEADER_FIELDS )
250
269
if extra_fields :
251
270
raise ValidationError (
0 commit comments