diff --git a/src/communicator/__init__.py b/src/communicator/__init__.py index c83c117e..5df58a9b 100755 --- a/src/communicator/__init__.py +++ b/src/communicator/__init__.py @@ -66,6 +66,8 @@ class OnionrCommunicatorDaemon: self.kv.put('onlinePeers', []) self.kv.put('currentDownloading', []) self.kv.put('announceCache', {}) + self.kv.put('newPeers', []) + self.kv.put('dbTimestamps', {}) if config.get('general.offline_mode', False): self.isOnline = False @@ -91,8 +93,7 @@ class OnionrCommunicatorDaemon: self.connectTimes = {} # list of peer's profiles (onionrpeers.PeerProfile instances) self.peerProfiles = [] - # Peers merged to us. Don't add to db until we know they're reachable - self.newPeers = [] + self.announceProgress = {} self.generating_blocks = [] @@ -103,10 +104,6 @@ class OnionrCommunicatorDaemon: # timestamp when the last online node was seen self.lastNodeSeen = None - # Dict of time stamps for peer's block list lookup times, - # to avoid downloading full lists all the time - self.dbTimestamps = {} - # Loads in and starts the enabled plugins plugins.reload() diff --git a/src/communicator/onlinepeers/removeonlinepeer.py b/src/communicator/onlinepeers/removeonlinepeer.py index 925aae13..60db6ca3 100644 --- a/src/communicator/onlinepeers/removeonlinepeer.py +++ b/src/communicator/onlinepeers/removeonlinepeer.py @@ -30,7 +30,7 @@ def remove_online_peer(comm_inst, peer): except KeyError: pass try: - del comm_inst.dbTimestamps[peer] + del kv.get('dbTimestamps')[peer] except KeyError: pass try: diff --git a/src/communicatorutils/announcenode.py b/src/communicatorutils/announcenode.py index f527da70..052486ae 100755 --- a/src/communicatorutils/announcenode.py +++ b/src/communicatorutils/announcenode.py @@ -4,6 +4,8 @@ Onionr - Private P2P Communication. Use a communicator instance to announce our transport address to connected nodes """ +from typing import TYPE_CHECKING + import logger from onionrutils import basicrequests from utils import gettransports @@ -11,6 +13,9 @@ from netcontroller import NetController from communicator import onlinepeers from coredb import keydb import onionrexceptions + +if TYPE_CHECKING: + from deadsimplekv import DeadSimpleKV """ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/src/communicatorutils/connectnewpeers.py b/src/communicatorutils/connectnewpeers.py index d357e378..bca92a0c 100755 --- a/src/communicatorutils/connectnewpeers.py +++ b/src/communicatorutils/connectnewpeers.py @@ -53,12 +53,12 @@ def connect_new_peer_to_communicator(comm_inst, peer='', useBootstrap=False): """ if len(peerList) < 8 or secrets.randbelow(4) == 3: tryingNew = [] - for x in comm_inst.newPeers: + for x in kv.get('newPeers'): if x not in peerList: peerList.append(x) tryingNew.append(x) for i in tryingNew: - comm_inst.newPeers.remove(i) + kv.get('newPeers').remove(i) if len(peerList) == 0 or useBootstrap: # Avoid duplicating bootstrap addresses in peerList diff --git a/src/communicatorutils/lookupadders.py b/src/communicatorutils/lookupadders.py index 9dfce1cb..d474af2d 100755 --- a/src/communicatorutils/lookupadders.py +++ b/src/communicatorutils/lookupadders.py @@ -1,13 +1,17 @@ -""" -Onionr - Private P2P Communication. +"""Onionr - Private P2P Communication. Lookup new peer transport addresses using the communicator """ +from typing import TYPE_CHECKING import logger from onionrutils import stringvalidators from communicator import peeraction, onlinepeers from utils import gettransports import onionrexceptions + + +if TYPE_CHECKING: + from deadsimplekv import DeadSimpleKV """ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -29,6 +33,7 @@ def lookup_new_peer_transports_with_communicator(comm_inst): tryAmount = 1 newPeers = [] transports = gettransports.get() + kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string("DeadSimpleKV") for i in range(tryAmount): # Download new peer address list from random online peers @@ -50,7 +55,7 @@ def lookup_new_peer_transports_with_communicator(comm_inst): for x in newPeers: x = x.strip() if not stringvalidators.validate_transport(x) \ - or x in comm_inst.newPeers or x in transports: + or x in kv.get('newPeers') or x in transports: # avoid adding if its our address invalid.append(x) for x in invalid: @@ -58,6 +63,6 @@ def lookup_new_peer_transports_with_communicator(comm_inst): newPeers.remove(x) except ValueError: pass - comm_inst.newPeers.extend(newPeers) + kv.get('newPeers').extend(newPeers) comm_inst.decrementThreadCount( 'lookup_new_peer_transports_with_communicator') diff --git a/src/communicatorutils/lookupblocks.py b/src/communicatorutils/lookupblocks.py index db147f8c..a5fed09f 100755 --- a/src/communicatorutils/lookupblocks.py +++ b/src/communicatorutils/lookupblocks.py @@ -76,7 +76,7 @@ def lookup_blocks_from_communicator(comm_inst): # to only fetch blocks since then. # Saved in memory only for privacy reasons try: - lastLookupTime = comm_inst.dbTimestamps[peer] + lastLookupTime = kv.get('dbTimestamps')[peer] except KeyError: lastLookupTime = epoch.get_epoch() - \ config.get("general.max_block_age", @@ -108,7 +108,7 @@ def lookup_blocks_from_communicator(comm_inst): # add blocks to download queue kv.get('blockQueue')[i] = [peer] new_block_count += 1 - comm_inst.dbTimestamps[peer] = \ + kv.get('dbTimestamps')[peer] = \ epoch.get_rounded_epoch(roundS=60) else: if peer not in kv.get('blockQueue')[i]: diff --git a/src/httpapi/miscpublicapi/announce.py b/src/httpapi/miscpublicapi/announce.py index 439aeef0..ac2dbba6 100755 --- a/src/httpapi/miscpublicapi/announce.py +++ b/src/httpapi/miscpublicapi/announce.py @@ -50,7 +50,7 @@ def handle_announce(request): if stringvalidators.validate_transport(newNode) and \ newNode not in announce_queue_list: g.shared_state.get( - OnionrCommunicatorDaemon).newPeers.append(newNode) + deadsimplekv.DeadSimpleKV).get('newPeers').append(newNode) announce_queue.put('new_peers', announce_queue_list.append(newNode)) announce_queue.flush()