-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreduced_genesis_set.py
64 lines (49 loc) · 1.91 KB
/
reduced_genesis_set.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
import json
import psycopg2
FUTURE_EPOCH = 2**64 - 1 # from spec
END_EPOCH = 32000
OUTPUT_FILENAME = 'reduced_genesis_set.json'
connection = psycopg2.connect(
user="chain", host="127.0.0.1", database="chain", password="medalla"
)
cursor = connection.cursor()
cursor.execute(
"SELECT f_index, f_activation_epoch, f_exit_epoch, f_slashed, f_public_key "
"FROM t_validators ORDER BY f_index"
)
validators = [{
'index': r[0],
'activation_epoch': FUTURE_EPOCH if r[1] is None else r[1],
'exit_epoch': FUTURE_EPOCH if r[2] is None else r[2],
'slashed': r[3],
'slasher': False,
'redeposit': False,
'pubkey': r[4].hex()
} for r in cursor.fetchall()]
pubkey_lookup = {v['pubkey']: v for v in validators}
# identify slashers
cursor.execute("SELECT f_inclusion_slot FROM t_proposer_slashings")
proposer_slashings = [s[0] for s in cursor.fetchall()]
cursor.execute("SELECT f_inclusion_slot FROM t_attester_slashings")
slashings = proposer_slashings + [s[0] for s in cursor.fetchall()]
for s in slashings:
cursor.execute(f"SELECT f_validator_index FROM t_proposer_duties WHERE f_slot = {s}")
validators[cursor.fetchone()[0]]["slasher"] = True
# check for any instances of validator deposits made to already active validators
cursor.execute("SELECT f_inclusion_slot, f_validator_pubkey FROM t_deposits")
for row in cursor.fetchall():
deposit_slot = row[0]
pubkey = row[1].hex()
if pubkey not in pubkey_lookup:
continue
else:
validator = pubkey_lookup[pubkey]
if deposit_slot // 32 > validator["activation_epoch"] and deposit_slot // 32 < validator["exit_epoch"]:
validator["redeposit"] = True
reduced_genesis_set = [
v["index"] for v in validators if v["activation_epoch"] == 0 and not (
v["slashed"] or v["slasher"] or v["redeposit"] or v["exit_epoch"] < END_EPOCH
)
]
with open(OUTPUT_FILENAME, 'w') as f:
json.dump(reduced_genesis_set, f)