Example #1
0
def test_marginal_utility():
    """Test the marginal utility."""
    currency_holdings = {"FET": 100}
    utility_params = {"good_id": 20.0}
    exchange_params = {"FET": 10.0}
    good_holdings = {"good_id": 2}
    tx_fee = 9
    preferences = Preferences()
    preferences.set(
        utility_params_by_good_id=utility_params,
        exchange_params_by_currency_id=exchange_params,
        tx_fee=tx_fee,
    )
    delta_good_holdings = {"good_id": 1}
    delta_currency_holdings = {"FET": -5}
    ownership_state = OwnershipState()
    ownership_state.set(
        amount_by_currency_id=currency_holdings,
        quantities_by_good_id=good_holdings,
    )
    marginal_utility = preferences.marginal_utility(
        ownership_state=ownership_state,
        delta_quantities_by_good_id=delta_good_holdings,
        delta_amount_by_currency_id=delta_currency_holdings,
    )
    assert marginal_utility is not None, "Marginal utility must not be none."
Example #2
0
def test_transaction_update_receive():
    """Test the transaction update when receiving tokens."""
    currency_endowment = {"FET": 75}
    good_endowment = {"good_id": 30}
    ownership_state = OwnershipState()
    ownership_state.set(
        amount_by_currency_id=currency_endowment,
        quantities_by_good_id=good_endowment,
    )
    assert ownership_state.amount_by_currency_id == currency_endowment
    assert ownership_state.quantities_by_good_id == good_endowment
    tx_message = TransactionMessage(
        performative=TransactionMessage.Performative.PROPOSE_FOR_SETTLEMENT,
        skill_callback_ids=[PublicId(AUTHOR, "a_skill", "0.1.0")],
        tx_id="transaction0",
        tx_sender_addr="agent_1",
        tx_counterparty_addr="pk",
        tx_amount_by_currency_id={"FET": 20},
        tx_sender_fee=5,
        tx_counterparty_fee=0,
        tx_quantities_by_good_id={"good_id": -10},
        info={"some_info_key": "some_info_value"},
        ledger_id="fetchai",
        tx_nonce="transaction nonce",
    )
    ownership_state._update(tx_message=tx_message)
    expected_amount_by_currency_id = {"FET": 90}
    expected_quantities_by_good_id = {"good_id": 20}
    assert ownership_state.amount_by_currency_id == expected_amount_by_currency_id
    assert ownership_state.quantities_by_good_id == expected_quantities_by_good_id
Example #3
0
def test_transaction_is_affordable_agent_is_buyer():
    """Check if the agent has the money to cover the sender_amount (the agent=sender is the buyer)."""
    currency_endowment = {"FET": 100}
    good_endowment = {"good_id": 20}
    ownership_state = OwnershipState()
    ownership_state.set(
        amount_by_currency_id=currency_endowment,
        quantities_by_good_id=good_endowment,
    )
    tx_message = TransactionMessage(
        performative=TransactionMessage.Performative.PROPOSE_FOR_SETTLEMENT,
        skill_callback_ids=[PublicId(AUTHOR, "a_skill", "0.1.0")],
        tx_id="transaction0",
        tx_sender_addr="agent_1",
        tx_counterparty_addr="pk",
        tx_amount_by_currency_id={"FET": -1},
        tx_sender_fee=0,
        tx_counterparty_fee=0,
        tx_quantities_by_good_id={"good_id": 10},
        info={"some_info_key": "some_info_value"},
        ledger_id="fetchai",
        tx_nonce="transaction nonce",
    )

    assert ownership_state.is_affordable_transaction(
        tx_message=tx_message), "We should have the money for the transaction!"
Example #4
0
def test_apply():
    """Test the apply function."""
    currency_endowment = {"FET": 100}
    good_endowment = {"good_id": 2}
    ownership_state = OwnershipState()
    ownership_state.set(
        amount_by_currency_id=currency_endowment,
        quantities_by_good_id=good_endowment,
    )
    tx_message = TransactionMessage(
        performative=TransactionMessage.Performative.PROPOSE_FOR_SETTLEMENT,
        skill_callback_ids=[PublicId(AUTHOR, "a_skill", "0.1.0")],
        tx_id="transaction0",
        tx_sender_addr="agent_1",
        tx_counterparty_addr="pk",
        tx_amount_by_currency_id={"FET": -20},
        tx_sender_fee=5,
        tx_counterparty_fee=0,
        tx_quantities_by_good_id={"good_id": 10},
        info={"some_info_key": "some_info_value"},
        ledger_id="fetchai",
        tx_nonce="transaction nonce",
    )
    list_of_transactions = [tx_message]
    state = ownership_state
    new_state = ownership_state.apply_transactions(
        transactions=list_of_transactions)
    assert (
        state != new_state
    ), "after applying a list_of_transactions must have a different state!"
Example #5
0
def tests_transaction_is_affordable_else_statement():
    """Check that the function returns false if we cannot satisfy any if/elif statements."""
    currency_endowment = {"FET": 0}
    good_endowment = {"good_id": 0}
    ownership_state = OwnershipState()
    ownership_state.set(
        amount_by_currency_id=currency_endowment,
        quantities_by_good_id=good_endowment,
    )
    tx_message = TransactionMessage(
        performative=TransactionMessage.Performative.PROPOSE_FOR_SETTLEMENT,
        skill_callback_ids=[PublicId(AUTHOR, "a_skill", "0.1.0")],
        tx_id="transaction0",
        tx_sender_addr="agent_1",
        tx_counterparty_addr="pk",
        tx_amount_by_currency_id={"FET": 10},
        tx_sender_fee=0,
        tx_counterparty_fee=0,
        tx_quantities_by_good_id={"good_id": 50},
        info={"some_info_key": "some_info_value"},
        ledger_id="fetchai",
        tx_nonce="transaction nonce",
    )

    assert not ownership_state.is_affordable_transaction(
        tx_message=tx_message), "We must reject the transaction."
Example #6
0
def test_transaction_is_affordable_there_is_no_wealth():
    """Reject the transaction when there is no wealth exchange."""
    currency_endowment = {"FET": 0}
    good_endowment = {"good_id": 0}
    ownership_state = OwnershipState()
    ownership_state.set(
        amount_by_currency_id=currency_endowment,
        quantities_by_good_id=good_endowment,
    )
    tx_message = TransactionMessage(
        performative=TransactionMessage.Performative.PROPOSE_FOR_SETTLEMENT,
        skill_callback_ids=[PublicId(AUTHOR, "a_skill", "0.1.0")],
        tx_id="transaction0",
        tx_sender_addr="agent_1",
        tx_counterparty_addr="pk",
        tx_amount_by_currency_id={"FET": 0},
        tx_sender_fee=0,
        tx_counterparty_fee=0,
        tx_quantities_by_good_id={"good_id": 0},
        info={"some_info_key": "some_info_value"},
        ledger_id="fetchai",
        tx_nonce="transaction nonce",
    )

    assert not ownership_state.is_affordable_transaction(
        tx_message=tx_message), "We must reject the transaction."
def test_non_initialized_ownership_state_raises_exception():
    """Test that non-initialized ownership state raises exception."""
    ownership_state = OwnershipState()

    with pytest.raises(ValueError):
        ownership_state.amount_by_currency_id

    with pytest.raises(ValueError):
        ownership_state.quantities_by_good_id
 def test_transaction_is_affordable_agent_is_seller(self):
     """Check if the agent has the goods (the agent=sender is the seller)."""
     currency_endowment = {"FET": 100}
     good_endowment = {"good_id": 20}
     ownership_state = OwnershipState()
     ownership_state.set(
         amount_by_currency_id=currency_endowment,
         quantities_by_good_id=good_endowment,
     )
     assert ownership_state.is_affordable_transaction(
         terms=self.seller_terms), "We must reject the transaction."
Example #9
0
def test_initialisation():
    """Test the initialisation of the ownership_state."""
    currency_endowment = {"FET": 100}
    good_endowment = {"good_id": 2}
    ownership_state = OwnershipState()
    ownership_state.set(
        amount_by_currency_id=currency_endowment, quantities_by_good_id=good_endowment,
    )
    assert ownership_state.amount_by_currency_id is not None
    assert ownership_state.quantities_by_good_id is not None
    assert ownership_state.is_initialized
 def test_transaction_is_affordable_malformed(self):
     """Reject the transaction when there is no wealth exchange."""
     currency_endowment = {"FET": 100}
     good_endowment = {"good_id": 20}
     ownership_state = OwnershipState()
     ownership_state.set(
         amount_by_currency_id=currency_endowment,
         quantities_by_good_id=good_endowment,
     )
     assert not ownership_state.is_affordable_transaction(
         terms=self.malformed_terms), "We must reject the transaction."
 def test_transaction_is_affordable_agent_is_buyer(self):
     """Check if the agent has the money to cover the sender_amount (the agent=sender is the buyer)."""
     currency_endowment = {"FET": 100}
     good_endowment = {"good_id": 20}
     ownership_state = OwnershipState()
     ownership_state.set(
         amount_by_currency_id=currency_endowment,
         quantities_by_good_id=good_endowment,
     )
     assert ownership_state.is_affordable(
         terms=self.buyer_terms
     ), "We should have the money for the transaction!"
def test_is_affordable_for_uninitialized():
    """Test the initialisation of the ownership_state."""
    ownership_state = OwnershipState()
    buyer_terms = Terms(
        ledger_id=ETHEREUM,
        sender_address="pk1",
        counterparty_address="pk2",
        amount_by_currency_id={"FET": -1},
        is_sender_payable_tx_fee=True,
        quantities_by_good_id={"good_id": 10},
        nonce="transaction nonce",
    )
    assert ownership_state.is_affordable(
        terms=buyer_terms), "Any transaction should be classed as affordable."
Example #13
0
 def test_apply(self):
     """Test the apply function."""
     currency_endowment = {"FET": 100}
     good_endowment = {"good_id": 2}
     ownership_state = OwnershipState()
     ownership_state.set(
         amount_by_currency_id=currency_endowment,
         quantities_by_good_id=good_endowment,
     )
     list_of_terms = [self.buyer_terms]
     state = ownership_state
     new_state = ownership_state.apply_transactions(list_of_terms=list_of_terms)
     assert (
         state != new_state
     ), "after applying a list_of_terms must have a different state!"
Example #14
0
def test_is_utility_enhancing_uninitialized():
    """Test is_utility_enhancing when the states are uninitialized."""
    ownership_state = OwnershipState()
    preferences = Preferences()
    terms = Terms(
        ledger_id="ethereum",
        sender_address="agent_1",
        counterparty_address="pk",
        amount_by_currency_id={"FET": -20},
        is_sender_payable_tx_fee=True,
        quantities_by_good_id={"good_id": 10},
        nonce="transaction nonce",
    )
    assert preferences.is_utility_enhancing(
        ownership_state=ownership_state,
        terms=terms), "Should enhance utility."
 def test_transaction_update_receive(self):
     """Test the transaction update when receiving tokens."""
     currency_endowment = {"FET": 100}
     good_endowment = {"good_id": 20}
     ownership_state = OwnershipState()
     ownership_state.set(
         amount_by_currency_id=currency_endowment,
         quantities_by_good_id=good_endowment,
     )
     assert ownership_state.amount_by_currency_id == currency_endowment
     assert ownership_state.quantities_by_good_id == good_endowment
     ownership_state.update(terms=self.seller_terms)
     expected_amount_by_currency_id = {"FET": 101}
     expected_quantities_by_good_id = {"good_id": 10}
     assert ownership_state.amount_by_currency_id == expected_amount_by_currency_id
     assert ownership_state.quantities_by_good_id == expected_quantities_by_good_id
Example #16
0
def test_score_diff_from_transaction():
    """Test the difference between the scores."""
    good_holdings = {"good_id": 2}
    currency_holdings = {"FET": 100}
    utility_params = {"good_id": 20.0}
    exchange_params = {"FET": 10.0}
    tx_fee = 3
    ownership_state = OwnershipState()
    ownership_state.set(amount_by_currency_id=currency_holdings,
                        quantities_by_good_id=good_holdings)
    preferences = Preferences()
    preferences.set(
        utility_params_by_good_id=utility_params,
        exchange_params_by_currency_id=exchange_params,
        tx_fee=tx_fee,
    )
    tx_message = TransactionMessage(
        performative=TransactionMessage.Performative.PROPOSE_FOR_SETTLEMENT,
        skill_callback_ids=[PublicId(AUTHOR, "a_skill", "0.1.0")],
        tx_id="transaction0",
        tx_sender_addr="agent_1",
        tx_counterparty_addr="pk",
        tx_amount_by_currency_id={"FET": -20},
        tx_sender_fee=preferences.seller_transaction_fee,
        tx_counterparty_fee=preferences.buyer_transaction_fee,
        tx_quantities_by_good_id={"good_id": 10},
        info={"some_info_key": "some_info_value"},
        ledger_id="fetchai",
        tx_nonce="transaction nonce",
    )

    cur_score = preferences.utility(quantities_by_good_id=good_holdings,
                                    amount_by_currency_id=currency_holdings)
    new_state = ownership_state.apply_transactions([tx_message])
    new_score = preferences.utility(
        quantities_by_good_id=new_state.quantities_by_good_id,
        amount_by_currency_id=new_state.amount_by_currency_id,
    )
    dif_scores = new_score - cur_score
    score_difference = preferences.utility_diff_from_transaction(
        ownership_state=ownership_state, tx_message=tx_message)
    assert (
        score_difference == dif_scores
    ), "The calculated difference must be equal to the return difference from the function."
Example #17
0
def test_score_diff_from_transaction():
    """Test the difference between the scores."""
    good_holdings = {"good_id": 2}
    currency_holdings = {"FET": 100}
    utility_params = {"good_id": 20.0}
    exchange_params = {"FET": 10.0}
    ownership_state = OwnershipState()
    ownership_state.set(
        amount_by_currency_id=currency_holdings, quantities_by_good_id=good_holdings
    )
    preferences = Preferences()
    preferences.set(
        utility_params_by_good_id=utility_params,
        exchange_params_by_currency_id=exchange_params,
    )
    terms = Terms(
        ledger_id=ETHEREUM,
        sender_address="agent_1",
        counterparty_address="pk",
        amount_by_currency_id={"FET": -20},
        is_sender_payable_tx_fee=True,
        quantities_by_good_id={"good_id": 10},
        nonce="transaction nonce",
    )
    cur_score = preferences.utility(
        quantities_by_good_id=good_holdings, amount_by_currency_id=currency_holdings
    )
    new_state = ownership_state.apply_transactions([terms])
    new_score = preferences.utility(
        quantities_by_good_id=new_state.quantities_by_good_id,
        amount_by_currency_id=new_state.amount_by_currency_id,
    )
    diff_scores = new_score - cur_score
    score_difference = preferences.utility_diff_from_transaction(
        ownership_state=ownership_state, terms=terms
    )
    assert (
        score_difference == diff_scores
    ), "The calculated difference must be equal to the return difference from the function."
    assert not preferences.is_utility_enhancing(
        ownership_state=ownership_state, terms=terms
    ), "Should not enhance utility."
Example #18
0
    def setup_class(cls):
        """Initialise the decision maker."""
        cls._patch_logger()
        cls.multiplexer = Multiplexer([_make_dummy_connection()])
        private_key_pem_path = os.path.join(CUR_PATH, "data",
                                            "fet_private_key.txt")
        eth_private_key_pem_path = os.path.join(CUR_PATH, "data",
                                                "fet_private_key.txt")
        cls.wallet = Wallet({
            FetchAICrypto.identifier:
            private_key_pem_path,
            EthereumCrypto.identifier:
            eth_private_key_pem_path,
        })
        cls.ledger_apis = LedgerApis(
            {FetchAICrypto.identifier: DEFAULT_FETCHAI_CONFIG},
            FetchAICrypto.identifier)
        cls.agent_name = "test"
        cls.identity = Identity(
            cls.agent_name,
            addresses=cls.wallet.addresses,
            default_address_key=FetchAICrypto.identifier,
        )
        cls.ownership_state = OwnershipState()
        cls.preferences = Preferences()
        cls.decision_maker_handler = DecisionMakerHandler(
            identity=cls.identity,
            wallet=cls.wallet,
            ledger_apis=cls.ledger_apis,
        )
        cls.decision_maker = DecisionMaker(cls.decision_maker_handler)
        cls.multiplexer.connect()

        cls.tx_id = "transaction0"
        cls.tx_sender_addr = "agent_1"
        cls.tx_counterparty_addr = "pk"
        cls.info = {"some_info_key": "some_info_value"}
        cls.ledger_id = "fetchai"

        cls.decision_maker.start()