From afccad292520c8be1924e31cf35e816721520e44 Mon Sep 17 00:00:00 2001
From: Jonathan Warren <git@jonwarren.org>
Date: Fri, 4 Jan 2013 17:21:33 -0500
Subject: [PATCH] various small changes

---
 bitmessagemain.py | 62 ++++++++++++++++++++++++++---------------------
 1 file changed, 35 insertions(+), 27 deletions(-)

diff --git a/bitmessagemain.py b/bitmessagemain.py
index 1bd4a2a0..8feac795 100644
--- a/bitmessagemain.py
+++ b/bitmessagemain.py
@@ -411,7 +411,7 @@ class receiveDataThread(QThread):
             self.receivedgetbiginv = True
             sqlLock.acquire()
             #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(t)
             queryreturn = sqlReturnQueue.get()
@@ -424,7 +424,7 @@ class receiveDataThread(QThread):
             #print 'bigInvList:', bigInvList
             for hash, storedValue in inventory.items():
                 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
             numberOfObjectsInInvMessage = 0
             payload = ''
@@ -890,6 +890,9 @@ class receiveDataThread(QThread):
         bitfieldBehaviors = self.data[readPosition:readPosition+4]
         readPosition += 4 #for the bitfield of behaviors and features
         addressVersion, varintLength = decodeVarint(self.data[readPosition:readPosition+10])
+        if addressVersion >= 2:
+            'This version of Bitmessgae cannot handle version', addressVersion,'addresses.'
+            return
         readPosition += varintLength
         streamNumber, varintLength = decodeVarint(self.data[readPosition:readPosition+10])
         readPosition += varintLength
@@ -930,6 +933,13 @@ class receiveDataThread(QThread):
         if not self.isProofOfWorkSufficient():
             print 'Proof of work in getpubkey message insufficient.'
             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()
         inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24])
         if inventoryHash in inventory:
@@ -942,25 +952,23 @@ class receiveDataThread(QThread):
             return
 
         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()
 
         #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])
-        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])
         if streamNumber <> self.streamNumber:
             print 'The streamNumber', streamNumber, 'doesn\'t match our stream number:', self.streamNumber
             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:
             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
@@ -1000,12 +1008,15 @@ class receiveDataThread(QThread):
                 sqlSubmitQueue.put(t)
                 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
-            sqlSubmitQueue.put('SELECT * FROM pubkeys WHERE hash=?')
+            sqlSubmitQueue.put('''SELECT * FROM pubkeys WHERE hash=? AND havecorrectnonce=1''')
             sqlSubmitQueue.put(t)
             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:
                 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.
@@ -1022,11 +1033,11 @@ class receiveDataThread(QThread):
             self.broadcastinv(inventoryHash)
 
         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.
             t = (self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength],) #this prevents SQL injection
             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)
             queryreturn = sqlReturnQueue.get()
             sqlLock.release()
@@ -1038,17 +1049,14 @@ class receiveDataThread(QThread):
                     hash, timeLastRequested = row
                     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)
-                        sqlSubmitQueue.put('''UPDATE pubkeys set time=? WHERE hash=?''')
+                        sqlSubmitQueue.put('''UPDATE pubkeys set time=? WHERE hash=? ''')
                         sqlSubmitQueue.put(t)
                         queryreturn = sqlReturnQueue.get()
-                inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24])
+                inventoryHash = calculateInventoryHash(transmitdata)
                 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)
-            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
     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 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 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()
             print 'Created messages database file'
@@ -1895,6 +1903,7 @@ class singleWorker(QThread):
         sqlLock.release()
         for row in queryreturn:
             toaddress, fromaddress, subject, message, ackdata = row
+            ackdataForWhichImWatching[ackdata] = 0
             status,addressVersionNumber,toStreamNumber,hash = decodeAddress(toaddress)
             #if hash == toRipe:
             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.
                             random.seed()
                             ackdata += pack('>Q',random.randrange(1, 18446744073709551615))
-                        ackdataForWhichImWatching[ackdata] = 0
                         sqlLock.acquire()
                         t = ('',toAddress,ripe,fromAddress,subject,message,ackdata,int(time.time()),'findingpubkey',1,1,'sent')
                         sqlSubmitQueue.put('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?)''')