Ejemplo n.º 1
0
    def test_generated_protocol_serialisation_ct(self):
        """Test that a generated protocol's serialisation + deserialisation work correctly."""
        # create a message with pt content
        some_dict = {1: True, 2: False, 3: True, 4: False}
        data_model = TProtocolMessage.DataModel(
            bytes_field=b"some bytes",
            int_field=42,
            float_field=42.7,
            bool_field=True,
            str_field="some string",
            set_field={1, 2, 3, 4, 5},
            list_field=["some string 1", "some string 2"],
            dict_field=some_dict,
        )
        message = TProtocolMessage(
            message_id=1,
            dialogue_reference=(str(0), ""),
            target=0,
            performative=TProtocolMessage.Performative.PERFORMATIVE_CT,
            content_ct=data_model,
        )

        # serialise the message
        encoded_message_in_bytes = TProtocolMessage.serializer.encode(message)

        # deserialise the message
        decoded_message = TProtocolMessage.serializer.decode(
            encoded_message_in_bytes)

        # Compare the original message with the serialised+deserialised message
        assert decoded_message.message_id == message.message_id
        assert decoded_message.dialogue_reference == message.dialogue_reference
        assert decoded_message.dialogue_reference[
            0] == message.dialogue_reference[0]
        assert decoded_message.dialogue_reference[
            1] == message.dialogue_reference[1]
        assert decoded_message.target == message.target
        assert decoded_message.performative == message.performative
        assert decoded_message.content_ct == message.content_ct
Ejemplo n.º 2
0
    def test_generated_protocol_serialisation_pt(self):
        """Test that a generated protocol's serialisation + deserialisation work correctly."""
        # create a message with pt content
        message = TProtocolMessage(
            message_id=1,
            dialogue_reference=(str(0), ""),
            target=0,
            performative=TProtocolMessage.Performative.PERFORMATIVE_PT,
            content_bytes=b"some bytes",
            content_int=42,
            content_float=42.7,
            content_bool=True,
            content_str="some string",
        )

        # serialise the message
        encoded_message_in_bytes = TProtocolMessage.serializer.encode(message)

        # deserialise the message
        decoded_message = TProtocolMessage.serializer.decode(
            encoded_message_in_bytes)

        # Compare the original message with the serialised+deserialised message
        assert decoded_message.message_id == message.message_id
        assert decoded_message.dialogue_reference == message.dialogue_reference
        assert decoded_message.dialogue_reference[
            0] == message.dialogue_reference[0]
        assert decoded_message.dialogue_reference[
            1] == message.dialogue_reference[1]
        assert decoded_message.target == message.target
        assert decoded_message.performative == message.performative
        assert decoded_message.content_bytes == message.content_bytes
        assert decoded_message.content_int == message.content_int
        # floats do not seem to lose some precision when serialised then deserialised using protobuf
        # assert decoded_message.content_float == message.content_float
        assert decoded_message.content_bool == message.content_bool
        assert decoded_message.content_str == message.content_str
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
    def test_generated_protocol_end_to_end(self):
        """Test that a generated protocol could be used in exchanging messages between two agents."""
        agent_name_1 = "my_aea_1"
        agent_name_2 = "my_aea_2"
        builder_1 = AEABuilder()
        builder_1.set_name(agent_name_1)
        builder_1.add_private_key(DEFAULT_LEDGER, self.private_key_path_1)
        builder_1.set_default_ledger(DEFAULT_LEDGER)
        builder_1.set_default_connection(PublicId.from_str("fetchai/oef:0.7.0"))
        builder_1.add_protocol(
            Path(ROOT_DIR, "packages", "fetchai", "protocols", "fipa")
        )
        builder_1.add_protocol(
            Path(ROOT_DIR, "packages", "fetchai", "protocols", "oef_search")
        )
        builder_1.add_component(
            ComponentType.PROTOCOL,
            Path(PATH_TO_T_PROTOCOL),
            skip_consistency_check=True,
        )
        builder_1.add_connection(
            Path(ROOT_DIR, "packages", "fetchai", "connections", "oef")
        )

        builder_2 = AEABuilder()
        builder_2.set_name(agent_name_2)
        builder_2.add_private_key(DEFAULT_LEDGER, self.private_key_path_2)
        builder_2.set_default_ledger(DEFAULT_LEDGER)
        builder_2.add_protocol(
            Path(ROOT_DIR, "packages", "fetchai", "protocols", "fipa")
        )
        builder_2.add_protocol(
            Path(ROOT_DIR, "packages", "fetchai", "protocols", "oef_search")
        )
        builder_2.set_default_connection(PublicId.from_str("fetchai/oef:0.7.0"))
        builder_2.add_component(
            ComponentType.PROTOCOL,
            Path(PATH_TO_T_PROTOCOL),
            skip_consistency_check=True,
        )
        builder_2.add_connection(
            Path(ROOT_DIR, "packages", "fetchai", "connections", "oef")
        )

        # create AEAs
        aea_1 = builder_1.build(connection_ids=[PublicId.from_str("fetchai/oef:0.7.0")])
        aea_2 = builder_2.build(connection_ids=[PublicId.from_str("fetchai/oef:0.7.0")])

        # dialogues
        dialogue_label_1 = DialogueLabel(
            (str(1), ""), aea_2.identity.address, aea_1.identity.address
        )
        aea_1_dialogue = TProtocolDialogue(
            dialogue_label_1, aea_1.identity.address, TProtocolDialogue.Role.ROLE_1
        )
        dialogue_label_2 = DialogueLabel(
            (str(1), str(1)), aea_1.identity.address, aea_1.identity.address
        )
        aea_2_dialogue = TProtocolDialogue(
            dialogue_label_2, aea_2.identity.address, TProtocolDialogue.Role.ROLE_2
        )

        # message 1
        message_1 = TProtocolMessage(
            message_id=1,
            dialogue_reference=(str(1), ""),
            target=0,
            performative=TProtocolMessage.Performative.PERFORMATIVE_PT,
            content_bytes=b"some bytes",
            content_int=42,
            content_float=42.7,
            content_bool=True,
            content_str="some string",
        )
        message_1.counterparty = aea_2.identity.address
        message_1.is_incoming = False

        # message 2
        message_2 = TProtocolMessage(
            message_id=2,
            dialogue_reference=(str(1), str(1)),
            target=1,
            performative=TProtocolMessage.Performative.PERFORMATIVE_PT,
            content_bytes=b"some other bytes",
            content_int=43,
            content_float=43.7,
            content_bool=False,
            content_str="some other string",
        )
        message_2.counterparty = aea_1.identity.address
        message_2.is_incoming = False

        # add handlers to AEA resources
        skill_context_1 = SkillContext(aea_1.context)
        skill_1 = Skill(SkillConfig("fake_skill", "fetchai", "0.1.0"), skill_context_1)
        skill_context_1._skill = skill_1

        agent_1_handler = Agent1Handler(
            skill_context=skill_context_1,
            name="fake_handler_1",
            dialogue=aea_1_dialogue,
        )
        aea_1.resources._handler_registry.register(
            (
                PublicId.from_str("fetchai/fake_skill:0.1.0"),
                TProtocolMessage.protocol_id,
            ),
            agent_1_handler,
        )
        skill_context_2 = SkillContext(aea_2.context)
        skill_2 = Skill(SkillConfig("fake_skill", "fetchai", "0.1.0"), skill_context_2)
        skill_context_2._skill = skill_2

        agent_2_handler = Agent2Handler(
            message=message_2,
            dialogue=aea_2_dialogue,
            skill_context=skill_context_2,
            name="fake_handler_2",
        )
        aea_2.resources._handler_registry.register(
            (
                PublicId.from_str("fetchai/fake_skill:0.1.0"),
                TProtocolMessage.protocol_id,
            ),
            agent_2_handler,
        )

        # Start threads
        t_1 = Thread(target=aea_1.start)
        t_2 = Thread(target=aea_2.start)
        try:
            t_1.start()
            t_2.start()
            time.sleep(1.0)
            aea_1_dialogue.update(message_1)
            aea_1.outbox.put_message(message_1)
            time.sleep(5.0)
            assert (
                agent_2_handler.handled_message.message_id == message_1.message_id
            ), "Message from Agent 1 to 2: message ids do not match"
            assert (
                agent_2_handler.handled_message.dialogue_reference
                == message_1.dialogue_reference
            ), "Message from Agent 1 to 2: dialogue references do not match"
            assert (
                agent_2_handler.handled_message.dialogue_reference[0]
                == message_1.dialogue_reference[0]
            ), "Message from Agent 1 to 2: dialogue reference[0]s do not match"
            assert (
                agent_2_handler.handled_message.dialogue_reference[1]
                == message_1.dialogue_reference[1]
            ), "Message from Agent 1 to 2: dialogue reference[1]s do not match"
            assert (
                agent_2_handler.handled_message.target == message_1.target
            ), "Message from Agent 1 to 2: targets do not match"
            assert (
                agent_2_handler.handled_message.performative == message_1.performative
            ), "Message from Agent 1 to 2: performatives do not match"
            assert (
                agent_2_handler.handled_message.content_bytes == message_1.content_bytes
            ), "Message from Agent 1 to 2: content_bytes do not match"
            assert (
                agent_2_handler.handled_message.content_int == message_1.content_int
            ), "Message from Agent 1 to 2: content_int do not match"
            # assert (
            #     agent_2_handler.handled_message.content_float == message_1.content_float
            # ), "Message from Agent 1 to 2: content_float do not match"
            assert (
                agent_2_handler.handled_message.content_bool == message_1.content_bool
            ), "Message from Agent 1 to 2: content_bool do not match"
            assert (
                agent_2_handler.handled_message.content_str == message_1.content_str
            ), "Message from Agent 1 to 2: content_str do not match"

            assert (
                agent_1_handler.handled_message.message_id == message_2.message_id
            ), "Message from Agent 1 to 2: dialogue references do not match"
            assert (
                agent_1_handler.handled_message.dialogue_reference
                == message_2.dialogue_reference
            ), "Message from Agent 2 to 1: dialogue references do not match"
            assert (
                agent_1_handler.handled_message.dialogue_reference[0]
                == message_2.dialogue_reference[0]
            ), "Message from Agent 2 to 1: dialogue reference[0]s do not match"
            assert (
                agent_1_handler.handled_message.dialogue_reference[1]
                == message_2.dialogue_reference[1]
            ), "Message from Agent 2 to 1: dialogue reference[1]s do not match"
            assert (
                agent_1_handler.handled_message.target == message_2.target
            ), "Message from Agent 2 to 1: targets do not match"
            assert (
                agent_1_handler.handled_message.performative == message_2.performative
            ), "Message from Agent 2 to 1: performatives do not match"
            assert (
                agent_1_handler.handled_message.content_bytes == message_2.content_bytes
            ), "Message from Agent 2 to 1: content_bytes do not match"
            assert (
                agent_1_handler.handled_message.content_int == message_2.content_int
            ), "Message from Agent 2 to 1: content_int do not match"
            # assert (
            #     agent_1_handler.handled_message.content_float == message_2.content_float
            # ), "Message from Agent 2 to 1: content_float do not match"
            assert (
                agent_1_handler.handled_message.content_bool == message_2.content_bool
            ), "Message from Agent 2 to 1: content_bool do not match"
            assert (
                agent_1_handler.handled_message.content_str == message_2.content_str
            ), "Message from Agent 2 to 1: content_str do not match"
            time.sleep(2.0)
        finally:
            aea_1.stop()
            aea_2.stop()
            t_1.join()
            t_2.join()
Ejemplo n.º 5
0
    def test_generated_protocol_end_to_end(self):
        """Test that a generated protocol could be used in exchanging messages between two agents."""
        builder_1 = AEABuilder()
        builder_1.set_name("my_aea_1")
        builder_1.add_private_key(FETCHAI, FETCHAI_PRIVATE_KEY_FILE)
        builder_1.set_default_ledger(FETCHAI)
        builder_1.add_component(
            ComponentType.PROTOCOL,
            Path(ROOT_DIR, "tests", "data", "generator", "t_protocol"),
            skip_consistency_check=True,
        )
        builder_1.add_connection(
            Path(ROOT_DIR, "packages", "fetchai", "connections", "oef"))

        builder_2 = AEABuilder()
        builder_2.set_name("my_aea_2")
        builder_2.add_private_key(FETCHAI, FETCHAI_PRIVATE_KEY_FILE)
        builder_2.set_default_ledger(FETCHAI)
        builder_2.add_component(
            ComponentType.PROTOCOL,
            Path(ROOT_DIR, "tests", "data", "generator", "t_protocol"),
            skip_consistency_check=True,
        )
        builder_2.add_connection(
            Path(ROOT_DIR, "packages", "fetchai", "connections", "oef"))

        # create AEAs
        aea_1 = builder_1.build(
            connection_ids=[PublicId.from_str("fetchai/oef:0.1.0")])
        aea_2 = builder_2.build(
            connection_ids=[PublicId.from_str("fetchai/oef:0.1.0")])

        # message 1
        message = TProtocolMessage(
            message_id=1,
            dialogue_reference=(str(0), ""),
            target=0,
            performative=TProtocolMessage.Performative.PERFORMATIVE_PT,
            content_bytes=b"some bytes",
            content_int=42,
            content_float=42.7,
            content_bool=True,
            content_str="some string",
        )
        encoded_message_in_bytes = TProtocolSerializer().encode(message)
        envelope = Envelope(
            to=aea_2.identity.address,
            sender=aea_1.identity.address,
            protocol_id=TProtocolMessage.protocol_id,
            message=encoded_message_in_bytes,
        )

        # message 2
        message_2 = TProtocolMessage(
            message_id=2,
            dialogue_reference=(str(0), ""),
            target=1,
            performative=TProtocolMessage.Performative.PERFORMATIVE_PT,
            content_bytes=b"some other bytes",
            content_int=43,
            content_float=43.7,
            content_bool=False,
            content_str="some other string",
        )
        encoded_message_2_in_bytes = TProtocolSerializer().encode(message_2)

        # add handlers to AEA resources
        agent_1_handler = Agent1Handler(skill_context=SkillContext(
            aea_1.context),
                                        name="fake_skill")
        aea_1.resources._handler_registry.register(
            (
                PublicId.from_str("fetchai/fake_skill:0.1.0"),
                TProtocolMessage.protocol_id,
            ),
            agent_1_handler,
        )

        agent_2_handler = Agent2Handler(
            encoded_messsage=encoded_message_2_in_bytes,
            skill_context=SkillContext(aea_2.context),
            name="fake_skill",
        )
        aea_2.resources._handler_registry.register(
            (
                PublicId.from_str("fetchai/fake_skill:0.1.0"),
                TProtocolMessage.protocol_id,
            ),
            agent_2_handler,
        )

        # Start threads
        t_1 = Thread(target=aea_1.start)
        t_2 = Thread(target=aea_2.start)
        try:
            t_1.start()
            t_2.start()
            time.sleep(1.0)
            aea_1.outbox.put(envelope)
            time.sleep(5.0)
            assert (agent_2_handler.handled_message.message_id ==
                    message.message_id
                    ), "Message from Agent 1 to 2: message ids do not match"
            assert (
                agent_2_handler.handled_message.dialogue_reference ==
                message.dialogue_reference
            ), "Message from Agent 1 to 2: dialogue references do not match"
            assert (
                agent_2_handler.handled_message.dialogue_reference[0] ==
                message.dialogue_reference[0]
            ), "Message from Agent 1 to 2: dialogue reference[0]s do not match"
            assert (
                agent_2_handler.handled_message.dialogue_reference[1] ==
                message.dialogue_reference[1]
            ), "Message from Agent 1 to 2: dialogue reference[1]s do not match"
            assert (agent_2_handler.handled_message.target == message.target
                    ), "Message from Agent 1 to 2: targets do not match"
            assert (agent_2_handler.handled_message.performative ==
                    message.performative
                    ), "Message from Agent 1 to 2: performatives do not match"
            assert (agent_2_handler.handled_message.content_bytes ==
                    message.content_bytes
                    ), "Message from Agent 1 to 2: content_bytes do not match"
            assert (agent_2_handler.handled_message.content_int ==
                    message.content_int
                    ), "Message from Agent 1 to 2: content_int do not match"
            # floats do not seem to lose some precision when serialised then deserialised using protobuf
            # assert agent_2_handler.handled_message.content_float == message.content_float, "Message from Agent 1 to 2: content_float do not match"
            assert (agent_2_handler.handled_message.content_bool ==
                    message.content_bool
                    ), "Message from Agent 1 to 2: content_bool do not match"
            assert (agent_2_handler.handled_message.content_str ==
                    message.content_str
                    ), "Message from Agent 1 to 2: content_str do not match"

            assert (
                agent_1_handler.handled_message.message_id ==
                message_2.message_id
            ), "Message from Agent 1 to 2: dialogue references do not match"
            assert (
                agent_1_handler.handled_message.dialogue_reference ==
                message_2.dialogue_reference
            ), "Message from Agent 2 to 1: dialogue references do not match"
            assert (
                agent_1_handler.handled_message.dialogue_reference[0] ==
                message_2.dialogue_reference[0]
            ), "Message from Agent 2 to 1: dialogue reference[0]s do not match"
            assert (
                agent_1_handler.handled_message.dialogue_reference[1] ==
                message_2.dialogue_reference[1]
            ), "Message from Agent 2 to 1: dialogue reference[1]s do not match"
            assert (agent_1_handler.handled_message.target == message_2.target
                    ), "Message from Agent 2 to 1: targets do not match"
            assert (agent_1_handler.handled_message.performative ==
                    message_2.performative
                    ), "Message from Agent 2 to 1: performatives do not match"
            assert (agent_1_handler.handled_message.content_bytes ==
                    message_2.content_bytes
                    ), "Message from Agent 2 to 1: content_bytes do not match"
            assert (agent_1_handler.handled_message.content_int ==
                    message_2.content_int
                    ), "Message from Agent 2 to 1: content_int do not match"
            # floats do not seem to lose some precision when serialised then deserialised using protobuf
            # assert agent_1_handler.handled_message.content_float == message_2.content_float, "Message from Agent 2 to 1: content_float do not match"
            assert (agent_1_handler.handled_message.content_bool ==
                    message_2.content_bool
                    ), "Message from Agent 2 to 1: content_bool do not match"
            assert (agent_1_handler.handled_message.content_str ==
                    message_2.content_str
                    ), "Message from Agent 2 to 1: content_str do not match"
            time.sleep(2.0)
        finally:
            aea_1.stop()
            aea_2.stop()
            t_1.join()
            t_2.join()