Merge pull request #54 from jaicis/py3convert

Solved inventory issue.
This commit is contained in:
jaicis 2020-03-18 16:18:00 +05:30 committed by GitHub
commit ee6f8f0fa7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 14 deletions

View File

@ -455,7 +455,7 @@ class singleWorker(StoppableThread):
inventoryHash = calculateInventoryHash(payload)
objectType = 1
Inventory()._realInventory[inventoryHash] = (
Inventory()[inventoryHash] = (
objectType, streamNumber, payload, embeddedTime,
doubleHashOfAddressData[32:]
)
@ -1239,7 +1239,7 @@ class singleWorker(StoppableThread):
objectType = 2
inventoryHashlist = (
objectType, toStreamNumber,encryptedPayload, embeddedTime, '')
Inventory()._realInventory[inventoryHash] = (
Inventory()[inventoryHash] = (
objectType, toStreamNumber, encryptedPayload, embeddedTime, '')
if BMConfigParser().has_section(toaddress) or \
not protocol.checkBitfield(behaviorBitfield, protocol.BITFIELD_DOESACK):
@ -1393,9 +1393,8 @@ class singleWorker(StoppableThread):
payload = self._doPOWDefaults(payload, TTL)
inventoryHash = calculateInventoryHash(payload)
objectType = 1
Inventory()._realInventory[inventoryHash] = (
Inventory()[inventoryHash] = (
objectType, streamNumber, payload, embeddedTime, '')
# Inventory()._realInventory[inventoryHashlist]
self.logger.info('sending inv (for the getpubkey message)')
queues.invQueue.put((streamNumber, inventoryHash))
# wait 10% past expiration

View File

@ -24,8 +24,8 @@ class Inventory():
# cheap inheritance copied from asyncore
def __getattr__(self, attr):
if attr == "__contains__":
self.numberOfInventoryLookupsPerformed += 1
# if attr == "__contains__":
# self.numberOfInventoryLookupsPerformed += 1
try:
realRet = getattr(self._realInventory, attr)
except AttributeError:
@ -36,6 +36,15 @@ class Inventory():
else:
return realRet
# on python3 we have separately added __contains__ method
def __contains__(self, attr):
self.numberOfInventoryLookupsPerformed += 1
return getattr(self._realInventory, '__contains__')(attr)
# on python3 we have separately added __setitem__ method
def __setitem__(self, hash_, value):
return getattr(self._realInventory,'__setitem__')(hash_,value)
# hint for pylint: this is dictionary like object
def __getitem__(self, key):
return self._realInventory[key]
return self._realInventory[key]

View File

@ -116,7 +116,7 @@ class BMObject(object): # pylint: disable=too-many-instance-attributes
# if it's a stem duplicate, pretend we don't have it
if Dandelion().hasHash(self.inventoryHash):
return
if self.inventoryHash in Inventory()._realInventory:
if self.inventoryHash in Inventory():
raise BMObjectAlreadyHaveError()
def checkObjectByType(self):

View File

@ -357,7 +357,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
if dandelion and not state.dandelion:
return True
for i in map(bytes, items):
if i in Inventory()._realInventory and not Dandelion().hasHash(i):
if i in Inventory() and not Dandelion().hasHash(i):
continue
if dandelion and not Dandelion().hasHash(i):
Dandelion().addHash(i, self)
@ -417,10 +417,9 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
del missingObjects[self.object.inventoryHash]
except KeyError:
pass
if self.object.inventoryHash in Inventory()._realInventory and Dandelion().hasHash(self.object.inventoryHash):
if self.object.inventoryHash in Inventory() and Dandelion().hasHash(self.object.inventoryHash):
Dandelion().removeHash(self.object.inventoryHash, "cycle detection")
Inventory()._realInventory[self.object.inventoryHash] = (
Inventory()[self.object.inventoryHash] = (
self.object.objectType, self.object.streamNumber,
memoryview(self.payload[objectOffset:]), self.object.expiresTime,
memoryview(self.object.tag)

View File

@ -61,7 +61,7 @@ class DownloadThread(StoppableThread):
payload = bytearray()
chunkCount = 0
for chunk in request:
if chunk in Inventory()._realInventory and not Dandelion().hasHash(chunk):
if chunk in Inventory() and not Dandelion().hasHash(chunk):
try:
del i.objectsNewToMe[chunk]
except KeyError:

View File

@ -211,7 +211,7 @@ class TCPConnection(BMProto, TLSDispatcher):
# may lock for a long time, but I think it's better than
# thousands of small locks
with self.objectsNewToThemLock:
for objHash in Inventory()._realInventory.unexpired_hashes_by_stream(stream):
for objHash in Inventory().unexpired_hashes_by_stream(stream):
# don't advertise stem objects on bigInv
if Dandelion().hasHash(objHash):
continue