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('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()

View File

@ -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:

View File

@ -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

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:
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

View File

@ -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')

View File

@ -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]:

View File

@ -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()