Onionr/static-data/default-plugins/torgossip/clientfuncs/__init__.py

73 lines
2.3 KiB
Python
Raw Normal View History

2021-02-08 07:32:31 +00:00
from typing import TYPE_CHECKING
2021-02-09 23:02:19 +00:00
import sys
import os
2021-02-16 05:01:01 +00:00
from kasten import Kasten
from kasten.generator import pack
2021-02-20 23:45:24 +00:00
from blockio.load import list_blocks_by_type
2021-02-16 05:01:01 +00:00
2021-02-09 23:02:19 +00:00
import logger
2021-02-16 05:01:01 +00:00
from blockio import store_block, subprocvalidate, list_all_blocks
2021-02-09 23:02:19 +00:00
import onionrblocks
2021-02-16 05:01:01 +00:00
from onionrblocks.exceptions import BlockExpired
2021-02-09 23:02:19 +00:00
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
2021-02-20 23:45:24 +00:00
from .fanout import fanout_to_peers
from .commandsender import command_sender
2021-02-09 23:02:19 +00:00
from commands import GossipCommands
2021-02-08 07:32:31 +00:00
if TYPE_CHECKING:
from socket import socket
2021-02-20 00:41:46 +00:00
def download_blocks(
safe_db, sock: 'socket', offset: int, block_type: str) -> int:
2021-02-20 23:45:24 +00:00
command_sender(sock, GossipCommands.LIST_BLOCKS_BY_TYPE_OFFSET, str(offset).encode('utf-8'), b',', block_type.encode('utf-8'))
2021-02-09 23:02:19 +00:00
bl_hashs = sock.recv(600000)
2021-02-20 23:45:24 +00:00
try:
existing_blocks = list_blocks_by_type(block_type, safe_db)
except KeyError:
existing_blocks = []
2021-02-16 05:01:01 +00:00
existing_blocks_hashes = b''
2021-02-20 00:41:46 +00:00
downloaded_total = 0 # Including non-succesful
2021-02-16 05:01:01 +00:00
for i in existing_blocks:
for x in i:
existing_blocks_hashes += int(x).to_bytes(1, 'little')
print('existing', existing_blocks_hashes)
2021-02-09 23:02:19 +00:00
hash = None
2021-02-20 23:45:24 +00:00
hash_count = len(bl_hashs)//64
logger.info(
f"[TorGossip] {hash_count} found {block_type} blocks",
terminal=True)
for i in range(hash_count):
2021-02-20 00:41:46 +00:00
downloaded_total += 1
2021-02-15 09:11:16 +00:00
hash = bl_hashs[:(i*64) + 64]
2021-02-16 05:01:01 +00:00
if hash in existing_blocks_hashes:
continue
2021-02-09 23:02:19 +00:00
sock.sendall(
2021-02-15 09:11:16 +00:00
str(int(GossipCommands.GET_BLOCK)).encode('utf-8') + hash)
2021-02-09 23:02:19 +00:00
bl_content = sock.recv(10**6)
2021-02-20 23:45:24 +00:00
if bl_content == b'0' or not bl_content:
logger.warn("[TorGossip] Ignoring empty block", terminal=True)
2021-02-16 05:01:01 +00:00
continue
2021-02-15 09:11:16 +00:00
print('got block', bl_content)
2021-02-16 05:01:01 +00:00
try:
store_block(
Kasten(
hash,
bl_content,
generator=onionrblocks.generators.AnonVDFGenerator),
safe_db
)
existing_blocks_hashes += hash
print('stored block!')
except BlockExpired:
print('Block expired', hash)
existing_blocks_hashes += hash
except ValueError:
#print('not storing dupe block')
existing_blocks_hashes += hash
2021-02-20 00:41:46 +00:00
return downloaded_total