コード例 #1
0
    def _process(self, msg):
        if msg.message_type != Message.TP_PROCESS_REQUEST:
            LOGGER.debug(
                "Transaction Processor recieved invalid message type. "
                "Message type should be TP_PROCESS_REQUEST,"
                " but is %s", Message.MessageType.Name(msg.message_type))
            return

        request = TpProcessRequest()
        request.ParseFromString(msg.content)
        state = Context(self._stream, request.context_id)
        header = request.header
        try:
            if not self._stream.is_ready():
                raise ValidatorConnectionError()
            handler = self._find_handler(header)
            if handler is None:
                return
            handler.apply(request, state)
            self._stream.send_back(
                message_type=Message.TP_PROCESS_RESPONSE,
                correlation_id=msg.correlation_id,
                content=TpProcessResponse(
                    status=TpProcessResponse.OK
                ).SerializeToString())
        except InvalidTransaction as it:
            LOGGER.warning("Invalid Transaction %s", it)
            try:
                self._stream.send_back(
                    message_type=Message.TP_PROCESS_RESPONSE,
                    correlation_id=msg.correlation_id,
                    content=TpProcessResponse(
                        status=TpProcessResponse.INVALID_TRANSACTION,
                        message=str(it),
                        extended_data=it.extended_data
                    ).SerializeToString())
            except ValidatorConnectionError as vce:
                # TP_PROCESS_REQUEST has made it through the
                # handler.apply and an INVALID_TRANSACTION would have been
                # sent back but the validator has disconnected and so it
                # doesn't care about the response.
                LOGGER.warning("during invalid transaction response: %s", vce)
        except InternalError as ie:
            LOGGER.warning("internal error: %s", ie)
            try:
                self._stream.send_back(
                    message_type=Message.TP_PROCESS_RESPONSE,
                    correlation_id=msg.correlation_id,
                    content=TpProcessResponse(
                        status=TpProcessResponse.INTERNAL_ERROR,
                        message=str(ie),
                        extended_data=ie.extended_data
                    ).SerializeToString())
            except ValidatorConnectionError as vce:
                # Same as the prior except block, but an internal error has
                # happened, but because of the disconnect the validator
                # probably doesn't care about the response.
                LOGGER.warning("during internal error response: %s", vce)
        except ValidatorConnectionError as vce:
            # Somewhere within handler.apply a future resolved with an
            # error status that the validator has disconnected. There is
            # nothing left to do but reconnect.
            LOGGER.warning("during handler.apply a future was resolved "
                           "with error status: %s", vce)
        except AuthorizationException as ae:
            LOGGER.warning("AuthorizationException: %s", ae)
            try:
                self._stream.send_back(
                    message_type=Message.TP_PROCESS_RESPONSE,
                    correlation_id=msg.correlation_id,
                    content=TpProcessResponse(
                        status=TpProcessResponse.INVALID_TRANSACTION,
                        message=str(ae),
                    ).SerializeToString())
            except ValidatorConnectionError as vce:
                # TP_PROCESS_REQUEST has made it through the
                # handler.apply and an INVALID_TRANSACTION would have been
                # sent back but the validator has disconnected and so it
                # doesn't care about the response.
                LOGGER.warning("during invalid transaction response: %s", vce)
コード例 #2
0
 def setUp(self):
     self.context_id = "test"
     self.mock_stream = Mock()
     self.context = Context(self.mock_stream, self.context_id)
     self.addresses = ["a", "b", "c"]
     self.data = [addr.encode() for addr in self.addresses]
コード例 #3
0
 def setUp(self):
     self.context_id = "test"
     self.mock_stream = Mock()
     self.context = Context(self.mock_stream, self.context_id)
     self.addresses = ["a", "b", "c"]
     self.data = [addr.encode() for addr in self.addresses]
コード例 #4
0
class ContextTest(unittest.TestCase):
    def setUp(self):
        self.context_id = "test"
        self.mock_stream = Mock()
        self.context = Context(self.mock_stream, self.context_id)
        self.addresses = ["a", "b", "c"]
        self.data = [addr.encode() for addr in self.addresses]

    def _make_future(self, message_type, content):
        f = Future(self.context_id)
        f.set_result(FutureResult(
            message_type=message_type,
            content=content))
        return f

    def _make_entries(self, protobuf=True):
        if protobuf:
            return [Entry(address=a, data=d)
                    for a, d in zip(self.addresses, self.data)]

        entries = OrderedDict()
        for a, d in zip(self.addresses, self.data):
            entries[a] = d
        return entries

    def test_state_get(self):
        """Tests that State gets addresses correctly."""
        self.mock_stream.send.return_value = self._make_future(
            message_type=Message.TP_STATE_GET_RESPONSE,
            content=TpStateGetResponse(
                status=TpStateGetResponse.OK,
                entries=self._make_entries()).SerializeToString())

        self.context.get_state(self.addresses)

        self.mock_stream.send.assert_called_with(
            Message.TP_STATE_GET_REQUEST,
            TpStateGetRequest(
                context_id=self.context_id,
                addresses=self.addresses).SerializeToString())

    def test_state_set(self):
        """Tests that State sets addresses correctly."""
        self.mock_stream.send.return_value = self._make_future(
            message_type=Message.TP_STATE_SET_RESPONSE,
            content=TpStateSetResponse(
                status=TpStateSetResponse.OK,
                addresses=self.addresses).SerializeToString())

        self.context.set_state(self._make_entries(protobuf=False))

        self.mock_stream.send.assert_called_with(
            Message.TP_STATE_SET_REQUEST,
            TpStateSetRequest(
                context_id=self.context_id,
                entries=self._make_entries()).SerializeToString())

    def test_state_delete(self):
        """Tests that State deletes addresses correctly."""
        self.mock_stream.send.return_value = self._make_future(
            message_type=Message.TP_STATE_DEL_RESPONSE,
            content=TpStateDeleteResponse(
                status=TpStateDeleteResponse.OK,
                addresses=self.addresses).SerializeToString())

        self.context.delete_state(self.addresses)

        self.mock_stream.send.assert_called_with(
            Message.TP_STATE_DEL_REQUEST,
            TpStateDeleteRequest(
                context_id=self.context_id,
                addresses=self.addresses).SerializeToString())

    def test_add_receipt_data(self):
        """Tests that State adds receipt data correctly."""
        self.mock_stream.send.return_value = self._make_future(
            message_type=Message.TP_ADD_RECEIPT_DATA_RESPONSE,
            content=TpAddReceiptDataResponse(
                status=TpAddReceiptDataResponse.OK).SerializeToString())

        self.context.add_receipt_data("test", b"test")

        self.mock_stream.send.assert_called_with(
            Message.TP_ADD_RECEIPT_DATA_REQUEST,
            TpAddReceiptDataRequest(
                context_id=self.context_id,
                data_type="test",
                data=b"test").SerializeToString())

    def test_add_event(self):
        """Tests that State adds events correctly."""
        self.mock_stream.send.return_value = self._make_future(
            message_type=Message.TP_ADD_EVENT_RESPONSE,
            content=TpAddEventResponse(
                status=TpAddEventResponse.OK).SerializeToString())

        self.context.add_event("test", [("test", "test")], b"test")

        self.mock_stream.send.assert_called_with(
            Message.TP_ADD_EVENT_REQUEST,
            TpAddEventRequest(
                context_id=self.context_id,
                event=Event(
                    event_type="test",
                    attributes=[Event.Attribute(key="test", value="test")],
                    data=b"test")).SerializeToString())
コード例 #5
0
class ContextTest(unittest.TestCase):
    def setUp(self):
        self.context_id = "test"
        self.mock_stream = Mock()
        self.context = Context(self.mock_stream, self.context_id)
        self.addresses = ["a", "b", "c"]
        self.data = [addr.encode() for addr in self.addresses]

    def _make_future(self, message_type, content):
        f = Future(self.context_id)
        f.set_result(FutureResult(
            message_type=message_type,
            content=content))
        return f

    def _make_entries(self, protobuf=True):
        if protobuf:
            return [
                TpStateEntry(address=a, data=d)
                for a, d in zip(self.addresses, self.data)
            ]

        entries = OrderedDict()
        for a, d in zip(self.addresses, self.data):
            entries[a] = d
        return entries

    def test_state_get(self):
        """Tests that State gets addresses correctly."""
        self.mock_stream.send.return_value = self._make_future(
            message_type=Message.TP_STATE_GET_RESPONSE,
            content=TpStateGetResponse(
                status=TpStateGetResponse.OK,
                entries=self._make_entries()).SerializeToString())

        self.context.get_state(self.addresses)

        self.mock_stream.send.assert_called_with(
            Message.TP_STATE_GET_REQUEST,
            TpStateGetRequest(
                context_id=self.context_id,
                addresses=self.addresses).SerializeToString())

    def test_state_set(self):
        """Tests that State sets addresses correctly."""
        self.mock_stream.send.return_value = self._make_future(
            message_type=Message.TP_STATE_SET_RESPONSE,
            content=TpStateSetResponse(
                status=TpStateSetResponse.OK,
                addresses=self.addresses).SerializeToString())

        self.context.set_state(self._make_entries(protobuf=False))

        self.mock_stream.send.assert_called_with(
            Message.TP_STATE_SET_REQUEST,
            TpStateSetRequest(
                context_id=self.context_id,
                entries=self._make_entries()).SerializeToString())

    def test_state_delete(self):
        """Tests that State deletes addresses correctly."""
        self.mock_stream.send.return_value = self._make_future(
            message_type=Message.TP_STATE_DELETE_RESPONSE,
            content=TpStateDeleteResponse(
                status=TpStateDeleteResponse.OK,
                addresses=self.addresses).SerializeToString())

        self.context.delete_state(self.addresses)

        self.mock_stream.send.assert_called_with(
            Message.TP_STATE_DELETE_REQUEST,
            TpStateDeleteRequest(
                context_id=self.context_id,
                addresses=self.addresses).SerializeToString())

    def test_add_receipt_data(self):
        """Tests that State adds receipt data correctly."""
        self.mock_stream.send.return_value = self._make_future(
            message_type=Message.TP_RECEIPT_ADD_DATA_RESPONSE,
            content=TpReceiptAddDataResponse(
                status=TpReceiptAddDataResponse.OK).SerializeToString())

        self.context.add_receipt_data(b"test")

        self.mock_stream.send.assert_called_with(
            Message.TP_RECEIPT_ADD_DATA_REQUEST,
            TpReceiptAddDataRequest(
                context_id=self.context_id,
                data=b"test").SerializeToString())

    def test_add_event(self):
        """Tests that State adds events correctly."""
        self.mock_stream.send.return_value = self._make_future(
            message_type=Message.TP_EVENT_ADD_RESPONSE,
            content=TpEventAddResponse(
                status=TpEventAddResponse.OK).SerializeToString())

        self.context.add_event("test", [("test", "test")], b"test")

        self.mock_stream.send.assert_called_with(
            Message.TP_EVENT_ADD_REQUEST,
            TpEventAddRequest(
                context_id=self.context_id,
                event=Event(
                    event_type="test",
                    attributes=[Event.Attribute(key="test", value="test")],
                    data=b"test")).SerializeToString())