4
4
from typing import Type
5
5
6
6
from eth2spec .utils .ssz .ssz_typing import (
7
- View , BasicView , uint , Container , List , boolean ,
8
- Vector , ByteVector , ByteList , Bitlist , Bitvector , Union
7
+ View ,
8
+ BasicView ,
9
+ uint ,
10
+ Container ,
11
+ List ,
12
+ boolean ,
13
+ Vector ,
14
+ ByteVector ,
15
+ ByteList ,
16
+ Bitlist ,
17
+ Bitvector ,
18
+ Union ,
9
19
)
10
20
11
21
# in bytes
@@ -35,12 +45,14 @@ def is_changing(self):
35
45
return self .value in [0 , 4 , 5 ]
36
46
37
47
38
- def get_random_ssz_object (rng : Random ,
39
- typ : Type [View ],
40
- max_bytes_length : int ,
41
- max_list_length : int ,
42
- mode : RandomizationMode ,
43
- chaos : bool ) -> View :
48
+ def get_random_ssz_object (
49
+ rng : Random ,
50
+ typ : Type [View ],
51
+ max_bytes_length : int ,
52
+ max_list_length : int ,
53
+ mode : RandomizationMode ,
54
+ chaos : bool ,
55
+ ) -> View :
44
56
"""
45
57
Create an object for a given type, filled with random data.
46
58
:param rng: The random number generator to use.
@@ -56,24 +68,26 @@ def get_random_ssz_object(rng: Random,
56
68
if issubclass (typ , ByteList ):
57
69
# ByteList array
58
70
if mode == RandomizationMode .mode_nil_count :
59
- return typ (b'' )
71
+ return typ (b"" )
60
72
elif mode == RandomizationMode .mode_max_count :
61
73
return typ (get_random_bytes_list (rng , min (max_bytes_length , typ .limit ())))
62
74
elif mode == RandomizationMode .mode_one_count :
63
75
return typ (get_random_bytes_list (rng , min (1 , typ .limit ())))
64
76
elif mode == RandomizationMode .mode_zero :
65
- return typ (b' \x00 ' * min (1 , typ .limit ()))
77
+ return typ (b" \x00 " * min (1 , typ .limit ()))
66
78
elif mode == RandomizationMode .mode_max :
67
- return typ (b' \xff ' * min (1 , typ .limit ()))
79
+ return typ (b" \xff " * min (1 , typ .limit ()))
68
80
else :
69
- return typ (get_random_bytes_list (rng , rng .randint (0 , min (max_bytes_length , typ .limit ()))))
81
+ return typ (
82
+ get_random_bytes_list (rng , rng .randint (0 , min (max_bytes_length , typ .limit ())))
83
+ )
70
84
if issubclass (typ , ByteVector ):
71
85
# Random byte vectors can be bigger than max bytes size, e.g. custody chunk data.
72
86
# No max-bytes-length limitation here.
73
87
if mode == RandomizationMode .mode_zero :
74
- return typ (b' \x00 ' * typ .type_byte_length ())
88
+ return typ (b" \x00 " * typ .type_byte_length ())
75
89
elif mode == RandomizationMode .mode_max :
76
- return typ (b' \xff ' * typ .type_byte_length ())
90
+ return typ (b" \xff " * typ .type_byte_length ())
77
91
else :
78
92
return typ (get_random_bytes_list (rng , typ .type_byte_length ()))
79
93
elif issubclass (typ , (boolean , uint )):
@@ -99,7 +113,9 @@ def get_random_ssz_object(rng: Random,
99
113
elif mode == RandomizationMode .mode_nil_count :
100
114
length = 0
101
115
102
- if typ .limit () < length : # SSZ imposes a hard limit on lists, we can't put in more than that
116
+ if (
117
+ typ .limit () < length
118
+ ): # SSZ imposes a hard limit on lists, we can't put in more than that
103
119
length = typ .limit ()
104
120
105
121
elem_type = typ .element_cls () if issubclass (typ , List ) else boolean
@@ -110,11 +126,14 @@ def get_random_ssz_object(rng: Random,
110
126
elif issubclass (typ , Container ):
111
127
fields = typ .fields ()
112
128
# Container
113
- return typ (** {
114
- field_name :
115
- get_random_ssz_object (rng , field_type , max_bytes_length , max_list_length , mode , chaos )
116
- for field_name , field_type in fields .items ()
117
- })
129
+ return typ (
130
+ ** {
131
+ field_name : get_random_ssz_object (
132
+ rng , field_type , max_bytes_length , max_list_length , mode , chaos
133
+ )
134
+ for field_name , field_type in fields .items ()
135
+ }
136
+ )
118
137
elif issubclass (typ , Union ):
119
138
options = typ .options ()
120
139
selector : int
@@ -129,7 +148,9 @@ def get_random_ssz_object(rng: Random,
129
148
if elem_type is None :
130
149
elem = None
131
150
else :
132
- elem = get_random_ssz_object (rng , elem_type , max_bytes_length , max_list_length , mode , chaos )
151
+ elem = get_random_ssz_object (
152
+ rng , elem_type , max_bytes_length , max_list_length , mode , chaos
153
+ )
133
154
return typ (selector = selector , value = elem )
134
155
else :
135
156
raise Exception (f"Type not recognized: typ={ typ } " )
0 commit comments