Example #1
0
def test_state_encode_decode():
    """Test encoding and decoding of state."""
    class StateProtobufObject:
        state_bytes = b""

    ledger_id = "some_ledger"
    body = {"state": "v"}
    state = State(ledger_id, body)
    State.encode(StateProtobufObject, state)
    recovered_state = State.decode(StateProtobufObject)
    assert state == recovered_state
Example #2
0
    def get_state(
        self,
        api: LedgerApi,
        message: ContractApiMessage,
        dialogue: ContractApiDialogue,
    ) -> ContractApiMessage:
        """
        Send the request 'get_state'.

        :param api: the API object.
        :param message: the Ledger API message
        :param dialogue: the contract API dialogue
        :return: None
        """
        contract = self.contract_registry.make(message.contract_id)
        method_to_call = getattr(contract, message.callable)
        try:
            data = method_to_call(api, message.contract_address, **message.kwargs.body)
            response = ContractApiMessage(
                performative=ContractApiMessage.Performative.STATE,
                message_id=message.message_id + 1,
                target=message.message_id,
                dialogue_reference=dialogue.dialogue_label.dialogue_reference,
                state=State(message.ledger_id, data),
            )
            response.counterparty = message.counterparty
            dialogue.update(response)
        except Exception as e:  # pylint: disable=broad-except  # pragma: nocover
            response = self.get_error_message(e, api, message, dialogue)
        return response
    def get_state(
        self,
        api: LedgerApi,
        message: LedgerApiMessage,
        dialogue: LedgerApiDialogue,
    ) -> LedgerApiMessage:
        """
        Send the request 'get_state'.

        :param api: the API object.
        :param message: the Ledger API message
        :return: None
        """
        result = api.get_state(message.callable, *message.args,
                               **message.kwargs.body)
        if result is None:  # pragma: nocover
            response = self.get_error_message(
                ValueError("Failed to get state"), api, message, dialogue)
        else:
            response = cast(
                LedgerApiMessage,
                dialogue.reply(
                    performative=LedgerApiMessage.Performative.STATE,
                    target_message=message,
                    state=State(message.ledger_id, result),
                    ledger_id=message.ledger_id,
                ),
            )
        return response
 def build_response(data: bytes) -> ContractApiMessage:
     return ContractApiMessage(
         performative=ContractApiMessage.Performative.STATE,
         message_id=message.message_id + 1,
         target=message.message_id,
         dialogue_reference=dialogue.dialogue_label.dialogue_reference,
         state=State(message.ledger_id, data),
     )
Example #5
0
def test_init_state():
    """Test the state object initialization."""
    ledger_id = "some_ledger"
    body = {"state": "v"}
    state = State(ledger_id, body)
    assert state.ledger_id == ledger_id
    assert state.body == body
    assert str(state) == f"State: ledger_id={ledger_id}, body={body}"
    assert state == state
Example #6
0
def test_init_state():
    """Test the state object initialization."""
    ledger_id = "some_ledger"
    body = "state"
    state = State(ledger_id, body)
    assert state.ledger_id == ledger_id
    assert state.body == body
    assert str(state) == "State: ledger_id=some_ledger, body=state"
    assert state == state
 def build_response(
     data: bytes, dialogue: ContractApiDialogue
 ) -> ContractApiMessage:
     return cast(
         ContractApiMessage,
         dialogue.reply(
             performative=ContractApiMessage.Performative.STATE,
             state=State(message.ledger_id, data),
         ),
     )
 def build_response(
         data: Union[bytes, JSONLike],
         dialogue: ContractApiDialogue) -> ContractApiMessage:
     if not isinstance(data, Mapping):
         raise ValueError(
             f"Invalid state type, got={type(data)}, expected={JSONLike}."
         )
     return cast(
         ContractApiMessage,
         dialogue.reply(
             performative=ContractApiMessage.Performative.STATE,
             state=State(message.ledger_id, data),
         ),
     )
Example #9
0
    def test_handle_unidentified_dialogue(self):
        """Test the _handle_unidentified_dialogue method of the signing handler."""
        # setup
        incorrect_dialogue_reference = ("", "")
        incoming_message = self.build_incoming_message(
            message_type=ContractApiMessage,
            dialogue_reference=incorrect_dialogue_reference,
            performative=ContractApiMessage.Performative.STATE,
            state=State("some_ledger_id", {"some_key": "some_value"}),
        )

        # operation
        with patch.object(self.logger, "log") as mock_logger:
            self.contract_api_handler.handle(incoming_message)

        # after
        mock_logger.assert_any_call(
            logging.INFO,
            f"received invalid contract_api message={incoming_message}, unidentified dialogue.",
        )