helper_sql #467

Merged
Atheros1 merged 19 commits from master into master 2013-09-03 05:20:20 +02:00
Showing only changes of commit 92c1368691 - Show all commits

View File

@ -19,6 +19,7 @@ import helper_generic
import helper_bitcoin import helper_bitcoin
import helper_inbox import helper_inbox
import helper_sent import helper_sent
from helper_sql import *
import tr import tr
#from bitmessagemain import shared.lengthOfTimeToLeaveObjectsInInventory, shared.lengthOfTimeToHoldOnToAllPubkeys, shared.maximumAgeOfAnObjectThatIAmWillingToAccept, shared.maximumAgeOfObjectsThatIAdvertiseToOthers, shared.maximumAgeOfNodesThatIAdvertiseToOthers, shared.numberOfObjectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHavePerPeer, shared.neededPubkeys #from bitmessagemain import shared.lengthOfTimeToLeaveObjectsInInventory, shared.lengthOfTimeToHoldOnToAllPubkeys, shared.maximumAgeOfAnObjectThatIAmWillingToAccept, shared.maximumAgeOfObjectsThatIAdvertiseToOthers, shared.maximumAgeOfNodesThatIAdvertiseToOthers, shared.numberOfObjectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHavePerPeer, shared.neededPubkeys
@ -281,16 +282,13 @@ class receiveDataThread(threading.Thread):
self.sendBigInv() self.sendBigInv()
def sendBigInv(self): def sendBigInv(self):
shared.sqlLock.acquire()
# Select all hashes which are younger than two days old and in this # Select all hashes which are younger than two days old and in this
# stream. # stream.
t = (int(time.time()) - shared.maximumAgeOfObjectsThatIAdvertiseToOthers, int( queryreturn = sqlQuery(
time.time()) - shared.lengthOfTimeToHoldOnToAllPubkeys, self.streamNumber) '''SELECT hash FROM inventory WHERE ((receivedtime>? and objecttype<>'pubkey') or (receivedtime>? and objecttype='pubkey')) and streamnumber=?''',
shared.sqlSubmitQueue.put( int(time.time()) - shared.maximumAgeOfObjectsThatIAdvertiseToOthers,
'''SELECT hash FROM inventory WHERE ((receivedtime>? and objecttype<>'pubkey') or (receivedtime>? and objecttype='pubkey')) and streamnumber=?''') int(time.time()) - shared.lengthOfTimeToHoldOnToAllPubkeys,
shared.sqlSubmitQueue.put(t) self.streamNumber)
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
bigInvList = {} bigInvList = {}
for row in queryreturn: for row in queryreturn:
hash, = row hash, = row
@ -507,15 +505,12 @@ class receiveDataThread(threading.Thread):
# won't be able to send this pubkey to others (without doing # won't be able to send this pubkey to others (without doing
# the proof of work ourselves, which this program is programmed # the proof of work ourselves, which this program is programmed
# to not do.) # to not do.)
t = (ripe.digest(), '\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF' + '\xFF\xFF\xFF\xFF' + data[ sqlExecute(
beginningOfPubkeyPosition:endOfPubkeyPosition], int(time.time()), 'yes') '''INSERT INTO pubkeys VALUES (?,?,?,?)''',
shared.sqlLock.acquire() ripe.digest(),
shared.sqlSubmitQueue.put( '\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF' + '\xFF\xFF\xFF\xFF' + data[beginningOfPubkeyPosition:endOfPubkeyPosition],
'''INSERT INTO pubkeys VALUES (?,?,?,?)''') int(time.time()),
shared.sqlSubmitQueue.put(t) 'yes')
shared.sqlReturnQueue.get()
shared.sqlSubmitQueue.put('commit')
shared.sqlLock.release()
# shared.workerQueue.put(('newpubkey',(sendersAddressVersion,sendersStream,ripe.digest()))) # shared.workerQueue.put(('newpubkey',(sendersAddressVersion,sendersStream,ripe.digest())))
# This will check to see whether we happen to be awaiting this # This will check to see whether we happen to be awaiting this
# pubkey in order to send a message. If we are, it will do the # pubkey in order to send a message. If we are, it will do the
@ -657,15 +652,11 @@ class receiveDataThread(threading.Thread):
# Let's store the public key in case we want to reply to this # Let's store the public key in case we want to reply to this
# person. # person.
t = (ripe.digest(), '\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF' + '\xFF\xFF\xFF\xFF' + decryptedData[ sqlExecute('''INSERT INTO pubkeys VALUES (?,?,?,?)''',
beginningOfPubkeyPosition:endOfPubkeyPosition], int(time.time()), 'yes') ripe.digest(),
shared.sqlLock.acquire() '\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF' + '\xFF\xFF\xFF\xFF' + decryptedData[beginningOfPubkeyPosition:endOfPubkeyPosition],
shared.sqlSubmitQueue.put( int(time.time()),
'''INSERT INTO pubkeys VALUES (?,?,?,?)''') 'yes')
shared.sqlSubmitQueue.put(t)
shared.sqlReturnQueue.get()
shared.sqlSubmitQueue.put('commit')
shared.sqlLock.release()
# shared.workerQueue.put(('newpubkey',(sendersAddressVersion,sendersStream,ripe.digest()))) # shared.workerQueue.put(('newpubkey',(sendersAddressVersion,sendersStream,ripe.digest())))
# This will check to see whether we happen to be awaiting this # This will check to see whether we happen to be awaiting this
# pubkey in order to send a message. If we are, it will do the POW # pubkey in order to send a message. If we are, it will do the POW
@ -802,14 +793,8 @@ class receiveDataThread(threading.Thread):
print 'This msg IS an acknowledgement bound for me.' print 'This msg IS an acknowledgement bound for me.'
del shared.ackdataForWhichImWatching[encryptedData[readPosition:]] del shared.ackdataForWhichImWatching[encryptedData[readPosition:]]
t = ('ackreceived', encryptedData[readPosition:]) sqlExecute('UPDATE sent SET status=? WHERE ackdata=?',
shared.sqlLock.acquire() 'ackreceived', encryptedData[readPosition:])
shared.sqlSubmitQueue.put(
'UPDATE sent SET status=? WHERE ackdata=?')
shared.sqlSubmitQueue.put(t)
shared.sqlReturnQueue.get()
shared.sqlSubmitQueue.put('commit')
shared.sqlLock.release()
shared.UISignalQueue.put(('updateSentItemStatusByAckdata', (encryptedData[readPosition:], tr.translateText("MainWindow",'Acknowledgement of the message received. %1').arg(unicode( shared.UISignalQueue.put(('updateSentItemStatusByAckdata', (encryptedData[readPosition:], tr.translateText("MainWindow",'Acknowledgement of the message received. %1').arg(unicode(
time.strftime(shared.config.get('bitmessagesettings', 'timeformat'), time.localtime(int(time.time()))), 'utf-8'))))) time.strftime(shared.config.get('bitmessagesettings', 'timeformat'), time.localtime(int(time.time()))), 'utf-8')))))
return return
@ -932,15 +917,12 @@ class receiveDataThread(threading.Thread):
ripe.update(sha.digest()) ripe.update(sha.digest())
# Let's store the public key in case we want to reply to this # Let's store the public key in case we want to reply to this
# person. # person.
t = (ripe.digest(), '\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF' + '\xFF\xFF\xFF\xFF' + decryptedData[ sqlExecute(
messageVersionLength:endOfThePublicKeyPosition], int(time.time()), 'yes') '''INSERT INTO pubkeys VALUES (?,?,?,?)''',
shared.sqlLock.acquire() ripe.digest(),
shared.sqlSubmitQueue.put( '\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF' + '\xFF\xFF\xFF\xFF' + decryptedData[messageVersionLength:endOfThePublicKeyPosition],
'''INSERT INTO pubkeys VALUES (?,?,?,?)''') int(time.time()),
shared.sqlSubmitQueue.put(t) 'yes')
shared.sqlReturnQueue.get()
shared.sqlSubmitQueue.put('commit')
shared.sqlLock.release()
# shared.workerQueue.put(('newpubkey',(sendersAddressVersionNumber,sendersStreamNumber,ripe.digest()))) # shared.workerQueue.put(('newpubkey',(sendersAddressVersionNumber,sendersStreamNumber,ripe.digest())))
# This will check to see whether we happen to be awaiting this # This will check to see whether we happen to be awaiting this
# pubkey in order to send a message. If we are, it will do the POW # pubkey in order to send a message. If we are, it will do the POW
@ -962,26 +944,18 @@ class receiveDataThread(threading.Thread):
return return
blockMessage = False # Gets set to True if the user shouldn't see the message according to black or white lists. blockMessage = False # Gets set to True if the user shouldn't see the message according to black or white lists.
if shared.config.get('bitmessagesettings', 'blackwhitelist') == 'black': # If we are using a blacklist if shared.config.get('bitmessagesettings', 'blackwhitelist') == 'black': # If we are using a blacklist
t = (fromAddress,) queryreturn = sqlQuery(
shared.sqlLock.acquire() '''SELECT label FROM blacklist where address=? and enabled='1' ''',
shared.sqlSubmitQueue.put( fromAddress)
'''SELECT label FROM blacklist where address=? and enabled='1' ''')
shared.sqlSubmitQueue.put(t)
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
if queryreturn != []: if queryreturn != []:
with shared.printLock: with shared.printLock:
print 'Message ignored because address is in blacklist.' print 'Message ignored because address is in blacklist.'
blockMessage = True blockMessage = True
else: # We're using a whitelist else: # We're using a whitelist
t = (fromAddress,) queryreturn = sqlQuery(
shared.sqlLock.acquire() '''SELECT label FROM whitelist where address=? and enabled='1' ''',
shared.sqlSubmitQueue.put( toAddress)
'''SELECT label FROM whitelist where address=? and enabled='1' ''')
shared.sqlSubmitQueue.put(t)
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
if queryreturn == []: if queryreturn == []:
print 'Message ignored because address not in whitelist.' print 'Message ignored because address not in whitelist.'
blockMessage = True blockMessage = True
@ -1111,14 +1085,9 @@ class receiveDataThread(threading.Thread):
if toRipe in shared.neededPubkeys: if toRipe in shared.neededPubkeys:
print 'We have been awaiting the arrival of this pubkey.' print 'We have been awaiting the arrival of this pubkey.'
del shared.neededPubkeys[toRipe] del shared.neededPubkeys[toRipe]
t = (toRipe,) sqlExecute(
shared.sqlLock.acquire() '''UPDATE sent SET status='doingmsgpow' WHERE toripe=? AND (status='awaitingpubkey' or status='doingpubkeypow') and folder='sent' ''',
shared.sqlSubmitQueue.put( toRipe)
'''UPDATE sent SET status='doingmsgpow' WHERE toripe=? AND (status='awaitingpubkey' or status='doingpubkeypow') and folder='sent' ''')
shared.sqlSubmitQueue.put(t)
shared.sqlReturnQueue.get()
shared.sqlSubmitQueue.put('commit')
shared.sqlLock.release()
shared.workerQueue.put(('sendmessage', '')) shared.workerQueue.put(('sendmessage', ''))
else: else:
with shared.printLock: with shared.printLock:
@ -1254,13 +1223,8 @@ class receiveDataThread(threading.Thread):
print 'publicEncryptionKey in hex:', publicEncryptionKey.encode('hex') print 'publicEncryptionKey in hex:', publicEncryptionKey.encode('hex')
t = (ripe,) queryreturn = sqlQuery(
shared.sqlLock.acquire() '''SELECT usedpersonally FROM pubkeys WHERE hash=? AND usedpersonally='yes' ''', ripe)
shared.sqlSubmitQueue.put(
'''SELECT usedpersonally FROM pubkeys WHERE hash=? AND usedpersonally='yes' ''')
shared.sqlSubmitQueue.put(t)
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
if queryreturn != []: # if this pubkey is already in our database and if we have used it personally: if queryreturn != []: # if this pubkey is already in our database and if we have used it personally:
print 'We HAVE used this pubkey personally. Updating time.' print 'We HAVE used this pubkey personally. Updating time.'
t = (ripe, data, embeddedTime, 'yes') t = (ripe, data, embeddedTime, 'yes')
@ -1268,13 +1232,7 @@ class receiveDataThread(threading.Thread):
print 'We have NOT used this pubkey personally. Inserting in database.' print 'We have NOT used this pubkey personally. Inserting in database.'
t = (ripe, data, embeddedTime, 'no') t = (ripe, data, embeddedTime, 'no')
# This will also update the embeddedTime. # This will also update the embeddedTime.
shared.sqlLock.acquire() sqlExecute('''INSERT INTO pubkeys VALUES (?,?,?,?)''', *t)
shared.sqlSubmitQueue.put(
'''INSERT INTO pubkeys VALUES (?,?,?,?)''')
shared.sqlSubmitQueue.put(t)
shared.sqlReturnQueue.get()
shared.sqlSubmitQueue.put('commit')
shared.sqlLock.release()
# shared.workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) # shared.workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe)))
self.possibleNewPubkey(ripe) self.possibleNewPubkey(ripe)
if addressVersion == 3: if addressVersion == 3:
@ -1323,13 +1281,7 @@ class receiveDataThread(threading.Thread):
print 'publicEncryptionKey in hex:', publicEncryptionKey.encode('hex') print 'publicEncryptionKey in hex:', publicEncryptionKey.encode('hex')
t = (ripe,) queryreturn = sqlQuery('''SELECT usedpersonally FROM pubkeys WHERE hash=? AND usedpersonally='yes' ''', ripe)
shared.sqlLock.acquire()
shared.sqlSubmitQueue.put(
'''SELECT usedpersonally FROM pubkeys WHERE hash=? AND usedpersonally='yes' ''')
shared.sqlSubmitQueue.put(t)
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
if queryreturn != []: # if this pubkey is already in our database and if we have used it personally: if queryreturn != []: # if this pubkey is already in our database and if we have used it personally:
print 'We HAVE used this pubkey personally. Updating time.' print 'We HAVE used this pubkey personally. Updating time.'
t = (ripe, data, embeddedTime, 'yes') t = (ripe, data, embeddedTime, 'yes')
@ -1337,13 +1289,7 @@ class receiveDataThread(threading.Thread):
print 'We have NOT used this pubkey personally. Inserting in database.' print 'We have NOT used this pubkey personally. Inserting in database.'
t = (ripe, data, embeddedTime, 'no') t = (ripe, data, embeddedTime, 'no')
# This will also update the embeddedTime. # This will also update the embeddedTime.
shared.sqlLock.acquire() sqlExecute('''INSERT INTO pubkeys VALUES (?,?,?,?)''', *t)
shared.sqlSubmitQueue.put(
'''INSERT INTO pubkeys VALUES (?,?,?,?)''')
shared.sqlSubmitQueue.put(t)
shared.sqlReturnQueue.get()
shared.sqlSubmitQueue.put('commit')
shared.sqlLock.release()
# shared.workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) # shared.workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe)))
self.possibleNewPubkey(ripe) self.possibleNewPubkey(ripe)
@ -1540,13 +1486,9 @@ class receiveDataThread(threading.Thread):
hash] hash]
self.sendData(objectType, payload) self.sendData(objectType, payload)
else: else:
t = (hash,) queryreturn = sqlQuery(
shared.sqlLock.acquire() '''select objecttype, payload from inventory where hash=?''',
shared.sqlSubmitQueue.put( hash)
'''select objecttype, payload from inventory where hash=?''')
shared.sqlSubmitQueue.put(t)
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
if queryreturn != []: if queryreturn != []:
for row in queryreturn: for row in queryreturn:
objectType, payload = row objectType, payload = row