def _stop(): p.start() time.sleep(0.1) p.terminate() OverlayInterface.stop_service() OverlayInterface.loop.call_soon_threadsafe( OverlayInterface.loop.stop)
def _stop(): self.assertTrue(hasattr(OverlayInterface, 'dht')) OverlayInterface.stop_service() self.assertTrue(OverlayInterface.event_sock.closed) self.assertIsInstance(OverlayInterface.cmd_sock, zmq.Socket) self.assertIsInstance(OverlayInterface.event_sock, zmq.Socket) OverlayInterface.loop.call_soon_threadsafe( OverlayInterface.loop.stop)
def test_start_stop_service(self): def _stop(): self.assertTrue(hasattr(OverlayInterface, 'dht')) OverlayInterface.stop_service() self.assertTrue(OverlayInterface.event_sock.closed) self.assertIsInstance(OverlayInterface.cmd_sock, zmq.Socket) self.assertIsInstance(OverlayInterface.event_sock, zmq.Socket) OverlayInterface.loop.call_soon_threadsafe( OverlayInterface.loop.stop) t = Timer(5, _stop) t.start() OverlayInterface.start_service( sk= '06391888e37a48cef1ded85a375490df4f9b2c74f7723e88c954a055f3d2685a')
def run_delegate(slot_num): from cilantro.logger import get_logger from cilantro.protocol.overlay.interface import OverlayInterface import os log = get_logger("DELEGATE FACTORY") def event_handler(e): log.critical('{}: {}'.format(os.getenv('HOST_IP'), e)) d_info = TESTNET_DELEGATES[slot_num] d_info['ip'] = os.getenv('HOST_IP') OverlayInterface.start_service(sk=d_info['sk']) OverlayInterface.listen_for_events(event_handler)
def run_witness(slot_num): from cilantro.logger import get_logger from cilantro.protocol.overlay.interface import OverlayInterface import os log = get_logger(__name__) def event_handler(e): log.critical('{}: {}'.format(os.getenv('HOST_IP'), e)) w_info = TESTNET_WITNESSES[slot_num] w_info['ip'] = os.getenv('HOST_IP') OverlayInterface.start_service(sk=w_info['sk']) OverlayInterface.listen_for_events(event_handler)
def run_mn(): from cilantro.logger import get_logger from cilantro.protocol.overlay.interface import OverlayInterface import os, time log = get_logger(__name__) def event_handler(e): log.critical('{}: {}'.format(os.getenv('HOST_IP'), e)) m_info = TESTNET_MASTERNODES[0] m_info['ip'] = os.getenv('HOST_IP') OverlayInterface.start_service(sk=m_info['sk']) OverlayInterface.listen_for_events(event_handler)
def _build_url(self, protocol, port, ip='', vk=''): assert protocol in ( 'ipc', 'tcp' ), "Got protocol {}, but only tcp and ipc are supported".format( protocol) if vk and not ip: node = OverlayInterface.get_node_from_vk(vk) self.log.critical(node) return "{}://{}:{}".format(protocol, ip, port)
def test_send_commands(self): def _stop(): p.start() time.sleep(0.1) p.terminate() OverlayInterface.stop_service() OverlayInterface.loop.call_soon_threadsafe( OverlayInterface.loop.stop) def _send_msg(): def _e_handler(e): OverlayInterface._test_res = e def _thread(): asyncio.set_event_loop(l) OverlayInterface.get_node_from_vk( '82540bb5a9c84162214c5540d6e43be49bbfe19cf49685660cab608998a65144' ) def _assert(): self.assertEqual(OverlayInterface._test_res['ip'], '127.0.0.1') l.call_soon_threadsafe(l.stop) th.join() th_a.join() l = asyncio.new_event_loop() asyncio.set_event_loop(l) asyncio.ensure_future(OverlayInterface.event_listener(_e_handler)) th = Timer(0.01, _thread) th.start() th_a = Timer(0.05, _assert) th_a.start() l.run_forever() p = Process(target=_send_msg) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) t = Timer(5, _stop) t.start() OverlayInterface.start_service( sk= '06391888e37a48cef1ded85a375490df4f9b2c74f7723e88c954a055f3d2685a') self.assertEqual(p.exitcode, 0)
def test_send_commands_fail(self): def _stop(): p.start() time.sleep(0.1) p.terminate() OverlayInterface.stop_service() OverlayInterface.loop.call_soon_threadsafe( OverlayInterface.loop.stop) def _send_msg(): def _e_handler(e): OverlayInterface._test_res = e def _thread(): asyncio.set_event_loop(l) OverlayInterface.get_node_from_vk('fsdfhsdfkjsdh') def _assert(): self.assertTrue(OverlayInterface._test_res['event'], 'not_found') l.call_soon_threadsafe(l.stop) th.join() th_a.join() l = asyncio.new_event_loop() asyncio.set_event_loop(l) asyncio.ensure_future(OverlayInterface.event_listener(_e_handler)) th = Timer(0.01, _thread) th.start() th_a = Timer(0.05, _assert) th_a.start() l.run_forever() p = Process(target=_send_msg) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) t = Timer(5, _stop) t.start() OverlayInterface.start_service( sk= '06391888e37a48cef1ded85a375490df4f9b2c74f7723e88c954a055f3d2685a') self.assertEqual(p.exitcode, 0)
def __init__(self, manager: ExecutorManager, signing_key: str, ip, name='Node'): super().__init__() self.log = get_logger("{}.Composer".format(name)) self.manager = manager self.ip = ip self.signing_key = signing_key self.verifying_key = wallet.get_vk(self.signing_key) self.overlay_fut = asyncio.ensure_future( OverlayInterface.event_listener(self._handle_overlay_event)) self.command_queue = {} # dict of UUID to kwargs self.pending_commands = deque( ) # To hold commands until the event loop is started # The 'future' task to flush pending_commands when event loop starts self.flush_pending_fut = asyncio.ensure_future( self._flush_pending_commands())
def _func(self, *args, **kwargs): contains_vk = 'vk' in kwargs and kwargs['vk'] contains_ip = 'ip' in kwargs and kwargs['ip'] if contains_vk and not contains_ip: # We can't call get_node_from_vk if the event loop is not running, so we add it to pending commands if not asyncio.get_event_loop().is_running(): self.log.debugv( "Cannot execute vk lookup yet as event loop is not running. Adding func {} to command " "queue".format(func.__name__)) self.pending_commands.append((func.__name__, args, kwargs)) return cmd_id = OverlayInterface.get_node_from_vk(kwargs['vk']) assert cmd_id not in self.command_queue, "Collision! Uuid {} already in command queue {}".format( cmd_id, self.command_queue) self.log.debugv( "Looking up vk {}, which returned command id {}".format( kwargs['vk'], cmd_id)) self.command_queue[cmd_id] = (func.__name__, args, kwargs) # If the 'ip' key is already set in kwargs, no need to do a lookup else: func(self, *args, **kwargs)
def _send_msg(): def _e_handler(e): OverlayInterface._test_res = e def _thread(): asyncio.set_event_loop(l) OverlayInterface.get_node_from_vk('fsdfhsdfkjsdh') def _assert(): self.assertTrue(OverlayInterface._test_res['event'], 'not_found') l.call_soon_threadsafe(l.stop) th.join() th_a.join() l = asyncio.new_event_loop() asyncio.set_event_loop(l) asyncio.ensure_future(OverlayInterface.event_listener(_e_handler)) th = Timer(0.01, _thread) th.start() th_a = Timer(0.05, _assert) th_a.start() l.run_forever()
def _send_msg(): def _e_handler(e): OverlayInterface._test_res = e def _thread(): asyncio.set_event_loop(l) OverlayInterface.get_node_from_vk( '82540bb5a9c84162214c5540d6e43be49bbfe19cf49685660cab608998a65144' ) def _assert(): self.assertEqual(OverlayInterface._test_res['ip'], '127.0.0.1') l.call_soon_threadsafe(l.stop) th.join() th_a.join() l = asyncio.new_event_loop() asyncio.set_event_loop(l) asyncio.ensure_future(OverlayInterface.event_listener(_e_handler)) th = Timer(0.01, _thread) th.start() th_a = Timer(0.05, _assert) th_a.start() l.run_forever()
def _thread(): asyncio.set_event_loop(l) OverlayInterface.get_node_from_vk('fsdfhsdfkjsdh')
def _thread(): asyncio.set_event_loop(l) OverlayInterface.get_node_from_vk( '82540bb5a9c84162214c5540d6e43be49bbfe19cf49685660cab608998a65144' )
from cilantro.protocol.overlay.interface import OverlayInterface from cilantro.logger.base import get_logger from threading import Thread import asyncio log = get_logger(__name__) def event_handler(e): log.important(e) OverlayInterface.start_service(sk='06391888e37a48cef1ded85a375490df4f9b2c74f7723e88c954a055f3d2685a') node = OverlayInterface.get_node_from_vk(vk='82540bb5a9c84162214c5540d6e43be49bbfe19cf49685660cab608998a65144')