-
Notifications
You must be signed in to change notification settings - Fork 2
/
chain_index.py
292 lines (250 loc) · 10.9 KB
/
chain_index.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
# -*- coding: utf-8 -*-
import os
import sys
import sqlite3
import threading
import json
import time
import string
try:
from flask import Flask
from flask import request
except:
pass
class ChainIndex:
def __init__(self, profile, watcher=None):
self.profile = profile
self.config = profile.config
self.dir = profile.profile_dir
self.db_filename = self.dir + '/store.db'
self.init_dir()
self.watcher = watcher
self.con = sqlite3.connect(self.db_filename)
self.con.row_factory = sqlite3.Row
self.cur = self.con.cursor()
self.reset()
if self.config.get('CHAIN_STATE_WEBSERVER'):
app = Flask(__name__)
db_filename = self.db_filename
@app.route("/tip")
def api_tip():
t0 = time.time()
db = sqlite3.connect(db_filename)
db.row_factory = sqlite3.Row
cur = db.cursor()
result = {}
try:
cur.execute("SELECT block, slot, id, tx, tuna_block, tuna_merkle_root FROM chain ORDER BY tuna_block DESC LIMIT 1;")
row = cur.fetchone()
result['result'] = dict(row)
db.close()
except Exception as e:
print(e)
result['error'] = 'error'
print(f"took {time.time()-t0} seconds")
return json.dumps(result)
@app.route("/block/<block>")
def api_block(block):
try:
block_number = int(block)
except:
return json.dumps({'error': 'block number must be an integer.'})
t0 = time.time()
db = sqlite3.connect(db_filename)
db.row_factory = sqlite3.Row
cur = db.cursor()
result = {}
try:
cur.execute("SELECT tuna_block, tuna_epoch, tuna_lz, tuna_dn, tuna_hash, tuna_nonce, tuna_miner, tuna_miner_cred_hash FROM chain WHERE tuna_block = ?;", (block_number,))
row = cur.fetchone()
result['result'] = dict(row)
db.close()
except Exception as e:
print(e)
result['error'] = 'error'
print(f"took {time.time()-t0} seconds")
return json.dumps(result)
@app.route("/blocks")
def api_blocks():
result = {}
db = sqlite3.connect(db_filename)
db.row_factory = sqlite3.Row
cur = db.cursor()
try:
start_block = int(request.args.get('s') or -1)
direction = int(request.args.get('d') or 0)
columns = "block,slot,id,tx,tuna_block,tuna_hash,tuna_lz,tuna_dn,tuna_epoch,tuna_posix_time,tuna_merkle_root,tuna_miner,tuna_miner_cred_hash,tuna_miner_data"
if direction == 1:
if start_block < 1:
cur.execute(f"SELECT {columns} FROM chain ORDER BY tuna_block ASC LIMIT 10;")
else:
cur.execute(f"SELECT {columns} FROM chain WHERE tuna_block > :tuna_block ORDER BY tuna_block ASC LIMIT 10;", {"tuna_block": start_block})
else:
if start_block < 1:
cur.execute(f"SELECT {columns} FROM chain ORDER BY tuna_block DESC LIMIT 10;")
else:
cur.execute(f"SELECT {columns} FROM chain WHERE tuna_block < :tuna_block ORDER BY tuna_block DESC LIMIT 10;", {"tuna_block": start_block})
result['result'] = [dict(x) for x in cur.fetchall()]
except Exception as e:
print(e)
result['error'] = 'error'
db.close()
return result
@app.route("/proof/<data>")
def api_proof(data):
try:
hash_bytes = bytes.fromhex(data)
except:
return json.dumps({'error': 'invalid hex data'})
result = {}
t0 = time.time()
try:
t = self.watcher.state['tuna'].state['trie']
result['result'] = {'cbor': t.prove_digest(hash_bytes).toCBOR()}
except Exception as e:
print(e)
result['error'] = 'proof error'
print(f"took {time.time()-t0} seconds")
return json.dumps(result)
@app.route("/tx_cbor/<txid>")
def txcbor(txid):
result = {}
try:
db = sqlite3.connect(db_filename)
db.row_factory = sqlite3.Row
cur = db.cursor()
except Exception as e:
result['error'] = 'db error'
return result
if len(txid) != 64:
result['error'] = 'invalid tx id'
return result
txid = txid.lower()
if not all(c in string.hexdigits for c in txid):
result['error'] = 'invalid tx id'
return result
try:
cur.execute(f"SELECT cbor FROM chain WHERE tx = :txid LIMIT 1;", {"txid": txid})
row = cur.fetchone()
result['result'] = row['cbor'].hex()
db.close()
return result
except:
result['error'] = 'db error'
return result
@app.route("/miner_cred_hashes_from_pkh/<pkh>")
def miner_cred_hashes_from_pkh(pkh):
result = {}
if len(pkh) != 56:
result['error'] = 'invalid pkh'
return result
pkh = pkh.lower()
if not all(c in string.hexdigits for c in pkh):
result['error'] = 'invalid pkh'
return result
try:
db = sqlite3.connect(db_filename)
db.row_factory = sqlite3.Row
cur = db.cursor()
except Exception as e:
result['error'] = 'db connect error'
return result
try:
cur.execute(f"SELECT distinct tuna_miner_cred_hash as hash, tuna_miner_data as data FROM chain WHERE tuna_miner = ?;", [pkh])
rows = cur.fetchall()
result['result'] = [dict(x) for x in rows]
db.close()
return result
except Exception as e:
print(f"miner_cred_hashes_from_pkh: error: {e}")
result['error'] = 'db error'
return result
if ':' in self.config.get('CHAIN_STATE_WEBSERVER'):
host_name, port = self.config.get('CHAIN_STATE_WEBSERVER').split(':')
else:
host_name, port = "127.0.0.1", 61631
threading.Thread(target=lambda: app.run(host=host_name, port=int(port), debug=True, use_reloader=False)).start()
def init_dir(self):
try:
os.makedirs(self.dir)
except:
pass
def reset(self):
self.cardano_state = {}
self.tuna_state = {}
self.cur.execute("""CREATE TABLE IF NOT EXISTS chain (
block UNSIGNED BIG INT,
slot UNSIGNED BIG INT,
id VARCHAR(64),
tx VARCHAR(64),
tuna_block UNSIGNED BIG INT,
tuna_hash VARCHAR(64),
tuna_lz INT,
tuna_dn INT,
tuna_epoch BIGINT,
tuna_posix_time BIGINT,
tuna_merkle_root VARCHAR(64)
);""")
self.cur.execute("""CREATE TABLE IF NOT EXISTS submissions (
tuna_block UNSIGNED BIG INT,
tuna_hash VARCHAR(64),
confirmed INT,
submit_time UNSIGNED BIG INT
);""")
try:
self.cur.execute('ALTER TABLE chain ADD COLUMN cbor BLOB;')
except:
pass
try:
self.cur.execute('ALTER TABLE chain ADD COLUMN tuna_miner VARCHAR(56);')
except:
pass
try:
self.cur.execute('ALTER TABLE chain ADD COLUMN tuna_nonce TEXT;')
except:
pass
try:
self.cur.execute('ALTER TABLE chain ADD COLUMN tuna_miner_cred_hash TEXT;')
except:
pass
try:
self.cur.execute('ALTER TABLE chain ADD COLUMN tuna_miner_data TEXT;')
except:
pass
try:
self.cur.execute('ALTER TABLE chain CREATE UNIQUE INDEX IF NOT EXISTS index_tuna_block ON chain(tuna_block);')
except:
pass
try:
self.cur.execute('ALTER TABLE chain CREATE UNIQUE INDEX IF NOT EXISTS index_block ON chain(block);')
except:
pass
def get_tuna_block(self, tuna_block):
self.cur.execute("SELECT block FROM chain WHERE tuna_block = ?", (tuna_block,))
results = self.cur.fetchone()
return results[0] if (results is not None and len(results) > 0) else None
def get_chain(self):
self.cur.execute("SELECT tuna_block, tuna_hash, tuna_merkle_root FROM chain ORDER BY tuna_block;")
return self.cur.fetchall()
def insert(self, record):
existing_tuna_block = self.get_tuna_block(record['tuna_block'])
if existing_tuna_block:
print("TODO: handle rollbacks")
os._exit(17)
self.cur.execute("""INSERT INTO chain (block, slot, id, tx, tuna_block, tuna_hash, tuna_lz, tuna_dn, tuna_epoch, tuna_posix_time, tuna_merkle_root, cbor, tuna_miner, tuna_nonce, tuna_miner_cred_hash, tuna_miner_data)
VALUES (:block, :slot, :id, :tx, :tuna_block, :tuna_hash, :tuna_lz, :tuna_dn, :tuna_epoch, :tuna_posix_time, :tuna_merkle_root, :cbor, :tuna_miner, :tuna_nonce, :tuna_miner_cred_hash, :tuna_miner_data);""", record);
self.con.commit()
def __repr__(self):
return f"<ChainIndex:{self.db_filename}>"
def get_state(self):
self.cur.execute("SELECT * FROM chain ORDER BY tuna_block DESC LIMIT 1;")
result = self.cur.fetchone()
return dict(result) if result else None
def rollback(self, height):
self.cur.execute("DELETE FROM chain WHERE block > :height;", {'height': height})
self.con.commit()
return self.cur.rowcount
def rollback_tuna(self, tuna_block):
self.cur.execute("DELETE FROM chain WHERE tuna_block >= :tuna_block;", {'tuna_block': tuna_block})
self.con.commit()
return self.cur.rowcount