示例#1
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'TProtocol' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(TProtocolMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        t_protocol_msg = t_protocol_pb2.TProtocolMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == TProtocolMessage.Performative.PERFORMATIVE_CT:
            performative = t_protocol_pb2.TProtocolMessage.Performative_Ct_Performative(
            )  # type: ignore
            content_ct = msg.content_ct
            DataModel.encode(performative.content_ct, content_ct)
            t_protocol_msg.performative_ct.CopyFrom(performative)
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_PT:
            performative = t_protocol_pb2.TProtocolMessage.Performative_Pt_Performative(
            )  # type: ignore
            content_bytes = msg.content_bytes
            performative.content_bytes = content_bytes
            content_int = msg.content_int
            performative.content_int = content_int
            content_float = msg.content_float
            performative.content_float = content_float
            content_bool = msg.content_bool
            performative.content_bool = content_bool
            content_str = msg.content_str
            performative.content_str = content_str
            t_protocol_msg.performative_pt.CopyFrom(performative)
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_PCT:
            performative = t_protocol_pb2.TProtocolMessage.Performative_Pct_Performative(
            )  # type: ignore
            content_set_bytes = msg.content_set_bytes
            performative.content_set_bytes.extend(content_set_bytes)
            content_set_int = msg.content_set_int
            performative.content_set_int.extend(content_set_int)
            content_set_float = msg.content_set_float
            performative.content_set_float.extend(content_set_float)
            content_set_bool = msg.content_set_bool
            performative.content_set_bool.extend(content_set_bool)
            content_set_str = msg.content_set_str
            performative.content_set_str.extend(content_set_str)
            content_list_bytes = msg.content_list_bytes
            performative.content_list_bytes.extend(content_list_bytes)
            content_list_int = msg.content_list_int
            performative.content_list_int.extend(content_list_int)
            content_list_float = msg.content_list_float
            performative.content_list_float.extend(content_list_float)
            content_list_bool = msg.content_list_bool
            performative.content_list_bool.extend(content_list_bool)
            content_list_str = msg.content_list_str
            performative.content_list_str.extend(content_list_str)
            t_protocol_msg.performative_pct.CopyFrom(performative)
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_PMT:
            performative = t_protocol_pb2.TProtocolMessage.Performative_Pmt_Performative(
            )  # type: ignore
            content_dict_int_bytes = msg.content_dict_int_bytes
            performative.content_dict_int_bytes.update(content_dict_int_bytes)
            content_dict_int_int = msg.content_dict_int_int
            performative.content_dict_int_int.update(content_dict_int_int)
            content_dict_int_float = msg.content_dict_int_float
            performative.content_dict_int_float.update(content_dict_int_float)
            content_dict_int_bool = msg.content_dict_int_bool
            performative.content_dict_int_bool.update(content_dict_int_bool)
            content_dict_int_str = msg.content_dict_int_str
            performative.content_dict_int_str.update(content_dict_int_str)
            content_dict_bool_bytes = msg.content_dict_bool_bytes
            performative.content_dict_bool_bytes.update(
                content_dict_bool_bytes)
            content_dict_bool_int = msg.content_dict_bool_int
            performative.content_dict_bool_int.update(content_dict_bool_int)
            content_dict_bool_float = msg.content_dict_bool_float
            performative.content_dict_bool_float.update(
                content_dict_bool_float)
            content_dict_bool_bool = msg.content_dict_bool_bool
            performative.content_dict_bool_bool.update(content_dict_bool_bool)
            content_dict_bool_str = msg.content_dict_bool_str
            performative.content_dict_bool_str.update(content_dict_bool_str)
            content_dict_str_bytes = msg.content_dict_str_bytes
            performative.content_dict_str_bytes.update(content_dict_str_bytes)
            content_dict_str_int = msg.content_dict_str_int
            performative.content_dict_str_int.update(content_dict_str_int)
            content_dict_str_float = msg.content_dict_str_float
            performative.content_dict_str_float.update(content_dict_str_float)
            content_dict_str_bool = msg.content_dict_str_bool
            performative.content_dict_str_bool.update(content_dict_str_bool)
            content_dict_str_str = msg.content_dict_str_str
            performative.content_dict_str_str.update(content_dict_str_str)
            t_protocol_msg.performative_pmt.CopyFrom(performative)
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_MT:
            performative = t_protocol_pb2.TProtocolMessage.Performative_Mt_Performative(
            )  # type: ignore
            if msg.is_set("content_union_1_type_DataModel"):
                performative.content_union_1_type_DataModel_is_set = True
                content_union_1_type_DataModel = msg.content_union_1_type_DataModel
                DataModel.encode(
                    performative.content_union_1_type_DataModel,
                    content_union_1_type_DataModel,
                )
            if msg.is_set("content_union_1_type_bytes"):
                performative.content_union_1_type_bytes_is_set = True
                content_union_1_type_bytes = msg.content_union_1_type_bytes
                performative.content_union_1_type_bytes = content_union_1_type_bytes
            if msg.is_set("content_union_1_type_int"):
                performative.content_union_1_type_int_is_set = True
                content_union_1_type_int = msg.content_union_1_type_int
                performative.content_union_1_type_int = content_union_1_type_int
            if msg.is_set("content_union_1_type_float"):
                performative.content_union_1_type_float_is_set = True
                content_union_1_type_float = msg.content_union_1_type_float
                performative.content_union_1_type_float = content_union_1_type_float
            if msg.is_set("content_union_1_type_bool"):
                performative.content_union_1_type_bool_is_set = True
                content_union_1_type_bool = msg.content_union_1_type_bool
                performative.content_union_1_type_bool = content_union_1_type_bool
            if msg.is_set("content_union_1_type_str"):
                performative.content_union_1_type_str_is_set = True
                content_union_1_type_str = msg.content_union_1_type_str
                performative.content_union_1_type_str = content_union_1_type_str
            if msg.is_set("content_union_1_type_set_of_int"):
                performative.content_union_1_type_set_of_int_is_set = True
                content_union_1_type_set_of_int = msg.content_union_1_type_set_of_int
                performative.content_union_1_type_set_of_int.extend(
                    content_union_1_type_set_of_int)
            if msg.is_set("content_union_1_type_list_of_bool"):
                performative.content_union_1_type_list_of_bool_is_set = True
                content_union_1_type_list_of_bool = (
                    msg.content_union_1_type_list_of_bool)
                performative.content_union_1_type_list_of_bool.extend(
                    content_union_1_type_list_of_bool)
            if msg.is_set("content_union_1_type_dict_of_str_int"):
                performative.content_union_1_type_dict_of_str_int_is_set = True
                content_union_1_type_dict_of_str_int = (
                    msg.content_union_1_type_dict_of_str_int)
                performative.content_union_1_type_dict_of_str_int.update(
                    content_union_1_type_dict_of_str_int)
            if msg.is_set("content_union_2_type_set_of_bytes"):
                performative.content_union_2_type_set_of_bytes_is_set = True
                content_union_2_type_set_of_bytes = (
                    msg.content_union_2_type_set_of_bytes)
                performative.content_union_2_type_set_of_bytes.extend(
                    content_union_2_type_set_of_bytes)
            if msg.is_set("content_union_2_type_set_of_int"):
                performative.content_union_2_type_set_of_int_is_set = True
                content_union_2_type_set_of_int = msg.content_union_2_type_set_of_int
                performative.content_union_2_type_set_of_int.extend(
                    content_union_2_type_set_of_int)
            if msg.is_set("content_union_2_type_set_of_str"):
                performative.content_union_2_type_set_of_str_is_set = True
                content_union_2_type_set_of_str = msg.content_union_2_type_set_of_str
                performative.content_union_2_type_set_of_str.extend(
                    content_union_2_type_set_of_str)
            if msg.is_set("content_union_2_type_list_of_float"):
                performative.content_union_2_type_list_of_float_is_set = True
                content_union_2_type_list_of_float = (
                    msg.content_union_2_type_list_of_float)
                performative.content_union_2_type_list_of_float.extend(
                    content_union_2_type_list_of_float)
            if msg.is_set("content_union_2_type_list_of_bool"):
                performative.content_union_2_type_list_of_bool_is_set = True
                content_union_2_type_list_of_bool = (
                    msg.content_union_2_type_list_of_bool)
                performative.content_union_2_type_list_of_bool.extend(
                    content_union_2_type_list_of_bool)
            if msg.is_set("content_union_2_type_list_of_bytes"):
                performative.content_union_2_type_list_of_bytes_is_set = True
                content_union_2_type_list_of_bytes = (
                    msg.content_union_2_type_list_of_bytes)
                performative.content_union_2_type_list_of_bytes.extend(
                    content_union_2_type_list_of_bytes)
            if msg.is_set("content_union_2_type_dict_of_str_int"):
                performative.content_union_2_type_dict_of_str_int_is_set = True
                content_union_2_type_dict_of_str_int = (
                    msg.content_union_2_type_dict_of_str_int)
                performative.content_union_2_type_dict_of_str_int.update(
                    content_union_2_type_dict_of_str_int)
            if msg.is_set("content_union_2_type_dict_of_int_float"):
                performative.content_union_2_type_dict_of_int_float_is_set = True
                content_union_2_type_dict_of_int_float = (
                    msg.content_union_2_type_dict_of_int_float)
                performative.content_union_2_type_dict_of_int_float.update(
                    content_union_2_type_dict_of_int_float)
            if msg.is_set("content_union_2_type_dict_of_bool_bytes"):
                performative.content_union_2_type_dict_of_bool_bytes_is_set = True
                content_union_2_type_dict_of_bool_bytes = (
                    msg.content_union_2_type_dict_of_bool_bytes)
                performative.content_union_2_type_dict_of_bool_bytes.update(
                    content_union_2_type_dict_of_bool_bytes)
            t_protocol_msg.performative_mt.CopyFrom(performative)
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_O:
            performative = t_protocol_pb2.TProtocolMessage.Performative_O_Performative(
            )  # type: ignore
            if msg.is_set("content_o_ct"):
                performative.content_o_ct_is_set = True
                content_o_ct = msg.content_o_ct
                DataModel.encode(performative.content_o_ct, content_o_ct)
            if msg.is_set("content_o_bool"):
                performative.content_o_bool_is_set = True
                content_o_bool = msg.content_o_bool
                performative.content_o_bool = content_o_bool
            if msg.is_set("content_o_set_int"):
                performative.content_o_set_int_is_set = True
                content_o_set_int = msg.content_o_set_int
                performative.content_o_set_int.extend(content_o_set_int)
            if msg.is_set("content_o_list_bytes"):
                performative.content_o_list_bytes_is_set = True
                content_o_list_bytes = msg.content_o_list_bytes
                performative.content_o_list_bytes.extend(content_o_list_bytes)
            if msg.is_set("content_o_dict_str_int"):
                performative.content_o_dict_str_int_is_set = True
                content_o_dict_str_int = msg.content_o_dict_str_int
                performative.content_o_dict_str_int.update(
                    content_o_dict_str_int)
            t_protocol_msg.performative_o.CopyFrom(performative)
        elif (performative_id ==
              TProtocolMessage.Performative.PERFORMATIVE_EMPTY_CONTENTS):
            performative = t_protocol_pb2.TProtocolMessage.Performative_Empty_Contents_Performative(
            )  # type: ignore
            t_protocol_msg.performative_empty_contents.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = t_protocol_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
    def encode(msg: Message) -> bytes:
        """
        Encode a 'ContractApi' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(ContractApiMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        contract_api_msg = contract_api_pb2.ContractApiMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == ContractApiMessage.Performative.GET_DEPLOY_TRANSACTION:
            performative = contract_api_pb2.ContractApiMessage.Get_Deploy_Transaction_Performative(
            )  # type: ignore
            ledger_id = msg.ledger_id
            performative.ledger_id = ledger_id
            contract_id = msg.contract_id
            performative.contract_id = contract_id
            callable = msg.callable
            performative.callable = callable
            kwargs = msg.kwargs
            Kwargs.encode(performative.kwargs, kwargs)
            contract_api_msg.get_deploy_transaction.CopyFrom(performative)
        elif performative_id == ContractApiMessage.Performative.GET_RAW_TRANSACTION:
            performative = contract_api_pb2.ContractApiMessage.Get_Raw_Transaction_Performative(
            )  # type: ignore
            ledger_id = msg.ledger_id
            performative.ledger_id = ledger_id
            contract_id = msg.contract_id
            performative.contract_id = contract_id
            contract_address = msg.contract_address
            performative.contract_address = contract_address
            callable = msg.callable
            performative.callable = callable
            kwargs = msg.kwargs
            Kwargs.encode(performative.kwargs, kwargs)
            contract_api_msg.get_raw_transaction.CopyFrom(performative)
        elif performative_id == ContractApiMessage.Performative.GET_RAW_MESSAGE:
            performative = contract_api_pb2.ContractApiMessage.Get_Raw_Message_Performative(
            )  # type: ignore
            ledger_id = msg.ledger_id
            performative.ledger_id = ledger_id
            contract_id = msg.contract_id
            performative.contract_id = contract_id
            contract_address = msg.contract_address
            performative.contract_address = contract_address
            callable = msg.callable
            performative.callable = callable
            kwargs = msg.kwargs
            Kwargs.encode(performative.kwargs, kwargs)
            contract_api_msg.get_raw_message.CopyFrom(performative)
        elif performative_id == ContractApiMessage.Performative.GET_STATE:
            performative = contract_api_pb2.ContractApiMessage.Get_State_Performative(
            )  # type: ignore
            ledger_id = msg.ledger_id
            performative.ledger_id = ledger_id
            contract_id = msg.contract_id
            performative.contract_id = contract_id
            contract_address = msg.contract_address
            performative.contract_address = contract_address
            callable = msg.callable
            performative.callable = callable
            kwargs = msg.kwargs
            Kwargs.encode(performative.kwargs, kwargs)
            contract_api_msg.get_state.CopyFrom(performative)
        elif performative_id == ContractApiMessage.Performative.STATE:
            performative = contract_api_pb2.ContractApiMessage.State_Performative(
            )  # type: ignore
            state = msg.state
            State.encode(performative.state, state)
            contract_api_msg.state.CopyFrom(performative)
        elif performative_id == ContractApiMessage.Performative.RAW_TRANSACTION:
            performative = contract_api_pb2.ContractApiMessage.Raw_Transaction_Performative(
            )  # type: ignore
            raw_transaction = msg.raw_transaction
            RawTransaction.encode(performative.raw_transaction,
                                  raw_transaction)
            contract_api_msg.raw_transaction.CopyFrom(performative)
        elif performative_id == ContractApiMessage.Performative.RAW_MESSAGE:
            performative = contract_api_pb2.ContractApiMessage.Raw_Message_Performative(
            )  # type: ignore
            raw_message = msg.raw_message
            RawMessage.encode(performative.raw_message, raw_message)
            contract_api_msg.raw_message.CopyFrom(performative)
        elif performative_id == ContractApiMessage.Performative.ERROR:
            performative = contract_api_pb2.ContractApiMessage.Error_Performative(
            )  # type: ignore
            if msg.is_set("code"):
                performative.code_is_set = True
                code = msg.code
                performative.code = code
            if msg.is_set("message"):
                performative.message_is_set = True
                message = msg.message
                performative.message = message
            data = msg.data
            performative.data = data
            contract_api_msg.error.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = contract_api_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
    def encode(self, msg: Message) -> bytes:
        """
        Decode the message.

        :param msg: the message object
        :return: the bytes
        """
        msg = cast(TACMessage, msg)
        tac_container = tac_pb2.TACMessage()

        if msg.type == TACMessage.Type.REGISTER:
            agent_name = msg.agent_name
            tac_msg = tac_pb2.TACAgent.Register()  # type: ignore
            tac_msg.agent_name = agent_name
            tac_container.register.CopyFrom(tac_msg)
        elif msg.type == TACMessage.Type.UNREGISTER:
            tac_msg = tac_pb2.TACAgent.Unregister()  # type: ignore
            tac_container.unregister.CopyFrom(tac_msg)
        elif msg.type == TACMessage.Type.TRANSACTION:
            tac_msg = tac_pb2.TACAgent.Transaction()  # type: ignore
            tac_msg.tx_id = msg.tx_id
            tac_msg.tx_sender_addr = msg.tx_sender_addr
            tac_msg.tx_counterparty_addr = msg.tx_counterparty_addr
            tac_msg.amount_by_currency_id.extend(
                _from_dict_to_pairs(msg.amount_by_currency_id))
            tac_msg.tx_sender_fee = msg.tx_sender_fee
            tac_msg.tx_counterparty_fee = msg.tx_counterparty_fee
            tac_msg.quantities_by_good_id.extend(
                _from_dict_to_pairs(msg.quantities_by_good_id))
            tac_msg.tx_nonce = msg.tx_nonce
            tac_msg.tx_sender_signature = msg.tx_sender_signature
            tac_msg.tx_counterparty_signature = msg.tx_counterparty_signature
            tac_container.transaction.CopyFrom(tac_msg)
        elif msg.type == TACMessage.Type.GET_STATE_UPDATE:
            tac_msg = tac_pb2.TACAgent.GetStateUpdate()  # type: ignore
            tac_container.get_state_update.CopyFrom(tac_msg)
        elif msg.type == TACMessage.Type.CANCELLED:
            tac_msg = tac_pb2.TACController.Cancelled()  # type: ignore
            tac_container.cancelled.CopyFrom(tac_msg)
        elif msg.type == TACMessage.Type.GAME_DATA:
            tac_msg = tac_pb2.TACController.GameData()  # type: ignore
            tac_msg.amount_by_currency_id.extend(
                _from_dict_to_pairs(msg.amount_by_currency_id))
            tac_msg.exchange_params_by_currency_id.extend(
                _from_dict_to_pairs(msg.exchange_params_by_currency_id))
            tac_msg.quantities_by_good_id.extend(
                _from_dict_to_pairs(msg.quantities_by_good_id))
            tac_msg.utility_params_by_good_id.extend(
                _from_dict_to_pairs(msg.utility_params_by_good_id))
            tac_msg.tx_fee = msg.tx_fee
            tac_msg.agent_addr_to_name.extend(
                _from_dict_to_pairs(msg.agent_addr_to_name))
            tac_msg.good_id_to_name.extend(
                _from_dict_to_pairs(msg.good_id_to_name))
            tac_msg.version_id = msg.version_id
            tac_container.game_data.CopyFrom(tac_msg)
        elif msg.type == TACMessage.Type.TRANSACTION_CONFIRMATION:
            tac_msg = tac_pb2.TACController.TransactionConfirmation(
            )  # type: ignore
            tac_msg.tx_id = msg.tx_id
            tac_msg.amount_by_currency_id.extend(
                _from_dict_to_pairs(msg.amount_by_currency_id))
            tac_msg.quantities_by_good_id.extend(
                _from_dict_to_pairs(msg.quantities_by_good_id))
            tac_container.transaction_confirmation.CopyFrom(tac_msg)
        # elif tac_type == TACMessage.Type.STATE_UPDATE:
        #     tac_msg = tac_pb2.TACController.StateUpdate()  # type: ignore
        #     game_data_json = msg.get("game_data")
        #     game_data = tac_pb2.TACController.GameData()  # type: ignore
        #     game_data.amount_by_currency_id.extend(_from_dict_to_pairs(cast(Dict[str, str], game_data_json["amount_by_currency_id"])))  # type: ignore
        #     game_data.exchange_params_by_currency_id.extend(_from_dict_to_pairs(cast(Dict[str, str], game_data_json["exchange_params_by_currency_id"])))  # type: ignore
        #     game_data.quantities_by_good_id.extend(_from_dict_to_pairs(cast(Dict[str, str], game_data_json["quantities_by_good_id"])))  # type: ignore
        #     game_data.utility_params_by_good_id.extend(_from_dict_to_pairs(cast(Dict[str, str], game_data_json["utility_params_by_good_id"])))  # type: ignore
        #     game_data.tx_fee = game_data_json["tx_fee"]  # type: ignore
        #     game_data.agent_addr_to_name.extend(_from_dict_to_pairs(cast(Dict[str, str], game_data_json["agent_addr_to_name"])))  # type: ignore
        #     game_data.good_id_to_name.extend(_from_dict_to_pairs(cast(Dict[str, str], game_data_json["good_id_to_name"])))  # type: ignore

        #     tac_msg.initial_state.CopyFrom(game_data)

        #     transactions = []
        #     msg_transactions = cast(List[Any], msg.get("transactions"))
        #     for t in msg_transactions:
        #         tx = tac_pb2.TACAgent.Transaction()  # type: ignore
        #         tx.transaction_id = t.get("transaction_id")
        #         tx.counterparty = t.get("counterparty")
        #         tx.amount_by_currency_id.extend(_from_dict_to_pairs(t.get("amount_by_currency_id")))
        #         tx.sender_tx_fee = t.get("sender_tx_fee")
        #         tx.counterparty_tx_fee = t.get("counterparty_tx_fee")
        #         tx.quantities_by_good_id.extend(_from_dict_to_pairs(t.get("quantities_by_good_id")))
        #         transactions.append(tx)
        #     tac_msg.txs.extend(transactions)
        #     tac_container.state_update.CopyFrom(tac_msg)
        elif msg.type == TACMessage.Type.TAC_ERROR:
            tac_msg = tac_pb2.TACController.Error()  # type: ignore
            tac_msg.error_code = msg.error_code.value
            if msg.is_set("info"):
                tac_msg.info.extend(_from_dict_to_pairs(msg.info))
            tac_container.error.CopyFrom(tac_msg)
        else:  # pragma: no cover
            raise ValueError("Type not recognized: {}.".format(msg.type))

        tac_message_bytes = tac_container.SerializeToString()
        return tac_message_bytes
示例#4
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'Tac' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(TacMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        tac_msg = tac_pb2.TacMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == TacMessage.Performative.REGISTER:
            performative = tac_pb2.TacMessage.Register_Performative(
            )  # type: ignore
            agent_name = msg.agent_name
            performative.agent_name = agent_name
            tac_msg.register.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.UNREGISTER:
            performative = tac_pb2.TacMessage.Unregister_Performative(
            )  # type: ignore
            tac_msg.unregister.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.TRANSACTION:
            performative = tac_pb2.TacMessage.Transaction_Performative(
            )  # type: ignore
            transaction_id = msg.transaction_id
            performative.transaction_id = transaction_id
            ledger_id = msg.ledger_id
            performative.ledger_id = ledger_id
            sender_address = msg.sender_address
            performative.sender_address = sender_address
            counterparty_address = msg.counterparty_address
            performative.counterparty_address = counterparty_address
            amount_by_currency_id = msg.amount_by_currency_id
            performative.amount_by_currency_id.update(amount_by_currency_id)
            fee_by_currency_id = msg.fee_by_currency_id
            performative.fee_by_currency_id.update(fee_by_currency_id)
            quantities_by_good_id = msg.quantities_by_good_id
            performative.quantities_by_good_id.update(quantities_by_good_id)
            nonce = msg.nonce
            performative.nonce = nonce
            sender_signature = msg.sender_signature
            performative.sender_signature = sender_signature
            counterparty_signature = msg.counterparty_signature
            performative.counterparty_signature = counterparty_signature
            tac_msg.transaction.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.CANCELLED:
            performative = tac_pb2.TacMessage.Cancelled_Performative(
            )  # type: ignore
            tac_msg.cancelled.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.GAME_DATA:
            performative = tac_pb2.TacMessage.Game_Data_Performative(
            )  # type: ignore
            amount_by_currency_id = msg.amount_by_currency_id
            performative.amount_by_currency_id.update(amount_by_currency_id)
            exchange_params_by_currency_id = msg.exchange_params_by_currency_id
            performative.exchange_params_by_currency_id.update(
                exchange_params_by_currency_id)
            quantities_by_good_id = msg.quantities_by_good_id
            performative.quantities_by_good_id.update(quantities_by_good_id)
            utility_params_by_good_id = msg.utility_params_by_good_id
            performative.utility_params_by_good_id.update(
                utility_params_by_good_id)
            fee_by_currency_id = msg.fee_by_currency_id
            performative.fee_by_currency_id.update(fee_by_currency_id)
            agent_addr_to_name = msg.agent_addr_to_name
            performative.agent_addr_to_name.update(agent_addr_to_name)
            currency_id_to_name = msg.currency_id_to_name
            performative.currency_id_to_name.update(currency_id_to_name)
            good_id_to_name = msg.good_id_to_name
            performative.good_id_to_name.update(good_id_to_name)
            version_id = msg.version_id
            performative.version_id = version_id
            if msg.is_set("info"):
                performative.info_is_set = True
                info = msg.info
                performative.info.update(info)
            tac_msg.game_data.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.TRANSACTION_CONFIRMATION:
            performative = tac_pb2.TacMessage.Transaction_Confirmation_Performative(
            )  # type: ignore
            transaction_id = msg.transaction_id
            performative.transaction_id = transaction_id
            amount_by_currency_id = msg.amount_by_currency_id
            performative.amount_by_currency_id.update(amount_by_currency_id)
            quantities_by_good_id = msg.quantities_by_good_id
            performative.quantities_by_good_id.update(quantities_by_good_id)
            tac_msg.transaction_confirmation.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.TAC_ERROR:
            performative = tac_pb2.TacMessage.Tac_Error_Performative(
            )  # type: ignore
            error_code = msg.error_code
            ErrorCode.encode(performative.error_code, error_code)
            if msg.is_set("info"):
                performative.info_is_set = True
                info = msg.info
                performative.info.update(info)
            tac_msg.tac_error.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = tac_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
示例#5
0
    def encode(self, msg: Message) -> bytes:
        """
        Decode the message.

        :param msg: the message object
        :return: the bytes
        """
        tac_type = TACMessage.Type(msg.get("type"))
        tac_container = tac_pb2.TACMessage()

        if tac_type == TACMessage.Type.REGISTER:
            agent_name = msg.get("agent_name")
            tac_msg = tac_pb2.TACAgent.Register()  # type: ignore
            tac_msg.agent_name = agent_name
            tac_container.register.CopyFrom(tac_msg)
        elif tac_type == TACMessage.Type.UNREGISTER:
            tac_msg = tac_pb2.TACAgent.Unregister()  # type: ignore
            tac_container.unregister.CopyFrom(tac_msg)
        elif tac_type == TACMessage.Type.TRANSACTION:
            tac_msg = tac_pb2.TACAgent.Transaction()  # type: ignore
            tac_msg.transaction_id = msg.get("transaction_id")
            tac_msg.is_sender_buyer = msg.get("is_sender_buyer")
            tac_msg.counterparty = msg.get("counterparty")
            tac_msg.amount = msg.get("amount")
            tac_msg.quantities.extend(
                _from_dict_to_pairs(msg.get("quantities_by_good_pbk")))
            tac_container.transaction.CopyFrom(tac_msg)
        elif tac_type == TACMessage.Type.GET_STATE_UPDATE:
            tac_msg = tac_pb2.TACAgent.GetStateUpdate()  # type: ignore
            tac_container.get_state_update.CopyFrom(tac_msg)
        elif tac_type == TACMessage.Type.CANCELLED:
            tac_msg = tac_pb2.TACController.Cancelled()  # type: ignore
            tac_container.cancelled.CopyFrom(tac_msg)
        elif tac_type == TACMessage.Type.GAME_DATA:
            tac_msg = tac_pb2.TACController.GameData()  # type: ignore
            tac_msg.money = msg.get("money")
            tac_msg.endowment.extend(msg.get("endowment"))
            tac_msg.utility_params.extend(msg.get("utility_params"))
            tac_msg.nb_agents = msg.get("nb_agents")
            tac_msg.nb_goods = msg.get("nb_goods")
            tac_msg.tx_fee = msg.get("tx_fee")
            tac_msg.agent_pbk_to_name.extend(
                _from_dict_to_pairs(msg.get("agent_pbk_to_name")))
            tac_msg.good_pbk_to_name.extend(
                _from_dict_to_pairs(msg.get("good_pbk_to_name")))
            tac_container.game_data.CopyFrom(tac_msg)
        elif tac_type == TACMessage.Type.TRANSACTION_CONFIRMATION:
            tac_msg = tac_pb2.TACController.TransactionConfirmation(
            )  # type: ignore
            tac_msg.transaction_id = msg.get("transaction_id")
            tac_container.transaction_confirmation.CopyFrom(tac_msg)
        elif tac_type == TACMessage.Type.STATE_UPDATE:
            tac_msg = tac_pb2.TACController.StateUpdate()  # type: ignore
            game_data_json = msg.get("initial_state")
            game_data = tac_pb2.TACController.GameData()  # type: ignore
            game_data.money = game_data_json["money"]  # type: ignore
            game_data.endowment.extend(
                game_data_json["endowment"])  # type: ignore
            game_data.utility_params.extend(
                game_data_json["utility_params"])  # type: ignore
            game_data.nb_agents = game_data_json["nb_agents"]  # type: ignore
            game_data.nb_goods = game_data_json["nb_goods"]  # type: ignore
            game_data.tx_fee = game_data_json["tx_fee"]  # type: ignore
            game_data.agent_pbk_to_name.extend(
                _from_dict_to_pairs(
                    cast(Dict[str, str],
                         game_data_json["agent_pbk_to_name"])))  # type: ignore
            game_data.good_pbk_to_name.extend(
                _from_dict_to_pairs(
                    cast(Dict[str, str],
                         game_data_json["good_pbk_to_name"])))  # type: ignore

            tac_msg.initial_state.CopyFrom(game_data)

            transactions = []
            msg_transactions = cast(List[Any], msg.get("transactions"))
            for t in msg_transactions:
                tx = tac_pb2.TACAgent.Transaction()  # type: ignore
                tx.transaction_id = t.get("transaction_id")
                tx.is_sender_buyer = t.get("is_sender_buyer")
                tx.counterparty = t.get("counterparty")
                tx.amount = t.get("amount")
                tx.quantities.extend(
                    _from_dict_to_pairs(t.get("quantities_by_good_pbk")))
                transactions.append(tx)
            tac_msg.txs.extend(transactions)
            tac_container.state_update.CopyFrom(tac_msg)
        elif tac_type == TACMessage.Type.TAC_ERROR:
            tac_msg = tac_pb2.TACController.Error()  # type: ignore
            tac_msg.error_code = TACMessage.ErrorCode(
                msg.get("error_code")).value
            if msg.is_set("error_msg"):
                tac_msg.error_msg = msg.get("error_msg")
            if msg.is_set("details"):
                tac_msg.details.update(msg.get("details"))

            tac_container.error.CopyFrom(tac_msg)
        else:
            raise ValueError("Type not recognized: {}.".format(tac_type))

        tac_message_bytes = tac_container.SerializeToString()
        return tac_message_bytes
示例#6
0
    def encode(self, msg: Message) -> bytes:
        """
        Encode a 'Tac' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(TacMessage, msg)
        tac_msg = tac_pb2.TacMessage()
        tac_msg.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        tac_msg.dialogue_starter_reference = dialogue_reference[0]
        tac_msg.dialogue_responder_reference = dialogue_reference[1]
        tac_msg.target = msg.target

        performative_id = msg.performative
        if performative_id == TacMessage.Performative.REGISTER:
            performative = tac_pb2.TacMessage.Register_Performative(
            )  # type: ignore
            agent_name = msg.agent_name
            performative.agent_name = agent_name
            tac_msg.register.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.UNREGISTER:
            performative = tac_pb2.TacMessage.Unregister_Performative(
            )  # type: ignore
            tac_msg.unregister.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.TRANSACTION:
            performative = tac_pb2.TacMessage.Transaction_Performative(
            )  # type: ignore
            tx_id = msg.tx_id
            performative.tx_id = tx_id
            tx_sender_addr = msg.tx_sender_addr
            performative.tx_sender_addr = tx_sender_addr
            tx_counterparty_addr = msg.tx_counterparty_addr
            performative.tx_counterparty_addr = tx_counterparty_addr
            amount_by_currency_id = msg.amount_by_currency_id
            performative.amount_by_currency_id.update(amount_by_currency_id)
            tx_sender_fee = msg.tx_sender_fee
            performative.tx_sender_fee = tx_sender_fee
            tx_counterparty_fee = msg.tx_counterparty_fee
            performative.tx_counterparty_fee = tx_counterparty_fee
            quantities_by_good_id = msg.quantities_by_good_id
            performative.quantities_by_good_id.update(quantities_by_good_id)
            tx_nonce = msg.tx_nonce
            performative.tx_nonce = tx_nonce
            tx_sender_signature = msg.tx_sender_signature
            performative.tx_sender_signature = tx_sender_signature
            tx_counterparty_signature = msg.tx_counterparty_signature
            performative.tx_counterparty_signature = tx_counterparty_signature
            tac_msg.transaction.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.GET_STATE_UPDATE:
            performative = tac_pb2.TacMessage.Get_State_Update_Performative(
            )  # type: ignore
            tac_msg.get_state_update.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.CANCELLED:
            performative = tac_pb2.TacMessage.Cancelled_Performative(
            )  # type: ignore
            tac_msg.cancelled.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.GAME_DATA:
            performative = tac_pb2.TacMessage.Game_Data_Performative(
            )  # type: ignore
            amount_by_currency_id = msg.amount_by_currency_id
            performative.amount_by_currency_id.update(amount_by_currency_id)
            exchange_params_by_currency_id = msg.exchange_params_by_currency_id
            performative.exchange_params_by_currency_id.update(
                exchange_params_by_currency_id)
            quantities_by_good_id = msg.quantities_by_good_id
            performative.quantities_by_good_id.update(quantities_by_good_id)
            utility_params_by_good_id = msg.utility_params_by_good_id
            performative.utility_params_by_good_id.update(
                utility_params_by_good_id)
            tx_fee = msg.tx_fee
            performative.tx_fee = tx_fee
            agent_addr_to_name = msg.agent_addr_to_name
            performative.agent_addr_to_name.update(agent_addr_to_name)
            currency_id_to_name = msg.currency_id_to_name
            performative.currency_id_to_name.update(currency_id_to_name)
            good_id_to_name = msg.good_id_to_name
            performative.good_id_to_name.update(good_id_to_name)
            version_id = msg.version_id
            performative.version_id = version_id
            if msg.is_set("info"):
                performative.info_is_set = True
                info = msg.info
                performative.info.update(info)
            tac_msg.game_data.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.TRANSACTION_CONFIRMATION:
            performative = tac_pb2.TacMessage.Transaction_Confirmation_Performative(
            )  # type: ignore
            tx_id = msg.tx_id
            performative.tx_id = tx_id
            amount_by_currency_id = msg.amount_by_currency_id
            performative.amount_by_currency_id.update(amount_by_currency_id)
            quantities_by_good_id = msg.quantities_by_good_id
            performative.quantities_by_good_id.update(quantities_by_good_id)
            tac_msg.transaction_confirmation.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.TAC_ERROR:
            performative = tac_pb2.TacMessage.Tac_Error_Performative(
            )  # type: ignore
            error_code = msg.error_code
            ErrorCode.encode(performative.error_code, error_code)
            if msg.is_set("info"):
                performative.info_is_set = True
                info = msg.info
                performative.info.update(info)
            tac_msg.tac_error.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        tac_bytes = tac_msg.SerializeToString()
        return tac_bytes
示例#7
0
    def encode(self, msg: Message) -> bytes:
        """
        Encode a 'TwoPartyNegotiation' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(TwoPartyNegotiationMessage, msg)
        two_party_negotiation_msg = (
            two_party_negotiation_pb2.TwoPartyNegotiationMessage()
        )
        two_party_negotiation_msg.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        two_party_negotiation_msg.dialogue_starter_reference = dialogue_reference[0]
        two_party_negotiation_msg.dialogue_responder_reference = dialogue_reference[1]
        two_party_negotiation_msg.target = msg.target

        performative_id = msg.performative
        if performative_id == TwoPartyNegotiationMessage.Performative.CFP:
            performative = two_party_negotiation_pb2.TwoPartyNegotiationMessage.Cfp()  # type: ignore
            query = msg.query
            performative.query = query
            two_party_negotiation_msg.cfp.CopyFrom(performative)
        elif performative_id == TwoPartyNegotiationMessage.Performative.PROPOSE:
            performative = two_party_negotiation_pb2.TwoPartyNegotiationMessage.Propose()  # type: ignore
            number = msg.number
            performative.number = number
            price = msg.price
            performative.price = price
            description = msg.description
            performative.description = description
            flag = msg.flag
            performative.flag = flag
            query = msg.query
            performative.query = query
            if msg.is_set("proposal"):
                proposal = msg.proposal
                performative.proposal.update(proposal)
            rounds = msg.rounds
            performative.rounds.extend(rounds)
            items = msg.items
            performative.items.extend(items)
            if msg.is_set("conditions_type_str"):
                conditions_type_str = msg.conditions_type_str
                performative.conditions_type_str = conditions_type_str
            if msg.is_set("conditions_type_dict_of_str_int"):
                conditions_type_dict_of_str_int = msg.conditions_type_dict_of_str_int
                performative.conditions_type_dict_of_str_int.update(
                    conditions_type_dict_of_str_int
                )
            if msg.is_set("conditions_type_set_of_DataModel"):
                conditions_type_set_of_DataModel = msg.conditions_type_set_of_DataModel
                performative.conditions_type_set_of_DataModel.extend(
                    conditions_type_set_of_DataModel
                )
            if msg.is_set("conditions_type_dict_of_str_float"):
                conditions_type_dict_of_str_float = (
                    msg.conditions_type_dict_of_str_float
                )
                performative.conditions_type_dict_of_str_float.update(
                    conditions_type_dict_of_str_float
                )
            two_party_negotiation_msg.propose.CopyFrom(performative)
        elif performative_id == TwoPartyNegotiationMessage.Performative.ACCEPT:
            performative = two_party_negotiation_pb2.TwoPartyNegotiationMessage.Accept()  # type: ignore
            two_party_negotiation_msg.accept.CopyFrom(performative)
        elif performative_id == TwoPartyNegotiationMessage.Performative.INFORM:
            performative = two_party_negotiation_pb2.TwoPartyNegotiationMessage.Inform()  # type: ignore
            inform_number = msg.inform_number
            performative.inform_number.extend(inform_number)
            two_party_negotiation_msg.inform.CopyFrom(performative)
        elif performative_id == TwoPartyNegotiationMessage.Performative.INFORM_REPLY:
            performative = two_party_negotiation_pb2.TwoPartyNegotiationMessage.Inform_Reply()  # type: ignore
            reply_message = msg.reply_message
            performative.reply_message.update(reply_message)
            two_party_negotiation_msg.inform_reply.CopyFrom(performative)
        elif performative_id == TwoPartyNegotiationMessage.Performative.DECLINE:
            performative = two_party_negotiation_pb2.TwoPartyNegotiationMessage.Decline()  # type: ignore
            two_party_negotiation_msg.decline.CopyFrom(performative)
        elif performative_id == TwoPartyNegotiationMessage.Performative.MATCH_ACCEPT:
            performative = two_party_negotiation_pb2.TwoPartyNegotiationMessage.Match_Accept()  # type: ignore
            two_party_negotiation_msg.match_accept.CopyFrom(performative)
        else:
            raise ValueError("Performative not valid: {}".format(performative_id))

        two_party_negotiation_bytes = two_party_negotiation_msg.SerializeToString()
        return two_party_negotiation_bytes
    def encode(msg: Message) -> bytes:
        """
        Encode a 'Prometheus' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(PrometheusMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        prometheus_msg = prometheus_pb2.PrometheusMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == PrometheusMessage.Performative.ADD_METRIC:
            performative = prometheus_pb2.PrometheusMessage.Add_Metric_Performative(
            )  # type: ignore
            type = msg.type
            performative.type = type
            title = msg.title
            performative.title = title
            description = msg.description
            performative.description = description
            labels = msg.labels
            performative.labels.update(labels)
            prometheus_msg.add_metric.CopyFrom(performative)
        elif performative_id == PrometheusMessage.Performative.UPDATE_METRIC:
            performative = prometheus_pb2.PrometheusMessage.Update_Metric_Performative(
            )  # type: ignore
            title = msg.title
            performative.title = title
            callable = msg.callable
            performative.callable = callable
            value = msg.value
            performative.value = value
            labels = msg.labels
            performative.labels.update(labels)
            prometheus_msg.update_metric.CopyFrom(performative)
        elif performative_id == PrometheusMessage.Performative.RESPONSE:
            performative = prometheus_pb2.PrometheusMessage.Response_Performative(
            )  # type: ignore
            code = msg.code
            performative.code = code
            if msg.is_set("message"):
                performative.message_is_set = True
                message = msg.message
                performative.message = message
            prometheus_msg.response.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = prometheus_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
示例#9
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'LedgerApi' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(LedgerApiMessage, msg)
        ledger_api_msg = ledger_api_pb2.LedgerApiMessage()
        ledger_api_msg.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        ledger_api_msg.dialogue_starter_reference = dialogue_reference[0]
        ledger_api_msg.dialogue_responder_reference = dialogue_reference[1]
        ledger_api_msg.target = msg.target

        performative_id = msg.performative
        if performative_id == LedgerApiMessage.Performative.GET_BALANCE:
            performative = ledger_api_pb2.LedgerApiMessage.Get_Balance_Performative(
            )  # type: ignore
            ledger_id = msg.ledger_id
            performative.ledger_id = ledger_id
            address = msg.address
            performative.address = address
            ledger_api_msg.get_balance.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.GET_RAW_TRANSACTION:
            performative = ledger_api_pb2.LedgerApiMessage.Get_Raw_Transaction_Performative(
            )  # type: ignore
            terms = msg.terms
            Terms.encode(performative.terms, terms)
            ledger_api_msg.get_raw_transaction.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.SEND_SIGNED_TRANSACTION:
            performative = ledger_api_pb2.LedgerApiMessage.Send_Signed_Transaction_Performative(
            )  # type: ignore
            signed_transaction = msg.signed_transaction
            SignedTransaction.encode(performative.signed_transaction,
                                     signed_transaction)
            ledger_api_msg.send_signed_transaction.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.GET_TRANSACTION_RECEIPT:
            performative = ledger_api_pb2.LedgerApiMessage.Get_Transaction_Receipt_Performative(
            )  # type: ignore
            transaction_digest = msg.transaction_digest
            TransactionDigest.encode(performative.transaction_digest,
                                     transaction_digest)
            ledger_api_msg.get_transaction_receipt.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.BALANCE:
            performative = ledger_api_pb2.LedgerApiMessage.Balance_Performative(
            )  # type: ignore
            ledger_id = msg.ledger_id
            performative.ledger_id = ledger_id
            balance = msg.balance
            performative.balance = balance
            ledger_api_msg.balance.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.RAW_TRANSACTION:
            performative = ledger_api_pb2.LedgerApiMessage.Raw_Transaction_Performative(
            )  # type: ignore
            raw_transaction = msg.raw_transaction
            RawTransaction.encode(performative.raw_transaction,
                                  raw_transaction)
            ledger_api_msg.raw_transaction.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.TRANSACTION_DIGEST:
            performative = ledger_api_pb2.LedgerApiMessage.Transaction_Digest_Performative(
            )  # type: ignore
            transaction_digest = msg.transaction_digest
            TransactionDigest.encode(performative.transaction_digest,
                                     transaction_digest)
            ledger_api_msg.transaction_digest.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.TRANSACTION_RECEIPT:
            performative = ledger_api_pb2.LedgerApiMessage.Transaction_Receipt_Performative(
            )  # type: ignore
            transaction_receipt = msg.transaction_receipt
            TransactionReceipt.encode(performative.transaction_receipt,
                                      transaction_receipt)
            ledger_api_msg.transaction_receipt.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.ERROR:
            performative = ledger_api_pb2.LedgerApiMessage.Error_Performative(
            )  # type: ignore
            code = msg.code
            performative.code = code
            if msg.is_set("message"):
                performative.message_is_set = True
                message = msg.message
                performative.message = message
            if msg.is_set("data"):
                performative.data_is_set = True
                data = msg.data
                performative.data = data
            ledger_api_msg.error.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        ledger_api_bytes = ledger_api_msg.SerializeToString()
        return ledger_api_bytes