Example #1
0
    def test_freeze_service(self):
        host, public_port, private_port = self.network.api_address(0)
        client = ExonumClient(host, public_port, private_port)

        # Create wallet
        alice_keys = KeyPair.generate()
        with ExonumCryptoAdvancedClient(client) as crypto_client:
            crypto_client.create_wallet(alice_keys, "Alice")
            with client.create_subscriber("transactions") as subscriber:
                subscriber.wait_for_new_event()
                alice_balance = crypto_client.get_balance(alice_keys)
                self.assertEqual(alice_balance, 100)

        # Freeze the service
        instances = {
            "crypto": {
                "artifact": "cryptocurrency",
                "action": "freeze"
            }
        }
        cryptocurrency_advanced_config_dict = generate_config(
            self.network, instances=instances, artifact_action="none")

        cryptocurrency_advanced_config = Configuration(
            cryptocurrency_advanced_config_dict)
        with Launcher(cryptocurrency_advanced_config) as launcher:
            launcher.deploy_all()
            launcher.wait_for_deploy()
            launcher.start_all()
            launcher.wait_for_start()

        # Check that the service status has been changed to `frozen`.
        for service in client.public_api.available_services().json(
        )["services"]:
            if service["spec"]["name"] == "crypto":
                self.assertEqual(service["status"]["type"], "frozen")

        # Try to create a new wallet. The operation should fail.
        with ExonumCryptoAdvancedClient(client) as crypto_client:
            bob_keys = KeyPair.generate()
            response = crypto_client.create_wallet(bob_keys, "Bob")
            self.assertEqual(response.status_code, 400)
            # Because the service is frozen, transaction should be inadmissible.
            self.assertEqual(response.json()["title"],
                             "Failed to add transaction to memory pool")

        # Check that we can use service endpoints for data retrieving. Check wallet once again.
        with ExonumCryptoAdvancedClient(client) as crypto_client:
            alice_balance = crypto_client.get_balance(alice_keys)
            self.assertEqual(alice_balance, 100)
Example #2
0
    def test_transfer_funds_insufficient(self):
        """Tests the transfer insufficient amount of funds is failed"""

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(
                validator_id)
            client = ExonumClient(host, public_port, private_port)
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                crypto_client.create_wallet(alice_keys,
                                            "Alice" + str(validator_id))
                bob_keys = KeyPair.generate()
                crypto_client.create_wallet(bob_keys,
                                            "Bob" + str(validator_id))
                with client.create_subscriber("blocks") as subscriber:
                    subscriber.wait_for_new_event()
                    tx_response = crypto_client.transfer(
                        110, alice_keys, bob_keys.public_key.value)
                    subscriber.wait_for_new_event()
                    tx_status = client.get_tx_info(
                        tx_response.json()['tx_hash']).json()['status']['type']
                    self.assertEqual(tx_status, 'service_error')
                    alice_balance = (crypto_client.get_wallet_info(
                        alice_keys).json()['wallet_proof']['to_wallet']
                                     ['entries'][0]['value']['balance'])
                    bob_balance = (crypto_client.get_wallet_info(
                        bob_keys).json()['wallet_proof']['to_wallet']
                                   ['entries'][0]['value']['balance'])
                    self.assertEqual(alice_balance, 100)
                    self.assertEqual(bob_balance, 100)
Example #3
0
    def test_transfer_funds(self):
        """Tests the transfer funds to another wallet"""

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(
                validator_id)
            client = ExonumClient(host, public_port, private_port)
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                crypto_client.create_wallet(alice_keys,
                                            "Alice" + str(validator_id))
                bob_keys = KeyPair.generate()
                crypto_client.create_wallet(bob_keys,
                                            "Bob" + str(validator_id))
                with client.create_subscriber("blocks") as subscriber:
                    subscriber.wait_for_new_event()
                    # TODO: Sometimes it fails without time.sleep() [ECR-3876]
                    time.sleep(2)
                    crypto_client.transfer(20, alice_keys,
                                           bob_keys.public_key.value)
                    subscriber.wait_for_new_event()
                    # TODO: Sometimes it fails without time.sleep() [ECR-3876]
                    time.sleep(2)
                    alice_balance = (crypto_client.get_wallet_info(
                        alice_keys).json()['wallet_proof']['to_wallet']
                                     ['entries'][0]['value']['balance'])
                    bob_balance = (crypto_client.get_wallet_info(
                        bob_keys).json()['wallet_proof']['to_wallet']
                                   ['entries'][0]['value']['balance'])
                    self.assertEqual(alice_balance, 80)
                    self.assertEqual(bob_balance, 120)
Example #4
0
    def test_transfer_funds(self):
        """Tests the transfer funds to another wallet"""

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(
                validator_id)
            client = ExonumClient(host, public_port, private_port)
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                bob_keys = KeyPair.generate()
                with client.create_subscriber("transactions") as subscriber:
                    crypto_client.create_wallet(alice_keys,
                                                "Alice" + str(validator_id))
                    subscriber.wait_for_new_event()
                    crypto_client.create_wallet(bob_keys,
                                                "Bob" + str(validator_id))
                    subscriber.wait_for_new_event()
                    crypto_client.transfer(20, alice_keys,
                                           bob_keys.public_key.value)
                    subscriber.wait_for_new_event()
                    alice_wallet = crypto_client.get_wallet_info(
                        alice_keys).json()
                    alice_balance = alice_wallet["wallet_proof"]["to_wallet"][
                        "entries"][0]["value"]["balance"]
                    bob_wallet = crypto_client.get_wallet_info(bob_keys).json()
                    bob_balance = bob_wallet["wallet_proof"]["to_wallet"][
                        "entries"][0]["value"]["balance"]
                    self.assertEqual(alice_balance, 80)
                    self.assertEqual(bob_balance, 120)
Example #5
0
    def test_transfer_funds_insufficient(self):
        """Tests the transfer insufficient amount of funds is failed"""

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(
                validator_id)
            client = ExonumClient(host, public_port, private_port)
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                crypto_client.create_wallet(alice_keys,
                                            "Alice" + str(validator_id))
                bob_keys = KeyPair.generate()
                crypto_client.create_wallet(bob_keys,
                                            "Bob" + str(validator_id))
                with client.create_subscriber("blocks") as subscriber:
                    subscriber.wait_for_new_event()
                    tx_response = crypto_client.transfer(
                        110, alice_keys, bob_keys.public_key)
                    subscriber.wait_for_new_event()
                    tx_info = client.public_api.get_tx_info(
                        tx_response.json()["tx_hash"]).json()
                    tx_status = tx_info["status"]["type"]
                    self.assertEqual(tx_status, "service_error")
                    alice_balance = crypto_client.get_balance(alice_keys)
                    bob_balance = crypto_client.get_balance(bob_keys)
                    self.assertEqual(alice_balance, 100)
                    self.assertEqual(bob_balance, 100)
Example #6
0
    def test_create_wallet_unique_for_key_pair(self):
        """Tests the transaction with the same keys for different wallets is failed"""

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(
                validator_id)
            client = ExonumClient(host, public_port, private_port)
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                tx_response = crypto_client.create_wallet(
                    alice_keys, "Alice" + str(validator_id))
                with client.create_subscriber("blocks") as subscriber:
                    subscriber.wait_for_new_event()
                    # TODO: Sometimes it fails without time.sleep() [ECR-3876]
                    time.sleep(2)
                tx_status = client.get_tx_info(
                    tx_response.json()['tx_hash']).json()['status']['type']
                self.assertEqual(tx_status, 'success')
                # create the wallet with the same keys again
                tx_same_keys = crypto_client.create_wallet(
                    alice_keys, "Alice_Dublicate" + str(validator_id))
                with client.create_subscriber("blocks") as subscriber:
                    subscriber.wait_for_new_event()
                tx_status = client.get_tx_info(
                    tx_same_keys.json()['tx_hash']).json()['status']['type']
                self.assertEqual(tx_status, 'service_error')
Example #7
0
 def _create_wallet(self, client: ExonumClient, wallet_name: str,
                    version: str) -> KeyPair:
     with ExonumCryptoAdvancedClient(client, INSTANCE_NAME,
                                     version) as crypto_client:
         keys = KeyPair.generate()
         response = crypto_client.create_wallet(keys, wallet_name)
         self.assertEqual(response.status_code, 200)
         with client.create_subscriber("transactions") as subscriber:
             subscriber.wait_for_new_event()
         return keys
Example #8
0
    def test_get_nonexistent_wallet(self):
        """Tests the wallet history is None for nonexistent wallet"""

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(
                validator_id)
            client = ExonumClient(host, public_port, private_port)
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                wallet_history = crypto_client.get_wallet_info(
                    alice_keys).json()["wallet_history"]
                self.assertIsNone(wallet_history)
Example #9
0
    def test_add_funds_to_nonexistent_wallet(self):
        """Tests the funds issue is failed if wallet doesn't exist"""

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(
                validator_id)
            client = ExonumClient(host, public_port, private_port)
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                tx_response = crypto_client.issue(alice_keys, 100)
                with client.create_subscriber("transactions") as subscriber:
                    subscriber.wait_for_new_event()
                    tx_info = client.public_api.get_tx_info(
                        tx_response.json()["tx_hash"]).json()
                    tx_status = tx_info["status"]["type"]
                    self.assertEqual(tx_status, "service_error")
Example #10
0
    def test_token_issue(self):
        """Tests the token issue"""

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(
                validator_id)
            client = ExonumClient(host, public_port, private_port)
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                crypto_client.create_wallet(alice_keys,
                                            "Alice" + str(validator_id))
                with client.create_subscriber("transactions") as subscriber:
                    subscriber.wait_for_new_event()
                    crypto_client.issue(alice_keys, 100)
                    subscriber.wait_for_new_event()
                    alice_balance = crypto_client.get_balance(alice_keys)
                    self.assertEqual(alice_balance, 200)
Example #11
0
    def test_add_funds_to_nonexistent_wallet(self):
        """Tests the funds issue is failed if wallet doesn't exist"""

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(
                validator_id)
            client = ExonumClient(host, public_port, private_port)
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                tx_response = crypto_client.issue(alice_keys, 100)
                with client.create_subscriber("blocks") as subscriber:
                    subscriber.wait_for_new_event()
                    # TODO: Sometimes it fails without time.sleep() [ECR-3876]
                    time.sleep(2)
                    tx_status = client.get_tx_info(
                        tx_response.json()['tx_hash']).json()['status']['type']
                    self.assertEqual(tx_status, 'service_error')
Example #12
0
    def test_create_wallet(self):
        """Tests the wallet creation"""

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(
                validator_id)
            client = ExonumClient(host, public_port, private_port)
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                crypto_client.create_wallet(alice_keys,
                                            "Alice" + str(validator_id))
                with client.create_subscriber("transactions") as subscriber:
                    subscriber.wait_for_new_event()
                self.assertEqual(
                    crypto_client.get_wallet_info(alice_keys).status_code, 200)
                alice_wallet = crypto_client.get_wallet_info(alice_keys).json()
                alice_balance = alice_wallet["wallet_proof"]["to_wallet"][
                    "entries"][0]["value"]["balance"]
                self.assertEqual(alice_balance, 100)
Example #13
0
 def test_create_wallet_same_name(self):
     """Tests the transaction with the same wallet name is rejected"""
     client = None
     for validator_id in range(self.network.validators_count()):
         host, public_port, private_port = self.network.api_address(
             validator_id)
         client = ExonumClient(host, public_port, private_port)
         with ExonumCryptoAdvancedClient(client) as crypto_client:
             alice_keys = KeyPair.generate()
             crypto_client.create_wallet(alice_keys,
                                         "Alice" + str(validator_id))
             with client.create_subscriber("transactions") as subscriber:
                 subscriber.wait_for_new_event()
             # create the wallet with the same name again
             crypto_client.create_wallet(alice_keys,
                                         "Alice" + str(validator_id))
             with client.create_subscriber("blocks") as subscriber:
                 subscriber.wait_for_new_event()
     # it should contain 4 txs for wallet creation plus 6 services txs
     self.assertEqual(client.public_api.stats().json()["tx_count"], 10)
Example #14
0
    def test_transfer_to_yourself(self):
        """Tests the transfer funds to yourself is impossible"""

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(
                validator_id)
            client = ExonumClient(host, public_port, private_port)
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                crypto_client.create_wallet(alice_keys,
                                            "Alice" + str(validator_id))
                with client.create_subscriber("blocks") as subscriber:
                    subscriber.wait_for_new_event()
                    crypto_client.transfer(10, alice_keys,
                                           alice_keys.public_key.value)
                    subscriber.wait_for_new_event()
                    alice_balance = (crypto_client.get_wallet_info(
                        alice_keys).json()['wallet_proof']['to_wallet']
                                     ['entries'][0]['value']['balance'])
                    self.assertEqual(alice_balance, 100)
Example #15
0
    def test_create_wallet(self):
        """Tests the wallet creation"""

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(
                validator_id)
            client = ExonumClient(host, public_port, private_port)
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                crypto_client.create_wallet(alice_keys,
                                            "Alice" + str(validator_id))
                with client.create_subscriber("blocks") as subscriber:
                    subscriber.wait_for_new_event()
                self.assertEqual(
                    crypto_client.get_wallet_info(alice_keys).status_code, 200)
                # TODO: Sometimes it fails without time.sleep() [ECR-3876]
                time.sleep(2)
                alice_balance = (crypto_client.get_wallet_info(
                    alice_keys).json()['wallet_proof']['to_wallet']['entries']
                                 [0]['value']['balance'])
                self.assertEqual(alice_balance, 100)
Example #16
0
    def test_token_issue(self):
        """Tests the token issue"""

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(
                validator_id)
            client = ExonumClient(host, public_port, private_port)
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                crypto_client.create_wallet(alice_keys,
                                            "Alice" + str(validator_id))
                with client.create_subscriber("blocks") as subscriber:
                    subscriber.wait_for_new_event()
                    # TODO: Sometimes it fails without time.sleep() [ECR-3876]
                    time.sleep(2)
                    crypto_client.issue(alice_keys, 100)
                    subscriber.wait_for_new_event()
                    # TODO: Sometimes it fails without time.sleep() [ECR-3876]
                    time.sleep(2)
                    alice_wallet = crypto_client.get_wallet_info(
                        alice_keys).json()
                    alice_balance = alice_wallet["wallet_proof"]["to_wallet"][
                        "entries"][0]["value"]["balance"]
                    self.assertEqual(alice_balance, 200)
Example #17
0
    def test_create_wallet_unique_for_key_pair(self):
        """Tests the transaction with the same keys for different wallets is failed"""

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(
                validator_id)
            client = ExonumClient(host, public_port, private_port)
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                tx_response = crypto_client.create_wallet(
                    alice_keys, "Alice" + str(validator_id))
                with client.create_subscriber("transactions") as subscriber:
                    subscriber.wait_for_new_event()
                tx_status = client.public_api.get_tx_info(
                    tx_response.json()["tx_hash"]).json()["status"]["type"]
                self.assertEqual(tx_status, "success")
                # create the wallet with the same keys again
                tx_same_keys = crypto_client.create_wallet(
                    alice_keys, "Alice_Dublicate" + str(validator_id))
                with client.create_subscriber("blocks") as subscriber:
                    subscriber.wait_for_new_event()
                tx_status = client.public_api.get_tx_info(
                    tx_same_keys.json()["tx_hash"]).json()["status"]["type"]
                self.assertEqual(tx_status, "service_error")
Example #18
0
    def full_migration_flow(self, action: str):
        host, public_port, private_port = self.network.api_address(0)
        client = ExonumClient(host, public_port, private_port)

        # Deploy a service with 0.2.0 version.
        instances = {INSTANCE_NAME: {"artifact": "cryptocurrency"}}
        config_dict = generate_config(self.network, instances=instances)
        deploy_config = Configuration(config_dict)

        with Launcher(deploy_config) as launcher:
            launcher.deploy_all()
            launcher.wait_for_deploy()

            self.wait_for_api_restart()
            explorer = launcher.explorer()
            for artifact in launcher.launch_state.completed_deployments():
                deployed = explorer.is_deployed(artifact)
                self.assertTrue(deployed)

        # Create Alice's wallet with 0.1.0 version of the service
        alice_keys = self._create_wallet(client, "Alice", "0.1.0")

        # Stop the working service with version 0.1.0.
        instances = {
            INSTANCE_NAME: {
                "artifact": "cryptocurrency",
                "action": action
            }
        }
        stop_config_dict = generate_config(self.network,
                                           instances=instances,
                                           artifact_action="none",
                                           artifact_version="0.1.0")
        stop_config = Configuration(stop_config_dict)

        with Launcher(stop_config) as launcher:
            launcher.start_all()
            launcher.wait_for_start()

            self.wait_for_api_restart()
            # Check that the service status has been changed to `stopped`.
            for service in client.public_api.available_services().json(
            )["services"]:
                if service["spec"]["name"] == INSTANCE_NAME:
                    self.assertEqual(
                        service["status"]["type"],
                        "stopped" if action == "stop" else "frozen")

        # Migrate service data from 0.1.0 to 0.2.0 version
        migrations = {
            INSTANCE_NAME: {
                "runtime": "rust",
                "name": "exonum-cryptocurrency",
                "version": "0.2.0"
            }
        }
        migrations_dict = generate_migration_config(self.network, migrations)
        migration_config = Configuration(migrations_dict)

        with Launcher(migration_config) as launcher:
            launcher.migrate_all()
            launcher.wait_for_migration()

            for service in client.public_api.available_services().json(
            )["services"]:
                if service["spec"]["name"] == INSTANCE_NAME:
                    self.assertEqual(service["data_version"], "0.2.0")

        # Switch service artifact from 0.1.0 to 0.2.0 version
        with Launcher(migration_config) as launcher:
            launcher.migrate_all()
            launcher.wait_for_migration()

            for service in client.public_api.available_services().json(
            )["services"]:
                if service["spec"]["name"] == INSTANCE_NAME:
                    self.assertEqual(service["spec"]["artifact"]["version"],
                                     "0.2.0")

        # Resume service with a new logic version 0.2.0
        instances = {
            INSTANCE_NAME: {
                "artifact": "cryptocurrency",
                "action": "resume"
            }
        }
        resume_config_dict = generate_config(self.network,
                                             instances=instances,
                                             artifact_action="none")
        resume_config = Configuration(resume_config_dict)

        with Launcher(resume_config) as launcher:
            launcher.start_all()
            launcher.wait_for_start()

            self.wait_for_api_restart()
            # Check that the service status has been changed to `active`.
            for service in client.public_api.available_services().json(
            )["services"]:
                if service["spec"]["name"] == INSTANCE_NAME:
                    self.assertEqual(service["status"]["type"], "active")
                    self.assertEqual(service["spec"]["artifact"]["version"],
                                     "0.2.0")

        # Unload artifact with version 0.1.0
        unload_config_dict = generate_config(self.network,
                                             instances=instances,
                                             artifact_action="unload",
                                             artifact_version="0.1.0")
        unload_config = Configuration(unload_config_dict)

        with Launcher(unload_config) as launcher:
            launcher.unload_all()
            launcher.wait_for_unload()

            self.wait_for_api_restart()
            explorer = launcher.explorer()

            for artifact in unload_config.artifacts.values():
                deployed = explorer.is_deployed(artifact)
                self.assertFalse(deployed)

        # Create Bob's wallet with version 0.2.0 of the service.
        bob_keys = self._create_wallet(client, "Bob", "0.2.0")

        # Transfer some coins and check balances and history length.
        with ExonumCryptoAdvancedClient(
                client, instance_name=INSTANCE_NAME) as crypto_client:
            alice_balance = crypto_client.get_balance(alice_keys)
            self.assertEqual(alice_balance, 100)
            alice_history_len = crypto_client.get_history_len(alice_keys)
            self.assertEqual(alice_history_len, 0)
            bob_balance = crypto_client.get_balance(bob_keys)
            self.assertEqual(bob_balance, 100)
            crypto_client.transfer(20, alice_keys, bob_keys.public_key)
            with client.create_subscriber("transactions") as subscriber:
                subscriber.wait_for_new_event()
                alice_balance = crypto_client.get_balance(alice_keys)
                self.assertEqual(alice_balance, 80)
                # Get a value from the new field `history_len`.
                alice_history_len = crypto_client.get_history_len(alice_keys)
                self.assertEqual(alice_history_len, 1)
                bob_balance = crypto_client.get_balance(bob_keys)
                self.assertEqual(bob_balance, 120)
Example #19
0
    def test_resume_after_freeze_service(self):
        host, public_port, private_port = self.network.api_address(0)
        client = ExonumClient(host, public_port, private_port)

        # Create wallet
        with ExonumCryptoAdvancedClient(client) as crypto_client:
            alice_keys = KeyPair.generate()
            crypto_client.create_wallet(alice_keys, "Alice")
            with client.create_subscriber("transactions") as subscriber:
                subscriber.wait_for_new_event()
                alice_balance = crypto_client.get_balance(alice_keys)
                self.assertEqual(alice_balance, 100)

        # Freeze the service
        instances = {
            "crypto": {
                "artifact": "cryptocurrency",
                "action": "freeze"
            }
        }
        cryptocurrency_advanced_config_dict = generate_config(
            self.network, instances=instances, artifact_action="none")
        cryptocurrency_advanced_config = Configuration(
            cryptocurrency_advanced_config_dict)
        with Launcher(cryptocurrency_advanced_config) as launcher:
            launcher.deploy_all()
            launcher.wait_for_deploy()
            launcher.start_all()
            launcher.wait_for_start()

        # Resume the service
        instances = {
            "crypto": {
                "artifact": "cryptocurrency",
                "action": "resume"
            }
        }
        cryptocurrency_advanced_config_dict = generate_config(
            self.network, instances=instances, artifact_action="none")
        cryptocurrency_advanced_config = Configuration(
            cryptocurrency_advanced_config_dict)
        with Launcher(cryptocurrency_advanced_config) as launcher:
            launcher.deploy_all()
            launcher.wait_for_deploy()
            launcher.start_all()
            launcher.wait_for_start()

        # Check that the service status has been changed to `active`.
        for service in client.public_api.available_services().json(
        )["services"]:
            if service["spec"]["name"] == "crypto":
                self.assertEqual(service["status"]["type"], "active")

        # Check that an ability to create wallets has been restored.
        with ExonumCryptoAdvancedClient(client) as crypto_client:
            bob_keys = KeyPair.generate()
            crypto_client.create_wallet(bob_keys, "Bob")
            with client.create_subscriber("transactions") as subscriber:
                subscriber.wait_for_new_event()
                bob_balance = crypto_client.get_balance(bob_keys)
                self.assertEqual(bob_balance, 100)
Example #20
0
    def test_deploy_regular_stop_and_resume_running_instance(self):
        """Tests the deploy mechanism to stop
        and resume running instance."""

        instances = {"crypto": {"artifact": "cryptocurrency"}}
        cryptocurrency_advanced_config_dict = generate_config(self.network, instances=instances)

        cryptocurrency_advanced_config = Configuration(cryptocurrency_advanced_config_dict)
        with Launcher(cryptocurrency_advanced_config) as launcher:
            explorer = launcher.explorer()

            launcher.deploy_all()
            launcher.wait_for_deploy()
            launcher.start_all()
            launcher.wait_for_start()

            self.wait_for_api_restart()
            for artifact in launcher.launch_state.completed_deployments():
                deployed = explorer.is_deployed(artifact)
                self.assertEqual(deployed, True)

            self.assertEqual(len(launcher.launch_state.completed_configs()), 1)

        # stop service
        instances = {"crypto": {"artifact": "cryptocurrency", "action": "stop"}}
        cryptocurrency_advanced_config_dict = generate_config(self.network, instances=instances, artifact_action="none")

        cryptocurrency_advanced_config = Configuration(cryptocurrency_advanced_config_dict)
        with Launcher(cryptocurrency_advanced_config) as launcher:
            launcher.deploy_all()
            launcher.wait_for_deploy()
            launcher.start_all()
            launcher.wait_for_start()

        # The changes restart the HTTP servers of the nodes, so we wait for the servers to restart.
        wait_network_to_start(self.network)

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(validator_id)
            client = ExonumClient(host, public_port, private_port)
            available_services = client.public_api.available_services().json()
            service_status = find_service_status(available_services, "crypto")
            self.assertEqual(service_status, "stopped")
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                tx_response = crypto_client.create_wallet(alice_keys, "Alice" + str(validator_id))
                # in case of stopped service its tx will not be processed
                self.assertEqual(tx_response.status_code, 400)
                self.assertIn("Cannot dispatch transaction to non-active service", str(tx_response.content))

        # resume service
        instances = {"crypto": {"artifact": "cryptocurrency", "action": "resume"}}
        cryptocurrency_advanced_config_dict = generate_config(self.network, instances=instances, artifact_action="none")

        cryptocurrency_advanced_config = Configuration(cryptocurrency_advanced_config_dict)
        with Launcher(cryptocurrency_advanced_config) as launcher:
            launcher.deploy_all()
            launcher.wait_for_deploy()
            launcher.start_all()
            launcher.wait_for_start()

        # The changes restart the HTTP servers of the nodes, so we wait for the servers to restart.
        self.wait_for_api_restart()

        for validator_id in range(self.network.validators_count()):
            host, public_port, private_port = self.network.api_address(validator_id)
            client = ExonumClient(host, public_port, private_port)
            available_services = client.public_api.available_services().json()
            service_status = find_service_status(available_services, "crypto")
            self.assertEqual(service_status, "active")
            with ExonumCryptoAdvancedClient(client) as crypto_client:
                alice_keys = KeyPair.generate()
                tx_response = crypto_client.create_wallet(alice_keys, "Alice" + str(validator_id))
                # resumed service must process txs as usual
                self.assertEqual(tx_response.status_code, 200)