2022-03-02 13:29:59 +00:00
|
|
|
from queue import Queue
|
2022-03-13 01:28:18 +00:00
|
|
|
from threading import Timer
|
2022-03-11 17:15:18 +00:00
|
|
|
from time import sleep
|
2022-03-13 01:28:18 +00:00
|
|
|
from secrets import choice
|
|
|
|
import traceback
|
2022-03-02 13:29:59 +00:00
|
|
|
|
2022-03-13 01:28:18 +00:00
|
|
|
from typing import TYPE_CHECKING, Tuple, List, Set
|
|
|
|
|
|
|
|
from onionrthreads import add_delayed_thread
|
|
|
|
from blockdb import add_block_to_db
|
|
|
|
import logger
|
|
|
|
|
|
|
|
from ..constants import BLACKHOLE_EVADE_TIMER_SECS, MAX_OUTBOUND_DANDELION_EDGE
|
2022-03-02 13:29:59 +00:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2022-03-11 17:15:18 +00:00
|
|
|
from ordered_set import OrderedSet
|
2022-03-02 13:29:59 +00:00
|
|
|
from onionrblocks import Block
|
|
|
|
from ..peer import Peer
|
2022-03-05 00:05:12 +00:00
|
|
|
from ..dandelion.phase import DandelionPhase
|
2022-03-02 13:29:59 +00:00
|
|
|
|
2022-03-13 01:28:18 +00:00
|
|
|
|
|
|
|
class NotEnoughEdges(ValueError): pass # noqa
|
|
|
|
|
|
|
|
|
|
|
|
def _setup_edge(
|
|
|
|
peer_set: OrderedSet['Peer'], exclude_set: OrderedSet['Peer']):
|
|
|
|
"""Negotiate stem connection with random peer, add to exclude set if fail"""
|
|
|
|
try:
|
|
|
|
peer: 'Peer' = choice(peer_set - exclude_set)
|
|
|
|
except IndexError:
|
|
|
|
raise NotEnoughEdges
|
|
|
|
try:
|
|
|
|
s = peer.get_socket()
|
|
|
|
except Exception:
|
|
|
|
logger.debug(traceback.format_exc())
|
|
|
|
exclude_set.add(peer)
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-03-02 13:29:59 +00:00
|
|
|
def stem_out(
|
2022-03-13 01:28:18 +00:00
|
|
|
block_queues: Tuple[Queue['Block'], Queue['Block']],
|
2022-03-11 17:15:18 +00:00
|
|
|
peer_set: OrderedSet['Peer'],
|
2022-03-05 00:05:12 +00:00
|
|
|
d_phase: 'DandelionPhase'):
|
2022-03-11 17:15:18 +00:00
|
|
|
|
2022-03-13 01:28:18 +00:00
|
|
|
# Spawn threads with deep copied block queue to add to db after time
|
|
|
|
# for black hole attack
|
|
|
|
for block_q in block_queues:
|
|
|
|
add_delayed_thread(
|
|
|
|
lambda q: set(map(add_block_to_db, q)),
|
|
|
|
BLACKHOLE_EVADE_TIMER_SECS, list(block_q.queue))
|
2022-03-11 17:15:18 +00:00
|
|
|
|
2022-03-13 01:28:18 +00:00
|
|
|
# don't bother if there are no possible outbound edges
|
2022-03-11 17:15:18 +00:00
|
|
|
if not len(peer_set):
|
|
|
|
sleep(1)
|
|
|
|
return
|
|
|
|
|
2022-03-13 01:28:18 +00:00
|
|
|
# Pick edges randomly
|
|
|
|
# Using orderedset for the tried edges to ensure random pairing with queue
|
|
|
|
tried_edges: OrderedSet['Peer'] = OrderedSet()
|
|
|
|
|
|
|
|
|
2022-03-02 13:29:59 +00:00
|
|
|
|