Ping loop while brainstorming.

This commit is contained in:
Kevin F 2022-02-22 14:34:19 -06:00
parent 8eec2167c8
commit d6b1c98cbd
5 changed files with 50 additions and 8 deletions

View File

@ -2,8 +2,10 @@
Dandelion ++ Gossip client logic Dandelion ++ Gossip client logic
""" """
import traceback
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from typing import Set from typing import Set
from time import sleep
from queue import Queue from queue import Queue
@ -11,7 +13,9 @@ if TYPE_CHECKING:
from onionrblocks import Block from onionrblocks import Block
from .peer import Peer from .peer import Peer
import logger
import onionrplugins import onionrplugins
from .commands import GossipCommands
""" """
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -28,9 +32,37 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
""" """
def gossip_client( def gossip_client(
peer_set: Set['Peer'], peer_set: Set['Peer'],
block_queue: Queue['Block'], block_queue: Queue['Block'],
dandelion_seed: bytes): dandelion_seed: bytes):
return """
Gossip client does the following:
Stem new blocks we created or downloaded *during stem phase*
Stream new blocks
"""
remove_peers = []
while True:
remove_peers.clear()
for peer in peer_set:
try:
sock = peer.get_socket()
except Exception:
logger.warn("Lost connection to " + peer.transport_address)
logger.warn(traceback.format_exc())
remove_peers.append(peer)
break
sock.sendall(int(GossipCommands.PING).to_bytes(1, 'big'))
if sock.recv(10) == b"PONG":
print("Got ping at peer")
while len(remove_peers):
try:
peer_set.remove(remove_peers.pop())
except KeyError:
pass
sleep(30)
return

View File

@ -1,9 +1,10 @@
from enum import Enum, auto from enum import IntEnum, auto
class GossipCommands(Enum): class GossipCommands(IntEnum):
PING = 1 PING = 1
ANNOUNCE = auto() ANNOUNCE = auto()
PEER_EXCHANGE = auto() PEER_EXCHANGE = auto()
STREAM_BLOCKS = auto() STREAM_BLOCKS = auto()
PUT_BLOCKS = auto() PUT_BLOCKS = auto()
CLOSE = auto()

View File

@ -33,10 +33,19 @@ def gossip_server(
async def peer_connected(reader, writer): async def peer_connected(reader, writer):
while True: while True:
cmd = asyncio.wait_for(await reader.read(1), 30) try:
match cmd: cmd = await asyncio.wait_for(reader.read(1), 60)
except asyncio.exceptions.CancelledError:
writer.close()
cmd = int.from_bytes(cmd, 'big')
if cmd == b'' or cmd == 0:
continue
match GossipCommands(cmd):
case GossipCommands.PING: case GossipCommands.PING:
writer.write(b'PONG') writer.write(b'PONG')
case GossipCommands.CLOSE:
writer.close()
await writer.drain() await writer.drain()

View File

@ -77,7 +77,7 @@ def on_bootstrap(api, data: Set[Peer] = None):
sleep(0.1) sleep(0.1)
socks_address, socks_port = get_socks()[0] socks_address, socks_port = get_socks()[0]
sleep(10) sleep(5)
for transport_address in bootstrap_nodes: for transport_address in bootstrap_nodes:
config.reload() config.reload()

View File

@ -4,7 +4,7 @@ import socks
class TorPeer: class TorPeer:
def __init__(self, socks_host, socks_port, onion_address): def __init__(self, socks_host, socks_port, onion_address):
self.onion_address = onion_address self.transport_address = self.onion_address = onion_address
self.socks_host = socks_host self.socks_host = socks_host
self.socks_port = socks_port self.socks_port = socks_port