2022-02-27 19:36:02 +00:00
|
|
|
import threading
|
2022-02-21 21:15:26 +00:00
|
|
|
from time import sleep
|
2022-03-11 17:15:18 +00:00
|
|
|
from typing import TYPE_CHECKING, Set, Tuple
|
2022-02-14 23:47:54 +00:00
|
|
|
from os import urandom
|
|
|
|
|
2022-02-10 01:29:16 +00:00
|
|
|
if TYPE_CHECKING:
|
2022-03-11 17:15:18 +00:00
|
|
|
from ordered_set import OrderedSet
|
2022-02-10 01:29:16 +00:00
|
|
|
from onionrblocks import Block
|
2022-03-20 17:54:57 +00:00
|
|
|
from queue import Queue
|
2022-02-10 01:29:16 +00:00
|
|
|
|
2022-02-14 23:47:54 +00:00
|
|
|
from .peer import Peer
|
|
|
|
|
2022-02-10 01:29:16 +00:00
|
|
|
from onionrthreads import add_onionr_thread
|
2022-02-11 06:56:19 +00:00
|
|
|
import onionrplugins
|
2022-02-27 19:36:02 +00:00
|
|
|
import logger
|
2022-02-11 06:56:19 +00:00
|
|
|
|
2022-02-26 07:07:18 +00:00
|
|
|
from .connectpeer import connect_peer
|
2022-02-11 06:56:19 +00:00
|
|
|
from .client import gossip_client
|
|
|
|
from .server import gossip_server
|
2022-02-17 05:58:04 +00:00
|
|
|
from .commands import GossipCommands
|
2022-03-02 13:29:59 +00:00
|
|
|
from .constants import BOOTSTRAP_ATTEMPTS
|
2022-02-10 01:29:16 +00:00
|
|
|
"""
|
|
|
|
Onionr uses a flavor of Dandelion++ epidemic routing
|
|
|
|
|
|
|
|
The starter creates a peer set and passes it to the client and server
|
|
|
|
as well as each of the plugins.
|
|
|
|
|
|
|
|
The transports forward incoming requests to the gossip server
|
|
|
|
|
2022-02-14 23:47:54 +00:00
|
|
|
When a new peer announcement is recieved an event is fired and the transport
|
|
|
|
plugin that handles it will (or wont) create a new peer object by connecting
|
|
|
|
to that peer
|
2022-02-10 01:29:16 +00:00
|
|
|
|
2022-02-14 23:47:54 +00:00
|
|
|
When a new block is generated, it is added to a queue in raw form passed to
|
|
|
|
the starter
|
2022-02-10 01:29:16 +00:00
|
|
|
|
2022-02-14 23:47:54 +00:00
|
|
|
In stem phase, client uploads recieved (stem) blocks to 2 random peers.
|
|
|
|
In stem phase, server disables diffusion
|
2022-02-10 01:29:16 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2022-02-14 23:47:54 +00:00
|
|
|
|
|
|
|
def start_gossip_threads(
|
2022-03-20 17:54:57 +00:00
|
|
|
peer_set: "OrderedSet[Peer]",
|
|
|
|
block_queues: Tuple["Queue[Block]"]):
|
2022-02-10 01:29:16 +00:00
|
|
|
# Peer set is largely handled by the transport plugins
|
|
|
|
# There is a unified set so gossip logic is not repeated
|
2022-02-14 23:47:54 +00:00
|
|
|
seed = urandom(32)
|
2022-02-11 06:56:19 +00:00
|
|
|
|
2022-02-14 23:47:54 +00:00
|
|
|
add_onionr_thread(
|
2022-03-02 13:29:59 +00:00
|
|
|
gossip_server, 1, peer_set, block_queues, seed, initial_sleep=0.2)
|
2022-02-27 19:36:02 +00:00
|
|
|
|
|
|
|
threading.Thread(
|
|
|
|
target=gossip_client,
|
2022-03-02 13:29:59 +00:00
|
|
|
args=[peer_set, block_queues, seed], daemon=True).start()
|
2022-02-11 06:56:19 +00:00
|
|
|
onionrplugins.events.event('gossip_start', data=peer_set, threaded=True)
|
2022-03-02 13:29:59 +00:00
|
|
|
for _ in range(BOOTSTRAP_ATTEMPTS):
|
2022-02-27 19:36:02 +00:00
|
|
|
onionrplugins.events.event(
|
|
|
|
'bootstrap', data={'peer_set': peer_set, 'callback': connect_peer},
|
|
|
|
threaded=False)
|
|
|
|
sleep(60)
|
|
|
|
if len(peer_set):
|
|
|
|
return
|
|
|
|
logger.error("Could not connect to any peers :(", terminal=True)
|