Work on gossip tests and cleaned up api server some
parent
e6b61c5f59
commit
1b37264eb7
@ -0,0 +1,44 @@
|
||||
"""Onionr - Private P2P Communication.
|
||||
|
||||
Serialized APIs
|
||||
"""
|
||||
|
||||
from asyncio.log import logger
|
||||
import secrets
|
||||
from flask import Blueprint, Response, request
|
||||
|
||||
from onionrblocks import Block
|
||||
|
||||
import logger
|
||||
from gossip import blockqueues
|
||||
from gossip.constants import BLOCK_ID_SIZE
|
||||
|
||||
"""
|
||||
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/>.
|
||||
"""
|
||||
blockapi = Blueprint('blockapi', __name__)
|
||||
|
||||
|
||||
stream_to_use = secrets.randbits(1)
|
||||
|
||||
# Add a block that we generated (or received from a transport like LAN/sneakernet)
|
||||
@blockapi.route('/addvdfblock', methods=['POST'])
|
||||
def block_serialized():
|
||||
req_data = request.data
|
||||
block_id = req_data[:BLOCK_ID_SIZE]
|
||||
block_data = req_data[BLOCK_ID_SIZE:]
|
||||
blockqueues.gossip_block_queues[stream_to_use].put(
|
||||
Block(block_id, block_data, auto_verify=False))
|
||||
logger.info("Added block" + block_id, terminal=True)
|
||||
return "ok"
|
@ -1,55 +0,0 @@
|
||||
"""Onionr - Private P2P Communication.
|
||||
|
||||
Event driven interface to trigger events in communicator
|
||||
"""
|
||||
from typing import Callable
|
||||
|
||||
from flask import Blueprint, request, Response, abort
|
||||
from werkzeug.exceptions import BadRequest
|
||||
from gevent import spawn
|
||||
|
||||
"""
|
||||
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/>.
|
||||
"""
|
||||
|
||||
|
||||
class DaemonEventsBP:
|
||||
def __init__(self):
|
||||
"""Create DaemonEvents instance, intended to be a singleton.
|
||||
|
||||
Attributes:
|
||||
listeners: callables that are called when a new event is added.
|
||||
The callables name should match the event name
|
||||
_too_many: TooManyObjects instance set by external code
|
||||
"""
|
||||
event_BP = Blueprint('event_BP', __name__)
|
||||
self.listeners = set([])
|
||||
self.flask_bp = event_BP
|
||||
event_BP = self.flask_bp
|
||||
|
||||
@event_BP.route('/daemon-event/<name>', methods=['POST'])
|
||||
def daemon_event_handler(name):
|
||||
handler: Callable
|
||||
|
||||
try:
|
||||
json_data = request.get_json(force=True)
|
||||
except BadRequest:
|
||||
json_data = {}
|
||||
for handler in self.listeners:
|
||||
if handler.__name__ == name:
|
||||
return Response(handler(**json_data))
|
||||
abort(404)
|
||||
|
||||
def register_listener(self, listener: Callable):
|
||||
self.listeners.add(listener)
|
@ -0,0 +1,67 @@
|
||||
import os, uuid
|
||||
from queue import Empty
|
||||
TEST_DIR = 'testdata/%s-%s' % (str(uuid.uuid4())[:5], os.path.basename(__file__)) + '/'
|
||||
print("Test directory:", TEST_DIR)
|
||||
os.environ["ONIONR_HOME"] = TEST_DIR
|
||||
|
||||
from time import sleep
|
||||
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 gossip.blockqueues import gossip_block_queues
|
||||
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 OnionrServerPutBlocksTest(unittest.TestCase):
|
||||
|
||||
|
||||
def test_put_blocks(self):
|
||||
|
||||
Thread(target=gossip_server, daemon=True).start()
|
||||
sleep(0.01)
|
||||
|
||||
blocks = []
|
||||
for _ in range(10):
|
||||
bl = onionrblocks.blockcreator.create_anonvdf_block(
|
||||
b"my test block" + os.urandom(16), b"txt", 2800)
|
||||
blockdb.add_block_to_db(bl)
|
||||
blocks.append(bl)
|
||||
|
||||
async def blocks_put_client():
|
||||
reader, writer = await asyncio.open_unix_connection(
|
||||
gossip_server_socket_file)
|
||||
writer.write(int(5).to_bytes(1, 'big'))
|
||||
await writer.drain()
|
||||
|
||||
for bl in blocks:
|
||||
writer.write(bl.id)
|
||||
writer.write(
|
||||
str(len(bl.raw)).zfill(BLOCK_MAX_SIZE_LEN).encode('utf-8'))
|
||||
writer.write(bl.raw)
|
||||
await writer.drain()
|
||||
|
||||
sleep(0.03)
|
||||
try:
|
||||
self.assertEqual(gossip_block_queues[0].get_nowait().raw, bl.raw)
|
||||
except Empty:
|
||||
self.assertEqual(gossip_block_queues[1].get_nowait().raw, bl.raw)
|
||||
|
||||
|
||||
asyncio.run(blocks_put_client())
|
||||
|
||||
unittest.main()
|
Loading…
Reference in New Issue