-
Notifications
You must be signed in to change notification settings - Fork 15
/
build_db.py
executable file
·385 lines (298 loc) · 10.9 KB
/
build_db.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env python3
import os
import json
import sqlite3
import argparse
from classifier import Classifier
from multi_classifier import MultiClassifier
from prepare_training_data import CLIENTS
DB_CLIENTS = [client for client in CLIENTS if client != "Other"]
def list_all_files(classify_dir):
for root, _, files in os.walk(classify_dir):
for filename in files:
yield os.path.join(root, filename)
def create_block_db(db_path):
if os.path.exists(db_path):
print("deleting existing database")
os.remove(db_path)
conn = sqlite3.connect(db_path)
conn.execute(
"""CREATE TABLE blocks (
slot INT,
parent_slot INTEGER,
proposer_index INT,
best_guess_single TEXT,
best_guess_multi TEXT,
pr_grandine FLOAT DEFAULT 0.0,
pr_lighthouse FLOAT,
pr_lodestar FLOAT,
pr_nimbus FLOAT,
pr_prysm FLOAT,
pr_teku FLOAT,
graffiti_guess TEXT,
UNIQUE(slot, proposer_index)
)
"""
)
conn.execute("CREATE INDEX block_proposers ON blocks (proposer_index)")
conn.execute("CREATE INDEX block_slots ON blocks (slot)")
return conn
def open_block_db(db_path):
if not os.path.exists(db_path):
raise Exception(f"no database found at {db_path}")
return sqlite3.connect(db_path)
def open_or_create_db(db_path, force_create=False):
if os.path.exists(db_path) and not force_create:
return open_block_db(db_path)
else:
return create_block_db(db_path)
def slot_range_from_filename(filename) -> (int, int):
parts = os.path.splitext(os.path.basename(filename))[0].split("_")
start_slot = int(parts[1])
end_slot = int(parts[3])
return (start_slot, end_slot)
def build_block_db(db_path, classifier, classify_dir, force_rebuild=False):
conn = open_or_create_db(db_path, force_create=force_rebuild)
for input_file in list_all_files(classify_dir):
start_slot, end_slot = slot_range_from_filename(input_file)
if slot_range_known_to_db(conn, start_slot, end_slot):
print(f"skipping {input_file} (assumed known)")
continue
print(f"classifying rewards from file {input_file}")
with open(input_file, "r") as f:
block_rewards = json.load(f)
update_block_db(conn, classifier, block_rewards)
return conn
def update_block_db(conn, classifier, block_rewards):
for block_reward in block_rewards:
label, multilabel, prob_by_client, graffiti_guess = classifier.classify(
block_reward
)
proposer_index = block_reward["meta"]["proposer_index"]
slot = int(block_reward["meta"]["slot"])
parent_slot = int(block_reward["meta"]["parent_slot"])
insert_block(
conn,
slot,
parent_slot,
proposer_index,
label,
multilabel,
prob_by_client,
graffiti_guess,
)
conn.commit()
def insert_block(
conn,
slot,
parent_slot,
proposer_index,
label,
multilabel,
prob_by_client,
graffiti_guess,
):
pr_clients = [prob_by_client.get(client) or 0.0 for client in DB_CLIENTS]
conn.execute(
"""INSERT INTO blocks (slot, parent_slot, proposer_index, best_guess_single,
best_guess_multi, pr_grandine, pr_lighthouse, pr_lodestar,
pr_nimbus, pr_prysm, pr_teku, graffiti_guess)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
(
slot,
parent_slot,
proposer_index,
label,
multilabel,
*pr_clients,
graffiti_guess,
),
)
def get_greatest_block_slot(block_db):
res = list(block_db.execute("SELECT MAX(slot) FROM blocks"))
assert len(res) == 1
slot = res[0][0]
if slot is None:
return 0
else:
return int(slot)
def get_missing_parent_blocks(block_db):
res = block_db.execute(
"""SELECT slot, parent_slot FROM blocks b1
WHERE
(SELECT slot FROM blocks WHERE slot = b1.parent_slot) IS NULL
AND slot <> 1"""
)
return [(int(x[0]), int(x[1])) for x in res]
def get_greatest_prior_block_slot(block_db, slot):
res = list(block_db.execute("SELECT MAX(slot) FROM blocks WHERE slot < ?", (slot,)))
assert len(res) == 1
slot = res[0][0]
if slot is None:
return None
else:
return int(slot)
def get_sync_gaps(block_db):
missing_parent_slots = get_missing_parent_blocks(block_db)
gaps = []
for block_slot, parent_slot in missing_parent_slots:
prior_slot = get_greatest_prior_block_slot(block_db, parent_slot)
if prior_slot is None:
start_slot = 0
else:
start_slot = prior_slot + 1
end_slot = block_slot - 1
assert end_slot >= start_slot
gaps.append({"start": start_slot, "end": end_slot})
return gaps
def slot_range_known_to_db(block_db, start_slot, end_slot):
res = list(
block_db.execute(
"SELECT COUNT(*) FROM blocks WHERE slot >= ? AND slot <= ?",
(start_slot, end_slot),
)
)
assert len(res) == 1
count = int(res[0][0])
return count > 0
def get_sync_status(block_db):
greatest_block_slot = get_greatest_block_slot(block_db)
synced = len(get_missing_parent_blocks(block_db)) == 0
return {"greatest_block_slot": greatest_block_slot, "synced": synced}
def get_blocks_per_client(block_db, start_slot, end_slot):
blocks_per_client = {client: 0 for client in ["Uncertain", *CLIENTS]}
client_counts = block_db.execute(
"""SELECT best_guess_single, COUNT(proposer_index)
FROM blocks
WHERE slot >= ? AND slot < ?
GROUP BY best_guess_single""",
(start_slot, end_slot),
)
for client, count in client_counts:
blocks_per_client[client] = int(count)
return blocks_per_client
def get_validator_blocks(block_db, validator_index, since_slot=None):
since_slot = since_slot or 0
rows = block_db.execute(
"""SELECT slot, best_guess_single, best_guess_multi, pr_grandine, pr_lighthouse,
pr_lodestar, pr_nimbus, pr_prysm, pr_teku
FROM blocks WHERE proposer_index = ? AND slot >= ?""",
(validator_index, since_slot),
)
def row_to_json(row):
slot = row[0]
best_guess_single = row[1]
best_guess_multi = row[2]
probability_map = {client: row[3 + i] for i, client in enumerate(DB_CLIENTS)}
return {
"slot": slot,
"best_guess_single": best_guess_single,
"best_guess_multi": best_guess_multi,
"probability_map": probability_map,
}
return [row_to_json(row) for row in rows]
def get_all_validators_latest_blocks(block_db):
rows = block_db.execute(
"""SELECT b1.proposer_index, b1.slot, b1.best_guess_single
FROM blocks b1
JOIN (SELECT MAX(slot) AS slot, proposer_index FROM blocks GROUP BY proposer_index)
AS b2 ON b1.slot = b2.slot AND b1.proposer_index = b2.proposer_index;"""
)
def row_to_json(row):
proposer_index = int(row[0])
slot = row[1]
best_guess_single = row[2]
return {
"proposer_index": proposer_index,
"slot": slot,
"best_guess_single": best_guess_single,
}
return [row_to_json(row) for row in rows]
def get_blocks(block_db, start_slot, end_slot=None):
end_slot = end_slot or (1 << 62)
rows = block_db.execute(
"""SELECT slot, proposer_index, best_guess_single, best_guess_multi, pr_grandine,
pr_lighthouse, pr_lodestar, pr_nimbus, pr_prysm, pr_teku
FROM blocks WHERE slot >= ? AND slot < ?""",
(start_slot, end_slot),
)
def row_to_json(row):
slot = row[0]
proposer_index = int(row[1])
best_guess_single = row[2]
best_guess_multi = row[3]
probability_map = {client: row[4 + i] for i, client in enumerate(DB_CLIENTS)}
return {
"slot": slot,
"proposer_index": proposer_index,
"best_guess_single": best_guess_single,
"best_guess_multi": best_guess_multi,
"probability_map": probability_map,
}
return [row_to_json(row) for row in rows]
def count_true_positives(block_db, client, slot_lower, slot_upper):
rows = block_db.execute(
"""SELECT COUNT(*) FROM blocks
WHERE best_guess_single = ? AND graffiti_guess = ? AND
slot >= ? AND slot < ?""",
(client, client, slot_lower, slot_upper),
)
return int(list(rows)[0][0])
def count_true_negatives(block_db, client, slot_lower, slot_upper):
rows = block_db.execute(
"""SELECT COUNT(*) FROM blocks
WHERE best_guess_single <> ? AND graffiti_guess <> ? AND graffiti_guess IS NOT NULL AND
slot >= ? AND slot < ?""",
(client, client, slot_lower, slot_upper),
)
return int(list(rows)[0][0])
def count_false_positives(block_db, client, slot_lower, slot_upper):
rows = block_db.execute(
"""SELECT COUNT(*) FROM blocks
WHERE best_guess_single = ? AND graffiti_guess <> ? AND graffiti_guess IS NOT NULL AND
slot >= ? AND slot < ?""",
(client, client, slot_lower, slot_upper),
)
return int(list(rows)[0][0])
def count_false_negatives(block_db, client, slot_lower, slot_upper):
rows = block_db.execute(
"""SELECT COUNT(*) FROM blocks
WHERE best_guess_single <> ? AND graffiti_guess = ? AND
slot >= ? AND slot < ?""",
(client, client, slot_lower, slot_upper),
)
return int(list(rows)[0][0])
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--db-path", required=True, help="path to sqlite database file")
parser.add_argument(
"--data-dir", required=True, help="training data for classifier(s)"
)
parser.add_argument("--classify-dir", required=True, help="data to classify")
parser.add_argument(
"--multi-classifier",
default=False,
action="store_true",
help="build MultiClassifier from datadir",
)
parser.add_argument(
"--force-rebuild", action="store_true", help="delete any existing database"
)
return parser.parse_args()
def main():
args = parse_args()
db_path = args.db_path
data_dir = args.data_dir
data_to_classify = args.classify_dir
if args.multi_classifier:
classifier = MultiClassifier(data_dir)
else:
print("loading single classifier")
classifier = Classifier(data_dir)
print("loaded")
conn = build_block_db(
db_path, classifier, data_to_classify, force_rebuild=args.force_rebuild
)
conn.close()
if __name__ == "__main__":
main()