work on torgossip

This commit is contained in:
Kevin Froman 2021-02-11 21:03:41 +00:00
parent 257bef6ca0
commit 4bf1acf446
6 changed files with 78 additions and 45 deletions

View File

@ -24,7 +24,7 @@ except InvalidID:
"Invalid block ID for " +
b85encode(block_hash).decode('utf-8'))
except ValueError as e:
# Supposed to be if rounds are not specifid in the block
# Supposed to be if rounds are not specified in the block
stderr.write(e.message)
except BlockExpired:
stderr.write(

View File

@ -7,6 +7,7 @@ Create streams to random peers
import sys
from base64 import b32encode
from os import path
from time import sleep
from typing import TYPE_CHECKING
from random import SystemRandom
@ -14,6 +15,7 @@ import socks as socket
from netcontroller.torcontrol.onionserviceonline import service_online_recently
from netcontroller.torcontrol import torcontroller
import logger
if TYPE_CHECKING:
from .peerdb import TorGossipPeers
@ -21,6 +23,8 @@ if TYPE_CHECKING:
sys.path.insert(0, path.dirname(path.realpath(__file__)))
from commands import GossipCommands
from clientfuncs import download_blocks
"""
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
@ -35,24 +39,11 @@ 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 client_funcs(shared_state, socket_pool):
controller = torcontroller.get_controller()
def _add_bootstrap_peers(peer_db: 'TorGossipPeers'):
bootstap_peers = path.dirname(path.realpath(__file__)) + "/bootstrap.txt"
with open(bootstap_peers, 'r') as bs_peers:
peers = bs_peers.split(',')
for peer in peers:
try:
peer_db.get(peer)
except KeyError:
pass
else:
continue
if peer and service_online_recently(controller, peer):
peer_db.add_peer(peer)
def _client_pool(shared_state, socket_pool: dict):
peer_db: 'TorGossipPeers' = shared_state.get_by_string('TorGossipPeers')
socks_port = shared_state.get_by_string('NetController').socksPort
@ -77,15 +68,45 @@ def _client_pool(shared_state, socket_pool: dict):
def client_loop(shared_state, socket_pool):
sleep_t = 60
block_db = shared_state.get_by_string('SafeDB')
peer_db = shared_state.get_by_string('TorGossipPeers')
while True:
if not socket_pool:
_client_pool(shared_state, socket_pool)
sync_
peers = list(socket_pool)
SystemRandom().shuffle(peers)
try:
peer = peers[0]
except IndexError:
logger.error(
"There are no known TorGossip peers." +
f"Sleeping for {sleep_t}s",
terminal=True)
sleep(sleep_t)
continue
download_blocks(socket_pool[peer], 0, 'txt')
_client_pool(shared_state, socket_pool)
client_loop(shared_state, socket_pool)
def _add_bootstrap_peers(peer_db: 'TorGossipPeers'):
if len(peer_db.db.db_conn.keys()):
return
bootstap_peers = path.dirname(path.realpath(__file__)) + "/bootstrap.txt"
with open(bootstap_peers, 'r') as bs_peers:
peers = bs_peers.read().split(',')
for peer in peers:
try:
peer_db.db.get(peer)
except KeyError:
pass
else:
continue
if peer:
peer_db.add_peer(peer)
def start_client(shared_state):
@ -93,5 +114,4 @@ def start_client(shared_state):
_add_bootstrap_peers(shared_state.get_by_string('TorGossipPeers'))
# create and fill pool of sockets to peers (over tor socks)
socket_pool = {}
_client_pool(shared_state, socket_pool)
client_loop(shared_state, socket_pool)
client_funcs(shared_state, socket_pool)

View File

@ -25,5 +25,6 @@ def download_blocks(sock: 'socket', offset: int, block_type: str):
int(GossipCommands.GET_BLOCK).to_bytes(
1, 'little') + hash)
bl_content = sock.recv(10**6)
print(bl_content)

View File

@ -35,6 +35,7 @@ import logger # noqa
try:
from server import start_server
from client import start_client
from peerdb import TorGossipPeers
from runtest import torgossip_runtest
except Exception as _: # noqa
@ -48,7 +49,7 @@ def on_init(api, data=None):
shared_state.get(TorGossipPeers)
hs = ""
hs = b""
try:
with open(HOSTNAME_FILE, "rb") as f:
@ -68,7 +69,9 @@ def on_init(api, data=None):
with open(HOSTNAME_FILE, "wb") as hf:
hf.write(hs)
logger.info("TorGossip server on " + b32encode(hs).lower().decode('utf-8'), terminal=True)
Thread(target=start_server, daemon=True, args=[shared_state]).start()
Thread(target=start_client, daemon=True, args=[shared_state]).start()

View File

@ -60,9 +60,9 @@ class TorGossipPeers: # name it this way to avoid collisions in SharedState
if peer == b'enc':
peer = self.db.db_conn.nextkey(peer)
assert len(peer) == 34
if not peer:
return []
assert len(peer) == 34
top = [(peer, self.db.get(peer))]

View File

@ -10,11 +10,16 @@ import selectors
import socket
from time import sleep
from kasten.main import Kasten
from onionrblocks.generators.anonvdf import AnonVDFGenerator
from blockio import store
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
from commands import GossipCommands # noqa
import commandhandlers
from constants import SERVER_SOCKET
from blockio import subprocgenerate, store_block
"""
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
@ -33,6 +38,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
def start_server(shared_state):
bl = subprocgenerate.vdf_block(b"yep", "txt", 120)
store_block(Kasten(bl.id, bl.get_packed(), generator=AnonVDFGenerator), shared_state.get_by_string('SafeDB'))
print(bl.id)
sel = selectors.DefaultSelector()
def accept(sock, mask):