def setUp(self): self.ctx = zmq.Context.instance() self.socket = self.ctx.socket(zmq.ROUTER) self.socket.bind('tcp://127.0.0.1:*') self.url = self.socket.getsockopt_string(zmq.LAST_ENDPOINT) self.connection_id = None self.engine = MockEngine() self.driver = ZmqDriver(self.engine)
def main(args=None): try: path_config = load_path_config() except LocalConfigurationError as local_config_err: LOGGER.error(str(local_config_err)) sys.exit(1) if args is None: args = sys.argv[1:] opts = parse_args(args) try: arg_config = create_pbft_config(opts) pbft_config = load_pbft_config(arg_config) log_config = get_log_config('bgx-pbft-engine-log-config.toml') if log_config is None: log_config = get_log_config('bgx-pbft-engine-log-config.yaml') if log_config is not None: log_configuration(log_config=log_config) else: log_dir = get_log_dir() log_configuration( log_dir=log_dir, name='pbft-engine') init_console_logging(verbose_level=opts.verbose) driver = ZmqDriver( PbftEngine( path_config=path_config, component_endpoint=opts.component, pbft_config=pbft_config)) LOGGER.debug('Start driver=%s endpoint=%s component=%s',driver,opts.connect,opts.component) attemps = 0 while attemps < MAX_CONNECT_ATTEMPTS: try: driver.start(endpoint=opts.connect) break except FutureTimeoutError: attemps += 1 LOGGER.debug('Start driver=%s endpoint=%s AGAIN=%s',driver,opts.connect,attemps) except KeyboardInterrupt: pass except Exception: # pylint: disable=broad-except LOGGER.exception("Error starting PBFT Engine") finally: pass
def main(args=None): if args is None: args = sys.argv[1:] opts = parse_args(args) try: log_dir = get_log_dir() log_configuration(log_dir=log_dir, name='poet-engine') init_console_logging(verbose_level=opts.verbose) driver = ZmqDriver(PoetEngine()) driver.start(endpoint=opts.connect) except KeyboardInterrupt: pass except Exception as e: # pylint: disable=broad-except print("Error: {}".format(e), file=sys.stderr) finally: pass
def main(args=None): if args is None: args = sys.argv[1:] opts = parse_args(args) try: log_dir = get_log_dir() log_configuration( log_dir=log_dir, name='poet-engine') init_console_logging(verbose_level=opts.verbose) driver = ZmqDriver(PoetEngine()) driver.start(endpoint=opts.connect) except KeyboardInterrupt: pass except Exception as e: # pylint: disable=broad-except print("Error: {}".format(e), file=sys.stderr) finally: pass
def main(args=None): try: path_config = load_path_config() except LocalConfigurationError as local_config_err: LOGGER.error(str(local_config_err)) sys.exit(1) if args is None: args = sys.argv[1:] opts = parse_args(args) try: log_config = get_log_config('poet-engine-log-config.toml') if log_config is None: log_config = get_log_config('poet-engine-log-config.yaml') if log_config is not None: log_configuration(log_config=log_config) else: log_dir = get_log_dir() log_configuration( log_dir=log_dir, name='poet-engine') init_console_logging(verbose_level=opts.verbose) driver = ZmqDriver( PoetEngine( path_config=path_config, component_endpoint=opts.component)) driver.start(endpoint=opts.connect) except KeyboardInterrupt: pass except Exception: # pylint: disable=broad-except LOGGER.exception("Error starting PoET Engine") finally: pass
class TestDriver(unittest.TestCase): def setUp(self): self.ctx = zmq.Context.instance() self.socket = self.ctx.socket(zmq.ROUTER) self.socket.bind('tcp://127.0.0.1:*') self.url = self.socket.getsockopt_string(zmq.LAST_ENDPOINT) self.connection_id = None self.engine = MockEngine() self.driver = ZmqDriver(self.engine) def tearDown(self): self.socket.close() def recv_rep(self, request_type, response, response_type): # pylint: disable=unbalanced-tuple-unpacking connection_id, message_bytes = self.socket.recv_multipart(0) self.connection_id = connection_id message = Message() message.ParseFromString(message_bytes) request = request_type() request.ParseFromString(message.content) reply = Message(message_type=response_type, content=response.SerializeToString(), correlation_id=message.correlation_id) self.socket.send_multipart( [self.connection_id, reply.SerializeToString()], 0) return request def send_req_rep(self, request, request_type): message = Message(message_type=request_type, correlation_id=generate_correlation_id(), content=request.SerializeToString()) self.socket.send_multipart( [self.connection_id, message.SerializeToString()], 0) # pylint: disable=unbalanced-tuple-unpacking _, reply_bytes = self.socket.recv_multipart(0) reply = Message() reply.ParseFromString(reply_bytes) self.assertEqual(reply.message_type, Message.CONSENSUS_NOTIFY_ACK) return reply.content def test_driver(self): # Start the driver in a separate thread to simulate the # validator and the driver driver_thread = threading.Thread(target=self.driver.start, args=(self.url, )) driver_thread.start() response = consensus_pb2.ConsensusRegisterResponse( status=consensus_pb2.ConsensusRegisterResponse.OK) request = self.recv_rep(consensus_pb2.ConsensusRegisterRequest, response, Message.CONSENSUS_REGISTER_RESPONSE) additional_protocols = \ [(p.name, p.version) for p in request.additional_protocols] self.assertEqual(request.name, 'test-name') self.assertEqual(request.version, 'test-version') self.assertEqual(additional_protocols, [('Test-Name', 'Test-Version')]) self.send_req_rep(consensus_pb2.ConsensusNotifyEngineActivated(), Message.CONSENSUS_NOTIFY_ENGINE_ACTIVATED) self.send_req_rep(consensus_pb2.ConsensusNotifyPeerConnected(), Message.CONSENSUS_NOTIFY_PEER_CONNECTED) self.send_req_rep(consensus_pb2.ConsensusNotifyPeerDisconnected(), Message.CONSENSUS_NOTIFY_PEER_DISCONNECTED) self.send_req_rep(consensus_pb2.ConsensusNotifyPeerMessage(), Message.CONSENSUS_NOTIFY_PEER_MESSAGE) self.send_req_rep(consensus_pb2.ConsensusNotifyBlockNew(), Message.CONSENSUS_NOTIFY_BLOCK_NEW) self.send_req_rep(consensus_pb2.ConsensusNotifyBlockValid(), Message.CONSENSUS_NOTIFY_BLOCK_VALID) self.send_req_rep(consensus_pb2.ConsensusNotifyBlockInvalid(), Message.CONSENSUS_NOTIFY_BLOCK_INVALID) self.send_req_rep(consensus_pb2.ConsensusNotifyBlockCommit(), Message.CONSENSUS_NOTIFY_BLOCK_COMMIT) self.send_req_rep(network_pb2.PingRequest(), Message.PING_REQUEST) self.assertEqual( [msg_type for (msg_type, data) in self.engine.updates], [ Message.CONSENSUS_NOTIFY_PEER_CONNECTED, Message.CONSENSUS_NOTIFY_PEER_DISCONNECTED, Message.CONSENSUS_NOTIFY_PEER_MESSAGE, Message.CONSENSUS_NOTIFY_BLOCK_NEW, Message.CONSENSUS_NOTIFY_BLOCK_VALID, Message.CONSENSUS_NOTIFY_BLOCK_INVALID, Message.CONSENSUS_NOTIFY_BLOCK_COMMIT, ]) self.driver.stop() driver_thread.join()
class TestDriver(unittest.TestCase): def setUp(self): self.ctx = zmq.Context.instance() self.socket = self.ctx.socket(zmq.ROUTER) self.socket.bind('tcp://127.0.0.1:*') self.url = self.socket.getsockopt_string(zmq.LAST_ENDPOINT) self.connection_id = None self.engine = MockEngine() self.driver = ZmqDriver(self.engine) def tearDown(self): self.socket.close() def recv_rep(self, request_type, response, response_type): # pylint: disable=unbalanced-tuple-unpacking connection_id, message_bytes = self.socket.recv_multipart(0) self.connection_id = connection_id message = Message() message.ParseFromString(message_bytes) request = request_type() request.ParseFromString(message.content) reply = Message( message_type=response_type, content=response.SerializeToString(), correlation_id=message.correlation_id) self.socket.send_multipart( [self.connection_id, reply.SerializeToString()], 0) return request def send_req_rep(self, request, request_type): message = Message( message_type=request_type, correlation_id=generate_correlation_id(), content=request.SerializeToString()) self.socket.send_multipart( [self.connection_id, message.SerializeToString()], 0) # pylint: disable=unbalanced-tuple-unpacking _, reply_bytes = self.socket.recv_multipart(0) reply = Message() reply.ParseFromString(reply_bytes) self.assertEqual( reply.message_type, Message.CONSENSUS_NOTIFY_ACK) return reply.content def test_driver(self): # Start the driver in a separate thread to simulate the # validator and the driver driver_thread = threading.Thread( target=self.driver.start, args=(self.url,)) driver_thread.start() response = consensus_pb2.ConsensusRegisterResponse( status=consensus_pb2.ConsensusRegisterResponse.OK) request = self.recv_rep( consensus_pb2.ConsensusRegisterRequest, response, Message.CONSENSUS_REGISTER_RESPONSE) self.assertEqual(request.name, 'test-name') self.assertEqual(request.version, 'test-version') self.send_req_rep( consensus_pb2.ConsensusNotifyEngineActivated(), Message.CONSENSUS_NOTIFY_ENGINE_ACTIVATED) self.send_req_rep( consensus_pb2.ConsensusNotifyPeerConnected(), Message.CONSENSUS_NOTIFY_PEER_CONNECTED) self.send_req_rep( consensus_pb2.ConsensusNotifyPeerDisconnected(), Message.CONSENSUS_NOTIFY_PEER_DISCONNECTED) self.send_req_rep( consensus_pb2.ConsensusNotifyPeerMessage(), Message.CONSENSUS_NOTIFY_PEER_MESSAGE) self.send_req_rep( consensus_pb2.ConsensusNotifyBlockNew(), Message.CONSENSUS_NOTIFY_BLOCK_NEW) self.send_req_rep( consensus_pb2.ConsensusNotifyBlockValid(), Message.CONSENSUS_NOTIFY_BLOCK_VALID) self.send_req_rep( consensus_pb2.ConsensusNotifyBlockInvalid(), Message.CONSENSUS_NOTIFY_BLOCK_INVALID) self.send_req_rep( consensus_pb2.ConsensusNotifyBlockCommit(), Message.CONSENSUS_NOTIFY_BLOCK_COMMIT) self.assertEqual( [msg_type for (msg_type, data) in self.engine.updates], [ Message.CONSENSUS_NOTIFY_PEER_CONNECTED, Message.CONSENSUS_NOTIFY_PEER_DISCONNECTED, Message.CONSENSUS_NOTIFY_PEER_MESSAGE, Message.CONSENSUS_NOTIFY_BLOCK_NEW, Message.CONSENSUS_NOTIFY_BLOCK_VALID, Message.CONSENSUS_NOTIFY_BLOCK_INVALID, Message.CONSENSUS_NOTIFY_BLOCK_COMMIT, ]) self.driver.stop() driver_thread.join()