forked from 0xFF0/QRLtoSQLite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQRLtoSqlite.py
306 lines (219 loc) · 12 KB
/
QRLtoSqlite.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*
"""
QRL Blockchain to SQlite
v1.0
Requirements/Setup:
> sudo apt-get install python3-pip
> pip3 install qrl
"""
__author__ = ['0xFF (https://github.com/0xFF0)']
__version__ = "1.0"
__date__ = '2022.01.29'
import sqlite3
import plyvel
import argparse
import binascii
import base64
import sys
import copy
from google.protobuf.json_format import MessageToJson, Parse, MessageToDict
from qrl.generated import qrl_pb2
DB_INSERT_MAX = 100000
DB_INSERT_OPTIMIZATION_TEMPLATE = {
"total" : 0,
"addresses": { "query": "INSERT INTO addresses VALUES (?,?,?,?,?)", "data" : [] },
"updateAddresses": { "query": "UPDATE addresses SET lastSeen=? WHERE address=?", "data" : [] },
"blockMetadata": { "query": "INSERT INTO blockMetadata VALUES (?,?,?,?,?)", "data": [] },
"otherTransactions": { "query": "INSERT INTO otherTransactions VALUES (?,?,?,?)", "data": [] },
"messages": { "query": "INSERT INTO messages VALUES (?,?,?)", "data": [] },
"tokens": { "query": "INSERT INTO tokens VALUES (?,?,?,?)", "data": [] }
}
DB_INSERT_OPTIMIZATION = copy.deepcopy(DB_INSERT_OPTIMIZATION_TEMPLATE)
TMP_ADDR_LIST = []
def insertSqliteData(cur,con):
global DB_INSERT_OPTIMIZATION
for table in DB_INSERT_OPTIMIZATION:
if table != "total":
if len(DB_INSERT_OPTIMIZATION[table]["data"]) > 0:
cur.executemany(DB_INSERT_OPTIMIZATION[table]["query"], DB_INSERT_OPTIMIZATION[table]["data"])
con.commit()
DB_INSERT_OPTIMIZATION = copy.deepcopy(DB_INSERT_OPTIMIZATION_TEMPLATE)
def createSqliteDB(stateFolder, outputDBName):
#Create database and table
con = sqlite3.connect(outputDBName)
cur = con.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS addresses (address text, balance real, firstSeen text, lastSeen text, tag text)''')
cur.execute('''CREATE TABLE IF NOT EXISTS blockMetadata (blockNum int, hashHeader text, timestampSeconds text, nbTransactions int, rewardBlock text)''')
cur.execute('''CREATE TABLE IF NOT EXISTS otherTransactions (blockNum int, transactionHash text, txType text, data text)''')
cur.execute('''CREATE TABLE IF NOT EXISTS tokens (tokenName text, tokenSymbol text, tokenOwner text, transactionHash text)''')
cur.execute('''CREATE TABLE IF NOT EXISTS messages (messageHash text, blockNum int, transactionHash text)''')
db = plyvel.DB(stateFolder)
blockheight = int.from_bytes(db.get(b'blockheight'), byteorder='big', signed=False)
pbdata = qrl_pb2.Block()
block_number_mapping = qrl_pb2.BlockNumberMapping()
print("Parsing block 0/" + str(blockheight))
for i in range(0, blockheight):
if DB_INSERT_OPTIMIZATION["total"] > DB_INSERT_MAX:
insertSqliteData(cur,con)
print("Parsing block " + str(i) + "/" + str(blockheight))
#Get block
hashHeader = Parse(db.get(str(i).encode()), block_number_mapping).headerhash
pbdata.ParseFromString(bytes(db.get(hashHeader)))
dictData = MessageToDict(pbdata)
#Block Metadata
blockNumber = i
hashHeaderHex = hashHeader.hex()
timestampSeconds = dictData["header"]["timestampSeconds"]
nbTransactions = len(dictData["transactions"])
rewardBlock = dictData["header"]["rewardBlock"]
DB_INSERT_OPTIMIZATION["blockMetadata"]["data"].append([blockNumber, hashHeaderHex, timestampSeconds, nbTransactions, rewardBlock])
DB_INSERT_OPTIMIZATION["total"] += 1
for t in dictData["transactions"]:
transactionProcessed = False
if "coinbase" in t:
addAddressInDB(cur, db, t["coinbase"]["addrTo"], timestampSeconds)
transactionProcessed = True
if "transfer" in t:
for a in t["transfer"]["addrsTo"]:
addAddressInDB(cur, db, a, timestampSeconds)
transactionProcessed = True
if "token" in t:
tokenName = base64.b64decode(t["token"]["name"]).decode("utf-8")
tokenSymbol = base64.b64decode(t["token"]["symbol"]).decode("utf-8")
tokenOwner = "Q" + base64.b64decode(t["token"]["owner"]).hex()
transactionHash = base64.b64decode(t["transactionHash"]).hex()
DB_INSERT_OPTIMIZATION["tokens"]["data"].append([tokenName, tokenSymbol, tokenOwner, transactionHash])
DB_INSERT_OPTIMIZATION["total"] += 1
transactionProcessed = True
if "message" in t:
try:
messageHash = base64.b64decode(t["message"]["messageHash"]).decode("utf-8")
except:
messageHash = base64.b64decode(t["message"]["messageHash"]).hex()
#https://github.com/theQRL/qips/blob/master/qips/QIP002.md
if messageHash.startswith("afaf"):
if messageHash.startswith("afafa1"):
try:
docText = binascii.a2b_hex(messageHash[46:]).decode("utf-8")
except:
docText = binascii.a2b_hex(messageHash[46:]).hex()
messageHash = "[Doc notarization] SHA1: " + messageHash[6:46] + " TEXT: " + docText
elif messageHash.startswith("afafa2"):
try:
docText = binascii.a2b_hex(messageHash[70:]).decode("utf-8")
except:
docText = binascii.a2b_hex(messageHash[70:]).hex()
messageHash = "[Doc notarization] SHA256: " + messageHash[6:70] + " TEXT: " + docText
elif messageHash.startswith("afafa3"):
try:
docText = binascii.a2b_hex(messageHash[38:]).decode("utf-8")
except:
docText = binascii.a2b_hex(messageHash[38:]).hex()
messageHash = "[Doc notarization] MD5: " + messageHash[6:38] + " TEXT: " + docText
#https://github.com/theQRL/message-transaction-encoding
elif messageHash.startswith("0f0f"):
msgHeader = "[Unknown]"
msgBegin = 8
text = ""
if messageHash.startswith("0f0f0000") or messageHash.startswith("0f0f0001"):
msgHeader = "[Reserved] "
elif messageHash.startswith("0f0f0002"):
if messageHash.startswith("0f0f0002af"):
msgHeader = "[Keybase-remove] "
elif messageHash.startswith("0f0f0002aa"):
msgHeader = "[Keybase-add] "
else:
msgHeader = "[Keybase-" + messageHash[8:10] + "] "
msgBegin = 12
try:
user = binascii.a2b_hex(messageHash[msgBegin:].split("20")[0]).decode("utf-8")
keybaseHex = binascii.a2b_hex(messageHash[msgBegin + len(user)*2 + 2:]).hex()
text = "USER: " + user + " KEYBASE_HEX: " + keybaseHex
except:
text = ""
elif messageHash.startswith("0f0f0003"):
if messageHash.startswith("0f0f0002af"):
msgHeader = "[Github-remove] "
elif messageHash.startswith("0f0f0002aa"):
msgHeader = "[Github-add] "
else:
msgHeader = "[Github-" + messageHash[8:10] + "] "
msgBegin = 18
text = binascii.a2b_hex(messageHash[msgBegin:]).hex()
elif messageHash.startswith("0f0f0004"):
msgHeader = "[Vote] "
if len(text) == 0:
try:
text = binascii.a2b_hex(messageHash[msgBegin:]).decode("utf-8")
except:
try:
text = binascii.a2b_hex(messageHash[msgBegin:]).hex()
except:
text = str(messageHash[msgBegin:])
messageHash = msgHeader + text
transactionHash = base64.b64decode(t["transactionHash"]).hex()
DB_INSERT_OPTIMIZATION["messages"]["data"].append([messageHash, blockNumber, transactionHash])
DB_INSERT_OPTIMIZATION["total"] += 1
transactionProcessed = True
if not transactionProcessed:
txHash = base64.b64decode(t["transactionHash"]).hex()
data = ""
txType = ""
if "slave" in t:
txType = "slave"
if "transferToken" in t:
txType = "transferToken"
if "multiSigCreate" in t:
txType = "multiSigCreate"
if "latticePK" in t:
txType = "latticePK"
if "multiSigSpend" in t:
txType = "multiSigSpend"
if "multiSigVote" in t:
txType = "multiSigVote"
if len(txType) == 0:
data = str(t)
DB_INSERT_OPTIMIZATION["otherTransactions"]["data"].append([blockNumber, txHash, txType, data])
DB_INSERT_OPTIMIZATION["total"] += 1
con.commit()
insertSqliteData(cur,con)
con.close()
db.close()
def addAddressInDB(dbCursor, levelDB, b64Addr, timeStamp):
addrData = qrl_pb2.AddressState()
addrByte = base64.b64decode(b64Addr)
address = "Q" + addrByte.hex()
try:
updateAddr = False
if address in TMP_ADDR_LIST:
updateAddr = True
else:
addrData.ParseFromString(levelDB.get(addrByte))
dictData = MessageToDict(addrData)
if "balance" in dictData:
balance = float(dictData["balance"])/1000000000
else:
balance = "0"
DB_INSERT_OPTIMIZATION["addresses"]["data"].append([address, balance, timeStamp, timeStamp, ""])
DB_INSERT_OPTIMIZATION["total"] += 1
TMP_ADDR_LIST.append(address)
if updateAddr:
DB_INSERT_OPTIMIZATION["updateAddresses"]["data"].append([timeStamp, address])
DB_INSERT_OPTIMIZATION["total"] += 1
except:
print("Error parsing " + address + ". Timestamp: " + timeStamp)
if __name__ == "__main__":
desc = "QRL Blockchain to SQlite v" + __version__
parser = argparse.ArgumentParser(description=desc)
parser.add_argument("-i", "--indexQRLStateFolder", help="QRL state folder (ex: /home/ubuntu/.qrl/data/state)", type=str)
parser.add_argument("-o", "--outputDBName", help="Output QRL sqlite db name (ex: qrl.sqlite)", type=str)
# Read arguments from the command line
options = parser.parse_args()
if len(sys.argv)==1:
parser.print_help()
sys.exit(1)
if options.indexQRLStateFolder is not None and options.outputDBName is not None:
createSqliteDB(options.indexQRLStateFolder, options.outputDBName)
else:
parser.print_help()