def _handle_state_update_message(self, state_update_message: StateUpdateMessage) -> None: """ Handle a state update message. :param state_update_message: the state update message :return: None """ assert self._ownership_state is None, "OwnershipState already initialized." assert self._preferences is None, "Preferences already initialized." currency_endowment = cast(CurrencyEndowment, state_update_message.get("currency_endowment")) good_endowment = cast(GoodEndowment, state_update_message.get("good_endowment")) self.ownership_state.init(currency_endowment=currency_endowment, good_endowment=good_endowment) utility_params = cast(UtilityParams, state_update_message.get("utility_params")) exchange_params = cast(ExchangeParams, state_update_message.get("exchange_params")) self.preferences.init(exchange_params=exchange_params, utility_params=utility_params)
def test_decision_maker_handle_state_update_initialize_and_apply(self): """Test the handle method for a stateUpdate message with Initialize and Apply performative.""" good_holdings = {"good_id": 2} currency_holdings = {"FET": 100} utility_params = {"good_id": 20.0} exchange_params = {"FET": 10.0} currency_deltas = {"FET": -10} good_deltas = {"good_id": 1} tx_fee = 1 state_update_message = StateUpdateMessage( performative=StateUpdateMessage.Performative.INITIALIZE, amount_by_currency_id=currency_holdings, quantities_by_good_id=good_holdings, exchange_params_by_currency_id=exchange_params, utility_params_by_good_id=utility_params, tx_fee=tx_fee, ) self.decision_maker.handle(state_update_message) assert (self.decision_maker_handler.context.ownership_state. amount_by_currency_id is not None) assert (self.decision_maker_handler.context.ownership_state. quantities_by_good_id is not None) assert (self.decision_maker_handler.context.preferences. exchange_params_by_currency_id is not None) assert (self.decision_maker_handler.context.preferences. utility_params_by_good_id is not None) state_update_message = StateUpdateMessage( performative=StateUpdateMessage.Performative.APPLY, amount_by_currency_id=currency_deltas, quantities_by_good_id=good_deltas, ) self.decision_maker.handle(state_update_message) expected_amount_by_currency_id = { key: currency_holdings.get(key, 0) + currency_deltas.get(key, 0) for key in set(currency_holdings) | set(currency_deltas) } expected_quantities_by_good_id = { key: good_holdings.get(key, 0) + good_deltas.get(key, 0) for key in set(good_holdings) | set(good_deltas) } assert ( self.decision_maker_handler.context.ownership_state. amount_by_currency_id == expected_amount_by_currency_id ), "The amount_by_currency_id must be equal with the expected amount." assert (self.decision_maker_handler.context.ownership_state. quantities_by_good_id == expected_quantities_by_good_id)
def _on_start(self, tac_message: TACMessage, controller_pbk: Address) -> None: """ Handle the 'start' event emitted by the controller. :param tac_message: the game data :return: None """ logger.info( "[{}]: Received start event from the controller. Starting to compete..." .format(self.context.agent_name)) game = cast(Game, self.context.game) game.init(tac_message, controller_pbk) game.update_game_phase(Phase.GAME) state_update_msg = StateUpdateMessage( performative=StateUpdateMessage.Performative.INITIALIZE, amount_by_currency=cast(Dict[str, int], tac_message.get("amount_by_currency")), quantities_by_good_pbk=cast( Dict[str, int], tac_message.get("quantities_by_good_pbk")), exchange_params_by_currency=cast( Dict[str, float], tac_message.get("exchange_params_by_currency")), utility_params_by_good_pbk=cast( Dict[str, float], tac_message.get("utility_params_by_good_pbk")), tx_fee=cast(int, tac_message.get("tx_fee"))) self.context.decision_maker_message_queue.put_nowait(state_update_msg)
def test_message_inconsistency(self): """Test for an error in consistency of a message.""" currency_endowment = {"FET": 100} good_endowment = {"a_good": 2} exchange_params = {"UNKNOWN": 10.0} utility_params = {"a_good": 20.0} tx_fee = 10 tx_msg = StateUpdateMessage( performative=StateUpdateMessage.Performative.INITIALIZE, amount_by_currency_id=currency_endowment, quantities_by_good_id=good_endowment, exchange_params_by_currency_id=exchange_params, utility_params_by_good_id=utility_params, tx_fee=tx_fee, ) assert not tx_msg._is_consistent()
def test_message_consistency(self): """Test for an error in consistency of a message.""" currency_endowment = {"FET": 100} good_endowment = {"a_good": 2} exchange_params = {"FET": 10.0} utility_params = {"a_good": 20.0} assert StateUpdateMessage( performative=StateUpdateMessage.Performative.INITIALIZE, amount_by_currency=currency_endowment, quantities_by_good_pbk=good_endowment, exchange_params_by_currency=exchange_params, utility_params_by_good_pbk=utility_params) currency_change = {"FET": -10} good_change = {"a_good": 1} assert StateUpdateMessage( performative=StateUpdateMessage.Performative.APPLY, amount_by_currency=currency_change, quantities_by_good_pbk=good_change)
def test_message_inconsistency(self): """Test for an error in consistency of a message.""" with pytest.raises(AssertionError): currency_endowment = {"FET": 100} good_endowment = {"a_good": 2} exchange_params = {"UNKNOWN": 10.0} utility_params = {"a_good": 20.0} assert StateUpdateMessage( performative=StateUpdateMessage.Performative.INITIALIZE, amount_by_currency=currency_endowment, quantities_by_good_pbk=good_endowment, exchange_params_by_currency=exchange_params, utility_params_by_good_pbk=utility_params)
def test_decision_maker_handle_state_update(self): """Test the execute method.""" good_holdings = {"good_pbk": 2} currency_holdings = {"FET": 100} utility_params = {"good_pbk": 20.0} exchange_params = {"FET": 10.0} tx_fee = 1 state_update_message = StateUpdateMessage( performative=StateUpdateMessage.Performative.INITIALIZE, amount_by_currency=currency_holdings, quantities_by_good_pbk=good_holdings, exchange_params_by_currency=exchange_params, utility_params_by_good_pbk=utility_params, tx_fee=tx_fee) self.decision_maker.handle(state_update_message) assert self.decision_maker.ownership_state.amount_by_currency is not None assert self.decision_maker.ownership_state.quantities_by_good_pbk is not None assert self.decision_maker.preferences.exchange_params_by_currency is not None assert self.decision_maker.preferences.utility_params_by_good_pbk is not None currency_deltas = {"FET": -10} good_deltas = {"good_pbk": 1} state_update_message = StateUpdateMessage( performative=StateUpdateMessage.Performative.APPLY, amount_by_currency=currency_deltas, quantities_by_good_pbk=good_deltas) self.decision_maker.handle(state_update_message) expected_amount_by_currency = { key: currency_holdings.get(key, 0) + currency_deltas.get(key, 0) for key in set(currency_holdings) | set(currency_deltas) } expected_quantities_by_good_pbk = { key: good_holdings.get(key, 0) + good_deltas.get(key, 0) for key in set(good_holdings) | set(good_deltas) } assert self.decision_maker.ownership_state.amount_by_currency == expected_amount_by_currency assert self.decision_maker.ownership_state.quantities_by_good_pbk == expected_quantities_by_good_pbk
def _handle_state_update_message( self, state_update_message: StateUpdateMessage) -> None: """ Handle a state update message. :param state_update_message: the state update message :return: None """ performative = state_update_message.get("performative") if performative == StateUpdateMessage.Performative.INITIALIZE: amount_by_currency = cast( Dict[str, int], state_update_message.get("amount_by_currency")) quantities_by_good_pbk = cast( Dict[str, int], state_update_message.get("quantities_by_good_pbk")) self.ownership_state.init( amount_by_currency=amount_by_currency, quantities_by_good_pbk=quantities_by_good_pbk, agent_name=self._agent_name) exchange_params_by_currency = cast( Dict[str, float], state_update_message.get("exchange_params_by_currency")) utility_params_by_good_pbk = cast( Dict[str, float], state_update_message.get("utility_params_by_good_pbk")) tx_fee = cast(int, state_update_message.get("tx_fee")) self.preferences.init( exchange_params_by_currency=exchange_params_by_currency, utility_params_by_good_pbk=utility_params_by_good_pbk, tx_fee=tx_fee, agent_name=self._agent_name) self.goal_pursuit_readiness.update( GoalPursuitReadiness.Status.READY) elif performative == StateUpdateMessage.Performative.APPLY: amount_by_currency = cast( Dict[str, int], state_update_message.get("amount_by_currency")) quantities_by_good_pbk = cast( Dict[str, int], state_update_message.get("quantities_by_good_pbk")) new_ownership_state = self.ownership_state.apply_state_update( amount_deltas_by_currency=amount_by_currency, quantity_deltas_by_good_pbk=quantities_by_good_pbk) self._ownership_state = new_ownership_state
def _on_transaction_confirmed(self, message: TACMessage, controller_pbk: Address) -> None: """ Handle 'on transaction confirmed' event emitted by the controller. :param tx_confirmation: the transaction confirmation :return: None """ logger.info( "[{}]: Received transaction confirmation from the controller: transaction_id={}" .format(self.context.agent_name, message.get("transaction_id"))) state_update_msg = StateUpdateMessage( performative=StateUpdateMessage.Performative.APPLY, amount_by_currency=cast(Dict[str, int], message.get("amount_by_currency")), quantities_by_good_pbk=cast(Dict[str, int], message.get("quantities_by_good_pbk"))) self.context.decision_maker_message_queue.put_nowait(state_update_msg)
def _update_ownership_and_preferences(self, tac_message: TacMessage) -> None: """ Update ownership and preferences. :param tac_message: the game data :return: None """ state_update_msg = StateUpdateMessage( performative=StateUpdateMessage.Performative.INITIALIZE, amount_by_currency_id=tac_message.amount_by_currency_id, quantities_by_good_id=tac_message.quantities_by_good_id, exchange_params_by_currency_id=tac_message. exchange_params_by_currency_id, utility_params_by_good_id=tac_message.utility_params_by_good_id, tx_fee=tac_message.tx_fee, ) self.context.decision_maker_message_queue.put_nowait(state_update_msg)
def _on_transaction_confirmed(self, message: TACMessage) -> None: """ Handle 'on transaction confirmed' event emitted by the controller. :param message: the TACMessage. :return: None """ self.context.logger.info( "[{}]: Received transaction confirmation from the controller: transaction_id={}" .format(self.context.agent_name, message.tx_id[-10:])) state_update_msg = StateUpdateMessage( performative=StateUpdateMessage.Performative.APPLY, amount_by_currency_id=message.amount_by_currency_id, quantities_by_good_id=message.quantities_by_good_id, ) self.context.decision_maker_message_queue.put_nowait(state_update_msg) if "confirmed_tx_ids" not in self.context.shared_state.keys(): self.context.shared_state["confirmed_tx_ids"] = [] self.context.shared_state["confirmed_tx_ids"].append(message.tx_id)
def _on_start(self, tac_message: TacMessage) -> None: """ Handle the 'start' event emitted by the controller. :param tac_message: the game data :return: None """ self.context.logger.info( "[{}]: Received start event from the controller. Starting to compete...".format( self.context.agent_name ) ) game = cast(Game, self.context.game) game.init(tac_message, tac_message.counterparty) game.update_game_phase(Phase.GAME) if game.is_using_contract: contract = self.context.contracts.erc1155 contract.set_deployed_instance( self.context.ledger_apis.apis.get("ethereum"), tac_message.get("contract_address"), ) self.context.logger.info( "We received a contract address: {}".format(contract.instance.address) ) state_update_msg = StateUpdateMessage( performative=StateUpdateMessage.Performative.INITIALIZE, amount_by_currency_id=tac_message.amount_by_currency_id, quantities_by_good_id=tac_message.quantities_by_good_id, exchange_params_by_currency_id=tac_message.exchange_params_by_currency_id, utility_params_by_good_id=tac_message.utility_params_by_good_id, tx_fee=tac_message.tx_fee, ) self.context.decision_maker_message_queue.put_nowait(state_update_msg)
def _on_start(self, tac_message: TACMessage) -> None: """ Handle the 'start' event emitted by the controller. :param tac_message: the game data :return: None """ self.context.logger.info( "[{}]: Received start event from the controller. Starting to compete..." .format(self.context.agent_name)) game = cast(Game, self.context.game) game.init(tac_message, tac_message.counterparty) game.update_game_phase(Phase.GAME) state_update_msg = StateUpdateMessage( performative=StateUpdateMessage.Performative.INITIALIZE, amount_by_currency_id=tac_message.amount_by_currency_id, quantities_by_good_id=tac_message.quantities_by_good_id, exchange_params_by_currency_id=tac_message. exchange_params_by_currency_id, utility_params_by_good_id=tac_message.utility_params_by_good_id, tx_fee=tac_message.tx_fee, ) self.context.decision_maker_message_queue.put_nowait(state_update_msg)