-
Notifications
You must be signed in to change notification settings - Fork 9
/
start.py
95 lines (79 loc) · 2.68 KB
/
start.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
import json
import os
import sys
import threading
import uvicorn
from dbClass import dbCalls
from dbPGClass import dbPGCalls
from tnClass import tnCalls
from otherClass import otherCalls
from tnChecker import TNChecker
from ethChecker import ETHChecker
from controlClass import controller
with open('config.json') as json_file:
config = json.load(json_file)
def initialisedb(db):
#get current TN block:
tnlatestBlock = tnCalls(config).currentBlock()
db.insHeights(tnlatestBlock, 'TN')
#get current ETH block:
ethlatestBlock = otherCalls(config).currentBlock()
db.insHeights(ethlatestBlock, 'ETH')
def main():
#check db
if config['main']['use-pg']:
#use PostGres
dbc = dbPGCalls(config)
if config["main"]["db-location"] != "":
path= os.getcwd()
dbfile = path + '/' + config["main"]["db-location"] + '/' + 'gateway.db'
dbfile = os.path.normpath(dbfile)
else:
dbfile = 'gateway.db'
if os.path.isfile(dbfile):
#import old db
print("INFO: importing old SQLite DB")
try:
dbc.createdb()
dbc.importSQLite()
dbfile_new = dbfile.replace('gateway.db', 'gateway.db.imported')
os.rename(dbfile, dbfile_new)
except Exception as e:
print ('Error %s' % e)
print("ERROR: Error occured during import of previous DB")
sys.exit()
else:
try:
result = dbc.lastScannedBlock("TN")
if not isinstance(result, int):
if len(result) == 0:
initialisedb()
except:
dbc.createdb()
initialisedb(dbc)
else:
#use SQLite
dbc = dbCalls(config)
try:
result = dbc.lastScannedBlock("TN")
if not isinstance(result, int):
if len(result) == 0:
initialisedb()
except:
dbc.createdb()
initialisedb(dbc)
dbc.createVerify()
dbc.updateExisting()
#load and start threads
tn = TNChecker(config, dbc)
eth = ETHChecker(config, dbc)
ctrl = controller(config, dbc)
ethThread = threading.Thread(target=eth.run)
tnThread = threading.Thread(target=tn.run)
ctrlThread = threading.Thread(target=ctrl.run)
ethThread.start()
tnThread.start()
ctrlThread.start()
#start app
uvicorn.run("gateway:app", host="0.0.0.0", port=config["main"]["port"], log_level="warning")
main()