def handle(self, connection_id, message_content): request = RegisterStateDeltaSubscriberRequest() request.ParseFromString(message_content) try: self._delta_processor.add_subscriber(connection_id, request.last_known_block_ids, request.address_prefixes) except NoKnownBlockError: LOGGER.debug('Subscriber %s added, but catch-up failed', connection_id) return HandlerResult(HandlerStatus.DROP) return HandlerResult(HandlerStatus.PASS)
def test_register_with_uknown_block_ids(self): """Tests that the handler will respond with an UNKNOWN_BLOCK when a subscriber does not supply a known block id in last_known_block_ids """ block_tree_manager = BlockTreeManager() delta_processor = StateDeltaProcessor( service=Mock(), state_delta_store=Mock(), block_store=block_tree_manager.block_store) handler = StateDeltaSubscriberValidationHandler(delta_processor) request = RegisterStateDeltaSubscriberRequest( last_known_block_ids=['a'], address_prefixes=['000000']).SerializeToString() response = handler.handle('test_conn_id', request) self.assertEqual(HandlerStatus.RETURN, response.status) self.assertEqual( RegisterStateDeltaSubscriberResponse.UNKNOWN_BLOCK, response.message_out.status)
def handle(self, connection_id, message_content): request = RegisterStateDeltaSubscriberRequest() request.ParseFromString(message_content) ack = RegisterStateDeltaSubscriberResponse() if self._delta_processor.is_valid_subscription( request.last_known_block_ids): ack.status = ack.OK return HandlerResult(HandlerStatus.RETURN_AND_PASS, message_out=ack, message_type=self._msg_type) else: ack.status = ack.UNKNOWN_BLOCK return HandlerResult(HandlerStatus.RETURN, message_out=ack, message_type=self._msg_type)
def handle(self, connection_id, message_content): request = RegisterStateDeltaSubscriberRequest() request.ParseFromString(message_content) ack = RegisterStateDeltaSubscriberResponse() try: self._delta_processor.add_subscriber(connection_id, request.last_known_block_ids, request.address_prefixes) ack.status = ack.OK except NoKnownBlockError: ack.status = ack.UNKNOWN_BLOCK return HandlerResult(HandlerStatus.RETURN, message_out=ack, message_type=self._msg_type)
def test_add_subscriber(self): """Tests that the handler for adding the subscriptions will properly add a subscriber. """ block_tree_manager = BlockTreeManager() delta_processor = StateDeltaProcessor( service=Mock(), state_delta_store=Mock(), block_store=block_tree_manager.block_store) handler = StateDeltaAddSubscriberHandler(delta_processor) request = RegisterStateDeltaSubscriberRequest( last_known_block_ids=[block_tree_manager.chain_head.identifier], address_prefixes=['0123456']).SerializeToString() response = handler.handle('test_conn_id', request) self.assertEqual(HandlerStatus.PASS, response.status) self.assertEqual(['test_conn_id'], delta_processor.subscriber_ids)
def test_register_subscriber(self): """Tests that the handler will add a valid subscriber and return an OK response. """ mock_block_store = {'a': Mock()} delta_processor = StateDeltaProcessor(service=Mock(), state_delta_store=Mock(), block_store=mock_block_store) handler = StateDeltaSubscriberHandler(delta_processor) request = RegisterStateDeltaSubscriberRequest( last_known_block_ids=['a'], address_prefixes=['000000']).SerializeToString() response = handler.handle('test_conn_id', request) self.assertEqual(HandlerStatus.RETURN, response.status) self.assertEqual(RegisterStateDeltaSubscriberResponse.OK, response.message_out.status)
def test_register_subscriber(self): """Tests that the handler will validate a correct subscriber and return an OK response. """ block_tree_manager = BlockTreeManager() delta_processor = StateDeltaProcessor( service=Mock(), state_delta_store=Mock(), block_store=block_tree_manager.block_store) handler = StateDeltaSubscriberValidationHandler(delta_processor) request = RegisterStateDeltaSubscriberRequest( last_known_block_ids=[block_tree_manager.chain_head.identifier], address_prefixes=['000000']).SerializeToString() response = handler.handle('test_conn_id', request) self.assertEqual(HandlerStatus.RETURN_AND_PASS, response.status) self.assertEqual(RegisterStateDeltaSubscriberResponse.OK, response.message_out.status)
def test_register_with_unknown_block_ids(self): """Tests that the handler will respond with a DROP when a subscriber does not supply a known block id in last_known_block_ids, but the subscriber is added. It passed validation, but a fork may have occured between validation and now. """ block_tree_manager = BlockTreeManager() delta_processor = StateDeltaProcessor( service=Mock(), state_delta_store=Mock(), block_store=block_tree_manager.block_store) handler = StateDeltaAddSubscriberHandler(delta_processor) request = RegisterStateDeltaSubscriberRequest( last_known_block_ids=['a'], address_prefixes=['000000']).SerializeToString() response = handler.handle('test_conn_id', request) self.assertEqual(HandlerStatus.DROP, response.status) self.assertEqual(['test_conn_id'], delta_processor.subscriber_ids)