forked from OKEAMAH/mesc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.py
More file actions
233 lines (214 loc) · 8.51 KB
/
validation.py
File metadata and controls
233 lines (214 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
from __future__ import annotations
import typing
from .exceptions import InvalidConfig
from .types import rpc_config_types, endpoint_types, profile_types
from . import network_utils
if typing.TYPE_CHECKING:
from typing import Sequence
from typing_extensions import Any
def is_valid(config: Any) -> bool:
try:
validate(config)
return True
except InvalidConfig:
return False
def validate(config: Any) -> None:
# all fields present with the correct types
if not isinstance(config, dict):
raise InvalidConfig('RpcConfig must be a dict')
for field, field_type in rpc_config_types.items():
if field not in config:
raise InvalidConfig('RpcConfig is missing field: ' + str(field))
value = config[field]
_check_type('RpcConfig', None, field, field_type, value)
if isinstance(value, dict):
_check_str_keys(field, value)
for name, endpoint in config['endpoints'].items():
for field, field_type in endpoint_types.items():
if field not in endpoint:
raise InvalidConfig(
'Endpoint ' + str(name) + ' is missing field: ' + str(field)
)
value = endpoint[field]
_check_type('Endpoint', name, field, field_type, value)
if isinstance(value, dict):
_check_str_keys(field, value)
for name, profile in config['profiles'].items():
for field, field_type in profile_types.items():
if field not in profile:
raise InvalidConfig(
'Profile ' + str(name) + ' is missing field: ' + str(field)
)
value = profile[field]
_check_type('Profile', name, field, field_type, value)
if isinstance(value, dict):
_check_str_keys(field, value)
for field in ['network_defaults', 'network_names']:
for value in config[field].values():
if not isinstance(value, str):
raise InvalidConfig(
'Entries in '
+ str(field)
+ ' must be strings, but there is a value of type '
+ str(type(value))
)
for name, profile in config['profiles'].items():
for field in ['network_defaults', 'network_names']:
for value in config[field].values():
if not isinstance(value, str):
raise InvalidConfig(
'Entries in '
+ str(field)
+ ' must be strings, but in the profile '
+ name
+ ' there is a value of type '
+ str(type(value))
)
# referenced endpoints exit
endpoint = config['default_endpoint']
if endpoint is not None and endpoint not in config['endpoints']:
raise InvalidConfig(
'Referenced default_endpoint does not exist: ' + str(endpoint)
)
for endpoint in config['network_defaults'].values():
if endpoint not in config['endpoints']:
raise InvalidConfig(
'Referenced endpoint in network_defaults does not exist: '
+ str(endpoint)
)
for name, profile in config['profiles'].items():
endpoint = profile['default_endpoint']
if endpoint is not None and endpoint not in config['endpoints']:
raise InvalidConfig(
'In profile '
+ name
+ ' referenced endpoint does not exist: '
+ str(endpoint)
)
for endpoint in profile['network_defaults'].values():
if endpoint not in config['endpoints']:
raise InvalidConfig(
'In profile '
+ name
+ ' referenced endpoint does not exist: '
+ str(endpoint)
)
# default endpoints of each network actually use that specified network
for chain_id, endpoint_name in config['network_defaults'].items():
if not network_utils.chain_ids_equal(
chain_id, config['endpoints'][endpoint_name]['chain_id']
):
raise InvalidConfig(
'Endpoint is set as the default endpoint of network '
+ chain_id
+ ", but the endpoint's chain_id is different "
+ config['endpoints'][endpoint_name]['chain_id']
)
for profile_name, profile in config['profiles'].items():
for chain_id, endpoint_name in profile['network_defaults'].items():
if not network_utils.chain_ids_equal(
chain_id, config['endpoints'][endpoint_name]['chain_id']
):
raise InvalidConfig(
'Endpoint is set as the default endpoint of network '
+ chain_id
+ ' in profile '
+ profile_name
+ ", but the endpoint's chain_id is different "
+ config['endpoints'][endpoint_name]['chain_id']
)
# endpoint map keys match endpoint name fields
for endpoint_name, endpoint in config['endpoints'].items():
if endpoint['name'] != endpoint_name:
raise InvalidConfig(
'Endpoint does not match endpoint mapping key: '
+ endpoint['name']
+ ' != '
+ endpoint_name
)
# profile map keys match profile name fields
for profile_name, profile in config['profiles'].items():
if profile['name'] != profile_name:
raise InvalidConfig(
'Profile does not match profile mapping key: '
+ profile['name']
+ ' != '
+ profile_name
)
# chain_id's are valid
for chain_id in config['network_defaults'].keys():
if not network_utils.is_chain_id(chain_id):
raise Exception(
'Invalid chain_id used in network_defaults: ' + str(chain_id)
)
for profile_name, profile in config['profiles'].items():
for chain_id in profile['network_defaults'].keys():
if not network_utils.is_chain_id(chain_id):
raise Exception(
'Invalid chain_id used in profile '
+ profile_name
+ ' network_defaults: '
+ str(chain_id)
)
for endpoint in config['endpoints'].values():
chain_id = endpoint['chain_id']
if chain_id is not None and not network_utils.is_chain_id(chain_id):
raise Exception(
'Invalid chain_id used in endpoint '
+ endpoint['name']
+ ': '
+ str(chain_id)
)
# no duplicate default network entries using decimal vs hex
ensure_no_chain_id_collisions(
list(config['network_defaults'].keys()), 'network defaults'
)
for profile_name, profile in config['profiles'].items():
ensure_no_chain_id_collisions(
list(config['network_defaults'].keys()), 'profile ' + profile_name
)
def ensure_no_chain_id_collisions(chain_ids: Sequence[str], name: str) -> None:
hex_numbers = set()
for chain_id in chain_ids:
as_hex = network_utils.chain_id_to_standard_hex(chain_id)
if as_hex in hex_numbers:
raise Exception(
'chain_id collision, '
+ str(name)
+ ' has multiple decimal/hex values for chain_id: '
+ str(chain_id)
)
else:
hex_numbers.add(as_hex)
def _check_type(
datatype: str,
name: str | None,
field: str,
field_type: type | tuple[type, ...],
value: str,
) -> None:
if not isinstance(value, field_type):
if name is not None:
display = datatype + ' ' + name
else:
display = datatype
raise InvalidConfig(
display
+ ' invalid type for field: '
+ str(field)
+ ', it should have type '
+ str(type(field_type))
+ ' but instead has type '
+ str(type(value))
)
def _check_str_keys(field: Any, value: Any) -> None:
for key in value.keys():
if not isinstance(key, str):
raise InvalidConfig(
'Each key within '
+ field
+ ' must be a string, instead '
+ key
+ ' is a '
+ str(type(value))
)