From be178f23ae91eae5dba72836de8db8bab3ca9373 Mon Sep 17 00:00:00 2001 From: "jai.s" Date: Wed, 18 Mar 2020 15:26:02 +0530 Subject: [PATCH] Solved inventory issue --- src/class_singleWorker.py | 7 +++---- src/inventory.py | 15 ++++++++++++--- src/network/bmobject.py | 2 +- src/network/bmproto.py | 7 +++---- src/network/downloadthread.py | 2 +- src/network/tcp.py | 2 +- 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/class_singleWorker.py b/src/class_singleWorker.py index d9f5ba31..26c16809 100644 --- a/src/class_singleWorker.py +++ b/src/class_singleWorker.py @@ -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 diff --git a/src/inventory.py b/src/inventory.py index fc06e455..d0c0a829 100644 --- a/src/inventory.py +++ b/src/inventory.py @@ -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] \ No newline at end of file diff --git a/src/network/bmobject.py b/src/network/bmobject.py index ee3bd388..12b997d7 100644 --- a/src/network/bmobject.py +++ b/src/network/bmobject.py @@ -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): diff --git a/src/network/bmproto.py b/src/network/bmproto.py index e98bdf93..e4f09caf 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -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) diff --git a/src/network/downloadthread.py b/src/network/downloadthread.py index bf0344b3..323819ca 100644 --- a/src/network/downloadthread.py +++ b/src/network/downloadthread.py @@ -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: diff --git a/src/network/tcp.py b/src/network/tcp.py index 72a66772..1d3696ed 100644 --- a/src/network/tcp.py +++ b/src/network/tcp.py @@ -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