Esempio n. 1
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')
Esempio n. 2
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)
Esempio n. 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)
Esempio n. 4
0
def create_wallet(client: ExonumClient, message_generator: MessageGenerator,
                  name: str) -> KeyPair:
    """Creates a wallet with the given name and returns a KeyPair for it."""
    key_pair = KeyPair.generate()

    # Load the "service.proto" from the Cryptocurrency service:
    cryptocurrency_module = ModuleManager.import_service_module(
        CRYPTOCURRENCY_ARTIFACT_NAME, "service")

    # Create a Protobuf message:
    create_wallet_message = cryptocurrency_module.TxCreateWallet()
    create_wallet_message.name = name

    # Convert the Protobuf message to an Exonum message and sign it:
    create_wallet_tx = message_generator.create_message(create_wallet_message)
    create_wallet_tx.sign(key_pair)

    # Send the transaction to Exonum:
    response = client.send_transaction(create_wallet_tx)
    ensure_status_code(response)
    tx_hash = response.json()["tx_hash"]

    # Wait for new blocks:
    with client.create_subscriber() as subscriber:
        subscriber.wait_for_new_block()
        subscriber.wait_for_new_block()

    ensure_transaction_success(client, tx_hash)

    print(f"Successfully created wallet with name '{name}'")

    return key_pair
Esempio n. 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)
Esempio n. 6
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)
Esempio n. 7
0
def transfer(
    client: ExonumClient, message_generator: MessageGenerator, from_keypair: KeyPair, to_key: PublicKey, amount: int
) -> Tuple[int, int]:
    """This example transfers tokens from one wallet to the other one and
    returns the balances of these wallets."""

    cryptocurrency_module = ModuleManager.import_service_module(CRYPTOCURRENCY_ARTIFACT_NAME, "service")
    # Note that since we are using the Cryptocurrency module,
    # we need to load helpers from this module and not from the main module:
    helpers_module = ModuleManager.import_service_module(CRYPTOCURRENCY_ARTIFACT_NAME, "helpers")

    transfer_message = cryptocurrency_module.Transfer()
    transfer_message.to.CopyFrom(helpers_module.PublicKey(data=to_key.value))
    transfer_message.amount = amount
    transfer_message.seed = Seed.get_seed()

    transfer_tx = message_generator.create_message(transfer_message)
    transfer_tx.sign(from_keypair)

    response = client.send_transaction(transfer_tx)
    ensure_status_code(response)
    tx_hash = response.json()["tx_hash"]

    # Wait for new blocks:

    with client.create_subscriber() as subscriber:
        subscriber.wait_for_new_block()
        subscriber.wait_for_new_block()

    ensure_transaction_success(client, tx_hash)

    from_balance = get_balance(client, from_keypair.public_key)
    to_balance = get_balance(client, to_key)

    return from_balance, to_balance
Esempio n. 8
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
Esempio n. 9
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)
Esempio n. 10
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)
Esempio n. 11
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")
Esempio n. 12
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")
Esempio n. 13
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)
Esempio n. 14
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')
Esempio n. 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("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)
Esempio n. 16
0
def transfer(client: ExonumClient, message_generator: MessageGenerator,
             from_keypair: KeyPair, to_key: PublicKey,
             amount: int) -> Tuple[int, int]:
    """This example transfers tokens from one wallet to the other one and
    returns the balances of these wallets."""

    cryptocurrency_module = ModuleManager.import_service_module(
        CRYPTOCURRENCY_ARTIFACT_NAME, CRYPTOCURRENCY_ARTIFACT_VERSION,
        "service")
    # Note that since we are using the Cryptocurrency module,
    # we need to load types from this module and not from the main module:
    types_module = ModuleManager.import_service_module(
        CRYPTOCURRENCY_ARTIFACT_NAME, CRYPTOCURRENCY_ARTIFACT_VERSION,
        "exonum.crypto.types")

    transfer_message = cryptocurrency_module.Transfer()
    # The address is a hash of a `Caller` protobuf message.
    hash_address = message_generator.pk_to_hash_address(to_key)
    if hash_address is None:
        raise Exception
    transfer_message.to.CopyFrom(types_module.Hash(data=hash_address.value))
    transfer_message.amount = amount
    transfer_message.seed = Seed.get_seed()

    transfer_tx = message_generator.create_message(transfer_message)
    transfer_tx.sign(from_keypair)

    response = client.public_api.send_transaction(transfer_tx)
    ensure_status_code(response)
    tx_hash = response.json()["tx_hash"]

    # Wait for new blocks:

    with client.create_subscriber("blocks") as subscriber:
        subscriber.wait_for_new_event()
        subscriber.wait_for_new_event()

    ensure_transaction_success(client, tx_hash)

    from_balance = get_balance(client, from_keypair.public_key)
    to_balance = get_balance(client, to_key)

    return from_balance, to_balance
Esempio n. 17
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)
Esempio n. 18
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)
Esempio n. 19
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)
Esempio n. 20
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)
Esempio n. 21
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)