work on torgossip

This commit is contained in:
Kevin Froman 2021-02-20 23:45:24 +00:00
parent 60dfa8fb7e
commit 346d30086e
4 changed files with 76 additions and 9 deletions

View File

@ -1 +1 @@
elqy5cbebhcn3xrhkru4odglxiuy5q6dremjq366d53a3c6rbdco6bqd 5pem4shocy4mteurcve5az4soctizh4v7wcncdxlo6v45ngmrdo4puyd

View File

@ -4,12 +4,16 @@ import os
from kasten import Kasten from kasten import Kasten
from kasten.generator import pack from kasten.generator import pack
from blockio.load import list_blocks_by_type
import logger import logger
from blockio import store_block, subprocvalidate, list_all_blocks from blockio import store_block, subprocvalidate, list_all_blocks
import onionrblocks import onionrblocks
from onionrblocks.exceptions import BlockExpired from onionrblocks.exceptions import BlockExpired
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) 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 from commands import GossipCommands
if TYPE_CHECKING: if TYPE_CHECKING:
@ -18,12 +22,13 @@ if TYPE_CHECKING:
def download_blocks( def download_blocks(
safe_db, sock: 'socket', offset: int, block_type: str) -> int: safe_db, sock: 'socket', offset: int, block_type: str) -> int:
sock.sendall( command_sender(sock, GossipCommands.LIST_BLOCKS_BY_TYPE_OFFSET, str(offset).encode('utf-8'), b',', block_type.encode('utf-8'))
str(int(GossipCommands.LIST_BLOCKS_BY_TYPE_OFFSET)).encode('utf-8') +
str(offset).encode('utf-8') + b',' +
block_type.encode('utf-8'))
bl_hashs = sock.recv(600000) 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'' existing_blocks_hashes = b''
downloaded_total = 0 # Including non-succesful downloaded_total = 0 # Including non-succesful
for i in existing_blocks: for i in existing_blocks:
@ -31,7 +36,11 @@ def download_blocks(
existing_blocks_hashes += int(x).to_bytes(1, 'little') existing_blocks_hashes += int(x).to_bytes(1, 'little')
print('existing', existing_blocks_hashes) print('existing', existing_blocks_hashes)
hash = None 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 downloaded_total += 1
hash = bl_hashs[:(i*64) + 64] hash = bl_hashs[:(i*64) + 64]
if hash in existing_blocks_hashes: if hash in existing_blocks_hashes:
@ -39,8 +48,8 @@ def download_blocks(
sock.sendall( sock.sendall(
str(int(GossipCommands.GET_BLOCK)).encode('utf-8') + hash) str(int(GossipCommands.GET_BLOCK)).encode('utf-8') + hash)
bl_content = sock.recv(10**6) bl_content = sock.recv(10**6)
if bl_content == b'0': if bl_content == b'0' or not bl_content:
existing_blocks.append(hash) logger.warn("[TorGossip] Ignoring empty block", terminal=True)
continue continue
print('got block', bl_content) print('got block', bl_content)
try: try:

View File

@ -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))

View File

@ -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 <https://www.gnu.org/licenses/>.
"""
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)