1.0.3 newMultiRequest function

This commit is contained in:
Vivek Teega 2022-01-03 11:01:32 +00:00
parent 6cae4a785e
commit a2ad56b625

View File

@ -20,31 +20,27 @@ from models import SystemData, ActiveTable, ConsumedTable, TransferLogs, Transac
goodblockset = {} goodblockset = {}
goodtxset = {} goodtxset = {}
def retryRequest(tempserverlist, apicall): def newMultiRequest(apicall):
while len(tempserverlist) != 0: current_server = serverlist[0]
while True:
try: try:
response = requests.get('{}api/{}'.format(tempserverlist[0], apicall)) response = requests.get('{}api/{}'.format(current_server, apicall))
except: except:
tempserverlist.pop(0) current_server = switchNeturl(current_server)
logger.info(f"newMultiRequest() switched to {current_server}")
time.sleep(2)
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) current_server = switchNeturl(current_server)
logger.info(f"newMultiRequest() switched to {current_server}")
if len(tempserverlist) == 0: time.sleep(2)
logger.error("None of the APIs are responding for the call {}".format(apicall))
return 0
def multiRequest(apicall, net):
return retryRequest(serverlist, apicall)
def pushData_SSEapi(message): def pushData_SSEapi(message):
signature = pybtc.sign_message(message.encode(), privKey) signature = pybtc.sign_message(message.encode(), privKey)
headers = {'Accept': 'application/json', 'Content-Type': 'application/json', 'Signature': signature} headers = {'Accept': 'application/json', 'Content-Type': 'application/json', 'Signature': signature}
@ -58,11 +54,10 @@ def pushData_SSEapi(message):
def processBlock(blockindex): def processBlock(blockindex):
logger.info(f'Processing block {blockindex}') logger.info(f'Processing block {blockindex}')
logger.info('Processing block ' + str(blockindex))
# Get block details # Get block details
response = multiRequest(f"block-index/{blockindex}", config['DEFAULT']['NET']) response = newMultiRequest(f"block-index/{blockindex}")
blockhash = response['blockHash'] blockhash = response['blockHash']
blockinfo = multiRequest(f"block/{blockhash}", config['DEFAULT']['NET']) blockinfo = newMultiRequest(f"block/{blockhash}")
# todo Rule 8 - read every transaction from every block to find and parse flodata # todo Rule 8 - read every transaction from every block to find and parse flodata
counter = 0 counter = 0
@ -85,7 +80,7 @@ def processBlock(blockindex):
current_index = -1 current_index = -1
while(current_index == -1): while(current_index == -1):
transaction_data = multiRequest(f"tx/{transaction}", config['DEFAULT']['NET']) transaction_data = newMultiRequest(f"tx/{transaction}")
try: try:
text = transaction_data["floData"] text = transaction_data["floData"]
text = text.replace("\n", " \n ") text = text.replace("\n", " \n ")
@ -136,14 +131,14 @@ def processBlock(blockindex):
def processApiBlock(blockhash): def processApiBlock(blockhash):
logger.info(config['DEFAULT']['NET']) logger.info(config['DEFAULT']['NET'])
blockinfo = multiRequest('block/{}'.format(str(blockhash)), config['DEFAULT']['NET']) blockinfo = newMultiRequest('block/{}'.format(str(blockhash)))
# todo Rule 8 - read every transaction from every block to find and parse flodata # todo Rule 8 - read every transaction from every block to find and parse flodata
counter = 0 counter = 0
acceptedTxList = [] acceptedTxList = []
# Scan every transaction # Scan every transaction
for transaction in blockinfo["tx"]: for transaction in blockinfo["tx"]:
transaction_data = multiRequest(f"tx/{transaction}", config['DEFAULT']['NET']) transaction_data = newMultiRequest(f"tx/{transaction}")
text = transaction_data["floData"] text = transaction_data["floData"]
text = text.replace("\n", " \n ") text = text.replace("\n", " \n ")
@ -219,7 +214,7 @@ def transferToken(tokenIdentification, tokenAmount, inputAddress, outputAddress,
elif availableTokens >= commentTransferAmount: elif availableTokens >= commentTransferAmount:
table = session.query(ActiveTable).filter(ActiveTable.address == inputAddress).all() table = session.query(ActiveTable).filter(ActiveTable.address == inputAddress).all()
block_data = multiRequest('block/{}'.format(transaction_data['blockhash']), config['DEFAULT']['NET']) block_data = newMultiRequest('block/{}'.format(transaction_data['blockhash']))
pidlst = [] pidlst = []
checksum = 0 checksum = 0
@ -325,7 +320,7 @@ def transferToken(tokenIdentification, tokenAmount, inputAddress, outputAddress,
session.commit() session.commit()
session.commit() session.commit()
block_data = multiRequest('block/{}'.format(transaction_data['blockhash']), config['DEFAULT']['NET']) block_data = newMultiRequest('block/{}'.format(transaction_data['blockhash']))
blockchainReference = neturl + 'tx/' + transaction_data['txid'] blockchainReference = neturl + 'tx/' + transaction_data['txid']
session.add(TransactionHistory(sourceFloAddress=inputAddress, destFloAddress=outputAddress, session.add(TransactionHistory(sourceFloAddress=inputAddress, destFloAddress=outputAddress,
@ -644,7 +639,7 @@ def processTransaction(transaction_data, parsed_data):
# todo Rule 40 - For each vin, find the feeding address and the fed value. Make an inputlist containing [inputaddress, n value] # todo Rule 40 - For each vin, find the feeding address and the fed value. Make an inputlist containing [inputaddress, n value]
for query in querylist: for query in querylist:
content = multiRequest('tx/{}'.format(str(query[0])), config['DEFAULT']['NET']) content = newMultiRequest('tx/{}'.format(str(query[0])))
for objec in content["vout"]: for objec in content["vout"]:
if objec["n"] == query[1]: if objec["n"] == query[1]:
inputadd = objec["scriptPubKey"]["addresses"][0] inputadd = objec["scriptPubKey"]["addresses"][0]
@ -2637,7 +2632,7 @@ def scanBlockchain():
# Find current block height # Find current block height
current_index = -1 current_index = -1
while(current_index == -1): while(current_index == -1):
response = multiRequest('blocks?limit=1', config['DEFAULT']['NET']) response = newMultiRequest('blocks?limit=1')
try: try:
current_index = response['blocks'][0]['height'] current_index = response['blocks'][0]['height']
except: except:
@ -2655,6 +2650,36 @@ def scanBlockchain():
# 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
def switchNeturl(currentneturl):
neturlindex = serverlist.index(currentneturl)
if neturlindex+1 >= len(serverlist):
return serverlist[neturlindex+1 - len(serverlist)]
else:
return serverlist[neturlindex+1]
def reconnectWebsocket(socket_variable):
# Switch a to different flosight
# neturl = switchNeturl(neturl)
# Connect to Flosight websocket to get data on new incoming blocks
i=0
newurl = serverlist[0]
while(not socket_variable.connected):
logger.info(f"While loop {i}")
logger.info(f"Sleeping for 3 seconds before attempting reconnect to {newurl}")
time.sleep(3)
try:
scanBlockchain()
logger.info(f"Websocket endpoint which is being connected to {newurl}socket.io/socket.io.js")
socket_variable.connect(f"{newurl}socket.io/socket.io.js")
i=i+1
except:
logger.info(f"disconnect block: Failed reconnect attempt to {newurl}")
newurl = switchNeturl(newurl)
i=i+1
# MAIN EXECUTION STARTS # MAIN EXECUTION STARTS
# Configuration of required variables # Configuration of required variables
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -2696,24 +2721,21 @@ if not os.path.isdir(dirpath):
# Read configuration # Read configuration
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read('config.ini') config.read('config.ini')
# todo - rename to flosight url
neturl = config['DEFAULT']['NETURL']
tokenapi_sse_url = config['DEFAULT']['TOKENAPI_SSE_URL']
# Assignment the flo-cli command # todo - write all assertions to make sure default configs are right
if (config['DEFAULT']['NET'] != 'mainnet') and (config['DEFAULT']['NET'] != 'testnet'): if (config['DEFAULT']['NET'] != 'mainnet') and (config['DEFAULT']['NET'] != 'testnet'):
logger.error("NET parameter in config.ini invalid. Options are either 'mainnet' or 'testnet'. Script is exiting now") logger.error("NET parameter in config.ini invalid. Options are either 'mainnet' or 'testnet'. Script is exiting now")
sys.exit(0) sys.exit(0)
# Specify mainnet and testnet server list for API calls and websocket calls # Specify mainnet and testnet server list for API calls and websocket calls
testserverlist = config['DEFAULT']['TESTNET_SERVER_LIST']
mainserverlist = config['DEFAULT']['MAINNET_SERVER_LIST']
serverlist = None serverlist = None
if config['DEFAULT']['NET'] == 'mainnet': if config['DEFAULT']['NET'] == 'mainnet':
serverlist = config['DEFAULT']['MAINNET_SERVER_LIST'] serverlist = config['DEFAULT']['MAINNET_FLOSIGHT_SERVER_LIST']
elif config['DEFAULT']['NET'] == 'testnet': elif config['DEFAULT']['NET'] == 'testnet':
serverlist = config['DEFAULT']['TESTNET_SERVER_LIST'] serverlist = config['DEFAULT']['TESTNET_FLOSIGHT_SERVER_LIST']
serverlist = serverlist.split(',') serverlist = serverlist.split(',')
neturl = config['DEFAULT']['FLOSIGHT_NETURL']
tokenapi_sse_url = config['DEFAULT']['TOKENAPI_SSE_URL']
# Delete database and smartcontract directory if reset is set to 1 # Delete database and smartcontract directory if reset is set to 1
if args.reset == 1: if args.reset == 1:
@ -2748,35 +2770,6 @@ if args.reset == 1:
session.close() session.close()
def switchNeturl(currentneturl):
neturlindex = serverlist.index(currentneturl)
if neturlindex+1 >= len(serverlist):
return serverlist[neturlindex+1 - len(serverlist)]
else:
return serverlist[neturlindex+1]
def reconnectWebsocket(socket_variable):
# Switch a to different flosight
# neturl = switchNeturl(neturl)
# Connect to Flosight websocket to get data on new incoming blocks
i=0
newurl = neturl
while(not socket_variable.connected):
logger.info(f"While loop {i}")
logger.info(f"Sleeping for 3 seconds before attempting reconnect to {newurl}")
time.sleep(3)
try:
scanBlockchain()
logger.info(f"Websocket endpoint which is being connected to {newurl}socket.io/socket.io.js")
socket_variable.connect(f"{newurl}socket.io/socket.io.js")
i=i+1
except:
logger.info(f"disconnect block: Failed reconnect attempt to {newurl}")
newurl = switchNeturl(newurl)
i=i+1
# MAIN LOGIC STARTS # MAIN LOGIC STARTS
# scan from the latest block saved locally to latest network block # scan from the latest block saved locally to latest network block
scanBlockchain() scanBlockchain()