-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_official_sim.py
357 lines (328 loc) · 11 KB
/
run_official_sim.py
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import argparse
import multiprocessing
import os
import sys
from datetime import datetime
from importlib import reload
import pathlib
import random
from simulator import SimulationBuilder
from eth2spec.config import config_util
from eth2spec.phase0 import spec
from pathvalidation import valid_writable_path
def calc_simtime(slot, epoch=None, seconds=None):
"""
Calculate timestamps for slots, epochs and timedeltas in seconds
If only slot is specified, the start slot time is calculated.
If both slot and epoch are specified, slot is relative to epoch.
"""
time = spec.SECONDS_PER_SLOT * slot
if epoch is not None:
time += epoch * spec.SECONDS_PER_SLOT * spec.SLOTS_PER_EPOCH
if seconds is not None:
time += seconds
return int(time)
def simulation0(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(1844674407370955161)\
.set_custom_latency_map(None, modifier=lambda latency: latency // 2)\
.beacon_node(4)\
.set_debug(True)\
.validators(64)\
.build()\
.build()\
.build()
def simulation1(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(1844674407370955161)\
.set_custom_latency_map(None, modifier=lambda latency: latency // 2)\
.beacon_node(11)\
.set_debug(True)\
.validators(21)\
.build()\
.build()\
.beacon_node(1)\
.set_debug(True)\
.validators(25)\
.build()\
.build()\
.build()
def simulation2(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(1844674407370955161)\
.set_custom_latency_map(None, modifier=lambda latency: latency // 2)\
.beacon_node(128)\
.set_debug(True)\
.validators(2)\
.build()\
.build()\
.build()
def simulation3(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(1844674407370955161)\
.set_custom_latency_map(None, modifier=lambda latency: latency // 2)\
.beacon_node(4)\
.set_debug(True)\
.validators(2048)\
.build()\
.build()\
.build()
def simulation4(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(1844674407370955161)\
.set_custom_latency_map(None, modifier=lambda latency: latency // 2)\
.beacon_node(11)\
.set_debug(True)\
.validators(682)\
.build()\
.build()\
.beacon_node(1)\
.set_debug(True)\
.validators(690)\
.build()\
.build()\
.build()
def simulation5(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(1844674407370955161)\
.set_custom_latency_map(None, modifier=lambda latency: latency // 2)\
.beacon_node(128)\
.set_debug(True)\
.validators(64)\
.build()\
.build()\
.build()
def simulation6(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(1844674407370955161)\
.beacon_node(4)\
.set_debug(True)\
.validators(4096)\
.build()\
.build()\
.build()
def simulation7(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(1844674407370955161)\
.beacon_node(8)\
.set_debug(True)\
.validators(2048)\
.build()\
.build()\
.build()
def simulation8(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(1844674407370955161)\
.beacon_node(3)\
.set_debug(True)\
.validators(33963)\
.build()\
.build()\
.beacon_node(1)\
.set_debug(True)\
.validators(33962)\
.build()\
.build()\
.build()
def simulation9(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(1844674407370955161)\
.beacon_node(7)\
.set_debug(True)\
.validators(16981)\
.build()\
.build()\
.beacon_node(1)\
.set_debug(True)\
.validators(16984)\
.build()\
.build()\
.build()
def simulation10(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(1844674407370955161)\
.set_custom_latency_map(None, modifier=lambda latency: latency * 2)\
.beacon_node(32)\
.set_debug(True)\
.validators(8)\
.build()\
.build()\
.build()
def simulation11(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(1844674407370955161)\
.set_custom_latency_map(None, modifier=lambda latency: latency * 8)\
.beacon_node(32)\
.set_debug(True)\
.validators(8)\
.build()\
.build()\
.build()
def simulation12(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(calc_simtime(slot=1, epoch=8))\
.set_custom_latency_map(None, modifier=lambda latency: latency // 2)\
.beacon_node(1)\
.set_debug(True)\
.set_mode('BlockSlashing')\
.validators(32)\
.build()\
.build()\
.beacon_node(7)\
.set_debug(True)\
.validators(32)\
.build()\
.build()\
.build()
def simulation13(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(calc_simtime(slot=1, epoch=8))\
.set_custom_latency_map(None, modifier=lambda latency: latency // 2)\
.beacon_node(1)\
.set_debug(True)\
.set_mode('AttesterSlashingSameHeight')\
.validators(32)\
.build()\
.build()\
.beacon_node(1)\
.set_debug(True)\
.set_mode('AttesterSlashingWithinSpan')\
.validators(32)\
.build()\
.build()\
.beacon_node(6)\
.set_debug(True)\
.validators(32)\
.build()\
.build()\
.build()
def simulation14(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(1844674407370955161)\
.beacon_node(8)\
.set_debug(True)\
.validators(32)\
.build()\
.build()\
.build()
def simulation15(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(calc_simtime(slot=1, epoch=13))\
.beacon_node(1)\
.set_debug(True)\
.set_mode('TimeAttacked')\
.set_attackinfo({
#'attack_start_slot': spec.Slot(32),
#'attack_end_slot': spec.Slot(48),
#'timedelta': spec.Slot(40)
'attack_start_slot': spec.Slot(16),
'attack_end_slot': spec.Slot(32),
'timedelta': spec.Slot(40)
})\
.validators(32)\
.build()\
.build()\
.beacon_node(7)\
.set_debug(True)\
.validators(32)\
.build()\
.build()\
.build()
def simulation16(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(calc_simtime(slot=1, epoch=4))\
.set_custom_latency_map(((0, 0),))\
.beacon_node(1)\
.set_debug(True)\
.set_mode('BalancingAttacking')\
.validators(64)\
.build()\
.build()\
.beacon_node(16)\
.set_debug(True)\
.validators(1)\
.build()\
.build()\
.build()
def simulation17(config, blockhash):
# fmt: off
return SimulationBuilder('../../configs', config, blockhash)\
.set_end_time(1844674407370955161)\
.beacon_node(32)\
.set_debug(True)\
.validators(8)\
.build()\
.build()\
.build()
def main():
# 0 minimal
simtype = int(sys.argv[1])
blockhash = bytes.fromhex('0000000000000000000000000000000000000000000000000000000000000000')
configmap = {
0: ('minimal', simulation0), # minimal 4/256
1: ('minimal', simulation1), # minimal, 12/256
2: ('minimal', simulation2), # minimal, 128/256
3: ('minimal', simulation3), # minimal, 4/8192
4: ('minimal', simulation4), # minimal, 12/8192
5: ('minimal', simulation5), # minimal, 128/8192
6: ('mainnet', simulation6), # mainnet, 4/16384
7: ('mainnet', simulation7), # mainnet, 8/16384
8: ('mainnet', simulation8), # mainnet, 4/13581
9: ('mainnet', simulation9), # mainnet, 8/13581
10: ('mainnet-minimized', simulation10), # latency, 128/256
11: ('mainnet-minimized', simulation11), # latency, 128/256
12: ('minimal', simulation12), # proposer slashing
13: ('minimal', simulation13), # attester slashings
14: ('mainnet-minimized', simulation14), # latency, 128/256
15: ('minimal', simulation15), # time attacked
16: ('minimal', simulation16), # balancing attacking
17: ('mainnet-minimized', simulation17) # paper: 128*2
}
# 10, 11, 12, 13, 15, 16, 17
# xx xx xx
mocked = simtype > 2
mocked_filename = "mocked_" if mocked else ""
config_util.prepare_config('../../configs', configmap[simtype][0])
# noinspection PyTypeChecker
reload(spec)
spec.bls.bls_active = False
print('Ethereum 2.0 Beacon Chain Simulator')
print(f'PID: {os.getpid()}')
print(f'Beacon Chain Configuration: {spec.CONFIG_NAME}')
print(f'Eth1BlockHash for Genesis Block: {blockhash}')
print(f'Bundled Simulation: {simtype}')
print(f'Mocked: {mocked}')
simulator = configmap[simtype][1](configmap[simtype][0], blockhash)
filename = f"{mocked_filename}{simtype}"
state_file = f"state_{filename}.ssz"
if pathlib.Path(state_file).is_file():
simulator.read_genesis(filename)
else:
simulator.generate_genesis(filename=simtype, mocked=mocked)
simulator.initialize_clients()
simulator.start_simulation()
if __name__ == '__main__':
try:
# multiprocessing.set_start_method("spawn")
main()
sys.exit(0)
except KeyboardInterrupt:
end = datetime.now()
sys.exit(0)