Example #1
0
def add_output_db(blockn):
  results=oa_in_block(blockn)

  for tx in results:
    try:
      if 'issued' in tx[1]:
        for outputs in tx[1]['issued']:
          #ISSUED FIRST, no check necessary

          btc=str(outputs['btc'])
          coloramt=str(outputs['quantity'])
          coloraddress=str(outputs['color_address'])   #THIS WORKED!
          spent="False"
          spentat=""
          destination=str(outputs['destination_address'])
          txhash=str(tx[0][0:len(tx[0])-2])
          txhash_index=str(outputs['txhash_index'])
          blockmade=str(blockn)
          prev_input=outputs['previous_inputs']
          databases.add_output(btc,coloramt,coloraddress, spent, spentat, destination, txhash, txhash_index, blockmade, prev_input)

          #ADD NEW ISSUED to COLORS META INFO
          oldamount=databases.read_color(coloraddress)
          if len(oldamount)==0:
            source_address=outputs['previous_inputs'][outputs['previous_inputs'].index(':')+1:len(outputs['previous_inputs'])]
            databases.add_color(coloraddress, source_address, coloramt, "color_name")
          else:
            oldamount=oldamount[0][2]
            databases.edit_color(coloraddress, int(oldamount)+int(coloramt))

        for inps in tx[1]['transferred']:
          #TRANSFERS
          btc=str(inps['btc'])
          coloramt=str(inps['quantity'])
          coloraddress=str(inps['color_address'])
          spent="False"
          spentat=""
          destination=str(inps['destination_address'])
          #print tx
          txhash=str(tx[0][0:len(tx[0])-2])
          txhash_index=str(inps['txhash_index'])
          blockmade=str(blockn)

          prev_inputs=inps['previous_inputs']
          #print prev_inputs

          # totalin=0
          # inputlist=[]
          # for x in prev_inputs:  #for each previnput txhash_with_index
          #   print "CHECKING PREV INPUT: "+str(x)
          #   old=databases.read_output(x,False)   #read that input
          #   print old
          #   if len(old)>0:   #if it is found in the DB
          #     old=old[0]  #get that element
          #     totalin=totalin+old[1]   #add its color amount to the total inputted
          #     coloraddress=databases.dbexecute("SELECT color_address from outputs WHERE txhash_index='"+x+"';",True)[0][0]   #get the color address of that input
          #     inputlist.append([x,old[1], coloraddress])  #append it to the total list
          #
          #   print inputlist

          #CHECK AMT ON PREVIOUS INPUT
              #oldamt=databases.read_output(prev_input, True)

          totalin=int(coloramt) #so it always passes
          if totalin>=int(coloramt): #LEGITIMATE
            #ADD NEW OUTPUT
            print "color address"+str(coloraddress)

            prev_input="FIX HERE"

            #decide which inputs to spend
            totalspent=0
            inputcounter=0
            cont=True
            while int(coloramt)-totalspent>0 and cont:
              if inputcounter<len(inputlist):
                prev_input=inputlist[inputcounter][0]
                totalspent=totalspent+inputlist[inputcounter][1]
                databases.add_output(btc,coloramt,coloraddress,spent,spentat,destination,txhash,txhash_index, blockmade, prev_input)
                inputcounter=inputcounter+1
              elif inputcounter>=len(inputlist):
                cont=False


            #MARK OLD OUTPUT AS SPENT
            #print str(prev_input)+"  "+str(txhash)
            databases.spend_output(prev_input, txhash, blockn)


          else:
            print "ILLEGITIMATE TX: "+str(tx[0])
            print str(totalin)+" / "+str(coloramt)

        previnplist=[]
        for previnps in tx[1]['transferred']:
          for x in previnps['previous_inputs']:
            previnplist.append([x,previnps['txhash_index']])
        for x in previnplist:
          databases.spend_output(x[0], x[1], blockn)
      else:
        print "Invalid OA TX cannot be processed: " +str(tx)+"   END "
    except:
      databases.dbexecute("insert into errors (txhash) values ('"+tx[0]+"')")

  #CHECK BLOCK SPENT TXS FOR VERACITY AT END OF BLOCK
  for tx in results:
    for inps in tx[1]['transferred']:
      prev_inputs=inps['previous_inputs']
      totalin=0
      inputlist=[]
      for x in prev_inputs:  #for each previnput txhash_with_index
        print "CHECKING PREV INPUT: "+str(x)
        old=databases.read_output(x,False)   #read that input
        print old
        if len(old)>0:   #if it is found in the DB
          old=old[0]  #get that element
          totalin=totalin+old[1]   #add its color amount to the total inputted
          coloraddress=databases.dbexecute("SELECT color_address from outputs WHERE txhash_index='"+x+"';",True)[0][0]   #get the color address of that input
          inputlist.append([x,old[1], coloraddress])  #append it to the total list

        print inputlist

    coloramt=str(inps['quantity'])
    if totalin>=int(coloramt):
      #everything was OK
      h=0
    else:
      txhash_index=str(outputs['txhash_index'])
      print "ILLEGITIMATE TX DETECTED "+txhash_index
Example #2
0
def output_db(blockn):
    #ADD OUTPUTS TO DB assuming correctness
    txdata=oa_in_block(blockn)
    for tx in txdata:
      #ISSUED
      for txissued in tx[1]['issued']:
        coloraddress = txissued['color_address']
        btc= str(txissued['btc'])
        coloramt = str(txissued['quantity'])
        spent=str(False)
        spentat=""
        destination=str(txissued['destination_address'])
        txhash_index=str(txissued['txhash_index'])
        txhash = txhash_index[0:len(txhash_index)-2]
        blockmade=str(blockn)
        prev_input=txissued['previous_inputs']
        databases.add_output(btc, coloramt, coloraddress, spent, spentat, destination, txhash, txhash_index, blockmade, prev_input)

          #EDIT COLOR OVERVIEW DATA
        oldamount=databases.read_color(coloraddress)
        if len(oldamount)==0:  #COLOR DOES NOT EXIST YET
          source_address=prev_input[7:len(prev_input)]
          databases.add_color(coloraddress, source_address, coloramt, "color_name")
        else:
          oldamount=oldamount[0][2]
          databases.edit_color(coloraddress, int(oldamount)+int(coloramt))

        #TRANSFERRED
      for txtransfer in tx[1]['transferred']:
        coloraddress="illegitimate"
        btc=str(txtransfer['btc'])
        coloramt=str(txtransfer['quantity'])
        spent=str(False)
        spentat=""
        destination=txtransfer['destination_address']
        txhash_index=txtransfer['txhash_index']
        txhash=txhash_index[0:len(txhash_index)-2]
        blockmade=str(blockn)
        prev_input=txtransfer['previous_inputs']
        databases.add_output(btc, coloramt, coloraddress, spent, spentat, destination, txhash, txhash_index, blockmade, prev_input)

    recentlyaddedtxs=databases.dbexecute("SELECT txhash FROM OUTPUTS WHERE blockmade="+str(blockn)+";", True)
    print "recently added txs  "
    print recentlyaddedtxs
    print ""
    for tx in recentlyaddedtxs:
      txhash=tx[0]
      totalin=0
      inputs=databases.dbexecute("SELECT previous_input from outputs where txhash='"+txhash+"';",True)
      inputs=inputs[0]

      for inp in inputs:
        if inp[0:7]=="source:": #WAS ISSUED, need not be checked
          totalin=999999999999
        else:
          inp=inp.split("_")
          inp=inp[0:len(inp)-1]
          print "MY INPUTS IN TRANSFER "
          print inp
          print ""
          for x in inp:
            dbstring="SELECT color_amount from outputs where txhash_index='"+x+"';"
            colinps=databases.dbexecute(dbstring,True)
            for colinp in colinps:
              totalin=totalin+colinp[0]

      #THEN SUM TOTAL OUT
      outps=databases.dbexecute("SELECT color_amount from outputs where blockmade="+str(blockn)+" and txhash='"+txhash+"'", True)
      totalout=0
      for outp in outps:
        totalout=totalout+outp[0]
      #IF TOTALIN>= TOTAL OUT, its OK, else, SPENT ALL OUTPUTS AND UNSPEND ALL INPUTS
      print "IN: "+str(totalin)+"  OUT: "+str(totalout)+"   for tx: "+str(tx)
      if totalout<=totalin:
        #everything OK
        print "legit tx: "+str(tx)

        #SPEND INPUTS FINALLY
        inputs=databases.dbexecute("SELECT previous_input from outputs where txhash='"+txhash+"';",True)

        for inp in inputs:
          for x in inp:
            if not x[0:7]=="source:":
              x=x.split("_")
              x=x[0:len(x)-1]
              print x

              #GET COLOR OF PREVIOUS INPUTS
              thecolor=databases.dbexecute("SELECT color_address from outputs where txhash_index='"+x[0]+"';",True)
              if len(thecolor)>0:
                thecolor=thecolor[0][0]
              else:
                thecolor="unknown"
              #SET COLOR
              databases.dbexecute("UPDATE outputs set color_address='"+thecolor+"' where txhash='"+txhash+"';",False)

              for y in x:
                databases.spend_output(str(y), txhash,blockn)
                print "SPENDING: "+str(y)
      else:
        print "ILLEGITIMATE TX DETECTED: "+str(tx)
        databases.dbexecute("delete from outputs * where color_address='illegitimate';",False)
Example #3
0
def output_db(blockn):
    #ADD OUTPUTS TO DB assuming correctness
    txdata=oa_in_block(blockn)
    for tx in txdata:
      try:
        #ISSUED
        for txissued in tx[1]['issued']:
          coloraddress = txissued['color_address']
          btc= str(txissued['btc'])
          coloramt = str(txissued['quantity'])
          spent=str(False)
          spentat=""
          destination=str(txissued['destination_address'])
          txhash_index=str(txissued['txhash_index'])
          txhash = txhash_index[0:len(txhash_index)-2]
          blockmade=str(blockn)
          prev_input=txissued['previous_inputs']
          databases.add_output(btc, coloramt, coloraddress, spent, spentat, destination, txhash, txhash_index, blockmade, prev_input)

          #EDIT COLOR OVERVIEW DATA
          oldamount=databases.read_color(coloraddress)
          if len(oldamount)==0:
            source_address=prev_input[7:len(prev_input)]
            databases.add_color(coloraddress, source_address, coloramt, "color_name")
          else:
            oldamount=oldamount[0][2]
            databases.edit_color(coloraddress, int(oldamount)+int(coloramt))

        #TRANSFERRED
        for txtransfer in tx[1]['transferred']:
          coloraddress=txtransfer['color_address']
          btc=str(txtransfer['btc'])
          coloramt=str(txtransfer['quantity'])
          spent=str(False)
          spentat=""
          destination=txtransfer['destination_address']
          txhash_index=txtransfer['txhash_index']
          txhash=txhash_index[0:len(txhash_index)-2]
          blockmade=str(blockn)
          prev_input=txtransfer['previous_inputs']
          databases.add_output(btc, coloramt, coloraddress, spent, spentat, destination, txhash, txhash_index, blockmade, prev_input)
      except:
        databases.dbexecute("insert into errors txhash values ('"+tx[0]+"');",False)

    #after entire block is processed check that the sums match, SPEND SPENT OUTPUTS
    recentlyaddedtxs=databases.dbexecute("SELECT txhash FROM OUTPUTS WHERE blockmade="+str(blockn)+";", True)

      #FIND TX (not outputs but parent) and SUM TOTAL IN
    for tx in recentlyaddedtxs:
      txhash=tx[0]
      #sum total in
      totalin=0
      inputs=databases.dbexecute("SELECT previous_input from outputs where txhash='"+txhash+"';",True)
      for inpa in inputs:
        for inp in inpa:
          if inp[0:7]=="source:": #WAS ISSUED, need not be check
            totalin=999999999999
          else:
            inp=inp.split("_")
            inp=inp[0:len(inp)-1]
            for x in inp:
              dbstring="SELECT color_amount from outputs where txhash_index='"+x+"';"
              #print dbstring
              colinps=databases.dbexecute(dbstring,True)
              for colinp in colinps:
                totalin=totalin+colinp[0]

      #THEN SUM TOTAL OUT
      outps=databases.dbexecute("SELECT color_amount from outputs where blockmade="+str(blockn)+" and txhash='"+txhash+"'", True)
      totalout=0
      for outp in outps:
        totalout=totalout+outp[0]
      #IF TOTALIN>= TOTAL OUT, its OK, else, SPENT ALL OUTPUTS AND UNSPEND ALL INPUTS

      if totalout<=totalin:
        #everything OK
        print "legit tx: "+str(tx)

        #SPEND INPUTS FINALLY
        inputs=databases.dbexecute("SELECT previous_input from outputs where txhash='"+txhash+"';",True)
        for inp in inputs:
          for x in inp:
            if not x[0:7]=="source:":
              x=x.split("_")
              x=x[0:len(inputs)-1]
              for y in x:
                databases.spend_output(str(y), txhash,blockn)

      else:
        print "ILLEGITIMATE TX DETECTED: "+str(tx)