示例#1
0
 def test_generate_good_ids_succeeds(self):
     """Test the generate_good_ids of Helpers module which succeeds."""
     expected_list = [1, 2, 3, 4, 5]
     with patch.object(ERC1155Contract,
                       "generate_token_ids",
                       return_value=expected_list):
         good_ids = generate_good_ids(5)
     assert good_ids == expected_list
示例#2
0
 def test_generate_good_ids_fails(self):
     """Test the generate_good_ids of Helpers module which fails because generate_token_ids generates wrong good ids."""
     expected_list = [1, 2, 3, 4, 5, 6]
     with patch.object(ERC1155Contract,
                       "generate_token_ids",
                       return_value=expected_list):
         with pytest.raises(
                 AEAEnforceError,
                 match="Length of good ids and number of goods must match.",
         ):
             assert generate_good_ids(5)
示例#3
0
    def act(self) -> None:
        """
        Implement the act.

        :return: None
        """
        game = cast(Game, self.context.game)
        parameters = cast(Parameters, self.context.parameters)
        contract = cast(ERC1155Contract, self.context.contracts.erc1155)
        ledger_api = self.context.ledger_apis.get_api(parameters.ledger)
        if game.phase.value == Phase.CONTRACT_DEPLOYING.value:
            tx_receipt = ledger_api.get_transaction_receipt(
                tx_digest=game.contract_manager.deploy_tx_digest
            )
            if tx_receipt is None:
                self.context.logger.info(
                    "[{}]: Cannot verify whether contract deployment was successful. Retrying...".format(
                        self.context.agent_name
                    )
                )
            elif tx_receipt.status != 1:
                self.context.is_active = False
                self.context.warning(
                    "[{}]: The contract did not deployed successfully. Transaction hash: {}. Aborting!".format(
                        self.context.agent_name, tx_receipt.transactionHash.hex()
                    )
                )
            else:
                self.context.logger.info(
                    "[{}]: The contract was successfully deployed. Contract address: {}. Transaction hash: {}".format(
                        self.context.agent_name,
                        tx_receipt.contractAddress,
                        tx_receipt.transactionHash.hex(),
                    )
                )
                contract.set_address(ledger_api, tx_receipt.contractAddress)
                configuration = Configuration(parameters.version_id, parameters.tx_fee,)
                currency_ids = generate_currency_ids(parameters.nb_currencies, contract)
                configuration.currency_id_to_name = generate_currency_id_to_name(
                    currency_ids
                )
                good_ids = generate_good_ids(parameters.nb_goods, contract)
                configuration.good_id_to_name = generate_good_id_to_name(good_ids)
                configuration.contract_address = tx_receipt.contractAddress
                game.conf = configuration
                game.phase = Phase.CONTRACT_DEPLOYED
        elif game.phase.value == Phase.TOKENS_CREATING.value:
            tx_receipt = ledger_api.get_transaction_receipt(
                tx_digest=game.contract_manager.create_tokens_tx_digest
            )
            if tx_receipt is None:
                self.context.logger.info(
                    "[{}]: Cannot verify whether token creation was successful. Retrying...".format(
                        self.context.agent_name
                    )
                )
            elif tx_receipt.status != 1:
                self.context.is_active = False
                self.context.warning(
                    "[{}]: The token creation wasn't successful. Transaction hash: {}. Aborting!".format(
                        self.context.agent_name, tx_receipt.transactionHash.hex()
                    )
                )
            else:
                self.context.logger.info(
                    "[{}]: Successfully created the tokens. Transaction hash: {}".format(
                        self.context.agent_name, tx_receipt.transactionHash.hex()
                    )
                )
                game.phase = Phase.TOKENS_CREATED
        elif game.phase.value == Phase.TOKENS_MINTING.value:
            for (
                agent_addr,
                tx_digest,
            ) in game.contract_manager.mint_tokens_tx_digests.items():
                if agent_addr in game.contract_manager.confirmed_mint_tokens_agents:
                    continue
                tx_receipt = ledger_api.get_transaction_receipt(tx_digest=tx_digest)
                if tx_receipt is None:
                    self.context.logger.info(
                        "[{}]: Cannot verify whether token minting for agent_addr={} was successful. Retrying...".format(
                            self.context.agent_name, agent_addr
                        )
                    )
                elif tx_receipt.status != 1:
                    self.context.is_active = False
                    self.context.logger.warning(
                        "[{}]: The token minting for agent_addr={} wasn't successful. Transaction hash: {}. Aborting!".format(
                            self.context.agent_name,
                            agent_addr,
                            tx_receipt.transactionHash.hex(),
                        )
                    )
                else:
                    self.context.logger.info(
                        "[{}]: Successfully minted the tokens for agent_addr={}. Transaction hash: {}".format(
                            self.context.agent_name,
                            agent_addr,
                            tx_receipt.transactionHash.hex(),
                        )
                    )
                    game.contract_manager.add_confirmed_mint_tokens_agents(agent_addr)
                    if len(game.contract_manager.confirmed_mint_tokens_agents) == len(
                        game.initial_agent_states
                    ):
                        self.context.logger.info("All tokens minted!")
                        game.phase = Phase.TOKENS_MINTED