Moved onlinePeers to KV to further reduce coupling

This commit is contained in:
Kevin 2020-07-25 22:28:32 -05:00
parent 0e4e7bb050
commit 6a6718c9fd
15 changed files with 56 additions and 25 deletions

View file

@ -30,6 +30,7 @@ import onionrexceptions
def announce_node(daemon):
"""Announce our node to our peers."""
ret_data = False
kv: "DeadSimpleKV" = daemon.shared_state.get_by_string("DeadSimpleKV")
# Do not let announceCache get too large
if len(daemon.announceCache) >= 10000:
@ -37,7 +38,7 @@ def announce_node(daemon):
if daemon.config.get('general.security_level', 0) == 0:
# Announce to random online peers
for i in daemon.onlinePeers:
for i in kv.get('onlinePeers'):
if i not in daemon.announceCache and\
i not in daemon.announceProgress:
peer = i

View file

@ -75,7 +75,7 @@ def connect_new_peer_to_communicator(comm_inst, peer='', useBootstrap=False):
if its already been tried/connected, or if its cooled down
"""
if len(address) == 0 or address in tried \
or address in comm_inst.onlinePeers \
or address in kv.get('onlinePeers') \
or address in comm_inst.cooldownPeer:
continue
if kv.get('shutdown'):
@ -87,9 +87,9 @@ def connect_new_peer_to_communicator(comm_inst, peer='', useBootstrap=False):
if address not in mainPeerList:
# Add a peer to our list if it isn't already since it connected
networkmerger.mergeAdders(address)
if address not in comm_inst.onlinePeers:
if address not in kv.get('onlinePeers'):
logger.info('Connected to ' + address, terminal=True)
comm_inst.onlinePeers.append(address)
kv.get('onlinePeers').append(address)
comm_inst.connectTimes[address] = epoch.get_epoch()
retData = address

View file

@ -2,8 +2,13 @@
Select random online peer in a communicator instance and have them "cool down"
"""
from typing import TYPE_CHECKING
from onionrutils import epoch
from communicator import onlinepeers
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
@ -22,8 +27,9 @@ from communicator import onlinepeers
def cooldown_peer(comm_inst):
"""Randomly add an online peer to cooldown, so we can connect a new one."""
kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string("DeadSimpleKV")
config = comm_inst.config
online_peer_amount = len(comm_inst.onlinePeers)
online_peer_amount = len(kv.get('onlinePeers'))
minTime = 300
cooldown_time = 600
to_cool = ''

View file

@ -66,7 +66,7 @@ def lookup_blocks_from_communicator(comm_inst):
continue
# if we've already tried all the online peers this time around, stop
if peer in triedPeers:
if len(comm_inst.onlinePeers) == len(triedPeers):
if len(kv.get('onlinePeers')) == len(triedPeers):
break
else:
continue

View file

@ -9,6 +9,11 @@ import logger
from utils import netutils
from onionrutils import localcommand, epoch
from . import restarttor
from typing import TYPE_CHECKING
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
@ -33,7 +38,7 @@ def net_check(comm_inst):
# for detecting if we have received incoming connections recently
rec = False
kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string("DeadSimpleKV")
if len(comm_inst.onlinePeers) == 0:
if len(kv.get('onlinePeers')) == 0:
try:
if (epoch.get_epoch() - int(localcommand.local_command
('/lastconnect'))) <= 60:

View file

@ -66,7 +66,7 @@ class OnionrCommunicatorTimers:
if self.count == self.frequency and not self.kv.get('shutdown'):
try:
if self.requires_peer and \
len(self.daemon_inst.onlinePeers) == 0:
len(self.kv.get('onlinePeers')) == 0:
raise onionrexceptions.OnlinePeerNeeded
except onionrexceptions.OnlinePeerNeeded:
return

View file

@ -17,6 +17,7 @@ from onionrutils import stringvalidators, basicrequests
import onionrcrypto
from communicator import onlinepeers
if TYPE_CHECKING:
from deadsimplekv import DeadSimpleKV
from communicator import OnionrCommunicatorDaemon
"""
This program is free software: you can redistribute it and/or modify
@ -38,6 +39,7 @@ def upload_blocks_from_communicator(comm_inst: 'OnionrCommunicatorDaemon'):
"""Accept a communicator instance + upload blocks from its upload queue."""
"""when inserting a block, we try to upload
it to a few peers to add some deniability & increase functionality"""
kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string("DeadSimpleKV")
TIMER_NAME = "upload_blocks_from_communicator"
session_manager: sessionmanager.BlockUploadSessionManager
@ -63,7 +65,7 @@ def upload_blocks_from_communicator(comm_inst: 'OnionrCommunicatorDaemon'):
comm_inst.decrementThreadCount(TIMER_NAME)
return
session = session_manager.add_session(bl)
for _ in range(min(len(comm_inst.onlinePeers), 6)):
for _ in range(min(len(kv.get('onlinePeers')), 6)):
try:
peer = onlinepeers.pick_online_peer(comm_inst)
except onionrexceptions.OnlinePeerNeeded:

View file

@ -4,6 +4,7 @@ Manager for upload 'sessions'
"""
from typing import List, Union, TYPE_CHECKING
if TYPE_CHECKING:
from deadsimplekv import DeadSimpleKV
from session import UploadSession
from onionrutils import bytesconverter
@ -84,10 +85,12 @@ class BlockUploadSessionManager:
comm_inst: 'OnionrCommunicatorDaemon' # type: ignore
comm_inst = self._too_many.get_by_string( # pylint: disable=E1101 type: ignore
"OnionrCommunicatorDaemon")
kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string(
"DeadSimpleKV")
sessions_to_delete = []
if comm_inst.getUptime() < 120:
return
onlinePeerCount = len(comm_inst.onlinePeers)
onlinePeerCount = len(kv.get('onlinePeers'))
# If we have no online peers right now,
if onlinePeerCount == 0: