From dde10b700579f307289baf5865166a8c24953b5f Mon Sep 17 00:00:00 2001 From: Kevin Date: Sun, 26 Jul 2020 19:12:52 -0500 Subject: [PATCH] Moved offllinePeers to KV to further reduce coupling --- src/communicator/__init__.py | 2 +- src/communicator/bootstrappeers.py | 8 +++++++- src/communicator/daemoneventhooks/__init__.py | 2 +- src/communicator/onlinepeers/clearofflinepeer.py | 2 +- src/communicatorutils/connectnewpeers.py | 2 +- src/communicatorutils/netcheck.py | 2 +- 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/communicator/__init__.py b/src/communicator/__init__.py index 51184e3b..051cedda 100755 --- a/src/communicator/__init__.py +++ b/src/communicator/__init__.py @@ -64,6 +64,7 @@ class OnionrCommunicatorDaemon: self.kv.put('blockQueue', {}) self.kv.put('shutdown', False) self.kv.put('onlinePeers', []) + self.kv.put('offlinePeers', []) self.kv.put('currentDownloading', []) self.kv.put('announceCache', {}) self.kv.put('newPeers', []) @@ -87,7 +88,6 @@ class OnionrCommunicatorDaemon: self.delay = 1 # lists of connected peers and peers we know we can't reach currently - self.offlinePeers = [] self.cooldownPeer = {} self.connectTimes = {} # list of peer's profiles (onionrpeers.PeerProfile instances) diff --git a/src/communicator/bootstrappeers.py b/src/communicator/bootstrappeers.py index b76f4546..6a183737 100644 --- a/src/communicator/bootstrappeers.py +++ b/src/communicator/bootstrappeers.py @@ -2,6 +2,11 @@ add bootstrap peers to the communicator peer list """ +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from deadsimplekv import DeadSimpleKV + from utils import readstatic, gettransports from coredb import keydb """ @@ -24,8 +29,9 @@ bootstrap_peers = readstatic.read_static('bootstrap-nodes.txt').split(',') def add_bootstrap_list_to_peer_list(comm_inst, peerList, db_only=False): """Add the bootstrap list to the peer list (no duplicates).""" + kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string("DeadSimpleKV") for i in bootstrap_peers: - if i not in peerList and i not in comm_inst.offlinePeers \ + if i not in peerList and i not in kv.get('offlinePeers') \ and i not in gettransports.get() and len(str(i).strip()) > 0: if not db_only: peerList.append(i) diff --git a/src/communicator/daemoneventhooks/__init__.py b/src/communicator/daemoneventhooks/__init__.py index 8482ed80..1a933014 100644 --- a/src/communicator/daemoneventhooks/__init__.py +++ b/src/communicator/daemoneventhooks/__init__.py @@ -68,7 +68,7 @@ def daemon_event_handlers(shared_state: 'TooMany'): def restart_tor(): restarttor.restart(comm_inst) - comm_inst.offlinePeers = [] + kv.put('offlinePeers', []) def test_runtime(): Thread(target=comm_inst.shared_state.get_by_string( diff --git a/src/communicator/onlinepeers/clearofflinepeer.py b/src/communicator/onlinepeers/clearofflinepeer.py index eb38c4b2..a8f14b75 100644 --- a/src/communicator/onlinepeers/clearofflinepeer.py +++ b/src/communicator/onlinepeers/clearofflinepeer.py @@ -26,7 +26,7 @@ if TYPE_CHECKING: def clear_offline_peer(comm_inst: 'OnionrCommunicatorDaemon'): """Remove the longest offline peer to retry later.""" try: - removed = comm_inst.offlinePeers.pop(0) + removed = comm_inst..pop(0) except IndexError: pass else: diff --git a/src/communicatorutils/connectnewpeers.py b/src/communicatorutils/connectnewpeers.py index bca92a0c..056dcf61 100755 --- a/src/communicatorutils/connectnewpeers.py +++ b/src/communicatorutils/connectnewpeers.py @@ -32,8 +32,8 @@ from coredb import keydb def connect_new_peer_to_communicator(comm_inst, peer='', useBootstrap=False): config = comm_inst.config retData = False - tried = comm_inst.offlinePeers kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string("DeadSimpleKV") + tried = kv.get('offlinePeers') transports = gettransports.get() if peer != '': if stringvalidators.validate_transport(peer): diff --git a/src/communicatorutils/netcheck.py b/src/communicatorutils/netcheck.py index b56a0f33..930e588b 100755 --- a/src/communicatorutils/netcheck.py +++ b/src/communicatorutils/netcheck.py @@ -54,7 +54,7 @@ def net_check(comm_inst): 'This is usually temporary, but bugs and censorship can cause this to persist, in which case you should report it to beardog [at] mailbox.org', # noqa terminal=True) restarttor.restart(comm_inst) - comm_inst.offlinePeers = [] + kv.put('offlinePeers', []) comm_inst.isOnline = False else: comm_inst.isOnline = True