-
Notifications
You must be signed in to change notification settings - Fork 1
/
block_details_database.py
112 lines (98 loc) · 3.67 KB
/
block_details_database.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
import postgres_connection
import json
def find_block_by_slotnumber(slot_number: int):
maprows = postgres_connection.query(
"""
SELECT * FROM (
SELECT
slot,
block_hash,
leader_identity,
processed_transactions,
successful_transactions,
(
SELECT
coalesce(sum(tx_slot.count),0) as count
FROM banking_stage_results_2.transaction_slot tx_slot
WHERE tx_slot.slot=blocks.slot
) AS banking_stage_errors,
total_cu_used,
total_cu_requested,
supp_infos
FROM banking_stage_results_2.blocks
-- this critera uses index idx_blocks_slot
WHERE slot = %s
) AS data
""", args=[slot_number])
assert len(maprows) <= 1, "Slot is primary key - find zero or one"
next_slot = (
postgres_connection.query(
"""
SELECT
min(slot) as next_slot
FROM banking_stage_results_2.blocks
WHERE slot > %s
""", args=[slot_number])
)
prev_slot = (
postgres_connection.query(
"""
SELECT
max(slot) as prev_slot
FROM banking_stage_results_2.blocks
WHERE slot < %s
""", args=[slot_number])
)
next_block = list(next_slot)[0]['next_slot']
prev_block = list(prev_slot)[0]['prev_slot']
if len(maprows) == 0:
block = {}
block["next_block"] = next_block
block["prev_block"] = prev_block
block["supp_infos"] = {}
return block
else:
block = list(maprows)[0]
slot = block["slot"]
block['supp_infos'] = json.loads(block['supp_infos'])
# note: sort order is undefined
accountinfos = (
postgres_connection.query(
"""
SELECT
amb.*,
acc.account_key
FROM banking_stage_results_2.accounts_map_blocks amb
INNER JOIN banking_stage_results_2.accounts acc ON acc.acc_id=amb.acc_id
WHERE slot = %s
""", args=[slot])
)
account_info_expanded = []
for account_info in accountinfos:
prio_fee_data = json.loads(account_info['prioritization_fees_info'])
info = {
'slot': account_info['slot'],
'key': account_info['account_key'],
'is_write_locked': account_info['is_write_locked'],
'cu_requested': account_info['total_cu_requested'],
'cu_consumed': account_info['total_cu_consumed'],
'min_pf': prio_fee_data['min'],
'median_pf': prio_fee_data['med'],
'max_pf': prio_fee_data['max']
}
account_info_expanded.append(info)
account_info_expanded.sort(key=lambda acc: int(acc['cu_consumed']), reverse=True)
block["heavily_writelocked_accounts_parsed"] = [acc for acc in account_info_expanded if acc['is_write_locked'] is True]
block["heavily_readlocked_accounts_parsed"] = [acc for acc in account_info_expanded if acc['is_write_locked'] is False]
block["next_block"] = next_block
block["prev_block"] = prev_block
return block
def is_matching_blockhash(block_hash):
maprows = postgres_connection.query(
"""
SELECT 1 FROM banking_stage_results_2.blocks
WHERE block_hash = %s
""", [block_hash])
return len(maprows) > 0
def main():
find_block_by_slotnumber(226352855)