don't use pubkeys table to send pubkeys to peers now that we maintain them in the inventory table for 28 days anyway

This commit is contained in:
Jonathan Warren 2013-04-22 16:01:41 -04:00
parent bf74b8488f
commit 973ea35177
2 changed files with 47 additions and 44 deletions

View File

@ -786,7 +786,7 @@ class receiveDataThread(QThread):
printLock.release() printLock.release()
#This is not an acknowledgement bound for me. See if it is a message bound for me by trying to decrypt it with my private keys. #This is not an acknowledgement bound for me. See if it is a message bound for me by trying to decrypt it with my private keys.
for key, cryptorObject in myECAddressHashes.items(): for key, cryptorObject in myECCryptorObjects.items():
try: try:
unencryptedData = cryptorObject.decrypt(encryptedData[readPosition:]) unencryptedData = cryptorObject.decrypt(encryptedData[readPosition:])
toRipe = key #This is the RIPE hash of my pubkeys. We need this below to compare to the destination_ripe included in the encrypted data. toRipe = key #This is the RIPE hash of my pubkeys. We need this below to compare to the destination_ripe included in the encrypted data.
@ -915,17 +915,8 @@ class receiveDataThread(QThread):
print 'fromAddress:', fromAddress print 'fromAddress:', fromAddress
print 'First 150 characters of message:', repr(message[:150]) print 'First 150 characters of message:', repr(message[:150])
#Look up the destination address (my address) based on the destination ripe hash. toAddress = myAddressesByHash[toRipe] #Look up my address based on the RIPE hash.
#I realize that I could have a data structure devoted to this task, or maintain an indexed table toLabel = config.get(toAddress, 'label')
#in the sql database, but I would prefer to minimize the number of data structures this program
#uses. Searching linearly through the user's short list of addresses doesn't take very long anyway.
configSections = config.sections()
for addressInKeysFile in configSections:
if addressInKeysFile <> 'bitmessagesettings':
status,addressVersionNumber,streamNumber,hash = decodeAddress(addressInKeysFile)
if hash == key:
toAddress = addressInKeysFile
toLabel = config.get(addressInKeysFile, 'label')
if toLabel == '': if toLabel == '':
toLabel = addressInKeysFile toLabel = addressInKeysFile
@ -1233,7 +1224,7 @@ class receiveDataThread(QThread):
return return
print 'the hash requested in this getpubkey request is:', requestedHash.encode('hex') print 'the hash requested in this getpubkey request is:', requestedHash.encode('hex')
sqlLock.acquire() """sqlLock.acquire()
t = (requestedHash,int(time.time())-lengthOfTimeToHoldOnToAllPubkeys) #this prevents SQL injection t = (requestedHash,int(time.time())-lengthOfTimeToHoldOnToAllPubkeys) #this prevents SQL injection
sqlSubmitQueue.put('''SELECT hash, transmitdata, time FROM pubkeys WHERE hash=? AND havecorrectnonce=1 AND time>?''') sqlSubmitQueue.put('''SELECT hash, transmitdata, time FROM pubkeys WHERE hash=? AND havecorrectnonce=1 AND time>?''')
sqlSubmitQueue.put(t) sqlSubmitQueue.put(t)
@ -1248,9 +1239,15 @@ class receiveDataThread(QThread):
inventoryHash = calculateInventoryHash(payload) inventoryHash = calculateInventoryHash(payload)
objectType = 'pubkey' objectType = 'pubkey'
inventory[inventoryHash] = (objectType, self.streamNumber, payload, timeEncodedInPubkey)#If the time embedded in this pubkey is more than 3 days old then this object isn't going to last very long in the inventory- the cleanerThread is going to come along and move it from the inventory in memory to the SQL inventory and then delete it from the SQL inventory. It should still find its way back to the original requestor if he is online however. inventory[inventoryHash] = (objectType, self.streamNumber, payload, timeEncodedInPubkey)#If the time embedded in this pubkey is more than 3 days old then this object isn't going to last very long in the inventory- the cleanerThread is going to come along and move it from the inventory in memory to the SQL inventory and then delete it from the SQL inventory. It should still find its way back to the original requestor if he is online however.
self.broadcastinv(inventoryHash) self.broadcastinv(inventoryHash)"""
else: #the pubkey is not in our database of pubkeys. Let's check if the requested key is ours (which would mean we should do the POW, put it in the pubkey table, and broadcast out the pubkey.) #else: #the pubkey is not in our database of pubkeys. Let's check if the requested key is ours (which would mean we should do the POW, put it in the pubkey table, and broadcast out the pubkey.)
if requestedHash in myECAddressHashes: #if this address hash is one of mine if requestedHash in myAddressesByHash: #if this address hash is one of mine
try:
lastPubkeySendTime = int(config.get(myAddressesByHash[requestedHash],'lastpubkeysendtime'))
except:
lastPubkeySendTime = 0
print 'lastPubkeySendTime is',lastPubkeySendTime
if lastPubkeySendTime < time.time()-lengthOfTimeToHoldOnToAllPubkeys: #If the last time we sent our pubkey was 28 days ago
printLock.acquire() printLock.acquire()
print 'Found getpubkey-requested-hash in my list of EC hashes. Telling Worker thread to do the POW for a pubkey message and send it out.' print 'Found getpubkey-requested-hash in my list of EC hashes. Telling Worker thread to do the POW for a pubkey message and send it out.'
printLock.release() printLock.release()
@ -1930,8 +1927,8 @@ def reloadMyAddressHashes():
printLock.acquire() printLock.acquire()
print 'reloading keys from keys.dat file' print 'reloading keys from keys.dat file'
printLock.release() printLock.release()
myRSAAddressHashes.clear() myECCryptorObjects.clear()
myECAddressHashes.clear() myAddressesByHash.clear()
#myPrivateKeys.clear() #myPrivateKeys.clear()
configSections = config.sections() configSections = config.sections()
for addressInKeysFile in configSections: for addressInKeysFile in configSections:
@ -1942,14 +1939,10 @@ def reloadMyAddressHashes():
if addressVersionNumber == 2: if addressVersionNumber == 2:
privEncryptionKey = decodeWalletImportFormat(config.get(addressInKeysFile, 'privencryptionkey')).encode('hex') #returns a simple 32 bytes of information encoded in 64 Hex characters, or null if there was an error privEncryptionKey = decodeWalletImportFormat(config.get(addressInKeysFile, 'privencryptionkey')).encode('hex') #returns a simple 32 bytes of information encoded in 64 Hex characters, or null if there was an error
if len(privEncryptionKey) == 64:#It is 32 bytes encoded as 64 hex characters if len(privEncryptionKey) == 64:#It is 32 bytes encoded as 64 hex characters
myECAddressHashes[hash] = highlevelcrypto.makeCryptor(privEncryptionKey) myECCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey)
elif addressVersionNumber == 1: myAddressesByHash[hash] = addressInKeysFile
n = config.getint(addressInKeysFile, 'n') else:
e = config.getint(addressInKeysFile, 'e') sys.stderr.write('Error in reloadMyAddressHashes: Can\'t handle address versions other than 2.\n')
d = config.getint(addressInKeysFile, 'd')
p = config.getint(addressInKeysFile, 'p')
q = config.getint(addressInKeysFile, 'q')
myRSAAddressHashes[hash] = rsa.PrivateKey(n,e,d,p,q)
#This function expects that pubkey begin with \x04 #This function expects that pubkey begin with \x04
def calculateBitcoinAddressFromPubkey(pubkey): def calculateBitcoinAddressFromPubkey(pubkey):
@ -1992,10 +1985,7 @@ def calculateTestnetAddressFromPubkey(pubkey):
def safeConfigGetBoolean(section,field): def safeConfigGetBoolean(section,field):
try: try:
if config.getboolean(section,field): return config.getboolean(section,field)
return True
else:
return False
except: except:
return False return False
@ -2339,14 +2329,15 @@ class singleWorker(QThread):
def doPOWForMyV2Pubkey(self,hash): #This function also broadcasts out the pubkey message once it is done with the POW def doPOWForMyV2Pubkey(self,hash): #This function also broadcasts out the pubkey message once it is done with the POW
#Look up my stream number based on my address hash #Look up my stream number based on my address hash
configSections = config.sections() """configSections = config.sections()
for addressInKeysFile in configSections: for addressInKeysFile in configSections:
if addressInKeysFile <> 'bitmessagesettings': if addressInKeysFile <> 'bitmessagesettings':
status,addressVersionNumber,streamNumber,hashFromThisParticularAddress = decodeAddress(addressInKeysFile) status,addressVersionNumber,streamNumber,hashFromThisParticularAddress = decodeAddress(addressInKeysFile)
if hash == hashFromThisParticularAddress: if hash == hashFromThisParticularAddress:
myAddress = addressInKeysFile myAddress = addressInKeysFile
break break"""
myAddress = myAddressesByHash[hash]
status,addressVersionNumber,streamNumber,hash = decodeAddress(myAddress)
embeddedTime = int(time.time()+random.randrange(-300, 300)) #the current time plus or minus five minutes embeddedTime = int(time.time()+random.randrange(-300, 300)) #the current time plus or minus five minutes
payload = pack('>I',(embeddedTime)) payload = pack('>I',(embeddedTime))
payload += encodeVarint(addressVersionNumber) #Address version number payload += encodeVarint(addressVersionNumber) #Address version number
@ -2382,13 +2373,13 @@ class singleWorker(QThread):
print '(For pubkey message) Found proof of work', trialValue, 'Nonce:', nonce print '(For pubkey message) Found proof of work', trialValue, 'Nonce:', nonce
payload = pack('>Q',nonce) + payload payload = pack('>Q',nonce) + payload
t = (hash,True,payload,embeddedTime,'no') """t = (hash,True,payload,embeddedTime,'no')
sqlLock.acquire() sqlLock.acquire()
sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?,?)''') sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?,?)''')
sqlSubmitQueue.put(t) sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get() queryreturn = sqlReturnQueue.get()
sqlSubmitQueue.put('commit') sqlSubmitQueue.put('commit')
sqlLock.release() sqlLock.release()"""
inventoryHash = calculateInventoryHash(payload) inventoryHash = calculateInventoryHash(payload)
objectType = 'pubkey' objectType = 'pubkey'
@ -2399,6 +2390,9 @@ class singleWorker(QThread):
printLock.release() printLock.release()
broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash))
self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"") self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"")
config.set(myAddress,'lastpubkeysendtime',str(int(time.time())))
with open(appdata + 'keys.dat', 'wb') as configfile:
config.write(configfile)
def sendBroadcast(self): def sendBroadcast(self):
sqlLock.acquire() sqlLock.acquire()
@ -5139,8 +5133,8 @@ class myTableWidgetItem(QTableWidgetItem):
selfInitiatedConnections = {} #This is a list of current connections (the thread pointers at least) selfInitiatedConnections = {} #This is a list of current connections (the thread pointers at least)
alreadyAttemptedConnectionsList = {} #This is a list of nodes to which we have already attempted a connection alreadyAttemptedConnectionsList = {} #This is a list of nodes to which we have already attempted a connection
sendDataQueues = [] #each sendData thread puts its queue in this list. sendDataQueues = [] #each sendData thread puts its queue in this list.
myRSAAddressHashes = {} myECCryptorObjects = {}
myECAddressHashes = {} myAddressesByHash = {} #The key in this dictionary is the hash encoded in an address and value is the address itself.
#myPrivateKeys = {} #myPrivateKeys = {}
inventory = {} #of objects (like msg payloads and pubkey payloads) Does not include protocol headers (the first 24 bytes of each packet). inventory = {} #of objects (like msg payloads and pubkey payloads) Does not include protocol headers (the first 24 bytes of each packet).
workerQueue = Queue.Queue() workerQueue = Queue.Queue()

View File

@ -96,13 +96,22 @@ def markAllInboxMessagesAsUnread():
conn.commit() conn.commit()
print 'done' print 'done'
def vacuum():
item = '''VACUUM'''
parameters = ''
cur.execute(item, parameters)
output = cur.fetchall()
conn.commit()
print 'done'
#takeInboxMessagesOutOfTrash() #takeInboxMessagesOutOfTrash()
#takeSentMessagesOutOfTrash() #takeSentMessagesOutOfTrash()
#markAllInboxMessagesAsUnread() #markAllInboxMessagesAsUnread()
readInbox() #readInbox()
#readSent() #readSent()
#readPubkeys() #readPubkeys()
#readSubscriptions() #readSubscriptions()
#readInventory() #readInventory()
vacuum() #will defragment and clean empty space from the messages.dat file.