From 346d30086ed3fc9918a3c96b0f4ba6832ab7705e Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 20 Feb 2021 23:45:24 +0000 Subject: [PATCH] work on torgossip --- .../default-plugins/torgossip/bootstrap.txt | 2 +- .../torgossip/clientfuncs/__init__.py | 25 +++++++---- .../torgossip/clientfuncs/commandsender.py | 14 ++++++ .../torgossip/clientfuncs/fanout.py | 44 +++++++++++++++++++ 4 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 static-data/default-plugins/torgossip/clientfuncs/commandsender.py create mode 100644 static-data/default-plugins/torgossip/clientfuncs/fanout.py diff --git a/static-data/default-plugins/torgossip/bootstrap.txt b/static-data/default-plugins/torgossip/bootstrap.txt index c94cd6e7..aaea970f 100644 --- a/static-data/default-plugins/torgossip/bootstrap.txt +++ b/static-data/default-plugins/torgossip/bootstrap.txt @@ -1 +1 @@ -elqy5cbebhcn3xrhkru4odglxiuy5q6dremjq366d53a3c6rbdco6bqd \ No newline at end of file +5pem4shocy4mteurcve5az4soctizh4v7wcncdxlo6v45ngmrdo4puyd \ No newline at end of file diff --git a/static-data/default-plugins/torgossip/clientfuncs/__init__.py b/static-data/default-plugins/torgossip/clientfuncs/__init__.py index 5c92ecd6..4e5aeeb9 100644 --- a/static-data/default-plugins/torgossip/clientfuncs/__init__.py +++ b/static-data/default-plugins/torgossip/clientfuncs/__init__.py @@ -4,12 +4,16 @@ import os from kasten import Kasten from kasten.generator import pack +from blockio.load import list_blocks_by_type import logger from blockio import store_block, subprocvalidate, list_all_blocks import onionrblocks from onionrblocks.exceptions import BlockExpired sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) + +from .fanout import fanout_to_peers +from .commandsender import command_sender from commands import GossipCommands if TYPE_CHECKING: @@ -18,12 +22,13 @@ if TYPE_CHECKING: def download_blocks( safe_db, sock: 'socket', offset: int, block_type: str) -> int: - sock.sendall( - str(int(GossipCommands.LIST_BLOCKS_BY_TYPE_OFFSET)).encode('utf-8') + - str(offset).encode('utf-8') + b',' + - block_type.encode('utf-8')) + command_sender(sock, GossipCommands.LIST_BLOCKS_BY_TYPE_OFFSET, str(offset).encode('utf-8'), b',', block_type.encode('utf-8')) + bl_hashs = sock.recv(600000) - existing_blocks = list_all_blocks(safe_db) + try: + existing_blocks = list_blocks_by_type(block_type, safe_db) + except KeyError: + existing_blocks = [] existing_blocks_hashes = b'' downloaded_total = 0 # Including non-succesful for i in existing_blocks: @@ -31,7 +36,11 @@ def download_blocks( existing_blocks_hashes += int(x).to_bytes(1, 'little') print('existing', existing_blocks_hashes) hash = None - for i in range(len(bl_hashs)//64): + hash_count = len(bl_hashs)//64 + logger.info( + f"[TorGossip] {hash_count} found {block_type} blocks", + terminal=True) + for i in range(hash_count): downloaded_total += 1 hash = bl_hashs[:(i*64) + 64] if hash in existing_blocks_hashes: @@ -39,8 +48,8 @@ def download_blocks( sock.sendall( str(int(GossipCommands.GET_BLOCK)).encode('utf-8') + hash) bl_content = sock.recv(10**6) - if bl_content == b'0': - existing_blocks.append(hash) + if bl_content == b'0' or not bl_content: + logger.warn("[TorGossip] Ignoring empty block", terminal=True) continue print('got block', bl_content) try: diff --git a/static-data/default-plugins/torgossip/clientfuncs/commandsender.py b/static-data/default-plugins/torgossip/clientfuncs/commandsender.py new file mode 100644 index 00000000..21f853d5 --- /dev/null +++ b/static-data/default-plugins/torgossip/clientfuncs/commandsender.py @@ -0,0 +1,14 @@ +import sys +import os +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from socket import socket +sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) +from commands import GossipCommands + + +def command_sender(sock: 'socket', cmd: GossipCommands, *command_data: bytes): + sock.sendall( + str(int(GossipCommands.LIST_BLOCKS_BY_TYPE_OFFSET)).encode( + 'utf-8') + b''.join(command_data)) diff --git a/static-data/default-plugins/torgossip/clientfuncs/fanout.py b/static-data/default-plugins/torgossip/clientfuncs/fanout.py new file mode 100644 index 00000000..87b102c8 --- /dev/null +++ b/static-data/default-plugins/torgossip/clientfuncs/fanout.py @@ -0,0 +1,44 @@ +"""Onionr - Private P2P Communication. + +Fanout blocks to Onionr TorGossip peer +""" +import os +import sys +from typing import TYPE_CHECKING +from random import SystemRandom + +if TYPE_CHECKING: + from socket import socket + +sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) + +from commands import GossipCommands +""" +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 +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +""" + + +def fanout_to_peers( + socket_pool: dict, + block_hash: bytes, + block_data: 'KastenPacked', + fanout_count: int = 4): + peers_to_use = [] + peers = list(socket_pool) + SystemRandom().shuffle(peers) + + for i in range(fanout_count): + peer: 'socket' = peers.pop() + peer.sendall(G) +