From a61cd273a8ddbccd371485d8481ba1f0f643b0b7 Mon Sep 17 00:00:00 2001 From: Kevin F Date: Tue, 5 Apr 2022 01:17:40 -0500 Subject: [PATCH] Implemented basic server diffusal test --- scripts/stats-server.py | 73 ------------------------------ src/gossip/server/__init__.py | 2 +- src/gossip/server/diffuseblocks.py | 2 +- tests/test_blockdb.py | 2 +- tests/test_server_diffuse.py | 51 +++++++++++++++++++++ 5 files changed, 54 insertions(+), 76 deletions(-) delete mode 100755 scripts/stats-server.py create mode 100644 tests/test_server_diffuse.py diff --git a/scripts/stats-server.py b/scripts/stats-server.py deleted file mode 100755 index 76d04953..00000000 --- a/scripts/stats-server.py +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env python3 -"""Onionr - Private P2P Communication. - -LAN transport server thread -""" -import sys -import os -os.chdir('../') -sys.path.append("src/") -from gevent.pywsgi import WSGIServer -from flask import Flask -from flask import Response -from flask import request -import stem -from stem.control import Controller -from netcontroller import getopenport -import json -""" - 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 . -""" - -#passw = secrets.token_hex(32) -port_num = int(input('tor control port')) -web_port = getopenport.get_open_port() - - -app = Flask(__name__) - -STATS_FILE = 'stats.json' - -@app.route('/sendstats/', methods = ['POST']) -def get_stats(node): - try: - with open(STATS_FILE, 'r') as f: - try: - data = json.loads(f.read()) - except json.decoder.JSONDecodeError: - raise FileNotFoundError - except FileNotFoundError: - data = {} - data[node] = request.get_data().decode('utf-8') - - with open(STATS_FILE, 'w') as f: - data = json.dumps(data) - f.write(data) - - return Response('ok') - -with Controller.from_port(port = port_num) as controller: - controller.authenticate(input('pass for tor')) # provide the password here if you set one - hs = controller.create_ephemeral_hidden_service( - {80: web_port}, - key_type = 'NEW', - key_content = 'ED25519-V3', - await_publication=True, - detached=True) - hs = hs.service_id - print(f'stats server {hs}') - - server = WSGIServer(('127.0.0.1', web_port), app, log=None) - server.serve_forever() - diff --git a/src/gossip/server/__init__.py b/src/gossip/server/__init__.py index 2ff0efe9..401d5eb2 100644 --- a/src/gossip/server/__init__.py +++ b/src/gossip/server/__init__.py @@ -69,7 +69,7 @@ def gossip_server(): onionrevents.event( 'announce_rec', data={'address': address, - 'callback': connect_peer}, + 'callback': connect_peer}, threaded=True) writer.write(int(1).to_bytes(1, 'big')) await asyncio.wait_for(_read_announce(), 10) diff --git a/src/gossip/server/diffuseblocks.py b/src/gossip/server/diffuseblocks.py index 25d88f1f..d0044b5a 100644 --- a/src/gossip/server/diffuseblocks.py +++ b/src/gossip/server/diffuseblocks.py @@ -49,7 +49,7 @@ async def diffuse_blocks(reader: 'StreamReader', writer: 'StreamWriter'): raise ValueError("Invalid time offset") time_offset = int(time_offset) - if time_offset - time() < -5: + if time_offset - time() > -5: raise ValueError( "Peer's specified time offset skewed too far into the future") diff --git a/tests/test_blockdb.py b/tests/test_blockdb.py index 7e144bbf..d2cb9dd1 100644 --- a/tests/test_blockdb.py +++ b/tests/test_blockdb.py @@ -29,7 +29,7 @@ class TestBlockDB(unittest.TestCase): def test_store_vdf_block(self): _delete_db() bl: Block = onionrblocks.create_anonvdf_block(os.urandom(10), b'bin', 2500) - blockdb.store_vdf_block(bl) + blockdb.add_block_to_db(bl) with dbm.open(blockdb.block_db_path, 'r') as b_db: b_db[bl.id] diff --git a/tests/test_server_diffuse.py b/tests/test_server_diffuse.py new file mode 100644 index 00000000..b3709d74 --- /dev/null +++ b/tests/test_server_diffuse.py @@ -0,0 +1,51 @@ +import os, uuid +TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/' +print("Test directory:", TEST_DIR) +os.environ["ONIONR_HOME"] = TEST_DIR + +from threading import Thread +import asyncio +import unittest +import sys +sys.path.append(".") +sys.path.append("src/") + +from ordered_set import OrderedSet +import onionrblocks + +import blockdb +from gossip.server import gossip_server +from filepaths import gossip_server_socket_file + + +BLOCK_MAX_SIZE = 1024 * 2000 +BLOCK_MAX_SIZE_LEN = len(str(BLOCK_MAX_SIZE)) +BLOCK_ID_SIZE = 128 +BLOCK_STREAM_OFFSET_DIGITS = 8 + + +class OnionrServerDiffuseTest(unittest.TestCase): + + def test_one_block(self): + + Thread(target=gossip_server, daemon=True).start() + + bl = onionrblocks.blockcreator.create_anonvdf_block( + b"my test block", b"txt", 2800) + blockdb.add_block_to_db(bl) + + async def diffuse_client(): + reader, writer = await asyncio.open_unix_connection( + gossip_server_socket_file) + writer.write(int(4).to_bytes(1, 'big')) + await writer.drain() + + writer.write('0'.zfill(BLOCK_STREAM_OFFSET_DIGITS).encode('utf-8')) + await writer.drain() + + self.assertEqual(bl.id, await reader.readexactly(BLOCK_ID_SIZE)) + + + asyncio.run(diffuse_client()) + +unittest.main()