Moved dbTimestamps and newPeers to KV for more decoupling

This commit is contained in:
Kevin 2020-07-26 15:49:34 -05:00
parent e00d41f8a9
commit 97a5f50271
7 changed files with 23 additions and 16 deletions

View File

@ -66,6 +66,8 @@ class OnionrCommunicatorDaemon:
self.kv.put('onlinePeers', []) self.kv.put('onlinePeers', [])
self.kv.put('currentDownloading', []) self.kv.put('currentDownloading', [])
self.kv.put('announceCache', {}) self.kv.put('announceCache', {})
self.kv.put('newPeers', [])
self.kv.put('dbTimestamps', {})
if config.get('general.offline_mode', False): if config.get('general.offline_mode', False):
self.isOnline = False self.isOnline = False
@ -91,8 +93,7 @@ class OnionrCommunicatorDaemon:
self.connectTimes = {} self.connectTimes = {}
# list of peer's profiles (onionrpeers.PeerProfile instances) # list of peer's profiles (onionrpeers.PeerProfile instances)
self.peerProfiles = [] self.peerProfiles = []
# Peers merged to us. Don't add to db until we know they're reachable
self.newPeers = []
self.announceProgress = {} self.announceProgress = {}
self.generating_blocks = [] self.generating_blocks = []
@ -103,10 +104,6 @@ class OnionrCommunicatorDaemon:
# timestamp when the last online node was seen # timestamp when the last online node was seen
self.lastNodeSeen = None 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 # Loads in and starts the enabled plugins
plugins.reload() plugins.reload()

View File

@ -30,7 +30,7 @@ def remove_online_peer(comm_inst, peer):
except KeyError: except KeyError:
pass pass
try: try:
del comm_inst.dbTimestamps[peer] del kv.get('dbTimestamps')[peer]
except KeyError: except KeyError:
pass pass
try: try:

View File

@ -4,6 +4,8 @@ Onionr - Private P2P Communication.
Use a communicator instance to announce Use a communicator instance to announce
our transport address to connected nodes our transport address to connected nodes
""" """
from typing import TYPE_CHECKING
import logger import logger
from onionrutils import basicrequests from onionrutils import basicrequests
from utils import gettransports from utils import gettransports
@ -11,6 +13,9 @@ from netcontroller import NetController
from communicator import onlinepeers from communicator import onlinepeers
from coredb import keydb from coredb import keydb
import onionrexceptions import onionrexceptions
if TYPE_CHECKING:
from deadsimplekv import DeadSimpleKV
""" """
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by

View File

@ -53,12 +53,12 @@ def connect_new_peer_to_communicator(comm_inst, peer='', useBootstrap=False):
""" """
if len(peerList) < 8 or secrets.randbelow(4) == 3: if len(peerList) < 8 or secrets.randbelow(4) == 3:
tryingNew = [] tryingNew = []
for x in comm_inst.newPeers: for x in kv.get('newPeers'):
if x not in peerList: if x not in peerList:
peerList.append(x) peerList.append(x)
tryingNew.append(x) tryingNew.append(x)
for i in tryingNew: for i in tryingNew:
comm_inst.newPeers.remove(i) kv.get('newPeers').remove(i)
if len(peerList) == 0 or useBootstrap: if len(peerList) == 0 or useBootstrap:
# Avoid duplicating bootstrap addresses in peerList # Avoid duplicating bootstrap addresses in peerList

View File

@ -1,13 +1,17 @@
""" """Onionr - Private P2P Communication.
Onionr - Private P2P Communication.
Lookup new peer transport addresses using the communicator Lookup new peer transport addresses using the communicator
""" """
from typing import TYPE_CHECKING
import logger import logger
from onionrutils import stringvalidators from onionrutils import stringvalidators
from communicator import peeraction, onlinepeers from communicator import peeraction, onlinepeers
from utils import gettransports from utils import gettransports
import onionrexceptions import onionrexceptions
if TYPE_CHECKING:
from deadsimplekv import DeadSimpleKV
""" """
This program is free software: you can redistribute it and/or modify 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 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 tryAmount = 1
newPeers = [] newPeers = []
transports = gettransports.get() transports = gettransports.get()
kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string("DeadSimpleKV")
for i in range(tryAmount): for i in range(tryAmount):
# Download new peer address list from random online peers # 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: for x in newPeers:
x = x.strip() x = x.strip()
if not stringvalidators.validate_transport(x) \ 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 # avoid adding if its our address
invalid.append(x) invalid.append(x)
for x in invalid: for x in invalid:
@ -58,6 +63,6 @@ def lookup_new_peer_transports_with_communicator(comm_inst):
newPeers.remove(x) newPeers.remove(x)
except ValueError: except ValueError:
pass pass
comm_inst.newPeers.extend(newPeers) kv.get('newPeers').extend(newPeers)
comm_inst.decrementThreadCount( comm_inst.decrementThreadCount(
'lookup_new_peer_transports_with_communicator') 'lookup_new_peer_transports_with_communicator')

View File

@ -76,7 +76,7 @@ def lookup_blocks_from_communicator(comm_inst):
# to only fetch blocks since then. # to only fetch blocks since then.
# Saved in memory only for privacy reasons # Saved in memory only for privacy reasons
try: try:
lastLookupTime = comm_inst.dbTimestamps[peer] lastLookupTime = kv.get('dbTimestamps')[peer]
except KeyError: except KeyError:
lastLookupTime = epoch.get_epoch() - \ lastLookupTime = epoch.get_epoch() - \
config.get("general.max_block_age", config.get("general.max_block_age",
@ -108,7 +108,7 @@ def lookup_blocks_from_communicator(comm_inst):
# add blocks to download queue # add blocks to download queue
kv.get('blockQueue')[i] = [peer] kv.get('blockQueue')[i] = [peer]
new_block_count += 1 new_block_count += 1
comm_inst.dbTimestamps[peer] = \ kv.get('dbTimestamps')[peer] = \
epoch.get_rounded_epoch(roundS=60) epoch.get_rounded_epoch(roundS=60)
else: else:
if peer not in kv.get('blockQueue')[i]: if peer not in kv.get('blockQueue')[i]:

View File

@ -50,7 +50,7 @@ def handle_announce(request):
if stringvalidators.validate_transport(newNode) and \ if stringvalidators.validate_transport(newNode) and \
newNode not in announce_queue_list: newNode not in announce_queue_list:
g.shared_state.get( g.shared_state.get(
OnionrCommunicatorDaemon).newPeers.append(newNode) deadsimplekv.DeadSimpleKV).get('newPeers').append(newNode)
announce_queue.put('new_peers', announce_queue.put('new_peers',
announce_queue_list.append(newNode)) announce_queue_list.append(newNode))
announce_queue.flush() announce_queue.flush()