Ejemplo n.º 1
0
    def test_propose(self):
        """Test that a Propose can be sent correctly."""
        propose_empty = FIPAMessage(
            message_id=0,
            dialogue_id=0,
            target=0,
            performative=FIPAMessage.Performative.PROPOSE,
            proposal=[])
        self.mailbox1.outbox.put_message(
            to=self.crypto2.public_key,
            sender=self.crypto1.public_key,
            protocol_id=FIPAMessage.protocol_id,
            message=FIPASerializer().encode(propose_empty))
        envelope = self.mailbox2.inbox.get(block=True, timeout=2.0)
        expected_propose_empty = FIPASerializer().decode(envelope.message)
        assert expected_propose_empty == propose_empty

        propose_descriptions = FIPAMessage(
            message_id=0,
            dialogue_id=0,
            target=0,
            performative=FIPAMessage.Performative.PROPOSE,
            proposal=[
                Description({"foo": "bar"},
                            DataModel("foobar", [Attribute("foo", str, True)]))
            ])
        self.mailbox1.outbox.put_message(
            to=self.crypto2.public_key,
            sender=self.crypto1.public_key,
            protocol_id=FIPAMessage.protocol_id,
            message=FIPASerializer().encode(propose_descriptions))
        envelope = self.mailbox2.inbox.get(block=True, timeout=2.0)
        expected_propose_descriptions = FIPASerializer().decode(
            envelope.message)
        assert expected_propose_descriptions == propose_descriptions
Ejemplo n.º 2
0
    def test_cfp(self):
        """Test that a CFP can be sent correctly."""
        cfp_bytes = FIPAMessage(
            message_id=0,
            dialogue_id=0,
            target=0,
            performative=FIPAMessage.Performative.CFP,
            query=Query([Constraint('something', ConstraintType('>', 1))]))
        self.mailbox1.outbox.put_message(
            to=self.crypto2.public_key,
            sender=self.crypto1.public_key,
            protocol_id=FIPAMessage.protocol_id,
            message=FIPASerializer().encode(cfp_bytes))
        envelope = self.mailbox2.inbox.get(block=True, timeout=5.0)
        expected_cfp_bytes = FIPASerializer().decode(envelope.message)
        assert expected_cfp_bytes == cfp_bytes

        cfp_none = FIPAMessage(message_id=0,
                               dialogue_id=0,
                               target=0,
                               performative=FIPAMessage.Performative.CFP,
                               query=None)
        self.mailbox1.outbox.put_message(
            to=self.crypto2.public_key,
            sender=self.crypto1.public_key,
            protocol_id=FIPAMessage.protocol_id,
            message=FIPASerializer().encode(cfp_none))
        envelope = self.mailbox2.inbox.get(block=True, timeout=5.0)
        expected_cfp_none = FIPASerializer().decode(envelope.message)
        assert expected_cfp_none == cfp_none
Ejemplo n.º 3
0
def test_fipa_cfp_serialization_bytes():
    """Test that the serialization - deserialization for the 'fipa' protocol works."""
    query = b'Hello'
    msg = FIPAMessage(message_id=0,
                      dialogue_reference=(str(0), ''),
                      target=0,
                      performative=FIPAMessage.Performative.CFP,
                      query=query)
    msg_bytes = FIPASerializer().encode(msg)
    envelope = Envelope(to="receiver",
                        sender="sender",
                        protocol_id=FIPAMessage.protocol_id,
                        message=msg_bytes)
    envelope_bytes = envelope.encode()

    actual_envelope = Envelope.decode(envelope_bytes)
    expected_envelope = envelope
    assert expected_envelope == actual_envelope

    actual_msg = FIPASerializer().decode(actual_envelope.message)
    expected_msg = msg
    assert expected_msg == actual_msg

    deserialised_msg = FIPASerializer().decode(envelope.message)
    assert msg.get("performative") == deserialised_msg.get("performative")
Ejemplo n.º 4
0
    def _handle_propose(self, msg: FIPAMessage, sender: str, message_id: int,
                        dialogue: Dialogue) -> None:
        """
        Handle the propose.

        :param msg: the message
        :param sender: the sender
        :param message_id: the message id
        :param dialogue: the dialogue object
        :return: None
        """
        new_message_id = message_id + 1
        new_target_id = message_id
        proposals = cast(List[Description], msg.get("proposal"))

        if proposals is not []:
            # only take the first proposal
            proposal = proposals[0]
            logger.info("[{}]: received proposal={} from sender={}".format(
                self.context.agent_name, proposal.values, sender[-5:]))
            strategy = cast(Strategy, self.context.strategy)
            acceptable = strategy.is_acceptable_proposal(proposal)
            affordable = self.context.ledger_apis.token_balance(
                'fetchai',
                cast(str,
                     self.context.agent_addresses.get('fetchai'))) >= cast(
                         int, proposal.values.get('price'))
            if acceptable and affordable:
                logger.info(
                    "[{}]: accepting the proposal from sender={}".format(
                        self.context.agent_name, sender[-5:]))
                dialogue.proposal = proposal
                accept_msg = FIPAMessage(
                    message_id=new_message_id,
                    dialogue_reference=dialogue.dialogue_label.
                    dialogue_reference,
                    target=new_target_id,
                    performative=FIPAMessage.Performative.ACCEPT)
                dialogue.outgoing_extend(accept_msg)
                self.context.outbox.put_message(
                    to=sender,
                    sender=self.context.agent_public_key,
                    protocol_id=FIPAMessage.protocol_id,
                    message=FIPASerializer().encode(accept_msg))
            else:
                logger.info(
                    "[{}]: declining the proposal from sender={}".format(
                        self.context.agent_name, sender[-5:]))
                decline_msg = FIPAMessage(
                    message_id=new_message_id,
                    dialogue_reference=dialogue.dialogue_label.
                    dialogue_reference,
                    target=new_target_id,
                    performative=FIPAMessage.Performative.DECLINE)
                dialogue.outgoing_extend(decline_msg)
                self.context.outbox.put_message(
                    to=sender,
                    sender=self.context.agent_public_key,
                    protocol_id=FIPAMessage.protocol_id,
                    message=FIPASerializer().encode(decline_msg))
Ejemplo n.º 5
0
    def is_valid_next_message(self, fipa_msg: FIPAMessage) -> bool:
        """
        Check whether this is a valid next message in the dialogue.

        :return: True if yes, False otherwise.
        """
        this_message_id = fipa_msg.get("message_id")
        this_target = fipa_msg.get("target")
        this_performative = cast(FIPAMessage.Performative,
                                 fipa_msg.get("performative"))
        last_outgoing_message = self.last_outgoing_message
        if last_outgoing_message is None:
            result = this_message_id == FIPAMessage.STARTING_MESSAGE_ID and \
                this_target == FIPAMessage.STARTING_TARGET and \
                this_performative == FIPAMessage.Performative.CFP
        else:
            last_message_id = cast(int,
                                   last_outgoing_message.get("message_id"))
            last_target = cast(int, last_outgoing_message.get("target"))
            last_performative = cast(FIPAMessage.Performative,
                                     last_outgoing_message.get("performative"))
            result = this_message_id == last_message_id + 1 and \
                this_target == last_target + 1 and \
                last_performative in VALID_PREVIOUS_PERFORMATIVES[this_performative]
        return result
Ejemplo n.º 6
0
def test_fipa_cfp_serialization():
    """Test that the serialization for the 'fipa' protocol works."""
    query = Query([Constraint('something', ConstraintType('>', 1))])
    msg = FIPAMessage(message_id=0,
                      dialogue_reference=(str(0), ''),
                      target=0,
                      performative=FIPAMessage.Performative.CFP,
                      query=query)
    msg_bytes = FIPASerializer().encode(msg)
    envelope = Envelope(to="receiver",
                        sender="sender",
                        protocol_id=FIPAMessage.protocol_id,
                        message=msg_bytes)
    envelope_bytes = envelope.encode()

    actual_envelope = Envelope.decode(envelope_bytes)
    expected_envelope = envelope
    assert expected_envelope == actual_envelope

    actual_msg = FIPASerializer().decode(actual_envelope.message)
    expected_msg = msg
    assert expected_msg == actual_msg

    msg.set("query", "not_supported_query")
    with pytest.raises(ValueError, match="Query type not supported:"):
        FIPASerializer().encode(msg)
Ejemplo n.º 7
0
    def _on_decline(self, decline: FIPAMessage, dialogue: Dialogue) -> None:
        """
        Handle a Decline.

        :param decline: the Decline message
        :param dialogue: the dialogue
        :return: None
        """
        logger.debug("[{}]: on_decline: msg_id={}, dialogue_id={}, origin={}, target={}"
                     .format(self.context.agent_name, decline.get("id"), decline.get("dialogue_id"), dialogue.dialogue_label.dialogue_opponent_pbk, decline.get("target")))
        target = decline.get("target")
        dialogues = cast(Dialogues, self.context.dialogues)
        if target == 1:
            dialogues.dialogue_stats.add_dialogue_endstate(Dialogue.EndState.DECLINED_CFP, dialogue.is_self_initiated)
        elif target == 2:
            dialogues.dialogue_stats.add_dialogue_endstate(Dialogue.EndState.DECLINED_PROPOSE, dialogue.is_self_initiated)
            transactions = cast(Transactions, self.context.transactions)
            transaction_msg = transactions.pop_pending_proposal(dialogue.dialogue_label, target)
            strategy = cast(Strategy, self.context.strategy)
            if strategy.is_world_modeling:
                pass  # TODO
                # strategy.world_state.update_on_declined_propose(transaction_msg)
        elif target == 3:
            dialogues.dialogue_stats.add_dialogue_endstate(Dialogue.EndState.DECLINED_ACCEPT, dialogue.is_self_initiated)
            transactions = cast(Transactions, self.context.transactions)
            transaction_msg = transactions.pop_pending_initial_acceptance(dialogue.dialogue_label, target)
            transactions.pop_locked_tx(transaction_msg)
Ejemplo n.º 8
0
    def on_propose(self, propose: Message, dialogue: Dialogue) -> Envelope:
        """
        Handle a Propose.

        :param propose: the message containing the Propose
        :param dialogue: the dialogue

        :return: an Accept or a Decline
        """
        logger.debug("[{}]: on propose as {}.".format(self.agent_name,
                                                      dialogue.role))
        assert propose.get("performative") == FIPAMessage.Performative.PROPOSE
        proposal = propose.get("proposal")[0]
        transaction_id = generate_transaction_id(
            self.crypto.public_key,
            dialogue.dialogue_label.dialogue_opponent_pbk,
            dialogue.dialogue_label, dialogue.is_seller)
        transaction = Transaction.from_proposal(
            proposal=proposal,
            transaction_id=transaction_id,
            is_sender_buyer=not dialogue.is_seller,
            counterparty=dialogue.dialogue_label.dialogue_opponent_pbk,
            sender=self.crypto.public_key)
        new_msg_id = propose.get("id") + 1
        is_profitable_transaction, propose_log_msg = self.game_instance.is_profitable_transaction(
            transaction, dialogue)
        logger.debug(propose_log_msg)
        if is_profitable_transaction:
            logger.debug("[{}]: Accepting propose (as {}).".format(
                self.agent_name, dialogue.role))
            self.game_instance.transaction_manager.add_locked_tx(
                transaction, as_seller=dialogue.is_seller)
            self.game_instance.transaction_manager.add_pending_initial_acceptance(
                dialogue.dialogue_label, new_msg_id, transaction)
            msg = FIPAMessage(message_id=new_msg_id,
                              dialogue_id=propose.get("dialogue_id"),
                              target=propose.get("id"),
                              performative=FIPAMessage.Performative.ACCEPT)
            dialogue.outgoing_extend([msg])
            msg_bytes = FIPASerializer().encode(msg)
            result = Envelope(to=dialogue.dialogue_label.dialogue_opponent_pbk,
                              sender=self.crypto.public_key,
                              protocol_id=FIPAMessage.protocol_id,
                              message=msg_bytes)
        else:
            logger.debug("[{}]: Declining propose (as {})".format(
                self.agent_name, dialogue.role))
            msg = FIPAMessage(message_id=new_msg_id,
                              dialogue_id=propose.get("dialogue_id"),
                              target=propose.get("id"),
                              performative=FIPAMessage.Performative.DECLINE)
            dialogue.outgoing_extend([msg])
            msg_bytes = FIPASerializer().encode(msg)
            result = Envelope(to=dialogue.dialogue_label.dialogue_opponent_pbk,
                              sender=self.crypto.public_key,
                              protocol_id=FIPAMessage.protocol_id,
                              message=msg_bytes)
            self.game_instance.stats_manager.add_dialogue_endstate(
                EndState.DECLINED_PROPOSE, dialogue.is_self_initiated)
        return result
Ejemplo n.º 9
0
    def _on_services_search_result(self, agent_pbks: List[str],
                                   is_searching_for_sellers: bool) -> None:
        """
        Process the search result for services.

        :param agent_pbks: the agent public keys matching the search query
        :param is_searching_for_sellers: whether it is searching for sellers or not

        :return: None
        """
        agent_pbks_set = set(agent_pbks)
        if self.crypto.public_key in agent_pbks_set:
            agent_pbks_set.remove(self.crypto.public_key)
        agent_pbks = list(agent_pbks_set)
        searched_for = "sellers" if is_searching_for_sellers else "buyers"
        logger.debug("[{}]: Found potential {}: {}".format(
            self.agent_name, searched_for, agent_pbks))

        services = self.game_instance.build_services_dict(
            is_supply=not is_searching_for_sellers)
        if services is None:
            response = "demanding" if is_searching_for_sellers else "supplying"
            logger.debug("[{}]: No longer {} any goods...".format(
                self.agent_name, response))
            return
        for agent_pbk in agent_pbks:
            dialogue = self.game_instance.dialogues.create_self_initiated(
                agent_pbk, self.crypto.public_key,
                not is_searching_for_sellers)
            cfp = FIPAMessage(
                message_id=STARTING_MESSAGE_ID,
                dialogue_id=dialogue.dialogue_label.dialogue_id,
                target=STARTING_MESSAGE_TARGET,
                performative=FIPAMessage.Performative.CFP,
                query=json.dumps(services).encode("utf-8"),
            )
            dialogue.outgoing_extend([cfp])
            cfp_bytes = FIPASerializer().encode(cfp)
            logger.debug(
                "[{}]: send_cfp_as_{}: msg_id={}, dialogue_id={}, destination={}, target={}, services={}"
                .format(
                    self.agent_name,
                    dialogue.role,
                    cfp.get("id"),
                    cfp.get("dialogue_id"),
                    agent_pbk,
                    cfp.get("target"),
                    services,
                ))
            self.mailbox.outbox.put_message(
                to=agent_pbk,
                sender=self.crypto.public_key,
                protocol_id=FIPAMessage.protocol_id,
                message=cfp_bytes,
            )
Ejemplo n.º 10
0
    def _handle_cfp(self, msg: FIPAMessage, sender: str, message_id: int,
                    dialogue: Dialogue) -> None:
        """
        Handle the CFP.

        If the CFP matches the supplied services then send a PROPOSE, otherwise send a DECLINE.

        :param msg: the message
        :param sender: the sender
        :param message_id: the message id
        :param dialogue: the dialogue object
        :return: None
        """
        new_message_id = message_id + 1
        new_target = message_id
        logger.info("[{}]: received CFP from sender={}".format(
            self.context.agent_name, sender[-5:]))
        query = cast(Query, msg.get("query"))
        strategy = cast(Strategy, self.context.strategy)

        if strategy.is_matching_supply(query):
            proposal, weather_data = strategy.generate_proposal_and_data(query)
            dialogue.weather_data = weather_data
            dialogue.proposal = proposal
            logger.info(
                "[{}]: sending sender={} a PROPOSE with proposal={}".format(
                    self.context.agent_name, sender[-5:], proposal.values))
            proposal_msg = FIPAMessage(
                message_id=new_message_id,
                dialogue_reference=dialogue.dialogue_label.dialogue_reference,
                target=new_target,
                performative=FIPAMessage.Performative.PROPOSE,
                proposal=[proposal])
            dialogue.outgoing_extend(proposal_msg)
            self.context.outbox.put_message(
                to=sender,
                sender=self.context.agent_public_key,
                protocol_id=FIPAMessage.protocol_id,
                message=FIPASerializer().encode(proposal_msg))
        else:
            logger.info("[{}]: declined the CFP from sender={}".format(
                self.context.agent_name, sender[-5:]))
            decline_msg = FIPAMessage(
                message_id=new_message_id,
                dialogue_reference=dialogue.dialogue_label.dialogue_reference,
                target=new_target,
                performative=FIPAMessage.Performative.DECLINE)
            dialogue.outgoing_extend(decline_msg)
            self.context.outbox.put_message(
                to=sender,
                sender=self.context.agent_public_key,
                protocol_id=FIPAMessage.protocol_id,
                message=FIPASerializer().encode(decline_msg))
Ejemplo n.º 11
0
def test_performative_not_recognized():
    """Tests an unknown Performative."""
    msg = FIPAMessage(
        performative=FIPAMessage.Performative.ACCEPT,
        message_id=0,
        dialogue_reference=(str(0), ''),
        target=1)

    with mock.patch("aea.protocols.fipa.message.FIPAMessage.Performative")\
            as mock_performative_enum:
        mock_performative_enum.ACCEPT.value = "unknown"
        assert not msg.check_consistency(),\
            "We expect that the check_consistency will return False"
Ejemplo n.º 12
0
    def handle_cfp(self, msg: FIPAMessage, sender: str, message_id: int,
                   dialogue_id: int) -> None:
        """
        Handle the CFP calls.

        :param msg: the message
        :param sender: the sender
        :param message_id: the message id
        :param dialogue_id: the dialogue id
        :return: None
        """
        new_message_id = message_id + 1
        new_target = message_id
        fetched_data = self.db.get_data_for_specific_dates(DATE_ONE, DATE_TWO)

        if len(fetched_data) >= 1:
            self.fetched_data = fetched_data
            total_price = self.fet_price * len(fetched_data)
            proposal = [
                Description({
                    "Rows": len(fetched_data),
                    "Price": total_price
                })
            ]
            logger.info(
                "[{}]: sending sender={} a proposal at price={}".format(
                    self.context.agent_name, sender, total_price))
            proposal_msg = FIPAMessage(
                message_id=new_message_id,
                dialogue_id=dialogue_id,
                target=new_target,
                performative=FIPAMessage.Performative.PROPOSE,
                proposal=proposal)
            self.context.outbox.put_message(
                to=sender,
                sender=self.context.agent_public_key,
                protocol_id=FIPAMessage.protocol_id,
                message=FIPASerializer().encode(proposal_msg))
        else:
            logger.info("[{}]: declined the CFP from sender={}".format(
                self.context.agent_name, sender))
            decline_msg = FIPAMessage(
                message_id=new_message_id,
                dialogue_id=dialogue_id,
                target=new_target,
                performative=FIPAMessage.Performative.DECLINE)
            self.context.outbox.put_message(
                to=sender,
                sender=self.context.agent_public_key,
                protocol_id=FIPAMessage.protocol_id,
                message=FIPASerializer().encode(decline_msg))
Ejemplo n.º 13
0
    def _on_match_accept(self, match_accept: FIPAMessage, dialogue: Dialogue) -> None:
        """
        Handle a matching Accept.

        :param match_accept: the MatchAccept message
        :param dialogue: the dialogue
        :return: None
        """
        transactions = cast(Transactions, self.context.transactions)
        assert dialogue.dialogue_label in transactions.pending_initial_acceptances \
            and match_accept.get("target") in transactions.pending_initial_acceptances[dialogue.dialogue_label]
        logger.debug("[{}]: on_match_accept: msg_id={}, dialogue_id={}, origin={}, target={}"
                     .format(self.context.agent_name, match_accept.get("id"), match_accept.get("dialogue_id"), dialogue.dialogue_label.dialogue_opponent_pbk, match_accept.get("target")))
        transaction_msg = transactions.pop_pending_initial_acceptance(dialogue.dialogue_label, cast(int, match_accept.get("target")))
        self.context.decision_maker_message_queue.put(transaction_msg)
Ejemplo n.º 14
0
    def decode(self, obj: bytes) -> Message:
        """Decode bytes into a FIPA message."""
        fipa_pb = fipa_pb2.FIPAMessage()
        fipa_pb.ParseFromString(obj)
        message_id = fipa_pb.message_id
        dialogue_reference = (fipa_pb.dialogue_starter_reference, fipa_pb.dialogue_responder_reference)
        target = fipa_pb.target

        performative = fipa_pb.WhichOneof("performative")
        performative_id = FIPAMessage.Performative(str(performative))
        performative_content = dict()
        if performative_id == FIPAMessage.Performative.CFP:
            query_type = fipa_pb.cfp.WhichOneof("query")
            if query_type == "nothing":
                query = None
            elif query_type == "query_bytes":
                query = pickle.loads(fipa_pb.cfp.query_bytes)
            elif query_type == "bytes":
                query = fipa_pb.cfp.bytes
            else:
                raise ValueError("Query type not recognized.")  # pragma: no cover
            performative_content["query"] = query
        elif performative_id == FIPAMessage.Performative.PROPOSE:
            descriptions = []
            for p_bytes in fipa_pb.propose.proposal:
                p = pickle.loads(p_bytes)  # type: Description
                descriptions.append(p)
            performative_content["proposal"] = descriptions
        elif performative_id == FIPAMessage.Performative.ACCEPT:
            pass
        elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT:
            pass
        elif performative_id == FIPAMessage.Performative.ACCEPT_W_ADDRESS:
            address = fipa_pb.accept_w_address.address
            performative_content['address'] = address
        elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT_W_ADDRESS:
            address = fipa_pb.match_accept_w_address.address
            performative_content['address'] = address
        elif performative_id == FIPAMessage.Performative.DECLINE:
            pass
        elif performative_id == FIPAMessage.Performative.INFORM:
            data = json.loads(fipa_pb.inform.bytes)
            performative_content["json_data"] = data
        else:
            raise ValueError("Performative not valid: {}.".format(performative))

        return FIPAMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target,
                           performative=performative, **performative_content)
Ejemplo n.º 15
0
    def send_fipa_message(self, envelope: Envelope) -> None:
        """
        Send fipa message handler.

        :param envelope: the message.
        :return: None
        """
        fipa_message = FIPASerializer().decode(envelope.message)
        id = fipa_message.get("message_id")
        dialogue_id = fipa_message.get("dialogue_id")
        destination = envelope.to
        target = fipa_message.get("target")
        performative = FIPAMessage.Performative(
            fipa_message.get("performative"))
        if performative == FIPAMessage.Performative.CFP:
            query = fipa_message.get("query")
            query = b"" if query is None else query
            if type(query) == Query:
                query = pickle.dumps(query)
            self.send_cfp(id, dialogue_id, destination, target, query)
        elif performative == FIPAMessage.Performative.PROPOSE:
            proposal = cast(List[Description], fipa_message.get("proposal"))
            proposal_b = pickle.dumps(proposal)  # type: bytes
            self.send_propose(id, dialogue_id, destination, target, proposal_b)
        elif performative == FIPAMessage.Performative.ACCEPT:
            self.send_accept(id, dialogue_id, destination, target)
        elif performative == FIPAMessage.Performative.MATCH_ACCEPT:
            self.send_accept(id, dialogue_id, destination, target)
        elif performative == FIPAMessage.Performative.DECLINE:
            self.send_decline(id, dialogue_id, destination, target)
        else:
            raise ValueError("OEF FIPA message not recognized.")
Ejemplo n.º 16
0
    def _handle_match_accept(self, msg: FIPAMessage, sender: str,
                             message_id: int, dialogue: Dialogue) -> None:
        """
        Handle the match accept.

        :param msg: the message
        :param sender: the sender
        :param message_id: the message id
        :param dialogue: the dialogue object
        :return: None
        """
        logger.info(
            "[{}]: received MATCH_ACCEPT_W_ADDRESS from sender={}".format(
                self.context.agent_name, sender[-5:]))
        address = cast(str, msg.get("address"))
        proposal = cast(Description, dialogue.proposal)
        tx_msg = TransactionMessage(
            performative=TransactionMessage.Performative.PROPOSE,
            skill_id="carpark_client",
            transaction_id="transaction0",
            sender=self.context.agent_public_keys['fetchai'],
            counterparty=address,
            is_sender_buyer=True,
            currency_pbk="FET",
            amount=proposal.values['price'],
            sender_tx_fee=0,
            counterparty_tx_fee=0,
            quantities_by_good_pbk={},
            dialogue_label=dialogue.dialogue_label.json,
            ledger_id='fetchai')
        self.context.decision_maker_message_queue.put_nowait(tx_msg)
        logger.info(
            "[{}]: proposing the transaction to the decision maker. Waiting for confirmation ..."
            .format(self.context.agent_name))
Ejemplo n.º 17
0
def test_fipa_propose_serialization():
    """Test that the serialization for the 'fipa' protocol works."""
    proposal = [
        Description({"foo1": 1, "bar1": 2}),
        Description({"foo2": 1, "bar2": 2}),
    ]
    msg = FIPAMessage(message_id=0,
                      dialogue_reference=(str(0), ''),
                      target=0,
                      performative=FIPAMessage.Performative.PROPOSE,
                      proposal=proposal)
    msg_bytes = FIPASerializer().encode(msg)
    envelope = Envelope(to="receiver",
                        sender="sender",
                        protocol_id=FIPAMessage.protocol_id,
                        message=msg_bytes)
    envelope_bytes = envelope.encode()

    actual_envelope = Envelope.decode(envelope_bytes)
    expected_envelope = envelope
    assert expected_envelope == actual_envelope

    actual_msg = FIPASerializer().decode(actual_envelope.message)
    expected_msg = msg

    p1 = actual_msg.get("proposal")
    p2 = expected_msg.get("proposal")
    assert p1[0].values == p2[0].values
    assert p1[1].values == p2[1].values
Ejemplo n.º 18
0
    def _handle_search(self, agents: List[str]) -> None:
        """
        Handle the search response.

        :param agents: the agents returned by the search
        :return: None
        """
        if len(agents) > 0:
            logger.info("[{}]: found agents={}, stopping search.".format(self.context.agent_name, list(map(lambda x: x[-5:], agents))))
            strategy = cast(Strategy, self.context.strategy)
            # stopping search
            strategy.is_searching = False
            # pick first agent found
            opponent_pbk = agents[0]
            dialogues = cast(Dialogues, self.context.dialogues)
            dialogue = dialogues.create_self_initiated(opponent_pbk, self.context.agent_public_key, is_seller=False)
            query = strategy.get_service_query()
            logger.info("[{}]: sending CFP to agent={}".format(self.context.agent_name, opponent_pbk[-5:]))
            cfp_msg = FIPAMessage(message_id=FIPAMessage.STARTING_MESSAGE_ID,
                                  dialogue_reference=dialogue.dialogue_label.dialogue_reference,
                                  performative=FIPAMessage.Performative.CFP,
                                  target=FIPAMessage.STARTING_TARGET,
                                  query=query)
            dialogue.outgoing_extend(cfp_msg)
            self.context.outbox.put_message(to=opponent_pbk,
                                            sender=self.context.agent_public_key,
                                            protocol_id=FIPAMessage.protocol_id,
                                            message=FIPASerializer().encode(cfp_msg))
        else:
            logger.info("[{}]: found no agents, continue searching.".format(self.context.agent_name))
Ejemplo n.º 19
0
    def handle(self, message: Message, sender: str) -> None:
        """
        Implement the reaction to a message.

        :param message: the message
        :param sender: the sender
        :return: None
        """
        tx_msg_response = cast(TransactionMessage, message)
        if tx_msg_response is not None and \
                TransactionMessage.Performative(tx_msg_response.get("performative")) == TransactionMessage.Performative.ACCEPT:
            logger.info("[{}]: transaction was successful.".format(self.context.agent_name))
            json_data = {'transaction_digest': tx_msg_response.get("transaction_digest")}
            dialogue_label = DialogueLabel.from_json(cast(Dict[str, str], tx_msg_response.get("dialogue_label")))
            dialogues = cast(Dialogues, self.context.dialogues)
            dialogue = dialogues.dialogues[dialogue_label]
            fipa_msg = cast(FIPAMessage, dialogue.last_incoming_message)
            new_message_id = cast(int, fipa_msg.get("message_id")) + 1
            new_target_id = cast(int, fipa_msg.get("target")) + 1
            counterparty_pbk = dialogue.dialogue_label.dialogue_opponent_pbk
            inform_msg = FIPAMessage(message_id=new_message_id,
                                     dialogue_reference=dialogue.dialogue_label.dialogue_reference,
                                     target=new_target_id,
                                     performative=FIPAMessage.Performative.INFORM,
                                     json_data=json_data)
            dialogue.outgoing_extend(inform_msg)
            self.context.outbox.put_message(to=counterparty_pbk,
                                            sender=self.context.agent_public_key,
                                            protocol_id=FIPAMessage.protocol_id,
                                            message=FIPASerializer().encode(inform_msg))
            logger.info("[{}]: informing counterparty={} of transaction digest.".format(self.context.agent_name, counterparty_pbk[-5:]))
            self._received_tx_message = True
        else:
            logger.info("[{}]: transaction was not successful.".format(self.context.agent_name))
Ejemplo n.º 20
0
    def _handle_match_accept(self, msg: FIPAMessage, sender: str,
                             message_id: int, dialogue: Dialogue) -> None:
        """
        Handle the match accept.

        :param msg: the message
        :param sender: the sender
        :param message_id: the message id
        :param dialogue: the dialogue object
        :return: None
        """
        new_message_id = message_id + 1
        new_target_id = message_id
        counterparty_pbk = dialogue.dialogue_label.dialogue_opponent_pbk
        inform_msg = FIPAMessage(
            message_id=new_message_id,
            dialogue_reference=dialogue.dialogue_label.dialogue_reference,
            target=new_target_id,
            performative=FIPAMessage.Performative.INFORM,
            json_data={"Done": "Sending payment via bank transfer"})
        dialogue.outgoing_extend(inform_msg)
        self.context.outbox.put_message(
            to=counterparty_pbk,
            sender=self.context.agent_public_key,
            protocol_id=FIPAMessage.protocol_id,
            message=FIPASerializer().encode(inform_msg))
        logger.info("[{}]: informing counterparty={} of payment.".format(
            self.context.agent_name, counterparty_pbk[-5:]))
        self._received_tx_message = True
Ejemplo n.º 21
0
    def handle(self, message: Message, sender: str) -> None:
        """
        Implement the reaction to a message.

        :param message: the message
        :param sender: the sender
        :return: None
        """
        # convenience representations
        fipa_msg = cast(FIPAMessage, message)
        msg_performative = FIPAMessage.Performative(message.get('performative'))
        message_id = cast(int, message.get("message_id"))

        # recover dialogue
        dialogues = cast(Dialogues, self.context.dialogues)
        if dialogues.is_belonging_to_registered_dialogue(fipa_msg, sender, self.context.agent_public_key):
            dialogue = cast(Dialogue, dialogues.get_dialogue(fipa_msg, sender, self.context.agent_public_key))
            dialogue.incoming_extend(fipa_msg)
        else:
            self._handle_unidentified_dialogue(fipa_msg, sender)
            return

        # handle message
        if msg_performative == FIPAMessage.Performative.PROPOSE:
            self._handle_propose(fipa_msg, sender, message_id, dialogue)
        elif msg_performative == FIPAMessage.Performative.DECLINE:
            self._handle_decline(fipa_msg, sender, message_id, dialogue)
        elif msg_performative == FIPAMessage.Performative.MATCH_ACCEPT_W_ADDRESS:
            self._handle_match_accept(fipa_msg, sender, message_id, dialogue)
        elif msg_performative == FIPAMessage.Performative.INFORM:
            self._handle_inform(fipa_msg, sender, message_id, dialogue)
Ejemplo n.º 22
0
    def _handle_accept(self, msg: FIPAMessage, sender: str, message_id: int,
                       dialogue: Dialogue) -> None:
        """
        Handle the ACCEPT.

        Respond with a MATCH_ACCEPT_W_ADDRESS which contains the address to send the funds to.

        :param msg: the message
        :param sender: the sender
        :param message_id: the message id
        :param dialogue: the dialogue object
        :return: None
        """
        new_message_id = message_id + 1
        new_target = message_id
        logger.info("[{}]: received ACCEPT from sender={}".format(
            self.context.agent_name, sender[-5:]))
        logger.info("[{}]: sending MATCH_ACCEPT_W_ADDRESS to sender={}".format(
            self.context.agent_name, sender[-5:]))
        match_accept_msg = FIPAMessage(
            message_id=new_message_id,
            dialogue_reference=dialogue.dialogue_label.dialogue_reference,
            target=new_target,
            performative=FIPAMessage.Performative.MATCH_ACCEPT_W_ADDRESS,
            address=self.context.agent_addresses['fetchai'])
        dialogue.outgoing_extend(match_accept_msg)
        self.context.outbox.put_message(
            to=sender,
            sender=self.context.agent_public_key,
            protocol_id=FIPAMessage.protocol_id,
            message=FIPASerializer().encode(match_accept_msg))
        strategy = cast(Strategy, self.context.strategy)
        strategy.db.set_dialogue_status(str(dialogue.dialogue_label),
                                        sender[-5:], "received_accept",
                                        "send_match_accept")
Ejemplo n.º 23
0
    def handle_envelope(self, envelope: Envelope) -> None:
        """
        Implement the reaction to an envelope.

        :param envelope: the envelope
        :return: None
        """
        msg = FIPASerializer().decode(envelope.message)
        msg_performative = FIPAMessage.Performative(msg.get('performative'))
        proposals = cast(List[Description], msg.get("proposal"))
        message_id = cast(int, msg.get("id"))
        dialogue_id = cast(int, msg.get("dialogue_id"))
        if msg_performative == FIPAMessage.Performative.PROPOSE:
            if proposals is not []:
                for item in proposals:
                    logger.info(
                        "[{}]: received proposal={} in dialogue={}".format(
                            self.context.agent_name, item.values, dialogue_id))
                    if "Price" in item.values.keys():
                        if item.values["Price"] < self.max_price:
                            self.handle_accept(envelope.sender, message_id,
                                               dialogue_id)
                        else:
                            self.handle_decline(envelope.sender, message_id,
                                                dialogue_id)
Ejemplo n.º 24
0
    def handle_envelope(self, envelope: Envelope) -> None:
        """
        Implement the reaction to an envelope.

        :param envelope: the envelope
        :return: None
        """
        msg = OEFSerializer().decode(envelope.message)
        msg_type = OEFMessage.Type(msg.get("type"))

        if msg_type is OEFMessage.Type.SEARCH_RESULT:
            agents = cast(List[str], msg.get("agents"))
            logger.info("[{}]: found agents={}".format(self.context.agent_name,
                                                       agents))
            for agent in agents:
                msg = FIPAMessage(message_id=STARTING_MESSAGE_ID,
                                  dialogue_id=self.dialogue_id,
                                  performative=FIPAMessage.Performative.CFP,
                                  target=STARTING_TARGET_ID,
                                  query=None)
                self.dialogue_id += 1
                self.context.outbox.put_message(
                    to=agent,
                    sender=self.context.agent_public_key,
                    protocol_id=FIPAMessage.protocol_id,
                    message=FIPASerializer().encode(msg))
Ejemplo n.º 25
0
    def on_propose(self, msg_id: int, dialogue_id: int, origin: str,
                   target: int, b_proposals: PROPOSE_TYPES) -> None:
        """
        On propose event handler.

        :param msg_id: the message id.
        :param dialogue_id: the dialogue id.
        :param origin: the public key of the sender.
        :param target: the message target.
        :param b_proposals: the proposals.
        :return: None
        """
        if type(b_proposals) == bytes:
            proposals = pickle.loads(b_proposals)  # type: List[Description]
        else:
            raise ValueError("No support for non-bytes proposals.")

        msg = FIPAMessage(message_id=msg_id,
                          dialogue_id=dialogue_id,
                          target=target,
                          performative=FIPAMessage.Performative.PROPOSE,
                          proposal=proposals)
        msg_bytes = FIPASerializer().encode(msg)
        envelope = Envelope(to=self.public_key,
                            sender=origin,
                            protocol_id=FIPAMessage.protocol_id,
                            message=msg_bytes)
        self.in_queue.put(envelope)
Ejemplo n.º 26
0
    def _handle_accept(self, msg: FIPAMessage, sender: str, message_id: int,
                       dialogue: Dialogue) -> None:
        """
        Handle the ACCEPT.

        Respond with a MATCH_ACCEPT_W_ADDRESS which contains the address to send the funds to.

        :param msg: the message
        :param sender: the sender
        :param message_id: the message id
        :param dialogue: the dialogue object
        :return: None
        """
        new_message_id = message_id + 1
        new_target = message_id
        logger.info("[{}]: received ACCEPT from sender={}".format(
            self.context.agent_name, sender[-5:]))
        logger.info("[{}]: sending MATCH_ACCEPT_W_ADDRESS to sender={}".format(
            self.context.agent_name, sender[-5:]))
        # proposal = cast(Description, dialogue.proposal)
        # identifier = cast(str, proposal.values.get("ledger_id"))
        match_accept_msg = FIPAMessage(
            message_id=new_message_id,
            dialogue_reference=dialogue.dialogue_label.dialogue_reference,
            target=new_target,
            performative=FIPAMessage.Performative.MATCH_ACCEPT_W_ADDRESS,
            address="no_address")
        dialogue.outgoing_extend(match_accept_msg)
        self.context.outbox.put_message(
            to=sender,
            sender=self.context.agent_public_key,
            protocol_id=FIPAMessage.protocol_id,
            message=FIPASerializer().encode(match_accept_msg))
Ejemplo n.º 27
0
    def on_cfp(self, msg_id: int, dialogue_id: int, origin: str, target: int,
               query: CFP_TYPES) -> None:
        """
        On cfp event handler.

        :param msg_id: the message id.
        :param dialogue_id: the dialogue id.
        :param origin: the public key of the sender.
        :param target: the message target.
        :param query: the query.
        :return: None
        """
        assert self.in_queue is not None
        assert self.loop is not None
        logger.warning(
            'Accepting on_cfp from deprecated API: msg_id={}, dialogue_id={}, origin={}, target={}. Continuing dialogue via envelopes!'
            .format(msg_id, dialogue_id, origin, target))
        try:
            query = pickle.loads(query)
        except Exception:
            pass
        msg = FIPAMessage(message_id=msg_id,
                          dialogue_reference=(str(dialogue_id), ''),
                          target=target,
                          performative=FIPAMessage.Performative.CFP,
                          query=query if query != b"" else None)
        msg_bytes = FIPASerializer().encode(msg)
        envelope = Envelope(to=self.public_key,
                            sender=origin,
                            protocol_id=FIPAMessage.protocol_id,
                            message=msg_bytes)
        asyncio.run_coroutine_threadsafe(self.in_queue.put(envelope),
                                         self.loop).result()
Ejemplo n.º 28
0
    def on_cfp(self, msg_id: int, dialogue_id: int, origin: str, target: int,
               query: CFP_TYPES) -> None:
        """
        On cfp event handler.

        :param msg_id: the message id.
        :param dialogue_id: the dialogue id.
        :param origin: the public key of the sender.
        :param target: the message target.
        :param query: the query.
        :return: None
        """
        try:
            query = pickle.loads(query)
        except Exception:
            pass
        msg = FIPAMessage(message_id=msg_id,
                          dialogue_id=dialogue_id,
                          target=target,
                          performative=FIPAMessage.Performative.CFP,
                          query=query if query != b"" else None)
        msg_bytes = FIPASerializer().encode(msg)
        envelope = Envelope(to=self.public_key,
                            sender=origin,
                            protocol_id=FIPAMessage.protocol_id,
                            message=msg_bytes)
        self.in_queue.put(envelope)
Ejemplo n.º 29
0
    def decode(self, obj: bytes) -> Message:
        """Decode bytes into a FIPA message."""
        fipa_pb = fipa_pb2.FIPAMessage()
        fipa_pb.ParseFromString(obj)
        message_id = fipa_pb.message_id
        dialogue_id = fipa_pb.dialogue_id
        target = fipa_pb.target

        performative = fipa_pb.WhichOneof("performative")
        performative_id = FIPAMessage.Performative(str(performative))
        performative_content = dict()
        if performative_id == FIPAMessage.Performative.CFP:
            query_type = fipa_pb.cfp.WhichOneof("query")
            if query_type == "nothing":
                query = None
            elif query_type == "query_bytes":
                query = pickle.loads(fipa_pb.cfp.query_bytes)
            elif query_type == "bytes":
                query = fipa_pb.cfp.bytes
            else:
                raise ValueError("Query type not recognized.")

            performative_content["query"] = query
        elif performative_id == FIPAMessage.Performative.PROPOSE:
            descriptions = []
            for p_bytes in fipa_pb.propose.proposal:
                p = pickle.loads(p_bytes)  # type: Description
                descriptions.append(p)
            performative_content["proposal"] = descriptions
        elif performative_id == FIPAMessage.Performative.ACCEPT:
            pass
        elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT:
            pass
        elif performative_id == FIPAMessage.Performative.DECLINE:
            pass
        else:
            raise ValueError(
                "Performative not valid: {}.".format(performative))

        return FIPAMessage(message_id=message_id,
                           dialogue_id=dialogue_id,
                           target=target,
                           performative=performative,
                           **performative_content)
Ejemplo n.º 30
0
    def test_error_skill_unsupported_protocol(self):
        """Test the unsupported error message."""
        msg = FIPAMessage(message_id=0,
                          dialogue_reference=(str(0), ''),
                          target=0,
                          performative=FIPAMessage.Performative.ACCEPT)
        msg_bytes = FIPASerializer().encode(msg)
        envelope = Envelope(to=self.public_key,
                            sender=self.public_key,
                            protocol_id=FIPAMessage.protocol_id,
                            message=msg_bytes)

        self.my_error_handler.send_unsupported_protocol(envelope)

        envelope = self.my_aea.inbox.get(block=True, timeout=1.0)
        msg = DefaultSerializer().decode(envelope.message)
        assert msg.get("type") == DefaultMessage.Type.ERROR
        assert msg.get("error_code"
                       ) == DefaultMessage.ErrorCode.UNSUPPORTED_PROTOCOL.value