Exemple #1
0
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a message using Protobuf.

        First, try to parse the input as a Protobuf 'Message';
        if it fails, parse the bytes as struct.
        """
        message_pb = ProtobufMessage()
        message_pb.ParseFromString(obj)
        message_type = message_pb.WhichOneof("message")
        if message_type == "body":
            body = dict(message_pb.body)  # pylint: disable=no-member
            msg = TMessage(_body=body)
            return msg
        if message_type == "dialogue_message":
            dialogue_message_pb = (
                message_pb.dialogue_message  # pylint: disable=no-member
            )
            message_id = dialogue_message_pb.message_id
            target = dialogue_message_pb.target
            dialogue_starter_reference = dialogue_message_pb.dialogue_starter_reference
            dialogue_responder_reference = (
                dialogue_message_pb.dialogue_responder_reference
            )
            body_json = Struct()
            body_json.ParseFromString(dialogue_message_pb.content)
            body = dict(body_json)
            body["message_id"] = message_id
            body["target"] = target
            body["dialogue_reference"] = (
                dialogue_starter_reference,
                dialogue_responder_reference,
            )
            return TMessage(_body=body)
        raise ValueError("Message type not recognized.")  # pragma: nocover
Exemple #2
0
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'StateUpdate' message.

        :param obj: the bytes object.
        :return: the 'StateUpdate' message.
        """
        message_pb = ProtobufMessage()
        state_update_pb = state_update_pb2.StateUpdateMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        state_update_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = state_update_pb.WhichOneof("performative")
        performative_id = StateUpdateMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == StateUpdateMessage.Performative.INITIALIZE:
            exchange_params_by_currency_id = (
                state_update_pb.initialize.exchange_params_by_currency_id)
            exchange_params_by_currency_id_dict = dict(
                exchange_params_by_currency_id)
            performative_content[
                "exchange_params_by_currency_id"] = exchange_params_by_currency_id_dict
            utility_params_by_good_id = (
                state_update_pb.initialize.utility_params_by_good_id)
            utility_params_by_good_id_dict = dict(utility_params_by_good_id)
            performative_content[
                "utility_params_by_good_id"] = utility_params_by_good_id_dict
            amount_by_currency_id = state_update_pb.initialize.amount_by_currency_id
            amount_by_currency_id_dict = dict(amount_by_currency_id)
            performative_content[
                "amount_by_currency_id"] = amount_by_currency_id_dict
            quantities_by_good_id = state_update_pb.initialize.quantities_by_good_id
            quantities_by_good_id_dict = dict(quantities_by_good_id)
            performative_content[
                "quantities_by_good_id"] = quantities_by_good_id_dict
        elif performative_id == StateUpdateMessage.Performative.APPLY:
            amount_by_currency_id = state_update_pb.apply.amount_by_currency_id
            amount_by_currency_id_dict = dict(amount_by_currency_id)
            performative_content[
                "amount_by_currency_id"] = amount_by_currency_id_dict
            quantities_by_good_id = state_update_pb.apply.quantities_by_good_id
            quantities_by_good_id_dict = dict(quantities_by_good_id)
            performative_content[
                "quantities_by_good_id"] = quantities_by_good_id_dict
        else:
            raise ValueError(
                "Performative not valid: {}.".format(performative_id))

        return StateUpdateMessage(message_id=message_id,
                                  dialogue_reference=dialogue_reference,
                                  target=target,
                                  performative=performative,
                                  **performative_content)
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'OefSearch' message.

        :param obj: the bytes object.
        :return: the 'OefSearch' message.
        """
        message_pb = ProtobufMessage()
        oef_search_pb = oef_search_pb2.OefSearchMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        oef_search_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = oef_search_pb.WhichOneof("performative")
        performative_id = OefSearchMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == OefSearchMessage.Performative.REGISTER_SERVICE:
            pb2_service_description = oef_search_pb.register_service.service_description
            service_description = Description.decode(pb2_service_description)
            performative_content["service_description"] = service_description
        elif performative_id == OefSearchMessage.Performative.UNREGISTER_SERVICE:
            pb2_service_description = (
                oef_search_pb.unregister_service.service_description)
            service_description = Description.decode(pb2_service_description)
            performative_content["service_description"] = service_description
        elif performative_id == OefSearchMessage.Performative.SEARCH_SERVICES:
            pb2_query = oef_search_pb.search_services.query
            query = Query.decode(pb2_query)
            performative_content["query"] = query
        elif performative_id == OefSearchMessage.Performative.SEARCH_RESULT:
            agents = oef_search_pb.search_result.agents
            agents_tuple = tuple(agents)
            performative_content["agents"] = agents_tuple
            pb2_agents_info = oef_search_pb.search_result.agents_info
            agents_info = AgentsInfo.decode(pb2_agents_info)
            performative_content["agents_info"] = agents_info
        elif performative_id == OefSearchMessage.Performative.SUCCESS:
            pb2_agents_info = oef_search_pb.success.agents_info
            agents_info = AgentsInfo.decode(pb2_agents_info)
            performative_content["agents_info"] = agents_info
        elif performative_id == OefSearchMessage.Performative.OEF_ERROR:
            pb2_oef_error_operation = oef_search_pb.oef_error.oef_error_operation
            oef_error_operation = OefErrorOperation.decode(
                pb2_oef_error_operation)
            performative_content["oef_error_operation"] = oef_error_operation
        else:
            raise ValueError(
                "Performative not valid: {}.".format(performative_id))

        return OefSearchMessage(message_id=message_id,
                                dialogue_reference=dialogue_reference,
                                target=target,
                                performative=performative,
                                **performative_content)
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'Fipa' message.

        :param obj: the bytes object.
        :return: the 'Fipa' message.
        """
        message_pb = ProtobufMessage()
        fipa_pb = fipa_pb2.FipaMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        fipa_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = fipa_pb.WhichOneof("performative")
        performative_id = FipaMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == FipaMessage.Performative.CFP:
            pb2_query = fipa_pb.cfp.query
            query = Query.decode(pb2_query)
            performative_content["query"] = query
        elif performative_id == FipaMessage.Performative.PROPOSE:
            pb2_proposal = fipa_pb.propose.proposal
            proposal = Description.decode(pb2_proposal)
            performative_content["proposal"] = proposal
        elif performative_id == FipaMessage.Performative.ACCEPT_W_INFORM:
            info = fipa_pb.accept_w_inform.info
            info_dict = dict(info)
            performative_content["info"] = info_dict
        elif performative_id == FipaMessage.Performative.MATCH_ACCEPT_W_INFORM:
            info = fipa_pb.match_accept_w_inform.info
            info_dict = dict(info)
            performative_content["info"] = info_dict
        elif performative_id == FipaMessage.Performative.INFORM:
            info = fipa_pb.inform.info
            info_dict = dict(info)
            performative_content["info"] = info_dict
        elif performative_id == FipaMessage.Performative.ACCEPT:
            pass
        elif performative_id == FipaMessage.Performative.DECLINE:
            pass
        elif performative_id == FipaMessage.Performative.MATCH_ACCEPT:
            pass
        elif performative_id == FipaMessage.Performative.END:
            pass
        else:
            raise ValueError("Performative not valid: {}.".format(performative_id))

        return FipaMessage(
            message_id=message_id,
            dialogue_reference=dialogue_reference,
            target=target,
            performative=performative,
            **performative_content
        )
Exemple #5
0
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'Gym' message.

        :param obj: the bytes object.
        :return: the 'Gym' message.
        """
        message_pb = ProtobufMessage()
        gym_pb = gym_pb2.GymMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        gym_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = gym_pb.WhichOneof("performative")
        performative_id = GymMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == GymMessage.Performative.ACT:
            pb2_action = gym_pb.act.action
            action = AnyObject.decode(pb2_action)
            performative_content["action"] = action
            step_id = gym_pb.act.step_id
            performative_content["step_id"] = step_id
        elif performative_id == GymMessage.Performative.PERCEPT:
            step_id = gym_pb.percept.step_id
            performative_content["step_id"] = step_id
            pb2_observation = gym_pb.percept.observation
            observation = AnyObject.decode(pb2_observation)
            performative_content["observation"] = observation
            reward = gym_pb.percept.reward
            performative_content["reward"] = reward
            done = gym_pb.percept.done
            performative_content["done"] = done
            pb2_info = gym_pb.percept.info
            info = AnyObject.decode(pb2_info)
            performative_content["info"] = info
        elif performative_id == GymMessage.Performative.STATUS:
            content = gym_pb.status.content
            content_dict = dict(content)
            performative_content["content"] = content_dict
        elif performative_id == GymMessage.Performative.RESET:
            pass
        elif performative_id == GymMessage.Performative.CLOSE:
            pass
        else:
            raise ValueError(
                "Performative not valid: {}.".format(performative_id))

        return GymMessage(message_id=message_id,
                          dialogue_reference=dialogue_reference,
                          target=target,
                          performative=performative,
                          **performative_content)
Exemple #6
0
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'Signing' message.

        :param obj: the bytes object.
        :return: the 'Signing' message.
        """
        message_pb = ProtobufMessage()
        signing_pb = signing_pb2.SigningMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        signing_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = signing_pb.WhichOneof("performative")
        performative_id = SigningMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == SigningMessage.Performative.SIGN_TRANSACTION:
            pb2_terms = signing_pb.sign_transaction.terms
            terms = Terms.decode(pb2_terms)
            performative_content["terms"] = terms
            pb2_raw_transaction = signing_pb.sign_transaction.raw_transaction
            raw_transaction = RawTransaction.decode(pb2_raw_transaction)
            performative_content["raw_transaction"] = raw_transaction
        elif performative_id == SigningMessage.Performative.SIGN_MESSAGE:
            pb2_terms = signing_pb.sign_message.terms
            terms = Terms.decode(pb2_terms)
            performative_content["terms"] = terms
            pb2_raw_message = signing_pb.sign_message.raw_message
            raw_message = RawMessage.decode(pb2_raw_message)
            performative_content["raw_message"] = raw_message
        elif performative_id == SigningMessage.Performative.SIGNED_TRANSACTION:
            pb2_signed_transaction = signing_pb.signed_transaction.signed_transaction
            signed_transaction = SignedTransaction.decode(
                pb2_signed_transaction)
            performative_content["signed_transaction"] = signed_transaction
        elif performative_id == SigningMessage.Performative.SIGNED_MESSAGE:
            pb2_signed_message = signing_pb.signed_message.signed_message
            signed_message = SignedMessage.decode(pb2_signed_message)
            performative_content["signed_message"] = signed_message
        elif performative_id == SigningMessage.Performative.ERROR:
            pb2_error_code = signing_pb.error.error_code
            error_code = ErrorCode.decode(pb2_error_code)
            performative_content["error_code"] = error_code
        else:
            raise ValueError(
                "Performative not valid: {}.".format(performative_id))

        return SigningMessage(message_id=message_id,
                              dialogue_reference=dialogue_reference,
                              target=target,
                              performative=performative,
                              **performative_content)
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'Prometheus' message.

        :param obj: the bytes object.
        :return: the 'Prometheus' message.
        """
        message_pb = ProtobufMessage()
        prometheus_pb = prometheus_pb2.PrometheusMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        prometheus_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = prometheus_pb.WhichOneof("performative")
        performative_id = PrometheusMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == PrometheusMessage.Performative.ADD_METRIC:
            type = prometheus_pb.add_metric.type
            performative_content["type"] = type
            title = prometheus_pb.add_metric.title
            performative_content["title"] = title
            description = prometheus_pb.add_metric.description
            performative_content["description"] = description
            labels = prometheus_pb.add_metric.labels
            labels_dict = dict(labels)
            performative_content["labels"] = labels_dict
        elif performative_id == PrometheusMessage.Performative.UPDATE_METRIC:
            title = prometheus_pb.update_metric.title
            performative_content["title"] = title
            callable = prometheus_pb.update_metric.callable
            performative_content["callable"] = callable
            value = prometheus_pb.update_metric.value
            performative_content["value"] = value
            labels = prometheus_pb.update_metric.labels
            labels_dict = dict(labels)
            performative_content["labels"] = labels_dict
        elif performative_id == PrometheusMessage.Performative.RESPONSE:
            code = prometheus_pb.response.code
            performative_content["code"] = code
            if prometheus_pb.response.message_is_set:
                message = prometheus_pb.response.message
                performative_content["message"] = message
        else:
            raise ValueError(
                "Performative not valid: {}.".format(performative_id))

        return PrometheusMessage(message_id=message_id,
                                 dialogue_reference=dialogue_reference,
                                 target=target,
                                 performative=performative,
                                 **performative_content)
Exemple #8
0
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'Http' message.

        :param obj: the bytes object.
        :return: the 'Http' message.
        """
        message_pb = ProtobufMessage()
        http_pb = http_pb2.HttpMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        http_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = http_pb.WhichOneof("performative")
        performative_id = HttpMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == HttpMessage.Performative.REQUEST:
            method = http_pb.request.method
            performative_content["method"] = method
            url = http_pb.request.url
            performative_content["url"] = url
            version = http_pb.request.version
            performative_content["version"] = version
            headers = http_pb.request.headers
            performative_content["headers"] = headers
            body = http_pb.request.body
            performative_content["body"] = body
        elif performative_id == HttpMessage.Performative.RESPONSE:
            version = http_pb.response.version
            performative_content["version"] = version
            status_code = http_pb.response.status_code
            performative_content["status_code"] = status_code
            status_text = http_pb.response.status_text
            performative_content["status_text"] = status_text
            headers = http_pb.response.headers
            performative_content["headers"] = headers
            body = http_pb.response.body
            performative_content["body"] = body
        else:
            raise ValueError("Performative not valid: {}.".format(performative_id))

        return HttpMessage(
            message_id=message_id,
            dialogue_reference=dialogue_reference,
            target=target,
            performative=performative,
            **performative_content
        )
Exemple #9
0
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'AgentEnvironment' message.

        :param obj: the bytes object.
        :return: the 'AgentEnvironment' message.
        """
        message_pb = ProtobufMessage()
        agent_environment_pb = agent_environment_pb2.AgentEnvironmentMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        agent_environment_pb.ParseFromString(
            message_pb.dialogue_message.content)
        performative = agent_environment_pb.WhichOneof("performative")
        performative_id = AgentEnvironmentMessage.Performative(
            str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == AgentEnvironmentMessage.Performative.TICK:
            tile_water = agent_environment_pb.tick.tile_water
            performative_content["tile_water"] = tile_water
            turn_number = agent_environment_pb.tick.turn_number
            performative_content["turn_number"] = turn_number
            agent_water = agent_environment_pb.tick.agent_water
            performative_content["agent_water"] = agent_water
            north_neighbour_id = agent_environment_pb.tick.north_neighbour_id
            performative_content["north_neighbour_id"] = north_neighbour_id
            east_neighbour_id = agent_environment_pb.tick.east_neighbour_id
            performative_content["east_neighbour_id"] = east_neighbour_id
            south_neighbour_id = agent_environment_pb.tick.south_neighbour_id
            performative_content["south_neighbour_id"] = south_neighbour_id
            west_neighbour_id = agent_environment_pb.tick.west_neighbour_id
            performative_content["west_neighbour_id"] = west_neighbour_id
            movement_last_turn = agent_environment_pb.tick.movement_last_turn
            performative_content["movement_last_turn"] = movement_last_turn
        elif performative_id == AgentEnvironmentMessage.Performative.ACTION:
            command = agent_environment_pb.action.command
            performative_content["command"] = command
        else:
            raise ValueError(
                "Performative not valid: {}.".format(performative_id))

        return AgentEnvironmentMessage(message_id=message_id,
                                       dialogue_reference=dialogue_reference,
                                       target=target,
                                       performative=performative,
                                       **performative_content)
Exemple #10
0
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'MlTrade' message.

        :param obj: the bytes object.
        :return: the 'MlTrade' message.
        """
        message_pb = ProtobufMessage()
        ml_trade_pb = ml_trade_pb2.MlTradeMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        ml_trade_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = ml_trade_pb.WhichOneof("performative")
        performative_id = MlTradeMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == MlTradeMessage.Performative.CFP:
            pb2_query = ml_trade_pb.cfp.query
            query = Query.decode(pb2_query)
            performative_content["query"] = query
        elif performative_id == MlTradeMessage.Performative.TERMS:
            pb2_terms = ml_trade_pb.terms.terms
            terms = Description.decode(pb2_terms)
            performative_content["terms"] = terms
        elif performative_id == MlTradeMessage.Performative.ACCEPT:
            pb2_terms = ml_trade_pb.accept.terms
            terms = Description.decode(pb2_terms)
            performative_content["terms"] = terms
            tx_digest = ml_trade_pb.accept.tx_digest
            performative_content["tx_digest"] = tx_digest
        elif performative_id == MlTradeMessage.Performative.DATA:
            pb2_terms = ml_trade_pb.data.terms
            terms = Description.decode(pb2_terms)
            performative_content["terms"] = terms
            payload = ml_trade_pb.data.payload
            performative_content["payload"] = payload
        else:
            raise ValueError(
                "Performative not valid: {}.".format(performative_id))

        return MlTradeMessage(message_id=message_id,
                              dialogue_reference=dialogue_reference,
                              target=target,
                              performative=performative,
                              **performative_content)
Exemple #11
0
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'Yoti' message.

        :param obj: the bytes object.
        :return: the 'Yoti' message.
        """
        message_pb = ProtobufMessage()
        yoti_pb = yoti_pb2.YotiMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        yoti_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = yoti_pb.WhichOneof("performative")
        performative_id = YotiMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == YotiMessage.Performative.GET_PROFILE:
            token = yoti_pb.get_profile.token
            performative_content["token"] = token
            dotted_path = yoti_pb.get_profile.dotted_path
            performative_content["dotted_path"] = dotted_path
            args = yoti_pb.get_profile.args
            args_tuple = tuple(args)
            performative_content["args"] = args_tuple
        elif performative_id == YotiMessage.Performative.PROFILE:
            info = yoti_pb.profile.info
            info_dict = dict(info)
            performative_content["info"] = info_dict
        elif performative_id == YotiMessage.Performative.ERROR:
            error_code = yoti_pb.error.error_code
            performative_content["error_code"] = error_code
            error_msg = yoti_pb.error.error_msg
            performative_content["error_msg"] = error_msg
        else:
            raise ValueError("Performative not valid: {}.".format(performative_id))

        return YotiMessage(
            message_id=message_id,
            dialogue_reference=dialogue_reference,
            target=target,
            performative=performative,
            **performative_content
        )
Exemple #12
0
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'Register' message.

        :param obj: the bytes object.
        :return: the 'Register' message.
        """
        message_pb = ProtobufMessage()
        register_pb = register_pb2.RegisterMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        register_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = register_pb.WhichOneof("performative")
        performative_id = RegisterMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == RegisterMessage.Performative.REGISTER:
            info = register_pb.register.info
            info_dict = dict(info)
            performative_content["info"] = info_dict
        elif performative_id == RegisterMessage.Performative.SUCCESS:
            info = register_pb.success.info
            info_dict = dict(info)
            performative_content["info"] = info_dict
        elif performative_id == RegisterMessage.Performative.ERROR:
            error_code = register_pb.error.error_code
            performative_content["error_code"] = error_code
            error_msg = register_pb.error.error_msg
            performative_content["error_msg"] = error_msg
            info = register_pb.error.info
            info_dict = dict(info)
            performative_content["info"] = info_dict
        else:
            raise ValueError("Performative not valid: {}.".format(performative_id))

        return RegisterMessage(
            message_id=message_id,
            dialogue_reference=dialogue_reference,
            target=target,
            performative=performative,
            **performative_content
        )
Exemple #13
0
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'Default' message.

        :param obj: the bytes object.
        :return: the 'Default' message.
        """
        message_pb = ProtobufMessage()
        default_pb = default_pb2.DefaultMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        default_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = default_pb.WhichOneof("performative")
        performative_id = DefaultMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == DefaultMessage.Performative.BYTES:
            content = default_pb.bytes.content
            performative_content["content"] = content
        elif performative_id == DefaultMessage.Performative.ERROR:
            pb2_error_code = default_pb.error.error_code
            error_code = ErrorCode.decode(pb2_error_code)
            performative_content["error_code"] = error_code
            error_msg = default_pb.error.error_msg
            performative_content["error_msg"] = error_msg
            error_data = default_pb.error.error_data
            error_data_dict = dict(error_data)
            performative_content["error_data"] = error_data_dict
        elif performative_id == DefaultMessage.Performative.END:
            pass
        else:
            raise ValueError(
                "Performative not valid: {}.".format(performative_id))

        return DefaultMessage(message_id=message_id,
                              dialogue_reference=dialogue_reference,
                              target=target,
                              performative=performative,
                              **performative_content)
Exemple #14
0
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'AgentAgent' message.

        :param obj: the bytes object.
        :return: the 'AgentAgent' message.
        """
        message_pb = ProtobufMessage()
        agent_agent_pb = agent_agent_pb2.AgentAgentMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        agent_agent_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = agent_agent_pb.WhichOneof("performative")
        performative_id = AgentAgentMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == AgentAgentMessage.Performative.SENDER_REQUEST:
            request = agent_agent_pb.sender_request.request
            performative_content["request"] = request
            turn_number = agent_agent_pb.sender_request.turn_number
            performative_content["turn_number"] = turn_number
        elif performative_id == AgentAgentMessage.Performative.RECEIVER_REPLY:
            reply = agent_agent_pb.receiver_reply.reply
            performative_content["reply"] = reply
        else:
            raise ValueError(
                "Performative not valid: {}.".format(performative_id))

        return AgentAgentMessage(message_id=message_id,
                                 dialogue_reference=dialogue_reference,
                                 target=target,
                                 performative=performative,
                                 **performative_content)
Exemple #15
0
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'TProtocol' message.

        :param obj: the bytes object.
        :return: the 'TProtocol' message.
        """
        message_pb = ProtobufMessage()
        t_protocol_pb = t_protocol_pb2.TProtocolMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        t_protocol_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = t_protocol_pb.WhichOneof("performative")
        performative_id = TProtocolMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == TProtocolMessage.Performative.PERFORMATIVE_CT:
            pb2_content_ct = t_protocol_pb.performative_ct.content_ct
            content_ct = DataModel.decode(pb2_content_ct)
            performative_content["content_ct"] = content_ct
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_PT:
            content_bytes = t_protocol_pb.performative_pt.content_bytes
            performative_content["content_bytes"] = content_bytes
            content_int = t_protocol_pb.performative_pt.content_int
            performative_content["content_int"] = content_int
            content_float = t_protocol_pb.performative_pt.content_float
            performative_content["content_float"] = content_float
            content_bool = t_protocol_pb.performative_pt.content_bool
            performative_content["content_bool"] = content_bool
            content_str = t_protocol_pb.performative_pt.content_str
            performative_content["content_str"] = content_str
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_PCT:
            content_set_bytes = t_protocol_pb.performative_pct.content_set_bytes
            content_set_bytes_frozenset = frozenset(content_set_bytes)
            performative_content[
                "content_set_bytes"] = content_set_bytes_frozenset
            content_set_int = t_protocol_pb.performative_pct.content_set_int
            content_set_int_frozenset = frozenset(content_set_int)
            performative_content["content_set_int"] = content_set_int_frozenset
            content_set_float = t_protocol_pb.performative_pct.content_set_float
            content_set_float_frozenset = frozenset(content_set_float)
            performative_content[
                "content_set_float"] = content_set_float_frozenset
            content_set_bool = t_protocol_pb.performative_pct.content_set_bool
            content_set_bool_frozenset = frozenset(content_set_bool)
            performative_content[
                "content_set_bool"] = content_set_bool_frozenset
            content_set_str = t_protocol_pb.performative_pct.content_set_str
            content_set_str_frozenset = frozenset(content_set_str)
            performative_content["content_set_str"] = content_set_str_frozenset
            content_list_bytes = t_protocol_pb.performative_pct.content_list_bytes
            content_list_bytes_tuple = tuple(content_list_bytes)
            performative_content[
                "content_list_bytes"] = content_list_bytes_tuple
            content_list_int = t_protocol_pb.performative_pct.content_list_int
            content_list_int_tuple = tuple(content_list_int)
            performative_content["content_list_int"] = content_list_int_tuple
            content_list_float = t_protocol_pb.performative_pct.content_list_float
            content_list_float_tuple = tuple(content_list_float)
            performative_content[
                "content_list_float"] = content_list_float_tuple
            content_list_bool = t_protocol_pb.performative_pct.content_list_bool
            content_list_bool_tuple = tuple(content_list_bool)
            performative_content["content_list_bool"] = content_list_bool_tuple
            content_list_str = t_protocol_pb.performative_pct.content_list_str
            content_list_str_tuple = tuple(content_list_str)
            performative_content["content_list_str"] = content_list_str_tuple
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_PMT:
            content_dict_int_bytes = (
                t_protocol_pb.performative_pmt.content_dict_int_bytes)
            content_dict_int_bytes_dict = dict(content_dict_int_bytes)
            performative_content[
                "content_dict_int_bytes"] = content_dict_int_bytes_dict
            content_dict_int_int = t_protocol_pb.performative_pmt.content_dict_int_int
            content_dict_int_int_dict = dict(content_dict_int_int)
            performative_content[
                "content_dict_int_int"] = content_dict_int_int_dict
            content_dict_int_float = (
                t_protocol_pb.performative_pmt.content_dict_int_float)
            content_dict_int_float_dict = dict(content_dict_int_float)
            performative_content[
                "content_dict_int_float"] = content_dict_int_float_dict
            content_dict_int_bool = t_protocol_pb.performative_pmt.content_dict_int_bool
            content_dict_int_bool_dict = dict(content_dict_int_bool)
            performative_content[
                "content_dict_int_bool"] = content_dict_int_bool_dict
            content_dict_int_str = t_protocol_pb.performative_pmt.content_dict_int_str
            content_dict_int_str_dict = dict(content_dict_int_str)
            performative_content[
                "content_dict_int_str"] = content_dict_int_str_dict
            content_dict_bool_bytes = (
                t_protocol_pb.performative_pmt.content_dict_bool_bytes)
            content_dict_bool_bytes_dict = dict(content_dict_bool_bytes)
            performative_content[
                "content_dict_bool_bytes"] = content_dict_bool_bytes_dict
            content_dict_bool_int = t_protocol_pb.performative_pmt.content_dict_bool_int
            content_dict_bool_int_dict = dict(content_dict_bool_int)
            performative_content[
                "content_dict_bool_int"] = content_dict_bool_int_dict
            content_dict_bool_float = (
                t_protocol_pb.performative_pmt.content_dict_bool_float)
            content_dict_bool_float_dict = dict(content_dict_bool_float)
            performative_content[
                "content_dict_bool_float"] = content_dict_bool_float_dict
            content_dict_bool_bool = (
                t_protocol_pb.performative_pmt.content_dict_bool_bool)
            content_dict_bool_bool_dict = dict(content_dict_bool_bool)
            performative_content[
                "content_dict_bool_bool"] = content_dict_bool_bool_dict
            content_dict_bool_str = t_protocol_pb.performative_pmt.content_dict_bool_str
            content_dict_bool_str_dict = dict(content_dict_bool_str)
            performative_content[
                "content_dict_bool_str"] = content_dict_bool_str_dict
            content_dict_str_bytes = (
                t_protocol_pb.performative_pmt.content_dict_str_bytes)
            content_dict_str_bytes_dict = dict(content_dict_str_bytes)
            performative_content[
                "content_dict_str_bytes"] = content_dict_str_bytes_dict
            content_dict_str_int = t_protocol_pb.performative_pmt.content_dict_str_int
            content_dict_str_int_dict = dict(content_dict_str_int)
            performative_content[
                "content_dict_str_int"] = content_dict_str_int_dict
            content_dict_str_float = (
                t_protocol_pb.performative_pmt.content_dict_str_float)
            content_dict_str_float_dict = dict(content_dict_str_float)
            performative_content[
                "content_dict_str_float"] = content_dict_str_float_dict
            content_dict_str_bool = t_protocol_pb.performative_pmt.content_dict_str_bool
            content_dict_str_bool_dict = dict(content_dict_str_bool)
            performative_content[
                "content_dict_str_bool"] = content_dict_str_bool_dict
            content_dict_str_str = t_protocol_pb.performative_pmt.content_dict_str_str
            content_dict_str_str_dict = dict(content_dict_str_str)
            performative_content[
                "content_dict_str_str"] = content_dict_str_str_dict
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_MT:
            if t_protocol_pb.performative_mt.content_union_1_type_DataModel_is_set:
                pb2_content_union_1_type_DataModel = (
                    t_protocol_pb.performative_mt.
                    content_union_1_type_DataModel)
                content_union_1 = DataModel.decode(
                    pb2_content_union_1_type_DataModel)
                performative_content["content_union_1"] = content_union_1
            if t_protocol_pb.performative_mt.content_union_1_type_bytes_is_set:
                content_union_1 = (
                    t_protocol_pb.performative_mt.content_union_1_type_bytes)
                performative_content["content_union_1"] = content_union_1
            if t_protocol_pb.performative_mt.content_union_1_type_int_is_set:
                content_union_1 = t_protocol_pb.performative_mt.content_union_1_type_int
                performative_content["content_union_1"] = content_union_1
            if t_protocol_pb.performative_mt.content_union_1_type_float_is_set:
                content_union_1 = (
                    t_protocol_pb.performative_mt.content_union_1_type_float)
                performative_content["content_union_1"] = content_union_1
            if t_protocol_pb.performative_mt.content_union_1_type_bool_is_set:
                content_union_1 = (
                    t_protocol_pb.performative_mt.content_union_1_type_bool)
                performative_content["content_union_1"] = content_union_1
            if t_protocol_pb.performative_mt.content_union_1_type_str_is_set:
                content_union_1 = t_protocol_pb.performative_mt.content_union_1_type_str
                performative_content["content_union_1"] = content_union_1
            if t_protocol_pb.performative_mt.content_union_1_type_set_of_int_is_set:
                content_union_1 = t_protocol_pb.performative_mt.content_union_1
                content_union_1_frozenset = frozenset(content_union_1)
                performative_content[
                    "content_union_1"] = content_union_1_frozenset
            if t_protocol_pb.performative_mt.content_union_1_type_list_of_bool_is_set:
                content_union_1 = t_protocol_pb.performative_mt.content_union_1
                content_union_1_tuple = tuple(content_union_1)
                performative_content["content_union_1"] = content_union_1_tuple
            if (t_protocol_pb.performative_mt.
                    content_union_1_type_dict_of_str_int_is_set):
                content_union_1 = t_protocol_pb.performative_mt.content_union_1
                content_union_1_dict = dict(content_union_1)
                performative_content["content_union_1"] = content_union_1_dict
            if t_protocol_pb.performative_mt.content_union_2_type_set_of_bytes_is_set:
                content_union_2 = t_protocol_pb.performative_mt.content_union_2
                content_union_2_frozenset = frozenset(content_union_2)
                performative_content[
                    "content_union_2"] = content_union_2_frozenset
            if t_protocol_pb.performative_mt.content_union_2_type_set_of_int_is_set:
                content_union_2 = t_protocol_pb.performative_mt.content_union_2
                content_union_2_frozenset = frozenset(content_union_2)
                performative_content[
                    "content_union_2"] = content_union_2_frozenset
            if t_protocol_pb.performative_mt.content_union_2_type_set_of_str_is_set:
                content_union_2 = t_protocol_pb.performative_mt.content_union_2
                content_union_2_frozenset = frozenset(content_union_2)
                performative_content[
                    "content_union_2"] = content_union_2_frozenset
            if t_protocol_pb.performative_mt.content_union_2_type_list_of_float_is_set:
                content_union_2 = t_protocol_pb.performative_mt.content_union_2
                content_union_2_tuple = tuple(content_union_2)
                performative_content["content_union_2"] = content_union_2_tuple
            if t_protocol_pb.performative_mt.content_union_2_type_list_of_bool_is_set:
                content_union_2 = t_protocol_pb.performative_mt.content_union_2
                content_union_2_tuple = tuple(content_union_2)
                performative_content["content_union_2"] = content_union_2_tuple
            if t_protocol_pb.performative_mt.content_union_2_type_list_of_bytes_is_set:
                content_union_2 = t_protocol_pb.performative_mt.content_union_2
                content_union_2_tuple = tuple(content_union_2)
                performative_content["content_union_2"] = content_union_2_tuple
            if (t_protocol_pb.performative_mt.
                    content_union_2_type_dict_of_str_int_is_set):
                content_union_2 = t_protocol_pb.performative_mt.content_union_2
                content_union_2_dict = dict(content_union_2)
                performative_content["content_union_2"] = content_union_2_dict
            if (t_protocol_pb.performative_mt.
                    content_union_2_type_dict_of_int_float_is_set):
                content_union_2 = t_protocol_pb.performative_mt.content_union_2
                content_union_2_dict = dict(content_union_2)
                performative_content["content_union_2"] = content_union_2_dict
            if (t_protocol_pb.performative_mt.
                    content_union_2_type_dict_of_bool_bytes_is_set):
                content_union_2 = t_protocol_pb.performative_mt.content_union_2
                content_union_2_dict = dict(content_union_2)
                performative_content["content_union_2"] = content_union_2_dict
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_O:
            if t_protocol_pb.performative_o.content_o_ct_is_set:
                pb2_content_o_ct = t_protocol_pb.performative_o.content_o_ct
                content_o_ct = DataModel.decode(pb2_content_o_ct)
                performative_content["content_o_ct"] = content_o_ct
            if t_protocol_pb.performative_o.content_o_bool_is_set:
                content_o_bool = t_protocol_pb.performative_o.content_o_bool
                performative_content["content_o_bool"] = content_o_bool
            if t_protocol_pb.performative_o.content_o_set_int_is_set:
                content_o_set_int = t_protocol_pb.performative_o.content_o_set_int
                content_o_set_int_frozenset = frozenset(content_o_set_int)
                performative_content[
                    "content_o_set_int"] = content_o_set_int_frozenset
            if t_protocol_pb.performative_o.content_o_list_bytes_is_set:
                content_o_list_bytes = t_protocol_pb.performative_o.content_o_list_bytes
                content_o_list_bytes_tuple = tuple(content_o_list_bytes)
                performative_content[
                    "content_o_list_bytes"] = content_o_list_bytes_tuple
            if t_protocol_pb.performative_o.content_o_dict_str_int_is_set:
                content_o_dict_str_int = (
                    t_protocol_pb.performative_o.content_o_dict_str_int)
                content_o_dict_str_int_dict = dict(content_o_dict_str_int)
                performative_content[
                    "content_o_dict_str_int"] = content_o_dict_str_int_dict
        elif (performative_id ==
              TProtocolMessage.Performative.PERFORMATIVE_EMPTY_CONTENTS):
            pass
        else:
            raise ValueError(
                "Performative not valid: {}.".format(performative_id))

        return TProtocolMessage(message_id=message_id,
                                dialogue_reference=dialogue_reference,
                                target=target,
                                performative=performative,
                                **performative_content)
Exemple #16
0
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'Tac' message.

        :param obj: the bytes object.
        :return: the 'Tac' message.
        """
        message_pb = ProtobufMessage()
        tac_pb = tac_pb2.TacMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        tac_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = tac_pb.WhichOneof("performative")
        performative_id = TacMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == TacMessage.Performative.REGISTER:
            agent_name = tac_pb.register.agent_name
            performative_content["agent_name"] = agent_name
        elif performative_id == TacMessage.Performative.UNREGISTER:
            pass
        elif performative_id == TacMessage.Performative.TRANSACTION:
            transaction_id = tac_pb.transaction.transaction_id
            performative_content["transaction_id"] = transaction_id
            ledger_id = tac_pb.transaction.ledger_id
            performative_content["ledger_id"] = ledger_id
            sender_address = tac_pb.transaction.sender_address
            performative_content["sender_address"] = sender_address
            counterparty_address = tac_pb.transaction.counterparty_address
            performative_content["counterparty_address"] = counterparty_address
            amount_by_currency_id = tac_pb.transaction.amount_by_currency_id
            amount_by_currency_id_dict = dict(amount_by_currency_id)
            performative_content[
                "amount_by_currency_id"] = amount_by_currency_id_dict
            fee_by_currency_id = tac_pb.transaction.fee_by_currency_id
            fee_by_currency_id_dict = dict(fee_by_currency_id)
            performative_content[
                "fee_by_currency_id"] = fee_by_currency_id_dict
            quantities_by_good_id = tac_pb.transaction.quantities_by_good_id
            quantities_by_good_id_dict = dict(quantities_by_good_id)
            performative_content[
                "quantities_by_good_id"] = quantities_by_good_id_dict
            nonce = tac_pb.transaction.nonce
            performative_content["nonce"] = nonce
            sender_signature = tac_pb.transaction.sender_signature
            performative_content["sender_signature"] = sender_signature
            counterparty_signature = tac_pb.transaction.counterparty_signature
            performative_content[
                "counterparty_signature"] = counterparty_signature
        elif performative_id == TacMessage.Performative.CANCELLED:
            pass
        elif performative_id == TacMessage.Performative.GAME_DATA:
            amount_by_currency_id = tac_pb.game_data.amount_by_currency_id
            amount_by_currency_id_dict = dict(amount_by_currency_id)
            performative_content[
                "amount_by_currency_id"] = amount_by_currency_id_dict
            exchange_params_by_currency_id = (
                tac_pb.game_data.exchange_params_by_currency_id)
            exchange_params_by_currency_id_dict = dict(
                exchange_params_by_currency_id)
            performative_content[
                "exchange_params_by_currency_id"] = exchange_params_by_currency_id_dict
            quantities_by_good_id = tac_pb.game_data.quantities_by_good_id
            quantities_by_good_id_dict = dict(quantities_by_good_id)
            performative_content[
                "quantities_by_good_id"] = quantities_by_good_id_dict
            utility_params_by_good_id = tac_pb.game_data.utility_params_by_good_id
            utility_params_by_good_id_dict = dict(utility_params_by_good_id)
            performative_content[
                "utility_params_by_good_id"] = utility_params_by_good_id_dict
            fee_by_currency_id = tac_pb.game_data.fee_by_currency_id
            fee_by_currency_id_dict = dict(fee_by_currency_id)
            performative_content[
                "fee_by_currency_id"] = fee_by_currency_id_dict
            agent_addr_to_name = tac_pb.game_data.agent_addr_to_name
            agent_addr_to_name_dict = dict(agent_addr_to_name)
            performative_content[
                "agent_addr_to_name"] = agent_addr_to_name_dict
            currency_id_to_name = tac_pb.game_data.currency_id_to_name
            currency_id_to_name_dict = dict(currency_id_to_name)
            performative_content[
                "currency_id_to_name"] = currency_id_to_name_dict
            good_id_to_name = tac_pb.game_data.good_id_to_name
            good_id_to_name_dict = dict(good_id_to_name)
            performative_content["good_id_to_name"] = good_id_to_name_dict
            version_id = tac_pb.game_data.version_id
            performative_content["version_id"] = version_id
            if tac_pb.game_data.info_is_set:
                info = tac_pb.game_data.info
                info_dict = dict(info)
                performative_content["info"] = info_dict
        elif performative_id == TacMessage.Performative.TRANSACTION_CONFIRMATION:
            transaction_id = tac_pb.transaction_confirmation.transaction_id
            performative_content["transaction_id"] = transaction_id
            amount_by_currency_id = (
                tac_pb.transaction_confirmation.amount_by_currency_id)
            amount_by_currency_id_dict = dict(amount_by_currency_id)
            performative_content[
                "amount_by_currency_id"] = amount_by_currency_id_dict
            quantities_by_good_id = (
                tac_pb.transaction_confirmation.quantities_by_good_id)
            quantities_by_good_id_dict = dict(quantities_by_good_id)
            performative_content[
                "quantities_by_good_id"] = quantities_by_good_id_dict
        elif performative_id == TacMessage.Performative.TAC_ERROR:
            pb2_error_code = tac_pb.tac_error.error_code
            error_code = ErrorCode.decode(pb2_error_code)
            performative_content["error_code"] = error_code
            if tac_pb.tac_error.info_is_set:
                info = tac_pb.tac_error.info
                info_dict = dict(info)
                performative_content["info"] = info_dict
        else:
            raise ValueError(
                "Performative not valid: {}.".format(performative_id))

        return TacMessage(message_id=message_id,
                          dialogue_reference=dialogue_reference,
                          target=target,
                          performative=performative,
                          **performative_content)
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'ContractApi' message.

        :param obj: the bytes object.
        :return: the 'ContractApi' message.
        """
        message_pb = ProtobufMessage()
        contract_api_pb = contract_api_pb2.ContractApiMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        contract_api_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = contract_api_pb.WhichOneof("performative")
        performative_id = ContractApiMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == ContractApiMessage.Performative.GET_DEPLOY_TRANSACTION:
            ledger_id = contract_api_pb.get_deploy_transaction.ledger_id
            performative_content["ledger_id"] = ledger_id
            contract_id = contract_api_pb.get_deploy_transaction.contract_id
            performative_content["contract_id"] = contract_id
            callable = contract_api_pb.get_deploy_transaction.callable
            performative_content["callable"] = callable
            pb2_kwargs = contract_api_pb.get_deploy_transaction.kwargs
            kwargs = Kwargs.decode(pb2_kwargs)
            performative_content["kwargs"] = kwargs
        elif performative_id == ContractApiMessage.Performative.GET_RAW_TRANSACTION:
            ledger_id = contract_api_pb.get_raw_transaction.ledger_id
            performative_content["ledger_id"] = ledger_id
            contract_id = contract_api_pb.get_raw_transaction.contract_id
            performative_content["contract_id"] = contract_id
            contract_address = contract_api_pb.get_raw_transaction.contract_address
            performative_content["contract_address"] = contract_address
            callable = contract_api_pb.get_raw_transaction.callable
            performative_content["callable"] = callable
            pb2_kwargs = contract_api_pb.get_raw_transaction.kwargs
            kwargs = Kwargs.decode(pb2_kwargs)
            performative_content["kwargs"] = kwargs
        elif performative_id == ContractApiMessage.Performative.GET_RAW_MESSAGE:
            ledger_id = contract_api_pb.get_raw_message.ledger_id
            performative_content["ledger_id"] = ledger_id
            contract_id = contract_api_pb.get_raw_message.contract_id
            performative_content["contract_id"] = contract_id
            contract_address = contract_api_pb.get_raw_message.contract_address
            performative_content["contract_address"] = contract_address
            callable = contract_api_pb.get_raw_message.callable
            performative_content["callable"] = callable
            pb2_kwargs = contract_api_pb.get_raw_message.kwargs
            kwargs = Kwargs.decode(pb2_kwargs)
            performative_content["kwargs"] = kwargs
        elif performative_id == ContractApiMessage.Performative.GET_STATE:
            ledger_id = contract_api_pb.get_state.ledger_id
            performative_content["ledger_id"] = ledger_id
            contract_id = contract_api_pb.get_state.contract_id
            performative_content["contract_id"] = contract_id
            contract_address = contract_api_pb.get_state.contract_address
            performative_content["contract_address"] = contract_address
            callable = contract_api_pb.get_state.callable
            performative_content["callable"] = callable
            pb2_kwargs = contract_api_pb.get_state.kwargs
            kwargs = Kwargs.decode(pb2_kwargs)
            performative_content["kwargs"] = kwargs
        elif performative_id == ContractApiMessage.Performative.STATE:
            pb2_state = contract_api_pb.state.state
            state = State.decode(pb2_state)
            performative_content["state"] = state
        elif performative_id == ContractApiMessage.Performative.RAW_TRANSACTION:
            pb2_raw_transaction = contract_api_pb.raw_transaction.raw_transaction
            raw_transaction = RawTransaction.decode(pb2_raw_transaction)
            performative_content["raw_transaction"] = raw_transaction
        elif performative_id == ContractApiMessage.Performative.RAW_MESSAGE:
            pb2_raw_message = contract_api_pb.raw_message.raw_message
            raw_message = RawMessage.decode(pb2_raw_message)
            performative_content["raw_message"] = raw_message
        elif performative_id == ContractApiMessage.Performative.ERROR:
            if contract_api_pb.error.code_is_set:
                code = contract_api_pb.error.code
                performative_content["code"] = code
            if contract_api_pb.error.message_is_set:
                message = contract_api_pb.error.message
                performative_content["message"] = message
            data = contract_api_pb.error.data
            performative_content["data"] = data
        else:
            raise ValueError(
                "Performative not valid: {}.".format(performative_id))

        return ContractApiMessage(message_id=message_id,
                                  dialogue_reference=dialogue_reference,
                                  target=target,
                                  performative=performative,
                                  **performative_content)
Exemple #18
0
    def decode(obj: bytes) -> Message:
        """
        Decode bytes into a 'LedgerApi' message.

        :param obj: the bytes object.
        :return: the 'LedgerApi' message.
        """
        message_pb = ProtobufMessage()
        ledger_api_pb = ledger_api_pb2.LedgerApiMessage()
        message_pb.ParseFromString(obj)
        message_id = message_pb.dialogue_message.message_id
        dialogue_reference = (
            message_pb.dialogue_message.dialogue_starter_reference,
            message_pb.dialogue_message.dialogue_responder_reference,
        )
        target = message_pb.dialogue_message.target

        ledger_api_pb.ParseFromString(message_pb.dialogue_message.content)
        performative = ledger_api_pb.WhichOneof("performative")
        performative_id = LedgerApiMessage.Performative(str(performative))
        performative_content = dict()  # type: Dict[str, Any]
        if performative_id == LedgerApiMessage.Performative.GET_BALANCE:
            ledger_id = ledger_api_pb.get_balance.ledger_id
            performative_content["ledger_id"] = ledger_id
            address = ledger_api_pb.get_balance.address
            performative_content["address"] = address
        elif performative_id == LedgerApiMessage.Performative.GET_RAW_TRANSACTION:
            pb2_terms = ledger_api_pb.get_raw_transaction.terms
            terms = Terms.decode(pb2_terms)
            performative_content["terms"] = terms
        elif performative_id == LedgerApiMessage.Performative.SEND_SIGNED_TRANSACTION:
            pb2_signed_transaction = (
                ledger_api_pb.send_signed_transaction.signed_transaction)
            signed_transaction = SignedTransaction.decode(
                pb2_signed_transaction)
            performative_content["signed_transaction"] = signed_transaction
        elif performative_id == LedgerApiMessage.Performative.GET_TRANSACTION_RECEIPT:
            pb2_transaction_digest = (
                ledger_api_pb.get_transaction_receipt.transaction_digest)
            transaction_digest = TransactionDigest.decode(
                pb2_transaction_digest)
            performative_content["transaction_digest"] = transaction_digest
        elif performative_id == LedgerApiMessage.Performative.BALANCE:
            ledger_id = ledger_api_pb.balance.ledger_id
            performative_content["ledger_id"] = ledger_id
            balance = ledger_api_pb.balance.balance
            performative_content["balance"] = balance
        elif performative_id == LedgerApiMessage.Performative.RAW_TRANSACTION:
            pb2_raw_transaction = ledger_api_pb.raw_transaction.raw_transaction
            raw_transaction = RawTransaction.decode(pb2_raw_transaction)
            performative_content["raw_transaction"] = raw_transaction
        elif performative_id == LedgerApiMessage.Performative.TRANSACTION_DIGEST:
            pb2_transaction_digest = ledger_api_pb.transaction_digest.transaction_digest
            transaction_digest = TransactionDigest.decode(
                pb2_transaction_digest)
            performative_content["transaction_digest"] = transaction_digest
        elif performative_id == LedgerApiMessage.Performative.TRANSACTION_RECEIPT:
            pb2_transaction_receipt = (
                ledger_api_pb.transaction_receipt.transaction_receipt)
            transaction_receipt = TransactionReceipt.decode(
                pb2_transaction_receipt)
            performative_content["transaction_receipt"] = transaction_receipt
        elif performative_id == LedgerApiMessage.Performative.ERROR:
            code = ledger_api_pb.error.code
            performative_content["code"] = code
            if ledger_api_pb.error.message_is_set:
                message = ledger_api_pb.error.message
                performative_content["message"] = message
            if ledger_api_pb.error.data_is_set:
                data = ledger_api_pb.error.data
                performative_content["data"] = data
        else:
            raise ValueError(
                "Performative not valid: {}.".format(performative_id))

        return LedgerApiMessage(message_id=message_id,
                                dialogue_reference=dialogue_reference,
                                target=target,
                                performative=performative,
                                **performative_content)