Change in reconnection functions for websocket API and flosight API
This commit is contained in:
parent
1efcea7cce
commit
a80a412ef5
@ -11,6 +11,7 @@ import requests
|
|||||||
import socketio
|
import socketio
|
||||||
from sqlalchemy import create_engine, func
|
from sqlalchemy import create_engine, func
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
import time
|
||||||
|
|
||||||
import parsing
|
import parsing
|
||||||
from config import *
|
from config import *
|
||||||
@ -18,22 +19,22 @@ from models import SystemData, ActiveTable, ConsumedTable, TransferLogs, Transac
|
|||||||
Base, ContractStructure, ContractBase, ContractParticipants, SystemBase, ActiveContracts, ContractAddressMapping, \
|
Base, ContractStructure, ContractBase, ContractParticipants, SystemBase, ActiveContracts, ContractAddressMapping, \
|
||||||
LatestCacheBase, ContractTransactionHistory, RejectedContractTransactionHistory, TokenContractAssociation
|
LatestCacheBase, ContractTransactionHistory, RejectedContractTransactionHistory, TokenContractAssociation
|
||||||
|
|
||||||
|
|
||||||
def retryRequest(tempserverlist, apicall):
|
def retryRequest(tempserverlist, apicall):
|
||||||
if len(tempserverlist) != 0:
|
while len(tempserverlist) != 0:
|
||||||
try:
|
try:
|
||||||
response = requests.get('{}api/{}'.format(tempserverlist[0], apicall))
|
response = requests.get('{}api/{}'.format(tempserverlist[0], apicall))
|
||||||
except:
|
except:
|
||||||
tempserverlist.pop(0)
|
tempserverlist.pop(0)
|
||||||
return retryRequest(tempserverlist, apicall)
|
|
||||||
else:
|
else:
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
return json.loads(response.content)
|
return json.loads(response.content)
|
||||||
else:
|
else:
|
||||||
tempserverlist.pop(0)
|
tempserverlist.pop(0)
|
||||||
return retryRequest(tempserverlist, apicall)
|
|
||||||
else:
|
if len(tempserverlist) == 0:
|
||||||
logger.error("None of the APIs are responding for the call {}".format(apicall))
|
logger.error("None of the APIs are responding for the call {}".format(apicall))
|
||||||
sys.exit(0)
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def multiRequest(apicall, net):
|
def multiRequest(apicall, net):
|
||||||
@ -149,6 +150,7 @@ def processApiBlock(blockhash):
|
|||||||
# Check smartContracts which will be triggered locally, and not by the contract committee
|
# Check smartContracts which will be triggered locally, and not by the contract committee
|
||||||
checkLocaltriggerContracts(blockinfo)
|
checkLocaltriggerContracts(blockinfo)
|
||||||
|
|
||||||
|
|
||||||
def updateLatestTransaction(transactionData, parsed_data):
|
def updateLatestTransaction(transactionData, parsed_data):
|
||||||
# connect to latest transaction db
|
# connect to latest transaction db
|
||||||
conn = sqlite3.connect('latestCache.db')
|
conn = sqlite3.connect('latestCache.db')
|
||||||
@ -2200,12 +2202,22 @@ def scanBlockchain():
|
|||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
# todo Rule 6 - Find current block height
|
# todo Rule 6 - Find current block height
|
||||||
# Rule 7 - Start analysing the block contents from starting block to current height
|
# Rule 7 - Start analysing the block contents from starting block to current height
|
||||||
|
|
||||||
# Find current block height
|
# Find current block height
|
||||||
response = multiRequest('blocks?limit=1', config['DEFAULT']['NET'])
|
current_index = -1
|
||||||
current_index = response['blocks'][0]['height']
|
while(current_index == -1):
|
||||||
logger.debug("Current block height is %s" % str(current_index))
|
response = multiRequest('blocks?limit=1', config['DEFAULT']['NET'])
|
||||||
|
try:
|
||||||
|
current_index = response['blocks'][0]['height']
|
||||||
|
except:
|
||||||
|
logger.debug('Latest block count response from multiRequest() is not in the right format. Displaying the data received in the log below')
|
||||||
|
logger.debug(response)
|
||||||
|
logger.debug('Program will wait for 10 seconds and try to reconnect')
|
||||||
|
time.sleep(10)
|
||||||
|
else:
|
||||||
|
logger.debug("Current block height is %s" % str(current_index))
|
||||||
|
break
|
||||||
|
|
||||||
for blockindex in range(startblock, current_index):
|
for blockindex in range(startblock, current_index):
|
||||||
processBlock(blockindex)
|
processBlock(blockindex)
|
||||||
@ -2303,7 +2315,7 @@ if args.reset == 1:
|
|||||||
# MAIN LOGIC
|
# MAIN LOGIC
|
||||||
# scan from the latest block saved locally to latest network block
|
# scan from the latest block saved locally to latest network block
|
||||||
scanBlockchain()
|
scanBlockchain()
|
||||||
scanBlockchain()
|
|
||||||
|
|
||||||
def switchNeturl(neturl):
|
def switchNeturl(neturl):
|
||||||
testserverlist = ['http://0.0.0.0:9000/', 'https://testnet-flosight.duckdns.org/', 'https://testnet.flocha.in/']
|
testserverlist = ['http://0.0.0.0:9000/', 'https://testnet-flosight.duckdns.org/', 'https://testnet.flocha.in/']
|
||||||
@ -2321,16 +2333,15 @@ def switchNeturl(neturl):
|
|||||||
else:
|
else:
|
||||||
return testserverlist[neturlindex+1]
|
return testserverlist[neturlindex+1]
|
||||||
|
|
||||||
def reconnectSSE(neturl):
|
|
||||||
|
|
||||||
|
def reconnectSSE(neturl):
|
||||||
# Connect to Flosight websocket to get data on new incoming blocks
|
# Connect to Flosight websocket to get data on new incoming blocks
|
||||||
sio = socketio.Client(reconnection=False)
|
sio = socketio.Client(reconnection=False)
|
||||||
try:
|
try:
|
||||||
sio.connect( neturl + "socket.io/socket.io.js")
|
sio.connect( neturl + "socket.io/socket.io.js")
|
||||||
except:
|
except:
|
||||||
logger.debug(f"Could not connect to the websocket endpoint {neturl}. Switching & retrying to next")
|
logger.debug(f"Could not connect to the websocket endpoint {neturl}. Switching & retrying to next")
|
||||||
neturl = switchNeturl(neturl)
|
return
|
||||||
reconnectSSE(neturl)
|
|
||||||
|
|
||||||
@sio.on('connect')
|
@sio.on('connect')
|
||||||
def on_connect():
|
def on_connect():
|
||||||
@ -2340,17 +2351,12 @@ def reconnectSSE(neturl):
|
|||||||
@sio.on('disconnect')
|
@sio.on('disconnect')
|
||||||
def disconnect():
|
def disconnect():
|
||||||
logger.debug(f"Token Tracker disconnected from websocket endpoint {neturl}")
|
logger.debug(f"Token Tracker disconnected from websocket endpoint {neturl}")
|
||||||
logger.debug('The script will rescan from the latest block in local db to latest server on FLO network')
|
return
|
||||||
scanBlockchain()
|
|
||||||
logger.debug("Rescan completed")
|
|
||||||
logger.debug('Attempting reconnect to websocket ...')
|
|
||||||
reconnectSSE(neturl)
|
|
||||||
|
|
||||||
@sio.on('connect_error')
|
@sio.on('connect_error')
|
||||||
def connect_error():
|
def connect_error():
|
||||||
logger.debug(f"CONNECTION_ERROR to the websocket endpoint {neturl}. Switching & retrying to next")
|
logger.debug(f"CONNECTION_ERROR to the websocket endpoint {neturl}. Switching & retrying to next")
|
||||||
neturl = switchNeturl(neturl)
|
return
|
||||||
reconnectSSE(neturl)
|
|
||||||
|
|
||||||
@sio.on('block')
|
@sio.on('block')
|
||||||
def on_block(data):
|
def on_block(data):
|
||||||
@ -2358,7 +2364,14 @@ def reconnectSSE(neturl):
|
|||||||
logger.debug(str(data))
|
logger.debug(str(data))
|
||||||
processApiBlock(data)
|
processApiBlock(data)
|
||||||
|
|
||||||
|
|
||||||
# At this point the script has updated to the latest block
|
# At this point the script has updated to the latest block
|
||||||
# Now we connect to flosight's websocket API to get information about the latest blocks
|
# Now we connect to flosight's websocket API to get information about the latest blocks
|
||||||
neturl = 'https://flosight.duckdns.org/'
|
neturl = 'https://flosight.duckdns.org/'
|
||||||
reconnectSSE(neturl)
|
while(True):
|
||||||
|
logger.debug('The script will rescan from the latest block in local db to latest server on FLO network')
|
||||||
|
scanBlockchain()
|
||||||
|
logger.debug("Rescan completed")
|
||||||
|
logger.debug('Attempting reconnect to websocket ...')
|
||||||
|
reconnectSSE(neturl)
|
||||||
|
neturl = switchNeturl(neturl)
|
||||||
Loading…
Reference in New Issue
Block a user