コード例 #1
0
    def __init__(
        self,
        dialogue_label: BaseDialogueLabel,
        self_address: Address,
        role: BaseDialogue.Role,
        message_class: Type[ContractApiMessage] = ContractApiMessage,
    ) -> None:
        """
        Initialize a dialogue.

        :param dialogue_label: the identifier of the dialogue
        :param self_address: the address of the entity for whom this dialogue is maintained
        :param role: the role of the agent this dialogue is maintained for

        :return: None
        """
        BaseContractApiDialogue.__init__(
            self,
            dialogue_label=dialogue_label,
            self_address=self_address,
            role=role,
            message_class=message_class,
        )
        self._terms = None  # type: Optional[Terms]
        self._callable = None  # type: Optional[ContractApiDialogue.Callable]
コード例 #2
0
    def get_raw_message(
        self,
        api: LedgerApi,
        message: ContractApiMessage,
        dialogue: ContractApiDialogue,
    ) -> ContractApiMessage:
        """
        Send the request 'get_raw_message'.

        :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:
            rm = method_to_call(api, message.contract_address, **message.kwargs.body)
            response = ContractApiMessage(
                performative=ContractApiMessage.Performative.RAW_MESSAGE,
                message_id=message.message_id + 1,
                target=message.message_id,
                dialogue_reference=dialogue.dialogue_label.dialogue_reference,
                raw_message=RawMessage(message.ledger_id, rm),
            )
            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
コード例 #3
0
    def dispatch_request(
        self,
        ledger_api: LedgerApi,
        message: ContractApiMessage,
        dialogue: ContractApiDialogue,
        response_builder: Callable[[bytes], ContractApiMessage],
    ) -> ContractApiMessage:
        """
        Dispatch a request to a user-defined contract method.

        :param ledger_api: the ledger apis.
        :param message: the contract API request message.
        :param dialogue: the contract API dialogue.
        :param response_builder: callable that from bytes builds a contract API message.
        :return: the response message.
        """
        contract = self.contract_registry.make(message.contract_id)
        try:
            data = self._get_data(ledger_api, message, contract)
            response = response_builder(data)
            response.counterparty = message.counterparty
            dialogue.update(response)
        except AEAException as e:
            self.logger.error(str(e))
            response = self.get_error_message(e, ledger_api, message, dialogue)
        except Exception as e:  # pylint: disable=broad-except  # pragma: nocover
            self.logger.error(
                f"An error occurred while processing the contract api request: '{str(e)}'."
            )
            response = self.get_error_message(e, ledger_api, message, dialogue)
        return response
コード例 #4
0
    def __init__(
        self,
        dialogue_label: BaseDialogueLabel,
        agent_address: Address,
        role: BaseDialogue.Role,
    ) -> None:
        """
        Initialize a dialogue.

        :param dialogue_label: the identifier of the dialogue
        :param agent_address: the address of the agent for whom this dialogue is maintained
        :param role: the role of the agent this dialogue is maintained for

        :return: None
        """
        BaseContractApiDialogue.__init__(
            self, dialogue_label=dialogue_label, agent_address=agent_address, role=role
        )
        self._terms = None  # type: Optional[Terms]
コード例 #5
0
 def build_response(
     rm: bytes, dialogue: ContractApiDialogue
 ) -> ContractApiMessage:
     return cast(
         ContractApiMessage,
         dialogue.reply(
             performative=ContractApiMessage.Performative.RAW_MESSAGE,
             raw_message=RawMessage(message.ledger_id, rm),
         ),
     )
コード例 #6
0
 def build_response(
     tx: bytes, dialogue: ContractApiDialogue
 ) -> ContractApiMessage:
     return cast(
         ContractApiMessage,
         dialogue.reply(
             performative=ContractApiMessage.Performative.RAW_TRANSACTION,
             raw_transaction=RawTransaction(message.ledger_id, tx),
         ),
     )
コード例 #7
0
 def build_response(
     data: bytes, dialogue: ContractApiDialogue
 ) -> ContractApiMessage:
     return cast(
         ContractApiMessage,
         dialogue.reply(
             performative=ContractApiMessage.Performative.STATE,
             state=State(message.ledger_id, data),
         ),
     )
コード例 #8
0
 def build_response(
         rm: Union[bytes, JSONLike],
         dialogue: ContractApiDialogue) -> ContractApiMessage:
     if not isinstance(rm, bytes):
         raise ValueError(
             f"Invalid message type, got={type(rm)}, expected=bytes.")
     return cast(
         ContractApiMessage,
         dialogue.reply(
             performative=ContractApiMessage.Performative.RAW_MESSAGE,
             raw_message=RawMessage(message.ledger_id, rm),
         ),
     )
コード例 #9
0
 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),
         ),
     )
コード例 #10
0
    def __init__(
        self,
        dialogue_label: DialogueLabel,
        self_address: Address,
        role: BaseDialogue.Role,
        message_class: Type[ContractApiMessage],
    ) -> None:
        """
        Initialize a dialogue.

        :param dialogue_label: the identifier of the dialogue
        :param self_address: the address of the entity for whom this dialogue is maintained
        :param role: the role of the agent this dialogue is maintained for

        :return: None
        """
        ContractApiDialogue.__init__(
            self,
            dialogue_label=dialogue_label,
            self_address=self_address,
            role=role,
            message_class=message_class,
        )
コード例 #11
0
 def build_response(
         tx: Union[bytes, JSONLike],
         dialogue: ContractApiDialogue) -> ContractApiMessage:
     if isinstance(tx, bytes):
         raise ValueError(
             f"Invalid transaction type, got={type(tx)}, expected={JSONLike}."
         )
     return cast(
         ContractApiMessage,
         dialogue.reply(
             performative=ContractApiMessage.Performative.
             RAW_TRANSACTION,
             raw_transaction=RawTransaction(message.ledger_id, tx),
         ),
     )
コード例 #12
0
    def create_dialogue(
        self, dialogue_label: BaseDialogueLabel, role: BaseDialogue.Role,
    ) -> ContractApiDialogue:
        """
        Create an instance of contract API dialogue.

        :param dialogue_label: the identifier of the dialogue
        :param role: the role of the agent this dialogue is maintained for

        :return: the created dialogue
        """
        dialogue = ContractApiDialogue(
            dialogue_label=dialogue_label, agent_address=str(CONNECTION_ID), role=role,
        )
        return dialogue