forked from Reecepbcups/interchain-indexer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL.py
362 lines (297 loc) · 12.2 KB
/
SQL.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
import json
import sqlite3
from chain_types import Block, Tx
class Database:
def __init__(self, db: str):
self.conn = sqlite3.connect(db)
self.cur = self.conn.cursor()
# self.optimize_db(vacuum=False) # never run vacuum here
# self.cur.execute("""PRAGMA temp_store=MEMORY""")
# self.optimize_tables()
def commit(self):
self.conn.commit()
def create_tables(self):
# height, time, txs_ids
self.cur.execute(
"""CREATE TABLE IF NOT EXISTS blocks (height INTEGER PRIMARY KEY, time TEXT, txs TEXT)"""
)
# txs: id int primary key auto inc, height, tx_amino, msg_types, tx_json
self.cur.execute(
"""CREATE TABLE IF NOT EXISTS txs (id INTEGER PRIMARY KEY AUTOINCREMENT, height INTEGER, tx_amino TEXT, msg_types TEXT, tx_json TEXT, address TEXT)"""
)
# users: address, height, tx_id
# self.cur.execute(
# """CREATE TABLE IF NOT EXISTS users (address TEXT, height INTEGER, tx_id INTEGER)"""
# )
# messages: message_type, height, count
# This may be extra? We could just iter txs but I guess it depends. Can add in the future
# NOTE: May remove later
self.cur.execute(
"""CREATE TABLE IF NOT EXISTS messages (message TEXT, height INTEGER, count INTEGER)"""
)
self.commit()
def optimize_tables(self):
# Only runs this after we have saved values
# self.cur.execute("""VACUUM""")
# for blocks, create an index at height
self.cur.execute("""CREATE INDEX IF NOT EXISTS blocks_height ON blocks (height)""")
# index txs by id & height
self.cur.execute(
"""CREATE INDEX IF NOT EXISTS txs_id_height ON txs (id, height, address)"""
)
# index messages by height
# self.cur.execute("""CREATE INDEX IF NOT EXISTS messages_height ON messages (height)""")
# index users by height
# self.cur.execute("""CREATE INDEX IF NOT EXISTS users_height ON users (height)""")
self.commit()
def optimize_db(self, vacuum: bool = False):
# self.optimize_tables()
# Set journal mode to WAL. - https://charlesleifer.com/blog/going-fast-with-sqlite-and-python/
self.cur.execute("""PRAGMA journal_mode=WAL""")
# self.cur.execute("""PRAGMA synchronous=OFF""") # off? or normal?
self.cur.execute("""PRAGMA mmap_size=30000000000""")
self.cur.execute(f"""PRAGMA page_size=32768""")
if vacuum:
self.cur.execute("""VACUUM""")
self.cur.execute("""PRAGMA optimize""")
self.commit()
def get_indexes(self):
self.cur.execute("""SELECT name FROM sqlite_master WHERE type='index';""")
return self.cur.fetchall()
def get_all_tables(self):
self.cur.execute("""SELECT name FROM sqlite_master WHERE type='table';""")
return self.cur.fetchall()
def get_table_schema(self, table: str):
self.cur.execute(f"""PRAGMA table_info({table})""")
return self.cur.fetchall()
# ===================================
# User
# ===================================
# def insert_user(self, address: str, height: int, tx_id: int):
# self.cur.execute(
# """INSERT INTO users (address, height, tx_id) VALUES (?, ?, ?)""",
# (address, height, tx_id),
# )
# ===================================
# Messages / Types (May not be needed)
# ===================================
def insert_msg_type(self, message_type: str, height: int, count: int):
self.cur.execute(
"""INSERT INTO messages (message, height, count) VALUES (?, ?, ?)""",
(message_type, height, count),
)
def insert_msg_type_count(self, msg_type: str, count: int, height: int):
self.cur.execute(
"""SELECT count FROM messages WHERE message=? AND height=?""",
(height, msg_type),
)
data = self.cur.fetchone()
if data is not None:
print(f"Block {height} already has {msg_type}")
return
self.cur.execute(
"""INSERT INTO messages (message, height, count) VALUES (?, ?, ?)""",
(msg_type, height, count),
)
def get_msg_type_count_at_exact_height(self, msg_type: str, height: int) -> int:
self.cur.execute(
"""SELECT count FROM messages WHERE message=? AND height=?""",
(msg_type, height),
)
data = self.cur.fetchone()
if data is None:
return 0
return data[0]
def get_msg_type_count_in_range(self, msg_type: str, start: int, end: int) -> int:
"""
If msg_type is '*', counts all messages
Returns a list of @ of Txs per block in the range requested
Ex: Blocks 10 through 20
Returns a list of 10 items, each item being the # of txs total
"""
if msg_type == "*" or msg_type == "" or msg_type is None:
self.cur.execute(
"""SELECT count FROM messages WHERE height>=? AND height<=?""",
(start, end),
)
else:
self.cur.execute(
"""SELECT count FROM messages WHERE message=? AND height>=? AND height<=?""",
(msg_type, start, end),
)
data = self.cur.fetchall()
if data is None:
return []
return sum([x[0] for x in data])
def get_msg_types_transactions_in_range(
self, msg_type: str, start: int, end: int
) -> list[Tx]:
"""
Returns a list of tx_ids that have the msg_type in the range requested
"""
self.cur.execute(
"""SELECT * FROM txs WHERE msg_types LIKE ? AND height>=? AND height<=?""",
(f"%{msg_type}%", start, end),
)
data = self.cur.fetchall()
if data is None:
return []
blankTx = Tx(0, 0, "", [], "", "")
txs: list[Tx] = []
for tx in data:
blankTx.id = tx[0]
blankTx.height = tx[1]
blankTx.tx_amino = tx[2]
blankTx.msg_types = json.loads(tx[3])
blankTx.tx_json = tx[4]
blankTx.address = tx[5]
txs.append(blankTx)
return txs
def get_msg_types_ids_in_range(
self, msg_type: str, start: int, end: int
) -> list[int]:
"""
Returns a list of tx_ids that have the msg_type in the range requested
"""
txs: list[int] = []
self.cur.execute(
"""SELECT id FROM txs WHERE msg_types LIKE ? AND height>=? AND height<=?""",
(f"%{msg_type}%", start, end),
)
data = self.cur.fetchall()
if data is None:
return []
for tx in data:
txs.append(tx[0])
return list(txs)
# ===================================
# Blocks
# ===================================
def insert_block(self, height: int, time: str, txs_ids: list[int]):
# insert the height and tx_amino.
self.cur.execute(
"""INSERT INTO blocks (height, time, txs) VALUES (?, ?, ?)""",
(height, time, json.dumps(txs_ids)),
)
def get_block(self, block_height: int) -> Block | None:
self.cur.execute(
"""SELECT * FROM blocks WHERE height=?""",
(block_height,),
)
data = self.cur.fetchone()
if data is None:
return None
return Block(data[0], data[1], json.loads(data[2]))
def get_earliest_block(self) -> Block | None:
self.cur.execute("""SELECT * FROM blocks ORDER BY height ASC LIMIT 1""")
data = self.cur.fetchone()
if data is None:
return None
return Block(data[0], data[1], json.loads(data[2]))
def get_latest_saved_block(self) -> Block | None:
self.cur.execute("""SELECT * FROM blocks ORDER BY height DESC LIMIT 1""")
data = self.cur.fetchone()
if data is None:
return None
return Block(data[0], data[1], json.loads(data[2]))
def get_total_blocks(self) -> int:
self.cur.execute("""SELECT COUNT(*) FROM blocks""")
data = self.cur.fetchone()
if data is None:
return 0
return data[0]
def get_missing_blocks(self, start_height, end_height) -> list[int]:
# get all blocks which we do not have value for between a range
self.cur.execute(
"""SELECT height FROM blocks WHERE height BETWEEN ? AND ?""",
(start_height, end_height),
)
data = self.cur.fetchall()
if data is None:
return list(range(start_height, end_height + 1))
found_heights = set(x[0] for x in data)
missing_heights = [
height
for height in range(start_height, end_height + 1)
if height not in found_heights
]
return missing_heights
# ===================================
# Transactions
# ===================================
def insert_tx(self, height: int, tx_amino: str):
# We insert the data without it being decoded. We can update later
# insert the height and tx_amino, then return the unique id
# fill the other collums with empty strings
# """CREATE TABLE IF NOT EXISTS txs (id INTEGER PRIMARY KEY AUTOINCREMENT, height INTEGER, tx_amino TEXT, msg_types TEXT, tx_json TEXT, address TEXT)"""
self.cur.execute(
"""INSERT INTO txs (height, tx_amino, msg_types, tx_json, address) VALUES (?, ?, ?, ?, ?)""",
(height, tx_amino, "", "", ""),
)
return self.cur.lastrowid
def update_tx(self, _id: int, tx_json: str, msg_types: str, address: str):
# update the data after we decode it (post insert_tx)
self.cur.execute(
"""UPDATE txs SET tx_json=?, msg_types=?, address=? WHERE id=?""",
(tx_json, msg_types, address, _id),
)
def get_tx(self, tx_id: int) -> Tx | None:
self.cur.execute(
"""SELECT * FROM txs WHERE id=?""",
(tx_id,),
)
data = self.cur.fetchone()
if data is None:
return None
return Tx(data[0], data[1], data[2], data[3], data[4], data[5])
def get_txs_in_range(self, start_height: int, end_height: int) -> list[Tx]:
latest_block = self.get_latest_saved_block()
if latest_block is None:
print("No (latest) blocks saved in Database")
return []
if end_height > latest_block.height:
end_height = latest_block.height
# select * from txs between height
# M<aybe not returning Amino here?
self.cur.execute(
"""SELECT * FROM txs WHERE height BETWEEN ? AND ?""",
(start_height, end_height),
)
data = self.cur.fetchall()
if data is None:
return []
tx = Tx(0, 0, "", [], "", "")
txs: list[Tx] = []
for x in data:
tx.id = x[0]
tx.height = x[1]
tx.tx_amino = x[2]
tx.msg_types = x[3]
tx.tx_json = x[4]
txs.append(tx)
# return [Tx(x[0], x[1], x[2], x[3], x[4]) for x in data]
return txs
# def get_users_txs_in_range(
# self, address: str, start_height: int, end_height: int
# ) -> list[Tx]:
# latest_block = self.get_latest_saved_block()
# if latest_block is None:
# print("No (latest) blocks saved in Database")
# return []
# if end_height > latest_block.height:
# end_height = latest_block.height
# print(address, start_height, end_height)
# # Select all tx_id from users WHERE address=? AND height BETWEEN ? AND ?
# self.cur.execute(
# """SELECT tx_id FROM users WHERE address=? AND height BETWEEN ? AND ?""",
# (address, start_height, end_height),
# )
# data = self.cur.fetchall()
# if data is None:
# return []
# txs = []
# for values in data:
# tx = self.get_tx(values[2])
# if tx is not None:
# txs.append(tx)
# return txs