various small changes

This commit is contained in:
Jonathan Warren 2013-01-04 17:21:33 -05:00
parent 90c997e71f
commit afccad2925
1 changed files with 35 additions and 27 deletions

View File

@ -411,7 +411,7 @@ class receiveDataThread(QThread):
self.receivedgetbiginv = True self.receivedgetbiginv = True
sqlLock.acquire() sqlLock.acquire()
#Select all hashes which are younger than two days old and in this stream. #Select all hashes which are younger than two days old and in this stream.
t = (int(time.time())-172800,self.streamNumber) t = (int(time.time())-maximumAgeOfObjectsThatIAdvertiseToOthers,self.streamNumber)
sqlSubmitQueue.put('''SELECT hash FROM inventory WHERE receivedtime>? and streamnumber=?''') sqlSubmitQueue.put('''SELECT hash FROM inventory WHERE receivedtime>? and streamnumber=?''')
sqlSubmitQueue.put(t) sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get() queryreturn = sqlReturnQueue.get()
@ -424,7 +424,7 @@ class receiveDataThread(QThread):
#print 'bigInvList:', bigInvList #print 'bigInvList:', bigInvList
for hash, storedValue in inventory.items(): for hash, storedValue in inventory.items():
objectType, streamNumber, payload, receivedTime = storedValue objectType, streamNumber, payload, receivedTime = storedValue
if streamNumber == self.streamNumber and receivedTime > int(time.time())-172800: if streamNumber == self.streamNumber and receivedTime > int(time.time())-maximumAgeOfObjectsThatIAdvertiseToOthers:
bigInvList[hash] = 0 bigInvList[hash] = 0
numberOfObjectsInInvMessage = 0 numberOfObjectsInInvMessage = 0
payload = '' payload = ''
@ -890,6 +890,9 @@ class receiveDataThread(QThread):
bitfieldBehaviors = self.data[readPosition:readPosition+4] bitfieldBehaviors = self.data[readPosition:readPosition+4]
readPosition += 4 #for the bitfield of behaviors and features readPosition += 4 #for the bitfield of behaviors and features
addressVersion, varintLength = decodeVarint(self.data[readPosition:readPosition+10]) addressVersion, varintLength = decodeVarint(self.data[readPosition:readPosition+10])
if addressVersion >= 2:
'This version of Bitmessgae cannot handle version', addressVersion,'addresses.'
return
readPosition += varintLength readPosition += varintLength
streamNumber, varintLength = decodeVarint(self.data[readPosition:readPosition+10]) streamNumber, varintLength = decodeVarint(self.data[readPosition:readPosition+10])
readPosition += varintLength readPosition += varintLength
@ -930,6 +933,13 @@ class receiveDataThread(QThread):
if not self.isProofOfWorkSufficient(): if not self.isProofOfWorkSufficient():
print 'Proof of work in getpubkey message insufficient.' print 'Proof of work in getpubkey message insufficient.'
return return
embeddedTime, = unpack('>I',self.data[32:36])
if embeddedTime > int(time.time())+10800:
print 'The time in this getpubkey message is too new. Ignoring it. Time:', embeddedTime
return
if embeddedTime < int(time.time())-maximumAgeOfAnObjectThatIAmWillingToAccept:
print 'The time in this getpubkey message is too old. Ignoring it. Time:', embeddedTime
return
inventoryLock.acquire() inventoryLock.acquire()
inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24]) inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24])
if inventoryHash in inventory: if inventoryHash in inventory:
@ -942,25 +952,23 @@ class receiveDataThread(QThread):
return return
objectType = 'pubkeyrequest' objectType = 'pubkeyrequest'
inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], int(time.time())) inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], embeddedTime)
inventoryLock.release() inventoryLock.release()
#Now let us make sure that the getpubkey request isn't too old or with a fake (future) time. #Now let us make sure that the getpubkey request isn't too old or with a fake (future) time.
embeddedTime, = unpack('>I',self.data[32:36])
if embeddedTime > int(time.time())+10800:
print 'The time in this getpubkey message is too new. Ignoring it. Time:', embeddedTime
return
if embeddedTime < int(time.time())-maximumAgeOfAnObjectThatIAmWillingToAccept:
print 'The time in this getpubkey message is too old. Ignoring it. Time:', embeddedTime
return
addressVersionNumber, addressVersionLength = decodeVarint(self.data[36:42]) addressVersionNumber, addressVersionLength = decodeVarint(self.data[36:42])
if addressVersionNumber > 1:
print 'The addressVersionNumber of the pubkey is too high. Can\'t understand. Ignoring it.'
return
streamNumber, streamNumberLength = decodeVarint(self.data[36+addressVersionLength:42+addressVersionLength]) streamNumber, streamNumberLength = decodeVarint(self.data[36+addressVersionLength:42+addressVersionLength])
if streamNumber <> self.streamNumber: if streamNumber <> self.streamNumber:
print 'The streamNumber', streamNumber, 'doesn\'t match our stream number:', self.streamNumber print 'The streamNumber', streamNumber, 'doesn\'t match our stream number:', self.streamNumber
return return
#This getpubkey request is valid so far. Forward to peers.
broadcastToSendDataQueues((self.streamNumber,'send',self.data[:self.payloadLength+24]))
if addressVersionNumber > 1:
print 'The addressVersionNumber of the pubkey is too high. Can\'t understand. Ignoring it.'
return
if self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength] in myAddressHashes: if self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength] in myAddressHashes:
print 'Found getpubkey requested hash in my list of hashes.' print 'Found getpubkey requested hash in my list of hashes.'
#check to see whether we have already calculated the nonce and transmitted this key before #check to see whether we have already calculated the nonce and transmitted this key before
@ -1000,12 +1008,15 @@ class receiveDataThread(QThread):
sqlSubmitQueue.put(t) sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get() queryreturn = sqlReturnQueue.get()
#Now that we have the key either from getting it earlier or making it and storing it ourselves... #Now that we have the full pubkey message ready either from making it just now or making it earlier, we can send it out.
t = (self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength],) #this prevents SQL injection t = (self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength],) #this prevents SQL injection
sqlSubmitQueue.put('SELECT * FROM pubkeys WHERE hash=?') sqlSubmitQueue.put('''SELECT * FROM pubkeys WHERE hash=? AND havecorrectnonce=1''')
sqlSubmitQueue.put(t) sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get() queryreturn = sqlReturnQueue.get()
if queryreturn == []:
sys.stderr.write('Error: pubkey which we just put in our pubkey database suddenly is not there. Is the database malfunctioning?')
sqlLock.release()
return
for row in queryreturn: for row in queryreturn:
hash, havecorrectnonce, payload, timeLastRequested = row hash, havecorrectnonce, payload, timeLastRequested = row
if timeLastRequested < int(time.time())+604800: #if the last time anyone asked about this hash was this week, extend the time. if timeLastRequested < int(time.time())+604800: #if the last time anyone asked about this hash was this week, extend the time.
@ -1022,11 +1033,11 @@ class receiveDataThread(QThread):
self.broadcastinv(inventoryHash) self.broadcastinv(inventoryHash)
else: else:
print 'Hash in getpubkey is not mine.' print 'Hash in getpubkey request is not for any of my keys.'
#..but lets see if we have it stored from when it came in from someone else. #..but lets see if we have it stored from when it came in from someone else.
t = (self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength],) #this prevents SQL injection t = (self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength],) #this prevents SQL injection
sqlLock.acquire() sqlLock.acquire()
sqlSubmitQueue.put('''SELECT hash, time FROM pubkeys WHERE hash=? AND havecorrectnonce='True' ''') sqlSubmitQueue.put('''SELECT hash, transmitdata, time FROM pubkeys WHERE hash=? AND havecorrectnonce=1''')
sqlSubmitQueue.put(t) sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get() queryreturn = sqlReturnQueue.get()
sqlLock.release() sqlLock.release()
@ -1038,17 +1049,14 @@ class receiveDataThread(QThread):
hash, timeLastRequested = row hash, timeLastRequested = row
if timeLastRequested < int(time.time())+604800: #if the last time anyone asked about this hash was this week, extend the time. if timeLastRequested < int(time.time())+604800: #if the last time anyone asked about this hash was this week, extend the time.
t = (int(time.time())+604800,hash) t = (int(time.time())+604800,hash)
sqlSubmitQueue.put('''UPDATE pubkeys set time=? WHERE hash=?''') sqlSubmitQueue.put('''UPDATE pubkeys set time=? WHERE hash=? ''')
sqlSubmitQueue.put(t) sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get() queryreturn = sqlReturnQueue.get()
inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24]) inventoryHash = calculateInventoryHash(transmitdata)
objectType = 'pubkey' objectType = 'pubkey'
inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], int(time.time())) inventory[inventoryHash] = (objectType, self.streamNumber, transmitdata, int(time.time()))
self.broadcastinv(inventoryHash) self.broadcastinv(inventoryHash)
else:
#We don't have it. We'll need to forward the getpubkey request to our peers.
print 'We don\' have the public key. Forwarding getpubkey message to peers.'
broadcastToSendDataQueues((self.streamNumber,'send',self.data[:self.payloadLength+24]))
#We have received an inv message #We have received an inv message
def recinv(self): def recinv(self):
@ -1611,7 +1619,7 @@ class sqlThread(QThread):
self.cur.execute( '''CREATE TABLE whitelist (label text, address text, enabled bool)''' ) self.cur.execute( '''CREATE TABLE whitelist (label text, address text, enabled bool)''' )
self.cur.execute( '''CREATE TABLE pubkeys (hash blob, havecorrectnonce bool, transmitdata blob, time blob, UNIQUE(hash, havecorrectnonce, transmitdata) ON CONFLICT REPLACE)''' ) self.cur.execute( '''CREATE TABLE pubkeys (hash blob, havecorrectnonce bool, transmitdata blob, time blob, UNIQUE(hash, havecorrectnonce, transmitdata) ON CONFLICT REPLACE)''' )
self.cur.execute( '''CREATE TABLE inventory (hash blob, objecttype text, streamnumber int, payload blob, receivedtime integer, UNIQUE(hash) ON CONFLICT REPLACE)''' ) self.cur.execute( '''CREATE TABLE inventory (hash blob, objecttype text, streamnumber int, payload blob, receivedtime integer, UNIQUE(hash) ON CONFLICT REPLACE)''' )
self.cur.execute( '''CREATE TABLE knownnodes (timelastseen int, stream int, services blob, host blob, port blob, UNIQUE(host) ON CONFLICT REPLACE)''' ) #This table isn't used in the program yet but I have a feeling that we'll need it. self.cur.execute( '''CREATE TABLE knownnodes (timelastseen int, stream int, services blob, host blob, port blob, UNIQUE(host, stream, port) ON CONFLICT REPLACE)''' ) #This table isn't used in the program yet but I have a feeling that we'll need it.
self.conn.commit() self.conn.commit()
print 'Created messages database file' print 'Created messages database file'
@ -1895,6 +1903,7 @@ class singleWorker(QThread):
sqlLock.release() sqlLock.release()
for row in queryreturn: for row in queryreturn:
toaddress, fromaddress, subject, message, ackdata = row toaddress, fromaddress, subject, message, ackdata = row
ackdataForWhichImWatching[ackdata] = 0
status,addressVersionNumber,toStreamNumber,hash = decodeAddress(toaddress) status,addressVersionNumber,toStreamNumber,hash = decodeAddress(toaddress)
#if hash == toRipe: #if hash == toRipe:
self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send the message.') self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send the message.')
@ -2786,7 +2795,6 @@ class MyForm(QtGui.QMainWindow):
for i in range(4): #This will make 32 bytes of random data. for i in range(4): #This will make 32 bytes of random data.
random.seed() random.seed()
ackdata += pack('>Q',random.randrange(1, 18446744073709551615)) ackdata += pack('>Q',random.randrange(1, 18446744073709551615))
ackdataForWhichImWatching[ackdata] = 0
sqlLock.acquire() sqlLock.acquire()
t = ('',toAddress,ripe,fromAddress,subject,message,ackdata,int(time.time()),'findingpubkey',1,1,'sent') t = ('',toAddress,ripe,fromAddress,subject,message,ackdata,int(time.time()),'findingpubkey',1,1,'sent')
sqlSubmitQueue.put('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?)''') sqlSubmitQueue.put('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?)''')