-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoutput-mysql.py
465 lines (338 loc) · 16.2 KB
/
output-mysql.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# -*- coding: utf-8 -*-
# MultiChain Feed Adapter (c) Coin Sciences Ltd
# All rights reserved under BSD 3-clause license
import cfg
import pymysql
import readconf
import multichain
import utils
import feed
import warnings
warnings.filterwarnings("ignore", category = pymysql.Warning)
class AdapterOutput(cfg.BaseOutput):
def initialize(self):
self.ptr_name=self.config['pointer']
self.stream_table_names = {}
self.checked_tables = {}
if not readconf.check_db_config(self.config):
return False
if readconf.is_missing(self.config,'port'):
self.config['port']=3306
self.sql_file=self.config['sql']
if not self.check_pointer_table():
utils.log_error("Couldn't create read pointers table")
return False
row=self.fetch_row(
"SELECT file, pos FROM pointers WHERE pointer=%s;",
(self.ptr_name,)
)
if row is None:
return False
self.pointer = (0, 0)
if len(row) > 0:
self.pointer = (row[0], row[1])
return True
def write(self, records, ptr):
if not self.begin_transaction():
return False
if not self.check_streammap_table():
utils.log_error("Couldn't create stream mapping table")
return False
for record in records:
event=feed.parse_record(record)
if not self.process_event(event):
utils.log_error("Couldn't process event {code:02x} at ({file:4d}, {offset:08x})".format(code=record['code'],file=record['file'],offset=record['offset']))
if event.table is None:
utils.log_error("Event record corrupted, ignored")
else:
return False
self.execute_sql(
"INSERT INTO pointers (pointer, file, pos) VALUES (%s,%s,%s) ON DUPLICATE KEY UPDATE file=%s,pos=%s;",
(self.ptr_name,ptr[0],ptr[1],ptr[0],ptr[1])
)
self.pointer=ptr
return self.commit_transaction()
def close(self):
return True
def create_pointer_table(self):
return self.execute_creates(
"CREATE TABLE IF NOT EXISTS pointers (pointer VARCHAR(64) PRIMARY KEY, file INTEGER NOT NULL, pos INTEGER NOT NULL) CHARACTER SET utf8;"
)
def create_streammap_table(self):
return self.execute_creates(
"CREATE TABLE IF NOT EXISTS streams (stream VARCHAR(32) PRIMARY KEY, dbtable VARCHAR(32) NOT NULL) CHARACTER SET utf8;"
)
def create_blocks_table(self):
return self.execute_creates(
"CREATE TABLE IF NOT EXISTS blocks (hash BINARY(32) PRIMARY KEY,height INTEGER,confirmed TIMESTAMP NULL,txcount INTEGER,size INTEGER,miner VARCHAR(64), index blocks_height_idx (height)) CHARACTER SET utf8;"
)
def create_stream_table(self,stream_name,table_name):
return self.execute_creates(
"CREATE TABLE IF NOT EXISTS "+table_name+"""(id BINARY(20) PRIMARY KEY,txid BINARY(32),vout INTEGER,flags INTEGER,
size BIGINT,format VARCHAR(8),binary_data LONGBLOB,text_data LONGTEXT,json_data LONGTEXT,dataref BINARY(40),
received TIMESTAMP NULL,confirmed TIMESTAMP NULL,blockhash BINARY(32),blockheight INTEGER,blockpos INTEGER,
index """ + table_name + "_pos_idx (blockheight,blockpos)) CHARACTER SET utf8; """
) and self.execute_creates(
"CREATE TABLE IF NOT EXISTS "+table_name+"_key (id BINARY(20) NOT NULL,itemkey VARCHAR(256) NOT NULL,PRIMARY KEY (id,itemkey(255)),"+
"index "+table_name+"_key_idx (itemkey(255)),"+
"CONSTRAINT "+table_name+"_key_fk_id FOREIGN KEY (id) REFERENCES "+table_name+" (id) ON DELETE CASCADE) CHARACTER SET utf8;"
) and self.execute_creates(
"CREATE TABLE IF NOT EXISTS "+table_name+"_pub (id BINARY(20) NOT NULL,publisher VARCHAR(190) NOT NULL,PRIMARY KEY (id,publisher),"+
"index "+table_name+"_pub_idx (publisher),"+
"CONSTRAINT "+table_name+"_pub_fk_id FOREIGN KEY (id) REFERENCES "+table_name+" (id) ON DELETE CASCADE) CHARACTER SET utf8;"
) and self.execute_creates(
"INSERT INTO streams (dbtable, stream) VALUES (%s,%s) ON DUPLICATE KEY UPDATE dbtable=dbtable",
(table_name,stream_name)
)
def event_block_add(self,event):
if not self.check_blocks_table(event):
return False
return self.execute_sql(
"INSERT INTO blocks (hash,height,txcount,confirmed,miner,size) VALUES (%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE txcount=%s,confirmed=%s,miner=%s,size=%s;",
(event.hash,event.height,event.txcount,event.time,event.miner,event.size,event.txcount,event.time,event.miner,event.size,)
)
def event_block_remove(self,event):
if not self.check_blocks_table(event):
return False
return self.execute_sql(
"""DELETE FROM blocks WHERE hash=%s;""",
(event.hash,)
)
def event_stream_item_received(self,event):
if not self.check_stream_table(event):
return False
return self.execute_sql(
"INSERT INTO "+event.table+""" (id,txid,vout,flags,received,size,format,binary_data,text_data,json_data,dataref) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
ON DUPLICATE KEY UPDATE flags=%s,received=%s,binary_data=%s,text_data=%s,json_data=%s,dataref=%s;""",
(event.id,event.txid,event.vout,event.flags,event.received,event.size,event.format,event.binary,event.text,event.json,event.dataref,
event.flags,event.received,event.binary,event.text,event.json,event.dataref,)
) and self.event_stream_item_received_keys(event) and self.event_stream_item_received_publishers(event)
# Not using INSERT IGNORE in the functions below because this would hide other errors we want reported.
def event_stream_item_received_keys(self,event):
if event.keys is None:
return True
for key in event.keys:
if not self.execute_sql("INSERT INTO "+event.table+"_key (id,itemkey) VALUES (%s,%s) ON DUPLICATE KEY UPDATE id=id;",(event.id,key,)):
return False
return True
def event_stream_item_received_publishers(self,event):
if event.publishers is None:
return True
for publisher in event.publishers:
if not self.execute_sql("INSERT INTO "+event.table+"_pub (id,publisher) VALUES (%s,%s) ON DUPLICATE KEY UPDATE id=id;",(event.id,publisher,)):
return False
return True
def event_stream_item_confirmed(self,event):
if not self.check_stream_table(event):
return False
return self.execute_sql(
"UPDATE "+event.table+" SET blockhash=%s,blockheight=%s,blockpos=%s,confirmed=%s,dataref=COALESCE(%s,dataref) WHERE id=%s;",
(event.hash,event.height,event.offset,event.time,event.dataref,event.id,)
)
def event_stream_item_unconfirmed(self,event):
if not self.check_stream_table(event):
return False
return self.execute_sql(
"UPDATE "+event.table+" SET blockhash=NULL,blockheight=NULL,blockpos=NULL,confirmed=NULL WHERE id=%s;",
(event.id,)
)
def event_stream_item_invalid(self,event):
if not self.check_stream_table(event):
return False
return self.execute_sql(
"DELETE FROM "+event.table+" WHERE id=%s;",
(event.id,)
)
def event_stream_offchain_available(self,event):
if not self.check_stream_table(event):
return False
return self.execute_sql(
"UPDATE "+event.table+" SET binary_data=%s,text_data=%s,json_data=%s,flags=%s,received=%s,dataref=COALESCE(%s,dataref) WHERE id=%s;",
(event.binary,event.text,event.json,event.flags,event.received,event.dataref,event.id,)
)
def event_stream_offchain_purged(self,event):
if not self.check_stream_table(event):
return False
return self.execute_sql(
"UPDATE "+event.table+" SET binary_data=NULL,text_data=NULL,json_data=NULL,flags=flags & 254,dataref=NULL WHERE id=%s;",
(event.id,)
)
def check_stream_table(self,event):
if event.stream is None:
return False
stream_name=event.stream
if stream_name not in self.stream_table_names:
sanitized=self.sanitized_stream_name(stream_name)
table_name=None
row=self.fetch_row(
"SELECT dbtable, stream FROM streams WHERE stream=%s;",
(stream_name,)
)
if row is None:
return False
if len(row) > 0:
table_name=row[0]
if table_name is None:
for ext in range(0,50):
try_name = sanitized
if ext > 0:
try_name += "_{ext:02d}".format(ext = ext)
row=self.fetch_row(
"SELECT dbtable, stream FROM streams WHERE dbtable=%s;",
(try_name,)
)
if row is None:
return False
if len(row) == 0:
table_name=try_name
break
if table_name is None:
utils.log_error("No available db table name found for stream " + stream_name)
return False
if not self.create_stream_table(stream_name,table_name):
return False
self.checked_tables[table_name]=True
self.stream_table_names[stream_name]=table_name
event.table=self.stream_table_names[stream_name]
return True
def check_pointer_table(self):
if not 'pointers' in self.checked_tables:
if not self.create_pointer_table():
return False
self.checked_tables['pointers']=True
return True
def check_streammap_table(self):
if not 'streams' in self.checked_tables:
if not self.create_streammap_table():
return False
self.checked_tables['streams']=True
return True
def check_blocks_table(self,event):
if not 'blocks' in self.checked_tables:
if not self.create_blocks_table():
return False
self.checked_tables['blocks']=True
event.table='blocks'
return True
def process_event(self,event):
if event.code == multichain.event_block_add_start:
return self.event_block_add(event)
elif event.code == multichain.event_block_remove_start:
return self.event_block_remove(event)
elif event.code == multichain.event_stream_item_received:
return self.event_stream_item_received(event)
elif event.code == multichain.event_stream_item_confirmed:
return self.event_stream_item_confirmed(event)
elif event.code == multichain.event_stream_item_unconfirmed:
return self.event_stream_item_unconfirmed(event)
elif event.code == multichain.event_stream_item_invalid:
return self.event_stream_item_invalid(event)
elif event.code == multichain.event_stream_offchain_available:
return self.event_stream_offchain_available(event)
elif event.code == multichain.event_stream_offchain_purged:
return self.event_stream_offchain_purged(event)
return True
def connect(self):
conn=pymysql.connect(host=self.config['host'],user=self.config['user'],passwd=self.config['password'],db=self.config['dbname'],port=self.config['port'])
return conn
def fetch_row(self,sql,params=()):
try:
conn=self.connect()
except pymysql.Error as e:
utils.log_error(str(e.args[1]))
return None
row=()
try:
cur=conn.cursor()
cur.execute(sql,params)
if cur.rowcount > 0:
row = cur.fetchone()
except pymysql.Error as e:
pass
if conn is not None:
conn.close()
return row
def replace_sql_binaries(self,raw_sql):
toHex = lambda x:''.join(format(c, '02x') for c in x)
params=()
head=""
tail=raw_sql[0]
for param in raw_sql[1]:
parts = tail.split("%s",1)
head += parts[0]
if isinstance(param, bytes):
head += "UNHEX('" + toHex(param) + "')"
else:
head += "%s"
params += (param,)
tail = parts[1]
return (head+tail,params)
def execute_transaction(self,sqls):
result=True
if len(sqls) == 0:
return result
f=None
if self.sql_file != None:
f = open(self.sql_file, "a")
conn=None
try:
commit_required=False
conn=self.connect()
cur=conn.cursor()
for raw_sql in sqls:
if raw_sql == "BEGIN;" or raw_sql == "COMMIT;":
if f is not None:
f.write (str(raw_sql,'utf-8') + "\n")
conn.commit()
commit_required=False
else:
if isinstance(raw_sql, tuple):
sql=self.replace_sql_binaries(raw_sql)
cur.execute(sql[0],tuple(sql[1]))
if f is not None:
f.write (cur._last_executed)
f.write ("\n")
else:
cur.execute(sql)
if f is not None:
f.write (cur._last_executed)
f.write ("\n")
commit_required=True
if commit_required:
conn.commit()
cur.close()
except pymysql.Error as e:
utils.log_error(str(e.args[1]))
result=False
if conn is not None:
conn.close()
if f is not None:
f.close
return result
def execute_creates(self,sql,params=()):
return self.execute_transaction([(sql,params)])
def execute_sql(self,sql,params=()):
self.sqls.append((sql,params))
return True
def begin_transaction(self):
self.sqls=[]
return True
def commit_transaction(self):
result=self.execute_transaction(self.sqls)
self.sqls=[]
return result
def abort_transaction(self):
self.sqls=[]
return True
def sanitized_stream_name(self,stream_name):
if stream_name == 'blocks':
return 'blocks_stream'
if stream_name == 'streams':
return 'streams_stream'
if stream_name == 'pointers':
return 'pointers_stream'
sanitized="".join([ c if c.isalnum() else "_" for c in stream_name ])
sanitized=sanitized.lower()
if not sanitized[0].isalpha():
sanitized = "ref_" + sanitized
return sanitized[0:20]