-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtnChecker.py
111 lines (92 loc) · 5.79 KB
/
tnChecker.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
import time
import traceback
import base58
import sharedfunc
from dbClass import dbCalls
from dbPGClass import dbPGCalls
from tnClass import tnCalls
from otherClass import otherCalls
from verification import verifier
class TNChecker(object):
def __init__(self, config, db = None):
self.config = config
if db == None:
if self.config['main']['use-pg']:
self.db = dbPGCalls(config)
else:
self.db = dbCalls(config)
else:
self.db = db
self.tnc = tnCalls(config, self.db)
self.verifier = verifier(config, self.db)
self.lastScannedBlock = self.db.lastScannedBlock("TN")
def run(self):
#main routine to run continuesly
#print('INFO: started checking tn blocks at: ' + str(self.lastScannedBlock))
while True:
try:
nextblock = self.tnc.currentBlock() - self.config['tn']['confirmations']
if nextblock > self.lastScannedBlock:
self.lastScannedBlock += 1
self.checkBlock(self.lastScannedBlock)
self.db.updHeights(self.lastScannedBlock, 'TN')
except Exception as e:
self.lastScannedBlock -= 1
print('ERROR: Something went wrong during tn block iteration: ' + str(traceback.TracebackException.from_exception(e)))
time.sleep(self.config['tn']['timeInBetweenChecks'])
def checkBlock(self, heightToCheck):
#check content of the block for valid transactions
block = self.tnc.getBlock(heightToCheck)
for transaction in block['transactions']:
targetAddress = self.tnc.checkTx(transaction)
if targetAddress is not None:
if targetAddress != "No attachment":
if not(otherCalls(self.config, self.db).validateaddress(targetAddress)):
self.faultHandler(transaction, "txerror")
else:
targetAddress = otherCalls(self.config, self.db).normalizeAddress(targetAddress)
amount = transaction['amount'] / pow(10, self.config['tn']['decimals'])
amount = round(amount, 8)
if amount < self.config['main']['min'] or amount > self.config['main']['max']:
self.faultHandler(transaction, "senderror", e='outside amount ranges')
else:
try:
txId = None
self.db.insTunnel('sending', transaction['sender'], targetAddress)
txId = otherCalls(self.config, self.db).sendTx(targetAddress, amount)
if 'error' in txId:
self.faultHandler(transaction, "senderror", e=txId)
self.db.updTunnel("error", transaction['sender'], targetAddress, statusOld="sending")
else:
print("INFO: send tx: " + str(txId))
self.db.insExecuted(transaction['sender'], targetAddress, txId, transaction['id'], amount, self.config['other']['fee'])
print('INFO: send tokens from tn to other!')
#self.db.delTunnel(transaction['sender'], targetAddress)
self.db.updTunnel("verifying", transaction['sender'], targetAddress, statusOld='sending')
except Exception as e:
self.faultHandler(transaction, "txerror", e=e)
continue
if txId is None:
if targetAddress != 'invalid address':
self.db.insError(transaction['sender'], targetAddress, transaction['id'], '', amount, 'tx failed to send - manual intervention required')
print("ERROR: tx failed to send - manual intervention required")
self.db.updTunnel("error", transaction['sender'], targetAddress, statusOld="sending")
else:
otherCalls(self.config, self.db).verifyTx(txId, transaction['sender'], targetAddress)
else:
self.faultHandler(transaction, 'noattachment')
def faultHandler(self, tx, error, e=""):
#handle transfers to the gateway that have problems
amount = tx['amount'] / pow(10, self.config['tn']['decimals'])
timestampStr = sharedfunc.getnow()
if error == "noattachment":
self.db.insError(tx['sender'], "", tx['id'], "", amount, "no attachment found on transaction")
print("ERROR: " + timestampStr + " - Error: no attachment found on transaction from " + tx['sender'] + " - check errors table.")
if error == "txerror":
targetAddress = base58.b58decode(tx['attachment']).decode()
self.db.insError(tx['sender'], targetAddress, tx['id'], "", amount, "tx error, possible incorrect address", str(e))
print("ERROR: " + timestampStr + " - Error: on outgoing transaction for transaction from " + tx['sender'] + " - check errors table.")
if error == "senderror":
targetAddress = base58.b58decode(tx['attachment']).decode()
self.db.insError(tx['sender'], targetAddress, tx['id'], "", amount, "tx error, check exception error", str(e))
print("ERROR: " + timestampStr + " - Error: on outgoing transaction for transaction from " + tx['sender'] + " - check errors table.")