fix KeyError bug in dht

This commit is contained in:
Alex Grintsvayg 2017-03-31 14:24:21 -04:00
parent d3173b157e
commit 502e2227b5

View file

@ -402,15 +402,15 @@ class OptimizedTreeRoutingTable(TreeRoutingTable):
# Put the new contact in our replacement cache for the # Put the new contact in our replacement cache for the
# corresponding k-bucket (or update it's position if # corresponding k-bucket (or update it's position if
# it exists already) # it exists already)
if not self._replacementCache.has_key(bucketIndex): if bucketIndex not in self._replacementCache:
self._replacementCache[bucketIndex] = [] self._replacementCache[bucketIndex] = []
if contact in self._replacementCache[bucketIndex]: if contact in self._replacementCache[bucketIndex]:
self._replacementCache[bucketIndex].remove(contact) self._replacementCache[bucketIndex].remove(contact)
# TODO: Using k to limit the size of the contact # TODO: Using k to limit the size of the contact
# replacement cache - maybe define a seperate value for # replacement cache - maybe define a separate value for
# this in constants.py? # this in constants.py?
elif len(self._replacementCache) >= constants.k: elif len(self._replacementCache[bucketIndex]) >= constants.k:
self._replacementCache.pop(0) self._replacementCache[bucketIndex].pop(0)
self._replacementCache[bucketIndex].append(contact) self._replacementCache[bucketIndex].append(contact)
def removeContact(self, contactID): def removeContact(self, contactID):
@ -428,8 +428,8 @@ class OptimizedTreeRoutingTable(TreeRoutingTable):
contact.failedRPCs += 1 contact.failedRPCs += 1
if contact.failedRPCs >= 5: if contact.failedRPCs >= 5:
self._buckets[bucketIndex].removeContact(contactID) self._buckets[bucketIndex].removeContact(contactID)
# Replace this stale contact with one from our replacemnent cache, if we have any # Replace this stale contact with one from our replacement cache, if we have any
if self._replacementCache.has_key(bucketIndex): if bucketIndex in self._replacementCache:
if len(self._replacementCache[bucketIndex]) > 0: if len(self._replacementCache[bucketIndex]) > 0:
self._buckets[bucketIndex].addContact( self._buckets[bucketIndex].addContact(
self._replacementCache[bucketIndex].pop()) self._replacementCache[bucketIndex].pop())