2022-07-19 05:32:54 +00:00
|
|
|
from asyncio import sleep
|
2022-07-28 21:10:36 +00:00
|
|
|
from threading import Thread
|
2022-07-19 05:32:54 +00:00
|
|
|
from queue import Empty
|
2022-07-28 21:10:36 +00:00
|
|
|
|
2022-03-20 17:53:40 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2022-09-27 17:21:00 +00:00
|
|
|
from logger import log as logging
|
2022-06-26 05:34:49 +00:00
|
|
|
from ...constants import BLOCK_ID_SIZE, BLOCK_MAX_SIZE, BLOCK_SIZE_LEN
|
2022-03-20 23:05:44 +00:00
|
|
|
|
2022-03-20 17:53:40 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from queue import Queue
|
|
|
|
import socket
|
|
|
|
from ...dandelion import DandelionPhase
|
|
|
|
|
|
|
|
from onionrblocks import Block
|
|
|
|
|
|
|
|
|
|
|
|
async def do_stem_stream(
|
|
|
|
peer_socket: 'socket.socket',
|
|
|
|
block_queue: "Queue[Block]",
|
|
|
|
d_phase: 'DandelionPhase'):
|
|
|
|
remaining_time = d_phase.remaining_time()
|
|
|
|
my_phase_id = d_phase.phase_id
|
|
|
|
|
2022-09-27 17:21:00 +00:00
|
|
|
|
2022-07-28 21:10:36 +00:00
|
|
|
while remaining_time > 1 and my_phase_id == d_phase.phase_id:
|
|
|
|
# Primary client component that communicate's with gossip.server.acceptstem
|
|
|
|
remaining_time = d_phase.remaining_time()
|
|
|
|
while remaining_time:
|
|
|
|
try:
|
|
|
|
# queues can't block because we're in async
|
|
|
|
bl = block_queue.get(block=False)
|
|
|
|
except Empty:
|
|
|
|
remaining_time = d_phase.remaining_time()
|
|
|
|
await sleep(1)
|
|
|
|
else:
|
|
|
|
break
|
2022-09-27 17:21:00 +00:00
|
|
|
logging.info("Sending block over dandelion++")
|
2022-07-28 21:10:36 +00:00
|
|
|
|
|
|
|
block_size = str(len(bl.raw)).zfill(BLOCK_SIZE_LEN)
|
|
|
|
def _send_it():
|
2022-07-30 20:41:11 +00:00
|
|
|
try:
|
|
|
|
with peer_socket:
|
|
|
|
try:
|
|
|
|
peer_socket.sendall(bl.id.zfill(BLOCK_ID_SIZE).encode('utf-8'))
|
|
|
|
except AttributeError:
|
|
|
|
peer_socket.sendall(bl.id.zfill(BLOCK_ID_SIZE))
|
|
|
|
peer_socket.sendall(block_size.encode('utf-8'))
|
|
|
|
peer_socket.sendall(bl.raw)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2022-07-28 21:10:36 +00:00
|
|
|
Thread(target=_send_it, daemon=True, name="stemout block").start()
|