2022-07-26 17:45:48 +00:00
|
|
|
from secrets import SystemRandom
|
|
|
|
from time import time
|
|
|
|
from typing import List, TYPE_CHECKING
|
|
|
|
|
2022-07-30 20:41:11 +00:00
|
|
|
#if TYPE_CHECKING:
|
|
|
|
from onionrblocks import Block
|
2022-07-26 17:45:48 +00:00
|
|
|
|
|
|
|
from gossip.commands import GossipCommands, command_to_byte
|
|
|
|
from blockdb import get_blocks_after_timestamp
|
|
|
|
from ...constants import BLOCK_ID_SIZE, BLOCK_SIZE_LEN
|
|
|
|
|
|
|
|
from ...peerset import gossip_peer_set
|
|
|
|
from ...server import lastincoming
|
|
|
|
|
|
|
|
SECS_ELAPSED_NO_INCOMING_BEFORE_STREAM = 3
|
|
|
|
|
|
|
|
class SendTimestamp:
|
|
|
|
timestamp: int = 0
|
|
|
|
|
|
|
|
def stream_to_peer():
|
|
|
|
if SECS_ELAPSED_NO_INCOMING_BEFORE_STREAM > time() - lastincoming.last_incoming_timestamp:
|
2022-07-30 20:41:11 +00:00
|
|
|
SendTimestamp.timestamp = int(time()) - 60
|
2022-07-26 17:45:48 +00:00
|
|
|
return
|
|
|
|
if not len(gossip_peer_set):
|
|
|
|
return
|
|
|
|
rand = SystemRandom()
|
|
|
|
peer = rand.choice(gossip_peer_set)
|
|
|
|
buffer: List['Block'] = []
|
|
|
|
|
|
|
|
def _do_upload():
|
2022-07-30 20:41:11 +00:00
|
|
|
print('uploading to', peer.transport_address)
|
2022-07-26 17:45:48 +00:00
|
|
|
with peer.get_socket(30) as p:
|
|
|
|
p.sendall(command_to_byte(GossipCommands.PUT_BLOCK_DIFFUSE))
|
|
|
|
|
|
|
|
while len(buffer):
|
|
|
|
try:
|
|
|
|
block = buffer.pop()
|
|
|
|
except IndexError:
|
|
|
|
break
|
|
|
|
p.sendall(block.id.zfill(BLOCK_ID_SIZE))
|
|
|
|
if int.from_bytes(p.recv(1), 'big') == 0:
|
|
|
|
continue
|
|
|
|
block_size = str(len(block.raw)).zfill(BLOCK_SIZE_LEN)
|
|
|
|
p.sendall(block_size.encode('utf-8'))
|
|
|
|
p.sendall(block.raw)
|
|
|
|
|
|
|
|
# Buffer some blocks so we're not streaming too many to one peer
|
|
|
|
# and to efficiently avoid connecting without sending anything
|
|
|
|
buffer_max = 10
|
|
|
|
for block in get_blocks_after_timestamp(SendTimestamp.timestamp):
|
2022-07-30 20:41:11 +00:00
|
|
|
assert isinstance(block, Block)
|
2022-07-26 17:45:48 +00:00
|
|
|
buffer.append(block)
|
|
|
|
if len(buffer) > buffer_max:
|
|
|
|
_do_upload(buffer)
|
|
|
|
if len(buffer):
|
|
|
|
_do_upload()
|
|
|
|
|
2022-07-30 20:41:11 +00:00
|
|
|
SendTimestamp.timestamp = int(time()) - 60
|