Skip to content

Commit 5faef8d

Browse files
committed
moved inventory in state - global runtime variable from singleton
1 parent d555a79 commit 5faef8d

17 files changed

+68
-54
lines changed

src/api.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
from helper_sql import (
9595
SqlBulkExecute, sqlExecute, sqlQuery, sqlStoredProcedure, sql_ready)
9696
from highlevelcrypto import calculateInventoryHash
97-
from inventory import Inventory
9897

9998
try:
10099
from network import BMConnectionPool
@@ -1340,7 +1339,7 @@ def HandleDisseminatePreparedObject(
13401339
encryptedPayload = pack('>Q', nonce) + encryptedPayload
13411340

13421341
inventoryHash = calculateInventoryHash(encryptedPayload)
1343-
Inventory()[inventoryHash] = (
1342+
state.Inventory[inventoryHash] = (
13441343
objectType, toStreamNumber, encryptedPayload,
13451344
expiresTime, b''
13461345
)
@@ -1396,7 +1395,7 @@ def HandleDissimatePubKey(self, payload):
13961395
inventoryHash = calculateInventoryHash(payload)
13971396
objectType = 1 # .. todo::: support v4 pubkeys
13981397
TTL = 28 * 24 * 60 * 60
1399-
Inventory()[inventoryHash] = (
1398+
state.Inventory[inventoryHash] = (
14001399
objectType, pubkeyStreamNumber, payload, int(time.time()) + TTL, ''
14011400
)
14021401
logger.info(

src/bitmessagecurses/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from addresses import addBMIfNotPresent, decodeAddress
3131
from bmconfigparser import config
3232
from helper_sql import sqlExecute, sqlQuery
33-
from inventory import Inventory
3433

3534
# pylint: disable=global-statement
3635

@@ -145,8 +144,8 @@ def scrollbox(d, text, height=None, width=None):
145144
def resetlookups():
146145
"""Reset the Inventory Lookups"""
147146
global inventorydata
148-
inventorydata = Inventory().numberOfInventoryLookupsPerformed
149-
Inventory().numberOfInventoryLookupsPerformed = 0
147+
inventorydata = state.Inventory.numberOfInventoryLookupsPerformed
148+
state.Inventory.numberOfInventoryLookupsPerformed = 0
150149
Timer(1, resetlookups, ()).start()
151150

152151

src/bitmessagemain.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ def start(self):
176176
# The closeEvent should command this thread to exit gracefully.
177177
sqlLookup.daemon = False
178178
sqlLookup.start()
179-
180-
Inventory() # init
179+
state.Inventory = Inventory() # init
181180

182181
if state.enableObjProc: # Not needed if objproc is disabled
183182
# Start the address generation thread

src/bitmessageqt/address_dialogs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
import queues
1111
import widgets
12+
import state
1213
from account import AccountMixin, GatewayAccount, MailchuckAccount, accountClass
1314
from addresses import addBMIfNotPresent, decodeAddress, encodeVarint
1415
from bmconfigparser import config as global_config
15-
from inventory import Inventory
1616
from tr import _translate
1717

1818

@@ -190,13 +190,13 @@ def _onSuccess(self, addressVersion, streamNumber, ripe):
190190
" broadcasts."
191191
))
192192
else:
193-
Inventory().flush()
193+
state.Inventory.flush()
194194
doubleHashOfAddressData = hashlib.sha512(hashlib.sha512(
195195
encodeVarint(addressVersion)
196196
+ encodeVarint(streamNumber) + ripe
197197
).digest()).digest()
198198
tag = doubleHashOfAddressData[32:]
199-
self.recent = Inventory().by_type_and_tag(3, tag)
199+
self.recent = state.Inventory.by_type_and_tag(3, tag)
200200
count = len(self.recent)
201201
if count == 0:
202202
self.checkBoxDisplayMessagesAlreadyInInventory.setText(

src/bitmessageqt/networkstatus.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import network.stats
1111
import state
1212
import widgets
13-
from inventory import Inventory
1413
from network import BMConnectionPool, knownnodes
1514
from retranslateui import RetranslateMixin
1615
from tr import _translate
@@ -50,7 +49,7 @@ def __init__(self, parent=None):
5049

5150
def startUpdate(self):
5251
"""Start a timer to update counters every 2 seconds"""
53-
Inventory().numberOfInventoryLookupsPerformed = 0
52+
state.Inventory.numberOfInventoryLookupsPerformed = 0
5453
self.runEveryTwoSeconds()
5554
self.timer.start(2000) # milliseconds
5655

@@ -229,8 +228,8 @@ def updateNetworkStatusTab(self, outbound, add, destination):
229228
def runEveryTwoSeconds(self):
230229
"""Updates counters, runs every 2 seconds if the timer is running"""
231230
self.labelLookupsPerSecond.setText(_translate("networkstatus", "Inventory lookups per second: %1").arg(
232-
str(Inventory().numberOfInventoryLookupsPerformed / 2)))
233-
Inventory().numberOfInventoryLookupsPerformed = 0
231+
str(state.Inventory.numberOfInventoryLookupsPerformed / 2)))
232+
state.Inventory.numberOfInventoryLookupsPerformed = 0
234233
self.updateNumberOfBytes()
235234
self.updateNumberOfObjectsToBeSynced()
236235

src/class_objectProcessor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
from fallback import RIPEMD160Hash
3232
from helper_sql import (
3333
sql_ready, sql_timeout, SqlBulkExecute, sqlExecute, sqlQuery)
34-
from inventory import Inventory
3534
from network import knownnodes
3635
from network.node import Peer
3736
from tr import _translate
@@ -736,7 +735,7 @@ def processmsg(self, data):
736735
objectType, toStreamNumber, expiresTime = \
737736
protocol.decodeObjectParameters(ackPayload)
738737
inventoryHash = highlevelcrypto.calculateInventoryHash(ackPayload)
739-
Inventory()[inventoryHash] = (
738+
state.Inventory[inventoryHash] = (
740739
objectType, toStreamNumber, ackPayload, expiresTime, b'')
741740
queues.invQueue.put((toStreamNumber, inventoryHash))
742741

src/class_singleCleaner.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import state
2828
from bmconfigparser import config
2929
from helper_sql import sqlExecute, sqlQuery
30-
from inventory import Inventory
3130
from network import BMConnectionPool, knownnodes, StoppableThread
3231
from tr import _translate
3332

@@ -69,7 +68,7 @@ def run(self): # pylint: disable=too-many-branches
6968
'updateStatusBar',
7069
'Doing housekeeping (Flushing inventory in memory to disk...)'
7170
))
72-
Inventory().flush()
71+
state.Inventory.flush()
7372
queues.UISignalQueue.put(('updateStatusBar', ''))
7473

7574
# If we are running as a daemon then we are going to fill up the UI
@@ -82,7 +81,7 @@ def run(self): # pylint: disable=too-many-branches
8281
tick = int(time.time())
8382
if timeWeLastClearedInventoryAndPubkeysTables < tick - 7380:
8483
timeWeLastClearedInventoryAndPubkeysTables = tick
85-
Inventory().clean()
84+
state.Inventory.clean()
8685
queues.workerQueue.put(('sendOnionPeerObj', ''))
8786
# pubkeys
8887
sqlExecute(

src/class_singleWorker.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from addresses import decodeAddress, decodeVarint, encodeVarint
2929
from bmconfigparser import config
3030
from helper_sql import sqlExecute, sqlQuery
31-
from inventory import Inventory
3231
from network import knownnodes, StoppableThread
3332
from six.moves import configparser, queue
3433

@@ -117,7 +116,7 @@ def run(self):
117116
# For the case if user deleted knownnodes
118117
# but is still having onionpeer objects in inventory
119118
if not knownnodes.knownNodesActual:
120-
for item in Inventory().by_type_and_tag(protocol.OBJECT_ONIONPEER):
119+
for item in state.Inventory.by_type_and_tag(protocol.OBJECT_ONIONPEER):
121120
queues.objectProcessorQueue.put((
122121
protocol.OBJECT_ONIONPEER, item.payload
123122
))
@@ -288,7 +287,7 @@ def doPOWForMyV2Pubkey(self, adressHash):
288287

289288
inventoryHash = highlevelcrypto.calculateInventoryHash(payload)
290289
objectType = 1
291-
Inventory()[inventoryHash] = (
290+
state.Inventory[inventoryHash] = (
292291
objectType, streamNumber, payload, embeddedTime, '')
293292

294293
self.logger.info(
@@ -376,7 +375,7 @@ def sendOutOrStoreMyV3Pubkey(self, adressHash):
376375

377376
inventoryHash = highlevelcrypto.calculateInventoryHash(payload)
378377
objectType = 1
379-
Inventory()[inventoryHash] = (
378+
state.Inventory[inventoryHash] = (
380379
objectType, streamNumber, payload, embeddedTime, '')
381380

382381
self.logger.info(
@@ -467,7 +466,7 @@ def sendOutOrStoreMyV4Pubkey(self, myAddress):
467466

468467
inventoryHash = highlevelcrypto.calculateInventoryHash(payload)
469468
objectType = 1
470-
Inventory()[inventoryHash] = (
469+
state.Inventory[inventoryHash] = (
471470
objectType, streamNumber, payload, embeddedTime,
472471
doubleHashOfAddressData[32:]
473472
)
@@ -503,7 +502,7 @@ def sendOnionPeerObj(self, peer=None):
503502
objectPayload = encodeVarint(peer.port) + protocol.encodeHost(peer.host)
504503
tag = highlevelcrypto.calculateInventoryHash(objectPayload)
505504

506-
if Inventory().by_type_and_tag(objectType, tag):
505+
if state.Inventory.by_type_and_tag(objectType, tag):
507506
return # not expired
508507

509508
payload = pack('>Q', embeddedTime)
@@ -516,7 +515,7 @@ def sendOnionPeerObj(self, peer=None):
516515
payload, TTL, log_prefix='(For onionpeer object)')
517516

518517
inventoryHash = highlevelcrypto.calculateInventoryHash(payload)
519-
Inventory()[inventoryHash] = (
518+
state.Inventory[inventoryHash] = (
520519
objectType, streamNumber, buffer(payload), # noqa: F821
521520
embeddedTime, buffer(tag) # noqa: F821
522521
)
@@ -684,7 +683,7 @@ def sendBroadcast(self):
684683

685684
inventoryHash = highlevelcrypto.calculateInventoryHash(payload)
686685
objectType = 3
687-
Inventory()[inventoryHash] = (
686+
state.Inventory[inventoryHash] = (
688687
objectType, streamNumber, payload, embeddedTime, tag)
689688
self.logger.info(
690689
'sending inv (within sendBroadcast function)'
@@ -843,7 +842,7 @@ def sendMsg(self):
843842
hexlify(privEncryptionKey))
844843
)
845844

846-
for value in Inventory().by_type_and_tag(1, toTag):
845+
for value in state.Inventory.by_type_and_tag(1, toTag):
847846
# if valid, this function also puts it
848847
# in the pubkeys table.
849848
if protocol.decryptAndCheckPubkeyPayload(
@@ -1301,7 +1300,7 @@ def sendMsg(self):
13011300

13021301
inventoryHash = highlevelcrypto.calculateInventoryHash(encryptedPayload)
13031302
objectType = 2
1304-
Inventory()[inventoryHash] = (
1303+
state.Inventory[inventoryHash] = (
13051304
objectType, toStreamNumber, encryptedPayload, embeddedTime, '')
13061305
if config.has_section(toaddress) or \
13071306
not protocol.checkBitfield(behaviorBitfield, protocol.BITFIELD_DOESACK):
@@ -1457,7 +1456,7 @@ def requestPubKey(self, toAddress):
14571456

14581457
inventoryHash = highlevelcrypto.calculateInventoryHash(payload)
14591458
objectType = 1
1460-
Inventory()[inventoryHash] = (
1459+
state.Inventory[inventoryHash] = (
14611460
objectType, streamNumber, payload, embeddedTime, '')
14621461
self.logger.info('sending inv (for the getpubkey message)')
14631462
queues.invQueue.put((streamNumber, inventoryHash))

src/inventory.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
"""The Inventory singleton"""
1+
"""The Inventory"""
22

33
# TODO make this dynamic, and watch out for frozen, like with messagetypes
44
import storage.filesystem
55
import storage.sqlite
66
from bmconfigparser import config
7-
from singleton import Singleton
87

98

109
def create_inventory_instance(backend="sqlite"):
@@ -17,10 +16,9 @@ def create_inventory_instance(backend="sqlite"):
1716
"{}Inventory".format(backend.title()))()
1817

1918

20-
@Singleton
21-
class Inventory():
19+
class Inventory:
2220
"""
23-
Inventory singleton class which uses storage backends
21+
Inventory class which uses storage backends
2422
to manage the inventory.
2523
"""
2624
def __init__(self):

src/network/bmobject.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import protocol
88
import state
99
from highlevelcrypto import calculateInventoryHash
10-
from inventory import Inventory
1110
from network.dandelion import Dandelion
1211

1312
logger = logging.getLogger('default')
@@ -115,7 +114,7 @@ def checkAlreadyHave(self):
115114
# if it's a stem duplicate, pretend we don't have it
116115
if Dandelion().hasHash(self.inventoryHash):
117116
return
118-
if self.inventoryHash in Inventory():
117+
if self.inventoryHash in state.Inventory:
119118
raise BMObjectAlreadyHaveError()
120119

121120
def checkObjectByType(self):

0 commit comments

Comments
 (0)