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

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