Testing with Flask API

This commit is contained in:
Vivek Teega 2019-03-05 15:44:11 +05:30
parent 1d88005f70
commit 38a00c9f2e
11 changed files with 449 additions and 5 deletions

BIN
.app.py.swp Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

18
app.py Normal file
View File

@ -0,0 +1,18 @@
from flask import Flask, render_template, jsonify
import os
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/getmarkerlist')
def marker_list():
dblist = os.listdir("databases/")
dbdict = {}
for idx, item in enumerate(dblist):
dbdict[idx] = item[:-3]
return jsonify(dbdict)
app.run(debug=True)

View File

@ -1,9 +1,10 @@
[DEFAULT]
NET = mainnet
NET = testnet
DB_NAME = tree.db
ROOT_ADDRESS = FHQ5rPqqMZT4r3oqpecM54E7tieQoJwZTK
INIT_TOKEN_NO = 21000000
FLO_CLI_PATH = /usr/local/bin/flo-cli
START_BLOCK = 461275
;if 'ranchimalltest#100' is on blockchain, set value to 'ranchimalltest'
PREFIX = ranchimall

BIN
databases/rmt.db Normal file

Binary file not shown.

BIN
databases/teega.db Normal file

Binary file not shown.

View File

@ -1,4 +0,0 @@
root address - oPounjEbJxY7YCBaVBm61Lf2ym9DgFnAdu
address 1 - oGxHPMg3AURY2comkMR1eHhGu7wU8RvMf1
address 2 - oMkS7p426AN9VBVrk2NutFJco5VKnMwS33
address 3 - oQ3KCEpKQVh9M1bEpExGuC1xNZXpFud8fV

46
models.py Normal file
View File

@ -0,0 +1,46 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, Float, String, ForeignKey
Base = declarative_base()
class Extra(Base):
__tablename__ = "extra"
id = Column('id', Integer, primary_key=True)
lastblockscanned = Column('lastblockscanned', Integer)
class TransactionHistory(Base):
__tablename__ = "transactionhistory"
id = Column('id', Integer, primary_key=True)
blockno = Column('blockno', Integer)
fromAddress = Column('fromAddress', String)
toAddress = Column('toAddress', String)
amount = Column('amount', Float)
blockchainReference = Column('blockchainReference', String)
class TransactionTable(Base):
__tablename__ = "transactiontable"
id = Column('id', Integer, primary_key=True)
address = Column('address', String)
parentid = Column('parentid', Integer)
transferBalance = Column('transferBalance', Float)
class TransferLogs(Base):
__tablename__ = "transferlogs"
id = Column('id', Integer, primary_key=True)
primaryIDReference = Column('primaryIDReference', Integer)
transferDescription = Column('transferDescription', String)
transferIDConsumed = Column('transferIDConsumed', Integer)
blockchainReference = Column('blockchainReference', String)
class Webtable(Base):
__tablename__ = "webtable"
id = Column('id', Integer, primary_key=True)
transferDescription = Column('transferDescription', String)
blockchainReference = Column('blockchainReference', String)

100
parsing.py Normal file
View File

@ -0,0 +1,100 @@
import re
marker = None
operation = None
address = None
amount = None
def isTransfer(text):
wordlist = ['transfer','send','give'] #keep everything lowercase
textList = text.split(' ')
for word in wordlist:
if word in textList:
return True
return False
def isIncorp(text):
wordlist = ['incorporate','create','start'] # keep everything lowercase
textList = text.split(' ')
for word in wordlist:
if word in textList:
return True
return False
def extractOperation(text):
operationList = ['send', 'transfer', 'give'] # keep everything lowercase
count = 0
returnval = None
for operation in operationList:
count = count + text.count(operation)
if count > 1:
return 'Too many'
if count == 1 and (returnval is None):
returnval = operation
return returnval
def extractAmount(text):
count = 0
returnval = None
splitText = re.split("\W+", text)
for word in splitText:
word = word.replace('rmt', '')
try:
float(word)
count = count + 1
returnval = float(word)
except ValueError:
pass
if count > 1:
return 'Too many'
return returnval
def extractMarker(text):
textList = text.split(' ')
for word in textList:
if word[-1] == '#':
return word
return False
def extractInitTokens(text):
base_units = {'thousand':10**3 , 'million':10**6 ,'billion':10**9, 'trillion':10**12}
textList = text.split(' ')
for idx,word in enumerate(textList):
try:
result = float(word)
if textList[idx+1] in base_units:
return result*base_units[textList[idx+1]]
return res
except:
continue
# Combine test
def parse_flodata(string):
if string[0:5] == 'text:':
string = string.split('text:')[1]
cleanstring = re.sub(' +', ' ', string)
cleanstring = cleanstring.lower()
if isTransfer(cleanstring):
marker = extractMarker(cleanstring)
operation = extractOperation(cleanstring)
amount = extractAmount(cleanstring)
parsed_data = {'type': 'transfer', 'flodata': string, 'marker': marker, 'operation': operation,
'amount': amount}
elif isIncorp(cleanstring):
incMarker = extractMarker(cleanstring)
initTokens = extractInitTokens(cleanstring)
parsed_data = {'type': 'incorporation', 'flodata': string, 'marker': incMarker, 'initTokens': initTokens}
else:
parsed_data = {'type': 'noise'}
return parsed_data

283
track-tokens-mod.py Normal file
View File

@ -0,0 +1,283 @@
import requests
import json
import sqlite3
import argparse
import configparser
import subprocess
import sys
import parsing
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import create_engine, func, desc
from models import Extra, TransactionHistory, TransactionTable, TransferLogs, Webtable, Base
def startWorking(transaction_data, parsed_data):
engine = create_engine('sqlite:///databases/{}.db'.format(parsed_data['marker'][:-1]), echo=True)
Base.metadata.create_all(bind=engine)
Session = sessionmaker(bind=engine)
session = Session()
# Play area
if parsed_data['type'] == 'transfer':
if parsed_data['amount'] is None:
print("Value for token transfer has not been specified")
return
elif parsed_data['type'] == 'incorporation':
if parsed_data['initTokens'] is None:
print("Value for token transfer has not been specified")
return
inputlist = []
querylist = []
for obj in transaction_data["vin"]:
querylist.append([obj["txid"], obj["vout"]])
inputval = 0
inputadd = ''
for query in querylist:
string = "{} getrawtransaction {} 1".format(localapi, str(query[0]))
response = subprocess.check_output(string, shell=True)
content = json.loads(response.decode("utf-8"))
for objec in content["vout"]:
if objec["n"] == query[1]:
inputadd = objec["scriptPubKey"]["addresses"][0]
inputval = inputval + objec["value"]
inputlist = [[inputadd, inputval]]
if len(inputlist) > 1:
print("Program has detected more than one input address ")
print("This transaction will be discarded")
break
outputlist = []
for obj in transaction_data["vout"]:
if obj["scriptPubKey"]["type"] == "pubkeyhash":
if inputlist[0][0] == obj["scriptPubKey"]["addresses"][0]:
continue
temp = []
temp.append(obj["scriptPubKey"]["addresses"][0])
temp.append(obj["value"])
outputlist.append(temp)
print("\n\nInput List")
print(inputlist)
print("\nOutput List")
print(outputlist)
if len(inputlist) > 1:
print("Program has detected more than one input address ")
print("This transaction will be discarded")
continue
# Check if the transaction is of the type 'incorporation'
if parsed_data['type'] == 'incorporation':
session.add(TransactionTable(address=outputlist[0][0], parentid=0, transferBalance=parsed_data['initTokens']))
session.commit()
#transferDescription = "Root address = " + str(root_address) + " has been initialized with " + str(root_init_value) + " tokens"
#blockchainReference = '{}tx/'.format(neturl)
#session.add(TransferLogs(primaryIDReference=1, transferDescription=transferDescription, transferIDConsumed=0, blockchainReference=blockchainReference))
#session.add(TransactionHistory(blockno=root_block_index, fromAddress='', toAddress=root_address, amount=root_init_value, blockchainReference=blockchainReference))
#session.commit()
return
availableTokens = session.query(func.sum(TransactionTable.transferBalance)).filter_by(address=inputlist[0][0]).all()[0][0]
commentTransferAmount = parsed_data['amount']
if availableTokens is None:
print("The input address dosen't exist in our database ")
elif availableTokens < commentTransferAmount:
print(
"\nThe transfer amount passed in the comments is more than the user owns\nThis transaction will be discarded\n")
continue
elif availableTokens >= commentTransferAmount:
'''if len(commentTransferAmount) != len(outputlist):
print("The parameters in the comments aren't enough")
print("This transaction will be discarded")
continue'''
# if output[0] == inputlist[0][0]:
# continue
#c.execute("SELECT * FROM transactiontable WHERE address=?", (inputlist[0][0],))
#table = c.fetchall()
table = session.query(TransactionTable).filter_by(address=inputlist[0][0]).all()
pidlst = []
checksum = 0
for row in table:
if checksum >= commentTransferAmount:
break
pidlst.append(row.id)
checksum = checksum + row.transferBalance
balance = commentTransferAmount
#c.execute("SELECT sum(transferBalance) FROM transactiontable WHERE address=?", (outputlist[i][0],))
#opbalance = c.fetchall()[0][0]
opbalance = session.query(func.sum(TransactionTable.transferBalance)).filter_by(address=outputlist[0][0]).all()[0][0]
if opbalance is None:
opbalance = 0
#c.execute("SELECT sum(transferBalance) FROM transactiontable WHERE address=?", (inputlist[0][0],))
#ipbalance = c.fetchall()[0][0]
ipbalance = session.query(func.sum(TransactionTable.transferBalance)).filter_by(address=inputlist[0][0]).all()[0][0]
for pid in pidlst:
#c.execute("SELECT transferBalance FROM transactiontable WHERE id=?", (pid,))
#temp = c.fetchall()
#temp = temp[0][0]
temp = session.query(TransactionTable.transferBalance).filter_by(id=pid).all()[0][0]
if balance <= temp:
#c.execute("INSERT INTO transactiontable (address, parentid, transferBalance) VALUES (?,?,?)",
# (outputlist[i][0], pid, balance))
#c.execute("UPDATE transactiontable SET transferBalance=? WHERE id=?", (temp - balance, pid))
session.add(TransactionTable(address=outputlist[0][0], parentid=pid, transferBalance=balance))
entry = session.query(TransactionTable).filter(TransactionTable.id == pid).all()
entry[0].transferBalance = temp - balance
session.commit()
## transaction logs section ##
result = session.query(TransactionTable.id).order_by(desc(TransactionTable.id)).all()
lastid = result[-1].id
transferDescription = str(balance) + " tokens transferred to " + str(
outputlist[0][0]) + " from " + str(inputlist[0][0])
blockchainReference = '{}tx/{}'.format(neturl, str(transaction))
session.add(TransferLogs(primaryIDReference=lastid, transferDescription=transferDescription,
transferIDConsumed=pid, blockchainReference=blockchainReference))
transferDescription = str(inputlist[0][0]) + " balance UPDATED from " + str(
temp) + " to " + str(temp - balance)
blockchainReference = '{}tx/{}'.format(neturl, str(transaction))
session.add(TransferLogs(primaryIDReference=pid, transferDescription=transferDescription,
blockchainReference=blockchainReference))
## transaction history table ##
session.add(TransactionHistory(blockno=blockindex, fromAddress=inputlist[0][0],
toAddress=outputlist[0][0], amount=str(balance),
blockchainReference=blockchainReference))
##webpage table section ##
transferDescription = str(commentTransferAmount) + " tokens transferred from " + str(
inputlist[0][0]) + " to " + str(outputlist[0][0])
session.add(
Webtable(transferDescription=transferDescription, blockchainReference=blockchainReference))
transferDescription = "UPDATE " + str(outputlist[0][0]) + " balance from " + str(
opbalance) + " to " + str(opbalance + commentTransferAmount)
session.add(
Webtable(transferDescription=transferDescription, blockchainReference=blockchainReference))
transferDescription = "UPDATE " + str(inputlist[0][0]) + " balance from " + str(
ipbalance) + " to " + str(ipbalance - commentTransferAmount)
session.add(
Webtable(transferDescription=transferDescription, blockchainReference=blockchainReference))
balance = 0
session.commit()
elif balance > temp:
session.add(TransactionTable(address=outputlist[0][0], parentid=pid, transferBalance=temp))
entry = session.query(TransactionTable).filter(TransactionTable.id == pid).all()
entry[0].transferBalance = 0
session.commit()
##transaction logs section ##
session.query(TransactionTable.id).order_by(desc(TransactionTable.id))
result = session.query(TransactionTable.id).order_by(desc(TransactionTable.id)).all()
lastid = result[-1].id
transferDescription = str(temp) + " tokens transferred to " + str(
outputlist[0][0]) + " from " + str(inputlist[0][0])
blockchainReference = '{}tx/{}'.format(neturl, str(transaction))
session.add(TransactionTable(primaryIDReference=lastid, transferDescription=transferDescription,
transferIDConsumed=pid, blockchainReference=blockchainReference))
transferDescription = str() + " balance UPDATED from " + str(temp) + " to " + str(0)
blockchainReference = '{}tx/{}'.format(neturl, str(transaction))
session.add(TransactionTable(primaryIDReference=pid, transferDescription=transferDescription,
blockchainReference=blockchainReference))
## transaction history table ##
session.add(TransactionHistory(blockno=blockindex, fromAddress=inputlist[0][0],
toAddress=outputlist[0][0], amount=str(balance),
blockchainReference=blockchainReference))
balance = balance - temp
session.commit()
# Finishing statements
session.commit()
session.close()
# Read configuration
config = configparser.ConfigParser()
config.read('config.ini')
# Read command line arguments
parser = argparse.ArgumentParser(description='Script tracks RMT using FLO data on the FLO blockchain - https://flo.cash')
parser.add_argument('-r', '--reset', nargs='?', const=1, type=int, help='Purge existing db and rebuild it')
args = parser.parse_args()
# Assignment the flo-cli command
if config['DEFAULT']['NET'] == 'mainnet':
neturl = 'https://florincoin.info/'
localapi = config['DEFAULT']['FLO_CLI_PATH']
elif config['DEFAULT']['NET'] == 'testnet':
neturl = 'https://testnet.florincoin.info/'
localapi = '{} --testnet'.format(config['DEFAULT']['FLO_CLI_PATH'])
else:
print("NET parameter is wrong\nThe script will exit now ")
# Delete database directory if reset is set to 1
if args.reset == 1:
import os
import shutil
apppath = os.path.dirname(os.path.realpath(__file__))
dirpath = os.path.join(apppath, 'databases')
shutil.rmtree(dirpath)
os.mkdir(dirpath)
# Read start block no
startblock = int(config['DEFAULT']['START_BLOCK'])
# Find current block height
string = "{} getblockcount".format(localapi)
response = subprocess.check_output(string, shell=True)
current_index = json.loads(response.decode("utf-8"))
print("current_block_height : " + str(current_index))
for blockindex in range( startblock, current_index ):
print(blockindex)
# Scan every block
string = "{} getblockhash {}".format(localapi, str(blockindex))
response = subprocess.check_output(string, shell=True)
blockhash = response.decode("utf-8")
string = "{} getblock {}".format(localapi, str(blockhash))
response = subprocess.check_output(string, shell=True)
blockinfo = json.loads(response.decode("utf-8"))
# Scan every transaction
for transaction in blockinfo["tx"]:
string = "{} getrawtransaction {} 1".format(localapi, str(transaction))
response = subprocess.check_output(string, shell=True)
transaction_data = json.loads(response.decode("utf-8"))
text = transaction_data["floData"]
text = text[5:]
parsed_data = parsing.parse_flodata(text)
if parsed_data['type'] != 'noise':
print(blockindex)
print(parsed_data['type'])
startWorking(transaction_data, parsed_data)