5
5
)
6
6
import functools
7
7
8
+ from typing import (
9
+ Any ,
10
+ AnyStr ,
11
+ Callable ,
12
+ Dict ,
13
+ List ,
14
+ Sequence ,
15
+ Tuple ,
16
+ )
17
+
8
18
from cytoolz import (
9
19
assoc_in ,
10
20
compose ,
15
25
)
16
26
import cytoolz .curried
17
27
28
+ from eth_typing import (
29
+ Address ,
30
+ )
31
+
18
32
from eth_utils import (
19
33
apply_formatters_to_dict ,
20
34
big_endian_to_int ,
41
55
is_cleanly_mergable ,
42
56
)
43
57
58
+ from eth .typing import (
59
+ AccountState ,
60
+ GeneralState ,
61
+ )
62
+
44
63
45
64
#
46
65
# Primitives
47
66
#
48
67
@functools .lru_cache (maxsize = 1024 )
49
- def normalize_int (value ) :
68
+ def normalize_int (value : Any ) -> int :
50
69
"""
51
70
Robust to integer conversion, handling hex values, string representations,
52
71
and special cases like `0x`.
@@ -66,7 +85,7 @@ def normalize_int(value):
66
85
raise TypeError ("Unsupported type: Got `{0}`" .format (type (value )))
67
86
68
87
69
- def normalize_bytes (value ) :
88
+ def normalize_bytes (value : Any ) -> bytes :
70
89
if is_bytes (value ):
71
90
return value
72
91
elif is_text (value ) and is_hex (value ):
@@ -78,7 +97,7 @@ def normalize_bytes(value):
78
97
79
98
80
99
@functools .lru_cache (maxsize = 1024 )
81
- def to_int (value ) :
100
+ def to_int (value : Any ) -> int :
82
101
"""
83
102
Robust to integer conversion, handling hex values, string representations,
84
103
and special cases like `0x`.
@@ -93,7 +112,7 @@ def to_int(value):
93
112
94
113
95
114
@functools .lru_cache (maxsize = 128 )
96
- def normalize_to_address (value ) :
115
+ def normalize_to_address (value : AnyStr ) -> Address :
97
116
if value :
98
117
return to_canonical_address (value )
99
118
else :
@@ -106,21 +125,28 @@ def normalize_to_address(value):
106
125
#
107
126
# Containers
108
127
#
109
- def dict_normalizer (formatters , required = None , optional = None ):
128
+
129
+ NormalizerType = Callable [[Dict [Any , Any ]], Iterable [Tuple [Any , Any ]]]
130
+
131
+
132
+ def dict_normalizer (formatters : Dict [Any , Any ],
133
+ required : Iterable [Any ]= None ,
134
+ optional : Iterable [Any ]= None ) -> NormalizerType :
135
+
110
136
all_keys = set (formatters .keys ())
111
137
112
138
if required is None and optional is None :
113
- required = all_keys
139
+ required_set_form = all_keys
114
140
elif required is not None :
115
- required = set (required )
141
+ required_set_form = set (required )
116
142
elif optional is not None :
117
- required = all_keys - set (optional )
143
+ required_set_form = all_keys - set (optional )
118
144
else :
119
145
raise ValueError ("Both required and optional keys specified" )
120
146
121
- def normalizer (d ) :
147
+ def normalizer (d : Dict [ Any , Any ]) -> Iterable [ Tuple [ Any , Any ]] :
122
148
keys = set (d .keys ())
123
- missing_keys = required - keys
149
+ missing_keys = required_set_form - keys
124
150
superfluous_keys = keys - all_keys
125
151
if missing_keys :
126
152
raise KeyError ("Missing required keys: {}" .format (", " .join (missing_keys )))
@@ -132,9 +158,9 @@ def normalizer(d):
132
158
return normalizer
133
159
134
160
135
- def dict_options_normalizer (normalizers ) :
161
+ def dict_options_normalizer (normalizers : Iterable [ Callable [..., Any ]]) -> Callable [..., Any ] :
136
162
137
- def normalize (d ) :
163
+ def normalize (d : Dict [ Any , Any ]) -> Callable [..., Any ] :
138
164
first_exception = None
139
165
for normalizer in normalizers :
140
166
try :
@@ -153,7 +179,7 @@ def normalize(d):
153
179
#
154
180
# Composition
155
181
#
156
- def state_definition_to_dict (state_definition ) :
182
+ def state_definition_to_dict (state_definition : GeneralState ) -> AccountState :
157
183
"""Convert a state definition to the canonical dict form.
158
184
159
185
State can either be defined in the canonical form, or as a list of sub states that are then
@@ -305,7 +331,9 @@ def state_definition_to_dict(state_definition):
305
331
#
306
332
# Fixture Normalizers
307
333
#
308
- def normalize_unsigned_transaction (transaction , indexes ):
334
+ def normalize_unsigned_transaction (transaction : Dict [str , Any ],
335
+ indexes : Dict [str , Any ]) -> Dict [str , Any ]:
336
+
309
337
normalized = normalize_transaction_group (transaction )
310
338
return merge (normalized , {
311
339
transaction_key : normalized [transaction_key ][indexes [index_key ]]
@@ -318,7 +346,7 @@ def normalize_unsigned_transaction(transaction, indexes):
318
346
})
319
347
320
348
321
- def normalize_account_state (account_state ) :
349
+ def normalize_account_state (account_state : AccountState ) -> AccountState :
322
350
return {
323
351
to_canonical_address (address ): {
324
352
'balance' : to_int (state ['balance' ]),
@@ -333,14 +361,17 @@ def normalize_account_state(account_state):
333
361
334
362
335
363
@to_dict
336
- def normalize_post_state (post_state ) :
364
+ def normalize_post_state (post_state : Dict [ str , Any ]) -> Iterable [ Tuple [ str , bytes ]] :
337
365
yield 'hash' , decode_hex (post_state ['hash' ])
338
366
if 'logs' in post_state :
339
367
yield 'logs' , decode_hex (post_state ['logs' ])
340
368
341
369
342
370
@curry
343
- def normalize_statetest_fixture (fixture , fork , post_state_index ):
371
+ def normalize_statetest_fixture (fixture : Dict [str , Any ],
372
+ fork : str ,
373
+ post_state_index : int ) -> Dict [str , Any ]:
374
+
344
375
post_state = fixture ['post' ][fork ][post_state_index ]
345
376
346
377
normalized_fixture = {
@@ -356,7 +387,7 @@ def normalize_statetest_fixture(fixture, fork, post_state_index):
356
387
return normalized_fixture
357
388
358
389
359
- def normalize_exec (exec_params ) :
390
+ def normalize_exec (exec_params : Dict [ str , Any ]) -> Dict [ str , Any ] :
360
391
return {
361
392
'origin' : to_canonical_address (exec_params ['origin' ]),
362
393
'address' : to_canonical_address (exec_params ['address' ]),
@@ -368,7 +399,7 @@ def normalize_exec(exec_params):
368
399
}
369
400
370
401
371
- def normalize_callcreates (callcreates ) :
402
+ def normalize_callcreates (callcreates : Sequence [ Dict [ str , Any ]]) -> List [ Dict [ str , Any ]] :
372
403
return [
373
404
{
374
405
'data' : decode_hex (created_call ['data' ]),
@@ -384,7 +415,7 @@ def normalize_callcreates(callcreates):
384
415
385
416
386
417
@to_dict
387
- def normalize_vmtest_fixture (fixture ) :
418
+ def normalize_vmtest_fixture (fixture : Dict [ str , Any ]) -> Iterable [ Tuple [ str , Any ]] :
388
419
yield 'env' , normalize_environment (fixture ['env' ])
389
420
yield 'exec' , normalize_exec (fixture ['exec' ])
390
421
yield 'pre' , normalize_account_state (fixture ['pre' ])
@@ -405,7 +436,7 @@ def normalize_vmtest_fixture(fixture):
405
436
yield 'logs' , decode_hex (fixture ['logs' ])
406
437
407
438
408
- def normalize_signed_transaction (transaction ) :
439
+ def normalize_signed_transaction (transaction : Dict [ str , Any ]) -> Dict [ str , Any ] :
409
440
return {
410
441
'data' : robust_decode_hex (transaction ['data' ]),
411
442
'gasLimit' : to_int (transaction ['gasLimit' ]),
@@ -420,7 +451,7 @@ def normalize_signed_transaction(transaction):
420
451
421
452
422
453
@curry
423
- def normalize_transactiontest_fixture (fixture , fork ) :
454
+ def normalize_transactiontest_fixture (fixture : Dict [ str , Any ], fork : str ) -> Dict [ str , Any ] :
424
455
425
456
normalized_fixture = {}
426
457
@@ -440,7 +471,7 @@ def normalize_transactiontest_fixture(fixture, fork):
440
471
return normalized_fixture
441
472
442
473
443
- def normalize_block_header (header ) :
474
+ def normalize_block_header (header : Dict [ str , Any ]) -> Dict [ str , Any ] :
444
475
normalized_header = {
445
476
'bloom' : big_endian_to_int (decode_hex (header ['bloom' ])),
446
477
'coinbase' : to_canonical_address (header ['coinbase' ]),
@@ -468,7 +499,7 @@ def normalize_block_header(header):
468
499
return normalized_header
469
500
470
501
471
- def normalize_block (block ) :
502
+ def normalize_block (block : Dict [ str , Any ]) -> Dict [ str , Any ] :
472
503
normalized_block = {}
473
504
474
505
try :
@@ -487,7 +518,7 @@ def normalize_block(block):
487
518
return normalized_block
488
519
489
520
490
- def normalize_blockchain_fixtures (fixture ) :
521
+ def normalize_blockchain_fixtures (fixture : Dict [ str , Any ]) -> Dict [ str , Any ] :
491
522
normalized_fixture = {
492
523
'blocks' : [normalize_block (block_fixture ) for block_fixture in fixture ['blocks' ]],
493
524
'genesisBlockHeader' : normalize_block_header (fixture ['genesisBlockHeader' ]),
0 commit comments