From 0dd49761d09f989b63b27dadbc178159be3d9d57 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Mon, 2 Dec 2019 14:10:52 +0200 Subject: [PATCH] Populate knownnodes from inventory when singleWorker starts --- src/class_objectProcessor.py | 1 + src/class_singleWorker.py | 10 ++++++++++ src/storage/sqlite.py | 24 ++++++++++++++++-------- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/class_objectProcessor.py b/src/class_objectProcessor.py index f435d8d4..1f582be3 100644 --- a/src/class_objectProcessor.py +++ b/src/class_objectProcessor.py @@ -176,6 +176,7 @@ class objectProcessor(threading.Thread): return peer = Peer(host, port) with knownnodes.knownNodesLock: + # FIXME: adjust expirestime knownnodes.addKnownNode( stream, peer, is_self=state.ownAddresses.get(peer)) diff --git a/src/class_singleWorker.py b/src/class_singleWorker.py index 6e2522d8..287f87c5 100644 --- a/src/class_singleWorker.py +++ b/src/class_singleWorker.py @@ -17,6 +17,7 @@ import helper_inbox import helper_msgcoding import helper_random import highlevelcrypto +import knownnodes import l10n import proofofwork import protocol @@ -114,6 +115,15 @@ class singleWorker(StoppableThread): ) del state.ackdataForWhichImWatching[oldack] + # For the case if user deleted knownnodes + # but is still having onionpeer objects in inventory + if not knownnodes.knownNodesActual: + for item in Inventory().by_type_and_tag(protocol.OBJECT_ONIONPEER): + queues.objectProcessorQueue.put(( + protocol.OBJECT_ONIONPEER, item.payload + )) + # FIXME: should also delete from inventory + # give some time for the GUI to start # before we start on existing POW tasks. self.stop.wait(10) diff --git a/src/storage/sqlite.py b/src/storage/sqlite.py index 0992c00e..50a2034e 100644 --- a/src/storage/sqlite.py +++ b/src/storage/sqlite.py @@ -70,15 +70,23 @@ class SqliteInventory(InventoryStorage): # pylint: disable=too-many-ancestors return len(self._inventory) + sqlQuery( 'SELECT count(*) FROM inventory')[0][0] - def by_type_and_tag(self, objectType, tag): - """Return objects filtered by object type and tag""" + def by_type_and_tag(self, objectType, tag=None): + """ + Get all inventory items of certain *objectType* + with *tag* if given. + """ + query = [ + 'SELECT objecttype, streamnumber, payload, expirestime, tag' + ' FROM inventory WHERE objecttype=?', objectType] + if tag: + query[0] += ' AND tag=?' + query.append(sqlite3.Binary(tag)) with self.lock: - values = [value for value in self._inventory.values() - if value.type == objectType and value.tag == tag] - values += (InventoryItem(*value) for value in sqlQuery( - 'SELECT objecttype, streamnumber, payload, expirestime, tag' - ' FROM inventory WHERE objecttype=? AND tag=?', - objectType, sqlite3.Binary(tag))) + values = [ + value for value in self._inventory.values() + if value.type == objectType + and tag is None or value.tag == tag + ] + [InventoryItem(*value) for value in sqlQuery(*query)] return values def unexpired_hashes_by_stream(self, stream):